<?php
//Start photographyThemes Functions - Please refrain from editing this file.

$functions_path = TEMPLATEPATH . '/functions/';
$includes_path = TEMPLATEPATH . '/includes/';

// Options panel variables and functions
require_once ($functions_path . 'admin-setup.php');
// Custom functions and plugins
require_once ($functions_path . 'admin-functions.php');
// Thumbnails
add_theme_support('post-thumbnails');
set_post_thumbnail_size(958, 9999);
add_image_size('post-thumb', 135, 135, true);
add_image_size('single-thumb', 940, true);
// Custom fields 
// require_once ($functions_path . 'admin-custom.php');
// More photographyThemes Page
require_once ($functions_path . 'admin-theme-page.php');
// Admin Interface!
require_once ($functions_path . 'admin-interface.php');
// Options panel settings
require_once ($includes_path . 'theme-options.php'); // What we do!
//Custom Theme Fucntions
require_once ($includes_path . 'theme-functions.php'); 
//Custom Comments
require_once ($includes_path . 'theme-comments.php'); 
// Load Javascript in wp_head
require_once ($includes_path . 'theme-js.php');
// Widgets  & Sidebars
require_once ($includes_path . 'sidebar-init.php');
require_once ($includes_path . 'theme-widgets.php');
// Taxonomy Pagi
require_once ($includes_path . 'taxanomy-pagi.php');
add_action('wp_head', 'photographythemes_wp_head');
add_action('admin_menu', 'photographythemes_add_admin');
add_action('admin_head', 'photographythemes_admin_head');    
//Custom Comments
require_once ($includes_path . 'theme-comments.php'); 
function new_excerpt_length($length) {
	return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit+ 1));
 if(count($words) > $word_limit) {
  array_pop($words);
  //add a ... at last article when more than limitword count
  echo implode(' ', $words)."..."; } else {
 //otherwise
 echo implode(' ', $words); }
}



// Registering Menus For Theme

add_action( 'init', 'register_my_menus' );
function register_my_menus() {
	register_nav_menus(
		array(
			'main-nav-menu' => __( 'Header' ),
			'footer-nav-menu' => __( 'Footer' )
	)
	);

register_post_type( 'portfolios',

		array(

		'labels' => array(

		'name' => __( 'Portfolio' ),

		'singular_name' => __( 'Portfolio' ),

		'all_items' => 'Portfolio Items',

		'add_new' => 'Add New Portfolio Item',

		'add_new_item' => __( 'Add New Portfolio Item' ),

		'edit' => __( 'Edit' ),

		'edit_item' => __( 'Edit Portfolio Item' ),

		'new_item' => __( 'New Portfolio Item' ),

		'view' => __( 'View Portfolio Item' ),

		'view_item' => __( 'View Portfolio Item' ),

		'search_items' => __( 'Search Portfolio Items' ),

		'not_found' => __( 'No Portfolio Items found' ),

		'not_found_in_trash' => __( 'No Portfolio Items found in Trash' ),

		'parent' => __( 'Parent Portfolio Item' )



		),

'public' => true,

'supports' => array('thumbnail','title','editor'),

'rewrite' => array( 'slug' => 'Portfolio', 'with_front' => true ),

'query_var' => true,

'exclude_from_search' => false,

'show_ui' => true,

'capability_type' => 'post'

		)
	);
register_taxonomy('gallery', 'portfolios', array(

		'hierarchical' => true,
		'labels' => array(
		'name' => _x( 'gallery', 'taxonomy general name' ),
		'singular_name' => _x( 'gallery', 'taxonomy singular name' ),
		'search_items' =>  __( 'Search Galleries' ),
		'all_items' => __( 'All Galleries' ),
		'parent_item' => __( 'Parent Gallery' ),
		'parent_item_colon' => __( 'Parent Gallery:' ),
		'edit_item' => __( 'Edit Gallery' ),
		'update_item' => __( 'Update Gallery' ),
		'add_new_item' => __( 'Add New Gallery' ),
		'new_item_name' => __( 'New GalleryName' ),
		'menu_name' => __( 'Galleries' )
		),
		// Control the slugs used for this taxonomy
		'rewrite' => array(
		'slug' => 'gallery', 
		'with_front' => true, 	
		'hierarchical' => true 
		),
	));
}

add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );  



add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 ); 



function remove_thumbnail_dimensions( $html ) {     



$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );    



return $html; } 







/*



Dropdown Menus



/*

// useless without this
if ( ! function_exists( 'wp_nav_menu' ) )
	return false;

/**
 * Tack on the blank option for urls not in the menu



 */



add_filter( 'wp_nav_menu_items', 'dropdown_add_blank_item', 10, 2 );



function dropdown_add_blank_item( $items, $args ) {



	if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) ) {



		if ( ( ! isset( $args->menu ) || empty( $args->menu ) ) && isset( $args->theme_location ) ) {



			$theme_locations = get_nav_menu_locations();



			$args->menu = wp_get_nav_menu_object( $theme_locations[ $args->theme_location ] );



		}



		$title = isset( $args->dropdown_title ) ? wptexturize( $args->dropdown_title ) : '&mdash; ' . 'Select a page' . ' &mdash;';



		if ( ! empty( $title ) )



			$items = '<option value="" class="blank">' . apply_filters( 'dropdown_blank_item_text', $title, $args ) . '</option>' . $items;



	}



	return $items;



}











/**



 * Remove empty options created in the sub levels output



 */



add_filter( 'wp_nav_menu_items', 'dropdown_remove_empty_items', 10, 2 );



function dropdown_remove_empty_items( $items, $args ) {



	if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) )



		$items = str_replace( "<option></option>", "", $items );



	return $items;



}

/**



 * Script to make it go (no jquery! (for once))



 */



add_action( 'wp_footer', 'dropdown_javascript' );



function dropdown_javascript() {



	if ( is_admin() ) return; ?>



	<script>



		var getElementsByClassName=function(a,b,c){if(document.getElementsByClassName){getElementsByClassName=function(a,b,c){c=c||document;var d=c.getElementsByClassName(a),e=b?new RegExp("\\b"+b+"\\b","i"):null,f=[],g;for(var h=0,i=d.length;h<i;h+=1){g=d[h];if(!e||e.test(g.nodeName)){f.push(g)}}return f}}else if(document.evaluate){getElementsByClassName=function(a,b,c){b=b||"*";c=c||document;var d=a.split(" "),e="",f="http://www.w3.org/1999/xhtml",g=document.documentElement.namespaceURI===f?f:null,h=[],i,j;for(var k=0,l=d.length;k<l;k+=1){e+="[contains(concat(' ', @class, ' '), ' "+d[k]+" ')]"}try{i=document.evaluate(".//"+b+e,c,g,0,null)}catch(m){i=document.evaluate(".//"+b+e,c,null,0,null)}while(j=i.iterateNext()){h.push(j)}return h}}else{getElementsByClassName=function(a,b,c){b=b||"*";c=c||document;var d=a.split(" "),e=[],f=b==="*"&&c.all?c.all:c.getElementsByTagName(b),g,h=[],i;for(var j=0,k=d.length;j<k;j+=1){e.push(new RegExp("(^|\\s)"+d[j]+"(\\s|$)"))}for(var l=0,m=f.length;l<m;l+=1){g=f[l];i=false;for(var n=0,o=e.length;n<o;n+=1){i=e[n].test(g.className);if(!i){break}}if(i){h.push(g)}}return h}}return getElementsByClassName(a,b,c)},



			dropdowns = document.getElementsByTagName( 'select' );



			console.log(dropdowns);



		for ( i=0; i<dropdowns.length; i++ )



			if ( dropdowns[i].className.match( '<?php echo apply_filters( 'dropdown_menus_class', 'dropdown-menu' ); ?>' ) ) dropdowns[i].onchange = function(){ if ( this.value != '' ) window.location.href = this.value; }



	</script>



	<?php



}
function dropdown_menu( $args ) {



	// if non array supplied use as theme location



	if ( ! is_array( $args ) )



		$args = array( 'menu' => $args );







	// enforce these arguments so it actually works



	$args[ 'walker' ] = new DropDown_Nav_Menu();



	$args[ 'items_wrap' ] = '<select id="%1$s" class="styled">%3$s</select>';







	// custom args for controlling indentation of sub menu items



	$args[ 'indent_string' ] = isset( $args[ 'indent_string' ] ) ? $args[ 'indent_string' ] : '&ndash;&nbsp;';



	$args[ 'indent_after' ] =  isset( $args[ 'indent_after' ] ) ? $args[ 'indent_after' ] : '';







	return wp_nav_menu( $args );



}

class DropDown_Nav_Menu extends Walker_Nav_Menu {







	// easy way to check it's this walker we're using to mod the output



	function is_dropdown() {



		return true;



	}

	function start_lvl( &$output, $depth ) {



		$output .= "</option>";



	}

	function end_lvl( &$output, $depth ) {



		$output .= "<option>";



	}

	function start_el( &$output, $item, $depth, $args ) {



		global $wp_query;



		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		$class_names = $value = '';

		$classes = empty( $item->classes ) ? array() : (array) $item->classes;

		$classes[] = 'menu-item-' . $item->ID;

		$classes[] = 'menu-item-depth-' . $depth;

		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_unique( array_filter( $classes ) ), $item, $args ) );



		$class_names = ' class="' . esc_attr( $class_names ) . '"';

		// select current item



		if ( apply_filters( 'dropdown_menus_select_current', true ) )



			$selected = in_array( 'current-menu-item', $classes ) ? ' selected="selected"' : '';

		$output .= $indent . '<option' . $class_names .' value="'. $item->url .'"'. $selected .'>';

		// push sub-menu items in as we can't nest optgroups



		$indent_string = str_repeat( apply_filters( 'dropdown_menus_indent_string', $args->indent_string, $item, $depth, $args ), ( $depth ) ? $depth : 0 );



		$indent_string .= !empty( $indent_string ) ? apply_filters( 'dropdown_menus_indent_after', $args->indent_after, $item, $depth, $args ) : '';

		$item_output = $args->before . $indent_string;



		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;



		$item_output .= $args->after;

		$output .= apply_filters( 'walker_nav_menu_dropdown_start_el', $item_output, $item, $depth, $args );

	}

	function end_el( &$output, $item, $depth ) {

		$output .= apply_filters( 'walker_nav_menu_dropdown_end_el', "</option>\n", $item, $depth);

	}
}

?>