<?php
function theme_shortcode_portfolio($atts, $content = null, $code) {
	$opts = shortcode_atts(array(
		'column' => 4,
		'cat' => '',
		'max' => 12,
		'nopaging' => 'false',
		'sortable' => 'false',
		'sortabletype'=>'one',
		'sortableasing'=>'swing',
		'desc'=> '',
		'title' => '',
		'titlelinkable' => 'false',
		'desc_length' => 'default',
		'carousel'=>'false',
		'calbar'=>'true',
		'caltitle'=>'',
		'caleasing'=>'swing',
		'calanimate'=>'scrollHorz',
		'calspeed'=>'1000',
		'caldelay'=>'5000',
		'calink'=>'',
		'caltext'=>'View More',
		'more' => '',
		'image_height'=>false,
		'image_width'=>false,
		'advancedesc'=>false,
		'list_height'=>false,
		'lightboxtitle' => 'portfolio',
		'morebutton'=>'false',
		'moretext' => '',
		'sidebar' => 'false',
		'sortable_all'=> 'true',
		'current' => '',
		'ids'=>'',
		'order'=> 'ASC',
		'orderby'=> 'menu_order', //none, id, author, title, date, modified, parent, rand, comment_count, menu_order
	), $atts);

	extract($opts);
	global $post;
	$layout = get_post_meta($post->ID, '_page_layout', true);
	if(empty($layout) || $layout == 'default'){
		$layout=theme_get_option('portfolio','layout');
	}
	switch($column){
	case 2:
		$column_class = 'portfolios_two_columns';
		if(empty($layout) || $layout == 'full'){
			if($carousel == 'true')
				$size = array(411, (int)theme_get_option('portfolio','2_columns_height'));
			else
				$size = array(431, (int)theme_get_option('portfolio','2_columns_height'));
		}else{
			if($carousel == 'true')
				$size = array(256, (int)theme_get_option('portfolio','2_columns_height'));
			else
				$size = array(266, (int)theme_get_option('portfolio','2_columns_height'));
		}
		break;
	case 3:
		$column_class = 'portfolios_three_columns';
		if(empty($layout) || $layout == 'full'){
			if($carousel == 'true')
				$size = array(260, (int)theme_get_option('portfolio','3_columns_height'));
			else
				$size = array(274, (int)theme_get_option('portfolio','3_columns_height'));
		}else{
			if($carousel == 'true')
				$size = array(157, (int)theme_get_option('portfolio','3_columns_height'));
			else
				$size = array(164, (int)theme_get_option('portfolio','3_columns_height'));
		}
		break;
	case 4:
	default:
		$column_class = 'portfolios_four_columns';
		if(empty($layout) || $layout == 'full'){
			if($carousel == 'true')
				$size = array(185, (int)theme_get_option('portfolio','4_columns_height'));
			else
				$size = array(195, (int)theme_get_option('portfolio','4_columns_height'));
		}else{
			if($carousel == 'true')
				$size = array(108, (int)theme_get_option('portfolio','4_columns_height'));
			else
				$size = array(113, (int)theme_get_option('portfolio','4_columns_height'));
		}
	}
	if($image_height && is_numeric($image_height)){
		$size[1] = $image_height;
	}
	$list_width = false;
	if($image_width && is_numeric($image_width)){
		$size[0] = $image_width;
		$list_width = $image_width + 40;
	}
    $item_height = '';
	if($list_height){
		$item_height = 'height:'.$list_height.'px';
	}
	//deprecated
	if($title == ''){
		if(theme_get_option('portfolio','display_title') || $column == 1){
			$title = true;
		}
	}
	if($desc == ''){
		if(theme_get_option('portfolio','display_excerpt') || $column == 1){
			$desc = true;
		}
	}
	switch($more){
	case '':
		if( theme_get_option('portfolio','display_more_button') ){
			$more = true;
		}
		break;
	case 'false':
		$more = false;
		break;
	case 'true':
	default:
		$more = true;
		break;
	}
	$opts['list_width'] = $list_width;
	$opts['size'] = $size;
	$opts['item_height']=$item_height;
	$opts['more']=$more;
	$opts['morebutton']=$morebutton;
	$opts['moretext']=$moretext;
	$opts['desc']=$desc;
	$opts['title']=$title;
	
	$opts['column_class']=$column_class;
	$opts['carousel']=$carousel;
	$output = '' ;
	$terms = array();
	
	if ($sortable != 'false' && $carousel=='false') {
		wp_print_scripts('jquery-quicksand');
		$output  = '<section class="portfolios sortable">';
		$output .= '<header class="sort_by_cat">';
		$output .= '<ul id="portfolio-filter" data-sortable="'.$sortabletype.'" data-options="'.htmlspecialchars(serialize($opts)).'">';
		if(!empty($current) && $sortabletype=='all'){
			$opts['current']=$current;
			$opts['cat'] = $current;
			if($sortable_all == 'true'){
				$output .= '<li><a href="#All" title="all">'.__('All',THEME_FRONT_DOMAIN).'</a></li>';
			}
		}else{
			$output .= '<li><a href="#All" title="all" class="current">'.__('All',THEME_FRONT_DOMAIN).'</a></li>';
		}
		if ($cat != '') {
			foreach(explode(',', $cat) as $term_slug) {
				$terms[] = get_term_by('slug', $term_slug, 'portfolio_category');
			}
		} else {
			$terms = get_terms('portfolio_category', 'hide_empty=1');
		}

		foreach($terms as $term) {
			if (! empty($term)) {
				if(!empty($current) && $sortabletype=='all' && $current==$term->slug){
					$output .= '<li><a title="'.$term->slug.'" href="#'.ucwords($term->slug).'" class="current">' . $term->name . '</a></li>';
				}else
					$output .= '<li><a title="'.$term->slug.'" href="#'.ucwords($term->slug).'">' . $term->name . '</a></li>';
			}
		}

		$output .= '</ul></header>';
	} else {
		$output = '<section class="portfolios">';
	}
	$carousel_params = '';
	if($carousel == 'true'){
		$carousel_params = 'data-animate="'.$calanimate.'" data-speed="'.$calspeed.'" data-delay="'.$caldelay.'" data-easing="'.$caleasing.'"';
	}
	$output .='<div class="portfolio-content '.($carousel=='false'?'':'carousel-box').'" '.$carousel_params.'>';
	if($carousel == 'true' && $calbar =='true'){
		$viewmore = empty($calink)? '': '<a class="viewmore" href="'.theme_get_superlink($calink).'">'.$caltext.'</a>';
		$output .= '<div class="portfolio-control">';
		$output .= '<div class="cycle-title alignleft"><h3>'.$caltitle.'</h3></div>';
		$output .= '<div class="cycle-control alignright">'.$viewmore.'<div class="cycleforward">&gt;</div><div class="cycledivider"></div><div class="cycleback">&lt;</div></div>';
		$output .= '</div>';
	}
	$list = theme_generator('portfolio_list',$opts);
	$output .= $list;
	$output .='</div></section>';
	return $output;


}
add_shortcode('portfolio', 'theme_shortcode_portfolio');
