<?php
class themeGenerator {
	function title(){


		global $page, $paged;
		
		$output = '';

		$title_format = wpml_t(THEME_NAME, 'Html Title Format',theme_get_option('advanced','title_format'));
		if(empty($title_format)){

			$output =  wp_title( '|', false, 'right' );

			// Add the blog name.
			$output .=  get_bloginfo( 'name' );

			// Add the blog description for the home/front page.
			$site_description = get_bloginfo( 'description', 'display' );
			if ( !empty($site_description) && is_front_page() )
				$output .=  " | $site_description";
		}else{

			$page_title = wp_title( '', false);
			$site_description = get_bloginfo( 'description', 'display' );
			if ( !empty($site_description) && is_front_page() )
				$page_title =  " | $site_description";
			
			$site_title = get_bloginfo( 'name' );

			$title_format =  str_replace("&%s%&",$site_title, $title_format);
			$title_format =  str_replace("&%p%&",$page_title, $title_format);
			$output = $title_format;
		}
		
		// Add a page number if necessary:
		if ( $paged >= 2 || $page >= 2 )
			$output .= ' | ' . sprintf( __( 'Page %s', THEME_FRONT_DOMAIN), max( $paged, $page ) );
		
		return $output;
	}

	function wpml_flags(){
		$output = '';
		if(function_exists('icl_get_languages')){
			$languages = icl_get_languages('skip_missing=0');
			if(!empty($languages) && is_array($languages)){
				$output .= '<div id="language_flags"><ul>';
				foreach($languages as $l){
					$output .= '<li>';
					if(!$l['active']) $output .=  '<a href="'.$l['url'].'" title="'.$l['native_name'].'">';
					$output .=  '<img src="'.$l['country_flag_url'].'" alt="'.$l['language_code'].'" />';
					if(!$l['active']) $output .=  '</a>';
					$output .=  '</li>';
				}
				$output .=  '</ul></div>';
			}
		}
		return $output;
	}

	function menu(){
		if (theme_get_option('general','enable_nav_menu') && has_nav_menu( 'primary-menu' ) ) {
			wp_nav_menu( array( 
				'theme_location' => 'primary-menu',
				'container' => 'nav',
				'menu_id'	=> 'dropmenu',
				'fallback_cb' => '',
				'walker' => new Theme_Walker_Nav_Menu
			));
		}else{
			$excluded_pages_with_childs = theme_get_excluded_pages();

			$active_class = (is_front_page()||(!get_option('page_on_front')&&is_home())) ? 'class="current_page_item"' : '';
			if($active_class)
			{
				add_filter('page_css_class','remove_post_current_css');
			}
			$output = '<nav class="menu-main-navigation-container">';
			$output .= '<ul id="dropmenu" class="menu">';
			$output .= '<li ' .$active_class. '><a href="' .get_option('home'). '">'.__('Home',THEME_FRONT_DOMAIN).'</a></li>';
			$output .= wp_list_pages("sort_column=menu_order&exclude=$excluded_pages_with_childs&title_li=&echo=0&depth=4");
			$output .= '</ul>';
			$output .= '</nav>';
			return $output;
			
		}
	}
	function author_box(){
		if(theme_get_option('blog','author_link_to_website')){
			$author = get_the_author_link();
		}else{
			$author = get_the_author_posts_link();
		}
		
		if ( get_the_author_meta( 'description' ) ) : 
?>		
			<div id="author" class="imagelist">
				<div class="gravatar"><?php echo get_avatar( get_the_author_meta('user_email'), '100' ); ?><span class='framedown'></span></div>
				<h4><?php echo $author;?></h4>
				<p><?php the_author_meta( 'description' ); ?></p>
				<p class="contact-icons">
<?php if ( theme_get_option('blog','author_mail')) : ?>
	<img src="<?php echo THEME_IMAGES;?>/icons/small-email.png" width="16" height="16" alt="" class="vm"/> <a href="#">Mail <?php the_author_meta( 'display_name' ); ?></a>				
<?php endif; ?>
<?php if ( theme_get_option('blog','author_facebook')) : ?>
	<img src="<?php echo THEME_IMAGES;?>/icons/small-facebook.png" width="16" height="16" alt="" class="vm"/> <a href="http://facebook.com/<?php the_author_meta( 'facebook' ); ?>">Facebook <?php the_author_meta( 'display_name' ); ?></a>				
<?php endif; ?>  
<?php if ( theme_get_option('blog','author_twitter')) : ?>
	<img src="<?php echo THEME_IMAGES;?>/icons/small-twitter.png" width="16" height="16" alt="" class="vm"/> <a href="http://twitter.com/<?php the_author_meta( 'twitter' ); ?>">Follow <?php the_author_meta( 'display_name' ); ?></a>						
<?php endif; ?> 
</p>			   
 </div>
<div class="clear"></div>
<?php
		 endif;
		
	}
	function sidebar(){
		sidebar_generator('get_sidebar',get_queried_object_id());
	}

	function footer_sidebar(){
		sidebar_generator('get_footer_sidebar');
	}
	function introduce($id = NULL,$type = null) {
		$slug = '';
		if($id != null)
			$type = get_post_meta($id, '_introduce_text_type', true);
		elseif(is_home()){
			$slug = 'blog';
			$type = theme_get_option('blog','introduce_type');
		}elseif( class_exists( 'Woocommerce' ) && is_shop() ){
			$slug = 'shop';
			$type = theme_get_option('woocommerce','shop_introduce_type');
		}
		if (empty($type) || $type == null){
			$type = 'default';
		}
		if($type == 'default'){
			if(is_singular('portfolio')){
				$slug = 'portfolio';
				$type = theme_get_option('portfolio','introduce_type');
			}elseif( class_exists( 'Woocommerce' ) && is_product() ){
				$slug = 'product';
				$type = theme_get_option('woocommerce','product_introduce_type');
			}elseif( class_exists( 'Woocommerce' ) && is_product_category() ){
				$slug = 'product_cat';
				$type = theme_get_option('woocommerce','product_cat_introduce_type');
			}elseif(is_single()){
				$slug = 'single';
				$type = theme_get_option('blog','single_introduce_type');
			}
			if($type == 'default'){
				$slug = 'general';
				$type = theme_get_option('general','introduce');
				if(! $type || $type=='disable'){
					return;
				}
			}
		}
		if ($type == 'slideshow'){
			$stype = 'nivo';
			$category = false;
			if($slug == 'blog'){
				$stype = theme_get_option('blog','slideshow_type');
				$category = theme_get_option('blog','slideshow_category');
				$number = theme_get_option('blog','slideshow_number');
				$nav = theme_get_option('blog','slideshow_nav');
				$height = theme_get_option('blog','slideshow_height');
				$navNumber = theme_get_option('blog','slideshow_nav_number');
			}elseif($slug == 'single'){
				$stype = theme_get_option('blog','single_slideshow_type');
				$category = theme_get_option('blog','singlee_slideshow_category');
				$number = theme_get_option('blog','single_slideshow_number');
				$nav = theme_get_option('blog','single_slideshow_nav');
				$height = theme_get_option('blog','single_slideshow_height');
				$navNumber = theme_get_option('blog','single_slideshow_nav_number');
			}elseif($slug == 'portfolio'){
				$stype = theme_get_option('portfolio','slideshow_type');
				$category = theme_get_option('portfolio','slideshow_category');
				$number = theme_get_option('portfolio','slideshow_number');
				$nav = theme_get_option('portfolio','slideshow_nav');
				$height = theme_get_option('portfolio','slideshow_height');
				$navNumber = theme_get_option('portfolio','slideshow_nav_number');
			}elseif($slug == 'general'){
				$stype = theme_get_option('general','general_slideshow_type');
				$category = theme_get_option('general','general_slideshow_category');
				$number = theme_get_option('general','general_slideshow_number');
				$nav = theme_get_option('general','general_slideshow_nav');
				$height = theme_get_option('general','general_slideshow_height');
				$navNumber = theme_get_option('general','general_slideshow_nav_number');
			}elseif($slug == 'shop'){
				$stype = theme_get_option('woocommerce','shop_slideshow_type');
				$category = theme_get_option('woocommerce','shop_slideshow_category');
				$number = theme_get_option('woocommerce','shop_slideshow_number');
				$nav = theme_get_option('woocommerce','shop_slideshow_nav');
				$height = theme_get_option('woocommerce','shop_slideshow_height');
				$navNumber = theme_get_option('woocommerce','shop_slideshow_nav_number');
			}elseif($slug == 'product'){
				$stype = theme_get_option('woocommerce','product_slideshow_type');
				$category = theme_get_option('woocommerce','product_slideshow_category');
				$number = theme_get_option('woocommerce','product_slideshow_number');
				$nav = theme_get_option('woocommerce','product_slideshow_nav');
				$height = theme_get_option('woocommerce','product_slideshow_height');
				$navNumber = theme_get_option('woocommerce','product_slideshow_nav_number');
			}elseif($slug == 'product_cat'){
				$stype = theme_get_option('woocommerce','product_cat_slideshow_type');
				$category = theme_get_option('woocommerce','product_cat_slideshow_category');
				$number = theme_get_option('woocommerce','product_cat_slideshow_number');
				$nav = theme_get_option('woocommerce','product_cat_slideshow_nav');
				$height = theme_get_option('woocommerce','product_cat_slideshow_height');
				$navNumber = theme_get_option('woocommerce','product_cat_slideshow_nav_number');
			}else{
				$stype = get_post_meta($id, '_slideshow_type', true);
				$category = get_post_meta($id, '_slideshow_category', true);
				$number = get_post_meta($id, '_slideshow_number', true);
				$nav = get_post_meta($id, '_slideshow_nav', true);
				$height = get_post_meta($id,'_slideshow_height',true);
				$navNumber = get_post_meta($id,'_slideshow_nav_number',true);
			}
			if(!is_bool($nav)){
				if($nav == 'false')
					$nav = false;
				else
					$nav = true;
			}
			return theme_generator('slideShow',$stype,$category,$number,$nav,$height,$navNumber);
		}else if($type == 'disable'){
			return;
		}
		$title = get_bloginfo('name');
		$header = '';
		$text = get_bloginfo('description');
		$text = empty($text) ? __('No Custom Blog Description Set, You can set this in the Administration Panel of the Theme.',THEME_FRONT_DOMAIN) : $text;
		if(is_home()){
			$title = __('Blog',THEME_FRONT_DOMAIN);
		}
		if( class_exists( 'Woocommerce' ) && is_shop() ){
			$title = __('Shop',THEME_FRONT_DOMAIN);
		}
		if (is_single()){
			$title = __('Blog',THEME_FRONT_DOMAIN);
			$show_in_header = theme_get_option('blog','show_in_header');
			if (is_singular('portfolio')){
				$title = __('Portfolio',THEME_FRONT_DOMAIN);
				$show_in_header = theme_get_option('portfolio','show_in_header');
			}
			if ( class_exists( 'Woocommerce' ) && is_product() ){
				$title = __('Product',THEME_FRONT_DOMAIN);
				$show_in_header = theme_get_option('woocommerce','show_in_header');
			}
			$header = get_the_title($id);
			if($show_in_header){
				$text = '<div class="entry_meta">';
				$text .= $this->post_meta(true);
				$text .= '</div>';
			}else{
				$display_post = &get_post($id);
				$text = $display_post->post_excerpt;
			}
		}
		if (is_page()){
			$header = '';
			$title =  get_the_title($id);
		}
		if ( is_archive() && $title != __('Shop',THEME_FRONT_DOMAIN) ){
			$title = __('Archives',THEME_FRONT_DOMAIN);
			if(function_exists('is_post_type_archive')){
				if(is_post_type_archive()){
					$title = wpml_t(THEME_NAME, get_query_var( 'post_type' ) . ' Post Type Archive Title',theme_get_option('advanced','archive_'.get_query_var( 'post_type' ).'_title'));
					if($title === false){
						$title = theme_get_option('advanced','archive_'.get_query_var( 'post_type' ).'_title');
					}
					$text = wpml_t(THEME_NAME, get_query_var( 'post_type' ) . ' Post Type Archive Text',theme_get_option('advanced','taxonomy_'.get_query_var( 'taxonomy' ).'_text'));
					if($text === false ){
						$text = theme_get_option('advanced','archive_'.get_query_var( 'post_type' ).'_text');
					}
					$post_type = get_post_type_object( get_query_var( 'post_type' ) );
					$title = sprintf($title,$post_type->name);
					$text = sprintf($text,$post_type->name);
				}
			}
			if(is_category()){
				$title = wpml_t(THEME_NAME, 'Category Archive Title',theme_get_option('advanced','category_title'));
				$header = wpml_t(THEME_NAME, 'Category Archive Text',theme_get_option('advanced','category_text'));
				$title = sprintf($title,single_cat_title('',false));
				$header = sprintf($header,single_cat_title('',false));
				$text = single_cat_title('',false);
			}elseif(is_tag()){
				$title = wpml_t(THEME_NAME, 'Tag Archive Title',theme_get_option('advanced','tag_title'));
				$header = wpml_t(THEME_NAME, 'Tag Archive Text',theme_get_option('advanced','tag_text'));
				$title = sprintf($title,single_tag_title('',false));
				$header = sprintf($header,single_tag_title('',false));
				$text = single_tag_title('',false);
			}elseif(is_day()){
				$title = wpml_t(THEME_NAME, 'Daily Archive Title',theme_get_option('advanced','daily_title'));
				$header = wpml_t(THEME_NAME, 'Daily Archive Text',theme_get_option('advanced','daily_text'));
				$title = sprintf($title,get_the_time('F jS, Y'));
				$header = sprintf($header,get_the_time('F jS, Y'));
				$text = get_the_time('F jS, Y');
			}elseif(is_month()){
				$title = wpml_t(THEME_NAME, 'Monthly Archive Title',theme_get_option('advanced','monthly_title'));
				$header = wpml_t(THEME_NAME, 'Monthly Archive Text',theme_get_option('advanced','monthly_text'));
				$title = sprintf($title,get_the_time('F,Y'));
				$header = sprintf($header,get_the_time('F,Y'));
				$text = get_the_time('F,Y');
			}elseif(is_year()){
				$title = wpml_t(THEME_NAME, 'Yearly Archive Title',theme_get_option('advanced','yearly_title'));
				$header = wpml_t(THEME_NAME, 'Yearly Archive Text',theme_get_option('advanced','yearly_text'));
				$title = sprintf($title,get_the_time('Y'));
				$header = sprintf($header,get_the_time('Y'));
				$text = get_the_time('Y');
			}elseif(is_author()){
				$title = wpml_t(THEME_NAME, 'Author Archive Title',theme_get_option('advanced','author_title'));
				$header = wpml_t(THEME_NAME, 'Author Archive Text',theme_get_option('advanced','author_text'));
				if(get_query_var('author_name')){
					$curauth = get_user_by('slug', get_query_var('author_name'));
				} else {
					$curauth = get_userdata(get_query_var('author'));
				}
				$title = sprintf($title,$curauth->nickname);
				$header = sprintf($header,$curauth->nickname);
				$text = $curauth->nickname;
			}elseif(isset($_GET['paged']) && !empty($_GET['paged'])) {
				$header = wpml_t(THEME_NAME, 'Blog Archive Title',theme_get_option('advanced','blog_title'));
				$text = wpml_t(THEME_NAME, 'Blog Archive Text',theme_get_option('advanced','blog_text'));
			}elseif(is_tax()){
				$title = wpml_t(THEME_NAME, get_query_var( 'taxonomy' ) . ' Taxonomy Archive Title',theme_get_option('advanced','taxonomy_'.get_query_var( 'taxonomy' ).'_title'));
				if($title === false){
					$title = theme_get_option('advanced','taxonomy_'.get_query_var( 'taxonomy' ).'_title');
				}
				if($title === false){
					$title = wpml_t(THEME_NAME, 'Taxonomy Archive Title',theme_get_option('advanced','taxonomy_title'));
				}
				$header = wpml_t(THEME_NAME, get_query_var( 'taxonomy' ) . ' Taxonomy Archive Text',theme_get_option('advanced','taxonomy_'.get_query_var( 'taxonomy' ).'_text'));
				if($header === false ){
					$header = theme_get_option('advanced','taxonomy_'.get_query_var( 'taxonomy' ).'_text');
				}
				if($header === false ){
					$header = wpml_t(THEME_NAME, 'Taxonomy Archive Text',theme_get_option('advanced','taxonomy_text'));
				}
				$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
				$title = sprintf($title,$term->name);
				$header = sprintf($header,$term->name);
				$text = $term->name;
			}
			$title = stripslashes($title);
			$text = stripslashes($text);
		}

		if (is_404()) {
			$title = wpml_t(THEME_NAME, '404 Page Title',theme_get_option('advanced','404_title'));
			$text = wpml_t(THEME_NAME, '404 Page Text',theme_get_option('advanced','404_text'));
			$title = stripslashes($title);
			$text = stripslashes($text);
		}
		
		if (is_search()) {
			$title = wpml_t(THEME_NAME, 'Search Page Title',theme_get_option('advanced','search_title'));
			$text = wpml_t(THEME_NAME, 'Search Page Text',theme_get_option('advanced','search_text'));
			$text = stripslashes( strip_tags( get_search_query() ) );
			$title = stripslashes($title);
			$text = stripslashes($text);
		}
		
		/*if(is_home()||is_single()){
			if(is_singular('portfolio'))
				$custom_title =  wpml_t(THEME_NAME, 'Portfolio Custom Title',theme_get_option('portfolio','custom_title'));
			else
				$custom_title = wpml_t(THEME_NAME, 'Blog Custom Title',theme_get_option('blog','custom_title'));
		}else
			$custom_title = trim(get_post_meta($id, '_custom_title', true));
		
		if(is_home()||(is_single()&&!is_singular('portfolio'))){
			if(is_singular('portfolio'))
				$custom_title = wpml_t(THEME_NAME, 'Portfolio Custom Text',theme_get_option('portfolio','custom_introduce_text'));
			else
				$custom_title = wpml_t(THEME_NAME, 'Blog Custom Title',theme_get_option('blog','custom_introduce_text'));
		}else	
			$custom_text = trim(get_post_meta($id, '_custom_introduce_text', true));*/
		switch($slug){
		case 'blog':
			$custom_title = wpml_t(THEME_NAME, 'Blog Custom Title',theme_get_option('blog','custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Blog Custom Title',theme_get_option('blog','custom_introduce_text'));
			break;
		case 'single':
			$custom_title = wpml_t(THEME_NAME, 'Single Blog Custom Title',theme_get_option('blog','single_custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Single Blog Custom Title',theme_get_option('blog','single_custom_introduce_text'));
			break;
		case 'portfolio':
			$custom_title =  wpml_t(THEME_NAME, 'Portfolio Custom Title',theme_get_option('portfolio','custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Portfolio Custom Text',theme_get_option('portfolio','custom_introduce_text'));
			break;
		case 'product':
			$custom_title = wpml_t(THEME_NAME, 'Single Product Custom Title',theme_get_option('woocommerce','product_custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Single Product Custom Text',theme_get_option('woocommerce','product_custom_introduce_text'));
			break;
		case 'product_cat':
			$custom_title = wpml_t(THEME_NAME, 'Product Category Custom Title',theme_get_option('woocommerce','product_cat_custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Product Category Custom Title',theme_get_option('woocommerce','product_cat_custom_introduce_text'));
			break;
		case 'shop':
			$custom_title = wpml_t(THEME_NAME, 'Shop Custom Title',theme_get_option('woocommerce','shop_custom_title'));
			$custom_text = wpml_t(THEME_NAME, 'Shop Custom Text',theme_get_option('woocommerce','shop_custom_introduce_text'));
			break;
		default:
			$custom_title = trim(get_post_meta($id, '_custom_title', true));
			$custom_text = trim(get_post_meta($id, '_custom_introduce_text', true));
		}
		
		if (!empty($custom_title)) {
			$title = $custom_title;
		}
		if (!empty($custom_text)) {
			$header = '';
			$text = $custom_text;
		}
?>
<div id="heading" class="heading-inner">
<div class="light-span">
<div class="wrap-center">
			<div class="cont-vert-description">
					 <div class="outer-vert-description">
						<div class="inner-vert-description">
						  <div class="vert-position-description page-description">
<?php	
		if (!empty($header)) {
			echo '<h2>' . $header . '</h2>';
		}

		echo '<p>';
		echo $text;
		echo '</p>';

?>

						  </div>
						</div>
					 </div>
			</div>
		 <div class="cont-vert-title">
					 <div class="outer-vert-title">
						<div class="inner-vert-title">
			<h1 class="vert-position-title page-title"><?php echo ucwords($title); ?></h1>
						</div>
					  </div>
			</div>
			<div class="clear"></div>
	 </div>
</div>
</div>
<?php
	}

	function breadcrumbs($post_id = NULL) {
		$output = '';
		$tag = true;
		if($post_id =='blog')
			$tag = theme_get_option('blog','disable_breadcrumb');
		else
			$tag = get_post_meta($post_id, '_disable_breadcrumb', true);
		if(!theme_is_enabled($tag, theme_get_option('general','disable_breadcrumb'))){
			if(!isset($disable) || $disable == -1){
				$output = breadcrumbs_plus(array(
					'prefix' => '<section id="breadcrumbs" class="page-subtitle">',
					'suffix' => '</section>',
					'title' => false,
					'home' => __( 'Home', THEME_FRONT_DOMAIN ),
					'sep' => '&raquo;',
					'front_page' => false,
					'bold' => false,
					'blog' => __( 'Blog', THEME_FRONT_DOMAIN ),
					'echo' => false
				));
			}
		}
		return $output;
	}

	function portfolio_featured_image($layout='') {
		if($layout == 'full'){
			$width = 936;
		}else{
			$width = 606;
		}
		
		$adaptive_height = theme_get_option('portfolio', 'adaptive_height');
	
		$source_type = 'form';
		if (has_post_thumbnail()){ 
			$image_src_array = @wp_get_attachment_image_src(get_post_thumbnail_id(),'full', true);
			$image_src = $image_src_array[0];
			$image_href = $image_src;
			$image_src= get_post_thumbnail_id();
			if($adaptive_height){
				$height = floor($width*($image_src_array[2]/$image_src_array[1]));
			}else{
				$height = theme_get_option('portfolio', 'fixed_height');
			}
		}else{
			$image_src =  theme_get_option('portfolio','default_portfolio_image');
			$image_href = theme_parse_image($image_src,'post-image-placer.png',true);
			$image_href = $image_href['media'];
			$image_src = theme_parse_image($image_src,'post-image-placer.png',false);
			$source_type = $image_src['method'];
			$image_src = $image_src['media'];

			if(!$adaptive_height)
				$height = theme_get_option('portfolio', 'fixed_height');
			else
				$height = '';
		}

		$resize =theme_resize($image_src,$width,$height,$source_type);
		$image_src = $resize['url'];
		$image_msg = $resize['msg'];
		$output = '<div class="post-image post-image-single">';
		if(theme_get_option('portfolio', 'featured_image_lightbox')){
			$output .= '<a class="image_icon_zoom" href="'.$image_href.'" rel="prettyPhoto">';	
			$output .= '<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$image_msg.'" src="'.$image_src.'" alt="'.get_the_title().'" style="width:'.$width.'px;height:'.$resize['height'].'px;opacity: 0; visibility: visible;">';
			$output .='<span class="overlay preload"></span>';
			$output .='</a>';
		}else{
			$output .='<a href="'.get_permalink().'" rel="bookmark">';
			$output .='<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$image_msg.'" src="'.$image_src.'" alt="'.get_the_title().'"style="width:'.$width.'px;height:'.$resize['height'].'px;opacity: 0; visibility: visible;">';
			$output .='<span class="overlay preload"></span>';
			$output .='</a>';
		}
		$output .= '</div>';

		return $output;
	}

	function blog_featured_image($type='full',$layout='',$height=null) {
		if($type == 'none')
			return;
		if($layout == 'full'){
			$width = 936;
		}elseif(is_numeric($layout)){
			$width = $layout-2;
		}else{
			$width = 608;
		}
		$source_type = 'form';
		if (has_post_thumbnail()){ 
			$image_src_array = @wp_get_attachment_image_src(get_post_thumbnail_id(),'full', true);
			$image_src = $image_src_array[0];
			$image_href = $image_src;
			$image_src= get_post_thumbnail_id();
			if($height == null){
				$adaptive_height = theme_get_option('blog', 'adaptive_height');
				if($adaptive_height){
					$height = floor($width*($image_src_array[2]/$image_src_array[1]));
				}else{
					$height = theme_get_option('blog', 'fixed_height');
				}
			}
		}else{
			$image_src =  theme_get_option('blog','default_blog_image');
			$image_href = theme_parse_image($image_src,'post-image-placer.png',true);
			$image_href = $image_href['media'];
			$image_src = theme_parse_image($image_src,'post-image-placer.png',false);
			$source_type = $image_src['method'];
			$image_src = $image_src['media'];
			if($height == null){
				$adaptive_height = theme_get_option('blog', 'adaptive_height');
				if($adaptive_height){
					$height = '';
				}else{
					$height = theme_get_option('blog', 'fixed_height');
				}
			}
		}
	
		if($type=='left'||$type=='right'){
			$width = theme_get_option('blog', 'left_width');
			if($height == null)
				$height = theme_get_option('blog', 'left_height');
			$resize = theme_resize($image_src,$width,$height,$source_type);
			$image_src = $resize['url'];
			$image_msg = $resize['msg'];
		}else{
			$resize = theme_resize($image_src,$width,$height,$source_type);
			$image_src = $resize['url'];
			$image_msg = $resize['msg'];
		}
		$lightbox = false;
		if(!is_single())
			$lightbox = theme_get_option('blog',"featured_image_lightbox");
		else
			$lightbox = theme_get_option('blog',"sinlge_featured_image_lightbox");
		$output = '';
		if(!is_single()){
			$lightbox_desc = theme_get_option('blog',"featured_image_lightbox_desc");
			$title = $lightbox_desc ? 'title="'. get_the_excerpt().'"' : 'title=""';
			$output .= '<div class="post-image" style="width:'.$width.'px;height:'.($height+1).'px">';
			$output .='<a href="'.($lightbox?$image_href:get_permalink()).'" '.($lightbox?'rel="prettyPhoto"':'rel="bookmark"').' '.$title.'>';	
			$output .='<img width="'.$width.'" height="'.$height.'" data-msg="'.$image_msg.'" src="'.$image_src.'" alt="'.get_the_title().'" style="width:'.$width.'px;opacity: 0; visibility: visible;">';
			$output .='<span class="overlay preload"></span>';
			$output .='</a>';
		}else{
			$lightbox_desc = theme_get_option('blog',"single_featured_image_lightbox_desc");
			$title = $lightbox_desc ? 'title="'. get_the_excerpt().'"' : 'title=""';
			$output .='<div class="post-image post-image-single" style="width:'.$width.'px;height:'.($height+1).'px">';
			$output .='<a href="'.$image_href.'" '.($lightbox?'rel="prettyPhoto"':'rel="bookmark"').' '.$title.'>';	
			$output .='<img width="'.$width.'" height="'.$height.'" data-msg="'.$image_msg.'" src="'.$image_src.'" alt="'.get_the_title().'" style="width:'.$width.'px;opacity: 0; visibility: visible;">';
			$output .='<span class="overlay preload"></span>';
			$output .='</a>';
		}
		$output .='</div>';
		return $output;
	}
	function attachment_content($type='full',$layout=''){
		global $post;
		$file_type = explode("/",$post->post_mime_type);
		if($file_type[0]=='image'){
			if($layout == 'full'){
				$width = 936;
			}elseif(is_numeric($layout)){
				$width = $layout-2;
			}else{
				$width = 606;
			}
			$height = '';
			$resize = theme_resize($post->ID,$width-2,$height,'form');
			$image_src = $resize['url'];
			$image_msg = $resize['msg'];
?>
			<div class="post-image">
				<a href="<?php echo $post->guid;?>">
<?php if($image_msg = 'error'):?>
				<img class="post-image-single" src="<?php echo $post->guid;?>" alt="<?php the_title();?>" style="max-width:<?php echo $width-2;?>px"/>
<?php else:?>
				<img class="post-image-single" width="<?php echo $resize['width'];?>" height="<?php echo $resize['height'];?>" data-msg="<?php echo $image_msg;?>" src="<?php echo $image_src;?>" alt="<?php the_title();?>"/>
<?php endif;?>			
				</a>   

			</div>
<?php
		}else{
?>
			<a href="<?php echo $post->guid;?>">
			   <?php the_title();?>
			</a> 
<?php
		}

	}
	function post_meta($single = false) {
		global $post;
		$output = '';
		if($single){
			if(is_singular('portfolio')){
				$meta_items = theme_get_option('portfolio','single_meta_items');
			}elseif( class_exists( 'Woocommerce' ) && is_product() ){
				$meta_items = theme_get_option('woocommerce','single_meta_items');
			}else{
				$meta_items = theme_get_option('blog','single_meta_items');
			}
		}else{
			$meta_items = theme_get_option('blog','meta_items');
		}
		if(!empty($meta_items)){
			$outputs = array();
			foreach($meta_items as $item){
				switch($item){
					case 'category':
							if(is_singular('portfolio')){
								$terms = get_the_terms(get_the_id(), 'portfolio_category');
								$terms_name = array();
								if (is_array($terms)) {
									foreach($terms as $term) {
										$terms_name[] = $term->name;
									}
								}
								$category_list = implode(' ',$terms_name);
							}elseif( class_exists( 'Woocommerce' ) && is_product() ){
								$terms = get_the_terms(get_the_id(), 'product_cat');
								$terms_name = array();
								if (is_array($terms)) {
									foreach($terms as $term) {
										$terms_name[] = $term->name;
									}
								}
								$category_list = implode(' ',$terms_name);
							}elseif(is_single()){
								$category_list = theme_get_all_category(', ',$post->ID);
							}
							if(is_single())
								$outputs[] = '<span class="categories">'.$category_list.'</span>';
						break;
					case 'tags':
						$tags = get_the_tag_list(__('Tags: ', THEME_FRONT_DOMAIN),', ','');
						if(!empty($tags)){
							$outputs[] = '<span class="tags">'.$tags.'</span>';
						}
						break;
					case 'author':
						global $authordata;
						if(!$authordata){
							$authordata = get_userdata($post->post_author);
						}
						if(theme_get_option('blog','author_link_to_website')){
							$outputs[] = '<span class="author">'.__('By: ', THEME_FRONT_DOMAIN).get_the_author_link().'</span>';
						}else{
							$outputs[] = '<span class="author">'.__('By: ', THEME_FRONT_DOMAIN).get_the_author_posts_link().'</span>';
						}
							
						break;
					case 'date':
						$outputs[] = '<time datetime="'.get_the_time('Y-m-d').'">'. ($single ? '' : __('Posted on ',THEME_FRONT_DOMAIN)).'<a href="'.get_month_link(get_the_time('Y'), get_the_time('m')).'">'.get_the_date().'</a></time>';
						break;
				}
			}
			$output = implode('<span class="sep">|</span>',$outputs);
			if($single && in_array('comment',$meta_items) && ($post->comment_count > 0 || comments_open())){
				ob_start();
				comments_popup_link(__('No Comments',THEME_FRONT_DOMAIN), __('1 Comment',THEME_FRONT_DOMAIN), __('% Comments',THEME_FRONT_DOMAIN));
				$output.= '<span class="comments">'.ob_get_clean().'</span>';
			}
		}		
		/*if( !$single ){
			ob_start();
			edit_post_link( __( 'Edit', THEME_FRONT_DOMAIN ), ' <span class="edit-link">', '</span>' );
			$output.= count($meta_items) >0 ? '<span class="sep">|</span>' : '';
			$output.=ob_get_clean();
		}*/
			
		if(!$single && in_array('category',$meta_items)){
			$content = get_the_category_list(', ');
			if(!empty($content)){
				$output.= '<span class="categories">'.__('Categories: ', THEME_FRONT_DOMAIN).theme_get_all_category(', ',$post->ID).'</span>';
			}
		}
		return $output;
	}

	function blog_popular_posts() {
		$r = new WP_Query(array(
			'category__not_in' => theme_get_option('blog','exclude_categorys'),
			'showposts' => 3, 
			'nopaging' => 0, 
			'orderby'=> 'comment_count', 
			'post_status' => 'publish', 
			'ignore_sticky_posts' => 1
		));
		$output = '';
		if ($r->have_posts()){
			$output = '<div class="popular_posts_wrap">';
			$output .= '<h3>'.__('Popular Posts',THEME_FRONT_DOMAIN).'</h3>';
			$output .= '<ul class="posts_list">';
			while ($r->have_posts()){
				$r->the_post();
				$output .= '<li>';
				$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
				if (has_post_thumbnail() ){
					$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
				}else{
					$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
				}
				$output .= '</a>';
				$output .= '<div class="post_extra_info">';
				$title = get_the_title();
				$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
				$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
				$output .= '</div>';
				$output .= '<div class="clear"></div>';
				$output .= '</li>';
			}
			$output .= '</ul>';
			$output .= '</div>';
		}

		wp_reset_postdata();
		return $output;
	}

	function blog_related_posts() {
		global $post;
		$backup = $post;  
		$tags = wp_get_post_tags($post->ID);
		$exclude_cats = theme_get_option('blog','exclude_categorys');
		$tagIDs = array();
		$related_post_found = false;
		$output = '';
		if ($tags) {
			$tagcount = count($tags);
			for ($i = 0; $i < $tagcount; $i++) {
				$tagIDs[$i] = $tags[$i]->term_id;
			}
			$r = new WP_Query(array(
				'tag__in' => $tagIDs,
				'post__not_in' => array($post->ID),
				'showposts'=>3,
				'ignore_sticky_posts'=>1,
				'category__not_in' => $exclude_cats,
			));
			if ($r->have_posts()){
				$related_post_found = true;
				$output = '<div class="Related_posts_wrap">';
				$output .= '<h3>'.__('Related Posts',THEME_FRONT_DOMAIN).'</h3>';
				$output .= '<ul class="posts_list">';
				while ($r->have_posts()){
					$r->the_post();
					$output .= '<li>';
					$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
					if (has_post_thumbnail() ){
						$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
					}else{
						$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
					}
					$output .= '</a>';
					$output .= '<div class="post_extra_info">';
					$title = get_the_title();
					$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
					$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
					$output .= '</div>';
					$output .= '<div class="clear"></div>';
					$output .= '</li>';
				}
				$output .= '</ul>';
				$output .= '</div>';
			}
			$post = $backup;
		}
		if(!$related_post_found){
			$r = new WP_Query(array(
				'showposts' => 3, 
				'nopaging' => 0, 
				'post_status' => 'publish', 
				'ignore_sticky_posts' => 1,
				'category__not_in' => $exclude_cats,
			));
			if ($r->have_posts()){
				$output = '<div class="related_posts_wrap">';
				$output .= '<h3>'.__('Related Posts',THEME_FRONT_DOMAIN).'</h3>';
				$output .= '<ul class="posts_list">';
				while ($r->have_posts()){
					$r->the_post();
					$output .= '<li>';
					$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
					if (has_post_thumbnail() ){
						$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
					}else{
						$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
					}
					$output .= '</a>';
					$output .= '<div class="post_extra_info">';
					$title = get_the_title();
					$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
					$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
					$output .= '</div>';
					$output .= '<div class="clear"></div>';
					$output .= '</li>';
				}
				$output .= '</ul>';
				$output .= '</div>';
			}
		}
		wp_reset_postdata();

		return $output;
	}

	function portfolio_recent_posts() {
		$r = new WP_Query(array(
			'showposts' => 3, 
			'nopaging' => 0, 
			'post_status' => 'publish', 
			'ignore_sticky_posts' => 1,
			'post_type' => 'portfolio', 
		));
		$output = '';
		if ($r->have_posts()){
			$output = '<div class="recent_portfolio_wrap">';
			$output .= '<h3>'.__('Recent Portfolio',THEME_FRONT_DOMAIN).'</h3>';
			$output .= '<ul class="posts_list">';
			while ($r->have_posts()){
				$r->the_post();
				$output .= '<li>';
				$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
				if (has_post_thumbnail() ){
					$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
				}else{
					$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
				}
				$output .= '</a>';
				$output .= '<div class="post_extra_info">';
				$title = get_the_title();
				$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
				$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
				$output .= '</div>';
				$output .= '<div class="clear"></div>';
				$output .= '</li>';
			}
			$output .= '</ul>';
			$output .= '</div>';
		}

		wp_reset_postdata();
		return $output;
	}

	function portfolio_related_posts() {
		global $post;
		$backup = $post;  
		$cats  =  wp_get_object_terms($post->ID, 'portfolio_category');
		$catSlugs = array();
		$related_post_found = false;
		$output = '';
		if ($cats) {
			$catcount = count($cats);
			for ($i = 0; $i < $catcount; $i++) {
				$catSlugs[$i] = $cats[$i]->slug;
			}
			$query = array(
				'post__not_in' => array($post->ID),
				'showposts'=>3,
				'ignore_sticky_posts'=>1,
				'post_type' => 'portfolio', 
			);
			global $wp_version;
			if(version_compare($wp_version, "3.1", '>=')){
				$query['tax_query'] = array(
					array(
						'taxonomy' => 'portfolio_category',
						'field' => 'slug',
						'terms' => $catSlugs
					)
				);
			}else{
				$query['taxonomy'] = 'portfolio_category';
				$query['term'] = implode(',',$catSlugs);
			}
			$r = new WP_Query($query);
			if ($r->have_posts()){
				$related_post_found = true;
				$output = '<div class="related_portfolio_wrap">';
				$output .= '<h3>'.__('Related Portfolio',THEME_FRONT_DOMAIN).'</h3>';
				$output .= '<ul class="posts_list">';
				while ($r->have_posts()){
					$r->the_post();
					$output .= '<li>';
					$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
					if (has_post_thumbnail() ){
						$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
					}else{
						$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
					}
					$output .= '</a>';
					$output .= '<div class="post_extra_info">';
					$title = get_the_title();
					$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
					$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
					$output .= '</div>';
					$output .= '<div class="clear"></div>';
					$output .= '</li>';
				}
				$output .= '</ul>';
				$output .= '</div>';
			}
		}
		if(!$related_post_found){
			$r = new WP_Query(array(
				'showposts' => 3, 
				'nopaging' => 0, 
				'orderby'=> 'comment_count', 
				'post_status' => 'publish', 
				'ignore_sticky_posts' => 1,
				'post_type' => 'portfolio', 
			));
			if ($r->have_posts()){
				$output = '<div class="related_portfolio_wrap">';
				$output .= '<h3>'.__('Related Portfolio',THEME_FRONT_DOMAIN).'</h3>';
				$output .= '<ul class="posts_list">';
				while ($r->have_posts()){
					$r->the_post();
					$output .= '<li>';
					$output .= '<a class="thumbnail" href="'.get_permalink().'" title="'.get_the_title().'">';
					if (has_post_thumbnail() ){
						$output .= get_the_post_thumbnail(get_the_ID(),array(65,65),array('title'=>get_the_title(),'alt'=>get_the_title()));
					}else{
						$output .= '<img src="'.THEME_IMAGES.'/widget_posts_thumbnail.png" width="65" height="65" title="'.get_the_title().'" alt="'. get_the_title().'"/>';
					}
					$output .= '</a>';
					$output .= '<div class="post_extra_info">';
					$title = get_the_title();
					$output .= '<a class="post_title" href="'.get_permalink().'" title="'.get_the_title().'" rel="bookmark">'.$title.'</a>';
					$output .= '<time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time>';
					$output .= '</div>';
					$output .= '<div class="clear"></div>';
					$output .= '</li>';
				}
				$output .= '</ul>';
				$output .= '</div>';
			}
		}
		$post = $backup;
		wp_reset_postdata();

		return $output;
	}
	function slideShow($type='nivo',$category='',$number ='-1',$nav=false,$height=null,$navNumber = '4') {
		if($type == '')
			return false;
		/** fix **/
		if(empty($category)){
			$category = '{s}'; 
		}elseif(strpos($category, '|') != false){
			list($target, $cat) = explode("|", $category);
			$category = '{'.$target.':'.$cat.'}';
		}
		/** end fix **/
		if(empty($height))
			$height = 396;
		$media_position = '';
		if($type == 'any'){
			$media_position = theme_get_option('slideshow','any_media_position');
		}
		if(empty($number)){
			$number = '-1';
		}
		if(empty($nav))
			$nav = false;
		if(!$type != 'any')
			echo '<div id="heading" class="heading-home">';
		echo '<div class="light-span '.$media_position.$type.'">';
		$width = ($type =='nivo')?'width:'.theme_get_option('slideshow','nivo_width').'px;':'';
		echo '<div id="slideshow" class="wrap-center slideshow" style="height:'.$height.'px;'.$width.'">';
		switch($type){
		case 'flow':
			$this->slideShow_flow($category,$number,$nav,$navNumber);
			break;
		case 'nivo':
			$this->slideShow_nivo($category,$number,$nav,$navNumber);
			break;
		case 'any':
			$this->slideShow_anything($category,$number,$nav,$navNumber,$height);
			break;
		}
		echo <<<HTML
			</div>
		</div>
	</div>
HTML;
	}
	function slideShowHeader_js($type = null) {
		if($type == null){
			$post_id = get_queried_object_id(); 
			$introduce_type = get_post_meta($post_id, '_introduce_text_type', true);
			if('slideshow' == $introduce_type){
				$type = get_post_meta($post_id,'_slideshow_type', true);
			}
		}
		wp_enqueue_script('slide-nav');
		wp_enqueue_script('slider-js');
		switch($type){
		case 'flow':
			$this->slideHeader_flow();
			break;
		case 'nivo':
			$this->slideHeader_nivo();
			break;
		case 'any':
			$this->slideHeader_anything();
			break;
		}
	}
	function slideShowHeader_css($type = null) {
		if($type == null){
			$post_id = get_queried_object_id(); 
			$introduce_type = get_post_meta($post_id, '_introduce_text_type', true);
			if('slideshow' == $introduce_type){
				$type = get_post_meta($post_id,'_slideshow_type', true);
			}
		}
		switch($type){
		case 'flow':
			wp_enqueue_style('css-feature-carousel');
			break;
		case 'nivo':
			wp_enqueue_style('css-nivo');
			break;
		case 'any':
			wp_enqueue_style('mediaelementjs-styles');
			break;
		}
	}
	function slideHeader_anything() {
		wp_enqueue_script('jquery-swfobject');
		wp_enqueue_script('mediaelementjs-scripts');
		wp_enqueue_script('slider-video');
		wp_enqueue_script('swfobject');
		wp_enqueue_script('anyInit-js');
	}
	function slideHeader_nivo() {
		wp_enqueue_script('jquery-nivo');
		wp_enqueue_script('nivoInit-js');
	}
	function slideHeader_flow() {
		wp_enqueue_script('jquery-featureCarousel');
		wp_enqueue_script('flowInit-js');
	}
	function slideShow_getItem($source_string='',$number='-1',$type){
		$pattern = '/{([sbpg]):{0,1}([^}]+?){0,1}}/i';
		if(class_exists('Woocommerce'))
			$pattern = '/{([sbpgw]):{0,1}([^}]+?){0,1}}/i';
		
		preg_match_all($pattern, $source_string, $match);
		$sources = array();
		if(empty($match[0])){
			$source_value = $source_string;
		}else{
			foreach($match[1] as $index => $cat){
				$sources[$cat] = $match[2][$index];
			}
		}
		$items = array();
		foreach($sources as $key=>$source_value){
			switch($key){
			case 'w':
				if(class_exists( 'Woocommerce' )){
					$query = array( 
						'post_type' => 'product', 
						'showposts'=>$number, 
						'orderby'=>'menu_order', 
						'order'=>'ASC',
					);
					if(!empty($source_value)){
						global $wp_version;
						if(version_compare($wp_version, "3.1", '>=')){
							$query['tax_query'] = array(
								array(
									'taxonomy' => 'product_cat',
									'field' => 'slug',
									'terms' => explode(',', $source_value)
								)
							);
						}else{
							$query['taxonomy'] = 'product_cat';
							$query['term'] = $source_value;
						}
					}

					$loop = new WP_Query($query);

					while ( $loop->have_posts() ) {
						$loop->the_post();
						$image_src='';
						$source_type='';
						if (has_post_thumbnail()){ 
							$image_src = get_post_thumbnail_id();
							$source_type = "form";
						}else{
							$image_src = woocommerce_placeholder_img_src();
							$source_type = 'url';
						}

						$res = array(
							'id' => get_the_ID(),
							'title' => get_the_title(),
							'desc'  => get_the_excerpt(),
							'media' => array(
								'type'=>'image',
								'media'=>$image_src,
								'method'=>$source_type,
							),
							'link' => get_permalink(),
							'link_target' => '_self'
						);
						$items[] = $res;
					}
				}
				break;
			case 'p':
				$query = array( 
					'post_type' => 'portfolio', 
					'showposts'=>$number, 
					'orderby'=>'menu_order', 
					'order'=>'ASC',
				);
				if(!empty($source_value)){
					global $wp_version;
					if(version_compare($wp_version, "3.1", '>=')){
						$query['tax_query'] = array(
							array(
								'taxonomy' => 'portfolio_category',
								'field' => 'slug',
								'terms' => explode(',', $source_value)
							)
						);
					}else{
						$query['taxonomy'] = 'portfolio_category';
						$query['term'] = $source_value;
					}
				}

				$loop = new WP_Query($query);

				while ( $loop->have_posts() ) {
					$loop->the_post();
					$image_src='';
					$source_type='';
					if (has_post_thumbnail()){ 
						$image_src = get_post_thumbnail_id();
						$source_type = "form";
					}else{
						$image_src =  theme_get_option('portfolio','default_portfolio_image');
						$image_src = theme_parse_image($image_src,'post-image-placer.png',false);
						$source_type = $image_src['method'];
						$image_src = $image_src['media'];
					}

					$res = array(
						'id' => get_the_ID(),
						'title' => get_the_title(),
						'desc'  => get_the_excerpt(),
						'media' => array(
							'type'=>'image',
							'media'=>$image_src,
							'method'=>$source_type,
						),
						'link' => get_permalink(),
						'link_target' => '_self'
					);
					$items[] = $res;
				}
				break;
			case 's':
				$query = array( 
					'post_type' => 'slideshow', 
					'showposts'=>$number, 
					'orderby'=>'menu_order', 
					'order'=>'ASC',
				);
				if(!empty($source_value)){
					global $wp_version;
					if(version_compare($wp_version, "3.1", '>=')){
						$query['tax_query'] = array(
							array(
								'taxonomy' => 'slideshow_category',
								'field' => 'slug',
								'terms' => explode(',', $source_value)
							)
						);
					}else{
						$query['taxonomy'] = 'slideshow_category';
						$query['term'] = explode(',', $source_value);
					}
				}

				$loop = new WP_Query($query);
				while ( $loop->have_posts() ) {
					$loop->the_post();
					$link_to = get_post_meta(get_the_ID(), '_link_to', true);
					$link = theme_get_superlink($link_to);
					$link_target = get_post_meta(get_the_ID(), '_link_target', true);
					$link_target = $link_target?$link_target:'_self';
					$media_params=get_post_meta(get_the_ID(), '_media_params', true);
					$media_params=(array)json_decode(stripslashes($media_params),true);
					$res = array(
						'id' => get_the_ID(),
						'title' => get_the_title(),
						'media'  => $media_params,
						'desc'  => get_post_meta(get_the_ID(), '_description', true),
						'link' => empty($link) ? '#':$link,
						'link_target' => $link_target,
						'content'=>get_the_content(),
					);
					$items[] = $res;
				}
				break;
			case 'b':
				$query = array( 
					'post_type' => 'post', 
					'showposts'=>$number, 
					'orderby'=>'date', 
					'order'=>'DESC',
					'meta_key'=>'_thumbnail_id',
				);
				if(!empty($source_value)){
					$query['category_name'] = $source_value;
				}

				$loop = new WP_Query($query);

				while ( $loop->have_posts() ) {
					$loop->the_post();
					$image_src='';
					$source_type='';
					if (has_post_thumbnail()){ 
						$image_src = get_post_thumbnail_id();
						$source_type = "form";
					}else{
						$image_src =  theme_get_option('blog','default_blog_image');
						$image_src = theme_parse_image($image_src,'post-image-placer.png',false);
						$source_type = $image_src['method'];
						$image_src = $image_src['media'];
					}
					$res = array(
						'id' => get_the_ID(),
						'title' => get_the_title(),
						'desc'  => get_the_excerpt(),
						'media' => array(
							'type'=>'image',
							'media'=>$image_src,
							'method'=>$source_type,
						),
						'link' => get_permalink(),
						'link_target' => '_self',
						'content'=>get_the_excerpt(),
					);
					$items[] = $res;
				}
				break;
			case 'g':
				$children = array(
					'post_parent' => get_queried_object_id(),
					'post_status' => 'inherit',
					'post_type' => 'attachment',
					'post_mime_type' => 'image',
					'order' => 'ASC',
					'orderby' => 'menu_order ID',
					'numberposts' => -1,
					'offset' => ''
				);

				/* Get image attachments. If none, return. */
				$attachments = get_children( $children );
				foreach ( $attachments as $id => $attachment ) {
					$items[] = array(
						'id' => $id,
						'title' => $attachment->post_title,
						'desc'  => '',
						'media' => array(
							'type'=>'image',
							'method'=>'form',
							'media'=>$id,
						),
						'link' => '#',
						'link_target' => '_self',
						'content'=> $attachment->post_content,
					);
				}
				break;
			}
		}
		wp_reset_query();
		if($number!='-1'){
			return array_slice($items, 0, $number);
		} else {
			return $items;
		}
	}
	function slideshow_Param($options,$print='true'){
		if($print)
			echo "\n<div class='slideshowParam' data-params='".htmlspecialchars(json_encode($options))."'></div>\n";
		else
			return htmlspecialchars(json_encode($options));
	}
	function slideShow_anything($category='',$number='-1',$nav=false,$navNumber = '4',$height=null) {
		$items = $this->slideShow_getItem($category,$number,'anything');
		$any_type =theme_get_option('slideshow','any_type');
		$media_width = theme_get_option('slideshow','anything_media_width');
		$media_height = theme_get_option('slideshow','anything_media_height');
		$media_position = theme_get_option('slideshow','any_media_position');
		$default_image=theme_get_option('slideshow','default_image');
		$default_image=theme_parse_image($default_image,'slide-default.jpg',false);
		$default_source = $default_image['method'];
		$default_image = $default_image['media'];
		$slide_options = array(
			'autoPlay' => theme_get_option('slideshow', 'anything_autoPlay'), 
			'mediaAnimate' => theme_get_option('slideshow', 'anything_media_animate'),
			'mediaEasing'=>theme_get_option('slideshow','anything_media_easing'),
			'textAnimate' => theme_get_option('slideshow', 'anything_text_animate'), 
			'textEasing' => theme_get_option('slideshow', 'anything_text_easing'), 
			'delay' => theme_get_option('slideshow', 'anything_delay'), 
			'animationTime' => theme_get_option('slideshow', 'anything_animationTime'), 
			'pauseOnHover'=>false,
			'width' =>$media_width,
			'height' =>$media_height,
			'navNumber'=>$navNumber,
			'type'=>'anything',
			'symplexnav'=>!$nav,
			'anythingnav'=>$nav,
			'anythingtype'=>$any_type,
			'readyswf'=>THEME_SWF.'/expressInstall.swf',
			'posterImage'=>THEME_IMAGES.'/video_play.png',
		);
		if(!empty($height))
			$height = 'style="height:'.$height.'px;"';
		echo '<div id="slidetext" class="slidetext '.$media_position.' '.$any_type.'" data-lock="false" '.$height.'>';
		foreach ($items as $item) {
			echo'<div class="text">';
			echo do_shortcode($item['content']);
			echo '<div class="clear"></div></div>';
		}
		echo '</div>';
		if ($any_type=='media') {
			echo '<div id="slideimage" class="'.$media_position.'"  '.$height.'>';
			echo '<div class="slideframe">';
			echo '<div id="slideload" class="preload"></div>';
			echo '<div id="slidelist">';
			foreach ($items as $item) {
				$media_params=$item['media'];
				echo '<div class="media">';
				if(empty($media_params['method'])){
					$resize = theme_resize($default_image,$media_width,$media_height,$default_source);
					echo '<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  alt="default"/>';
				}
				if($media_params['method']=='form'){
					$resize = theme_resize($media_params['media'],$media_width,$media_height,$media_params['method']);
					echo '<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  alt="'.$item['title'].'"/></a>';
				}elseif($media_params['method']=='url'){
					switch ($media_params['type']) {
					case 'image':
						/*echo	'<img src="'.wp_get_attachment_image($media_params['media']).'" width="389px" height="244px"/>';*/
						echo '<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$media_width.'" height="'.$media_height.'" src="'.$media_params['media'].'"  alt="'.$media_params['title'].'"/></a>';
						break;
					case 'video':
						$source = $media_params['media'];
						$source = split('&',$source);
						$mp4_source = '';
						$flash_src = '';	
						$webm_source = '';
						$ogg_source = '';
						$flv_source = '';
						$wmv_source = '';				
						foreach($source as $value){
							$value = split('=',$value);
							$value[1] = urldecode($value[1]);
							switch($value[0]){
							case 'wurl':
								$webm_source = '<source type="video/webm" src="'.htmlspecialchars($value[1]).'" />';
								if(empty($flash_src)){
									$flash_src = htmlspecialchars($value[1]);
								}
								break;
							case 'ourl':
								$ogg_source = '<source type="video/ogg" src="'.htmlspecialchars($value[1]).'" />';
								if(empty($flash_src)){
									$flash_src = htmlspecialchars($value[1]);
								}
								break;
							case 'murl':
								$mp4_source = '<source type="video/mp4" src="'.htmlspecialchars($value[1]).'" />';
								if(empty($flash_src)){
									$flash_src = htmlspecialchars($value[1]);
								}
								break;
							case 'furl':
								$flv_source = '<source type="video/flv" src="'.htmlspecialchars($value[1]).'" />';
								if(empty($flash_src)){
									$flash_src = htmlspecialchars($value[1]);
								}
								break;
							case 'vurl':
								$wmv_source = '<source type="video/wmv" src="'.htmlspecialchars($value[1]).'" />';
								if(empty($flash_src)){
									$flash_src = htmlspecialchars($value[1]);
								}
								break;

							}
						}
						$captions_source = '<track src="'.$media_params['title'].'" kind="subtitles" srclang="en" />';
						echo '<video width="'.$media_width.'" height="'.$media_height.'" controls="controls" class="video">'."\n";
						echo $mp4_source."\n";
						echo $webm_source."\n";
						echo $ogg_source."\n";
						echo $flv_source."\n";
						echo $wmv_source."\n";
						echo $captions_source."\n";
						echo '<object width="'.$media_width.'" height="'.$media_height.'" type="application/x-shockwave-flash" data="'.THEME_URI.'/mediaelement/flashmediaelement.swf">'."\n";
						echo '<param name="movie" value="'.THEME_URI.'/mediaelement/flashmediaelement.swf" />'."\n";
						echo '<param name="flashvars" value="controls=true&amp;file='.$flash_src.'" />'."\n";
						echo '<param name="wmode" value="transparent" />'."\n";
						echo '<param value="'.THEME_SWF.'/expressInstall.swf'.'" name="expressInstaller"/>';
						echo '</object>'."\n";
						echo '</video>'."\n";
						break;
					case 'youtube':
						echo '<object class="youtube" type="application/x-shockwave-flash" width="'.$media_width.'" height="'.$media_height.'" data="http://www.youtube.com/v/'.$media_params['media'].'">'."\n";
						echo '<param name="allowScriptAccess" value="always"/>'."\n";
						echo '<param name="allowFullScreen" value="true"/>'."\n";
						echo '<param name="wmode" value="transparent" />'."\n";
						echo '<param value="'.THEME_SWF.'/expressInstall.swf'.'" name="expressInstaller"/>'."\n";
						echo '</object>'."\n";
						break;
					case 'vimeo':
						echo '<object class="vimeo" type="application/x-shockwave-flash" width="'.$media_width.'" height="'.$media_height.'" data="http://vimeo.com/moogaloop.swf?clip_id='.$media_params['media'].'">'."\n";
						echo '<param name="allowScriptAccess" value="always"/>'."\n";
						echo '<param name="allowFullScreen" value="true"/>'."\n";
						echo '<param name="wmode" value="transparent" />'."\n";
						echo '<param value="'.THEME_SWF.'/expressInstall.swf'.'" name="expressInstaller"/>'."\n";
						echo '</object>'."\n";
						break;
					case 'flash':
						echo '<object class="flash" width="'.$media_width.'" height="'.$media_height.'" type="application/x-shockwave-flash" data="'.$media_params['media'].'">'."\n";
						echo '<param name="movie" value="'.$media_params['media'].'"/>'."\n";
						echo '<param name="allowFullScreen" value="true"/>'."\n";
						echo '<param name="allowscriptaccess" value="always"/>'."\n";
						echo '<param value="'.THEME_SWF.'/expressInstall.swf'.'" name="expressInstaller"/>'."\n";
						echo '<param name="play" value="play"/>'."\n";
						echo '<param name="wmode" value="transparent" />'."\n";
						echo '<embed src="'.$media_params['media'].'" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" width="'.$media_width.'" height="'.$media_height.'"/>'."\n";
						echo '</object>'."\n";
						break;

					}
				}else{
					$resize = theme_resize($default_image,$media_width,$media_height,$default_source);
					echo '<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  alt="default"/>';
				}
				echo '</div>';					
			}
			echo '</div></div></div>';
		}
		if(!$nav)
			$this->slideShowNav($items);
		elseif(theme_get_option('slideshow', 'any_default_nav')){
			echo '<ul class="anything-control" style="display: none;">
					<li><a class="slideshowprev">Prev</a></li>
					<li>
						<div class="slide-button-pause" style="display:"><a href="#" class="slidepause">Pause Slideshow</a></div>
						<div class="slide-button-play" style="display:none"><a href="#" class="slideresume">Resume Slideshow</a></div>
					</li>
					<li><a class="slideshownext">Next</a></li>
				</ul>';
		}else{
			$slide_options['anythingnav'] = false;
		}
		$this->slideshow_Param($slide_options);

	}
	function slideShow_nivo($category='',$number='-1',$nav=false,$navNumber = '4') {
		$items = $this->slideShow_getItem($category,$number,'nivo');
		$width = theme_get_option('slideshow','nivo_width');
		$height = theme_get_option('slideshow','nivo_height');
		$use_border=theme_get_option('slideshow','nivo_use_border');
		$top=theme_get_option('slideshow','nivo_offset_top');
		$captions = theme_get_option('slideshow', 'nivo_captions');
		$stop_atend=theme_get_option('slideshow','nivo_stopatend');
		$default_image=theme_get_option('slideshow','default_image');
		$default_image=theme_parse_image($default_image,'slide-default.jpg',false);
		$default_source = $default_image['method'];
		$default_image = $default_image['media'];
		$auto = theme_get_option('slideshow', 'nvio_slide_autoPlay');
		$slide_options = array(
			'effect' => theme_get_option('slideshow', 'nivo_effect'), 
			'slices' => theme_get_option('slideshow', 'nivo_slices'), 
			'animSpeed' => theme_get_option('slideshow', 'nivo_animSpeed'), 
			'pauseTime' => theme_get_option('slideshow', 'nivo_pauseTime'), 
			'pauseOnHover' => theme_get_option('slideshow', 'nivo_pauseOnHover'), 
			'manualAdvance' => theme_get_option('slideshow', 'nivo_manualAdvance'),
			'captions' => theme_get_option('slideshow', 'nivo_captions'),
			'captionOpacity'=>theme_get_option('slideshow','nivo_captionOpacity'),
			'stop' => !theme_get_option('slideshow', 'nvio_slide_autoPlay'),
			'stopAtEnd' => theme_get_option('slideshow','nivo_stopatend'),
			'boxRows'=> theme_get_option('slideshow','nivo_boxRows'),
			'boxCols'=> theme_get_option('slideshow','nivo_boxCols'),
			'autoPlay'=>theme_get_option('slideshow','nivo_auto'),
			'navNumber'=>$navNumber,
			'symplexnav'=>!$nav,
			'randomStart'=>false,
			'type'=>'nivo',
		);
		$slide_options['control'] = $nav ? true : false;
		$captionPosition = theme_get_option('slideshow', 'nivo_captionPosition');
		$captionPosition = empty($captionPosition)?'top':$captionPosition;
		if(!empty($items)){
			echo '<div class="nivoSlider '.$captionPosition.'" style="top:'.$top.'px;width:'.$width.'px;height:'.$height.'px;">';
			foreach($items as $item) {
				$media_params=$item['media'];
				if(!empty($media_params['type'])){
					$captions_text = $captions?$item['content']:'';
					if(empty($captions_text))$captions_text='';
					if($media_params['type'] !="image"){
						$resize = theme_resize($default_image,$width,$height,$default_source);
						if($item['link'] != ''){
							echo "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" alt="'.$item['title'].'"/></a>';
						}else{
							echo "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  alt="'.$item['title'].'"/>';
						}
					}
					else{
						$url=$default_image;
						$source_type = 'dir';
						if(!empty($media_params['media']))
							$url = $media_params['media'];
						if(!empty($media_params['method']))
							$source_type = $media_params['method'];
						$resize = theme_resize($url,$width,$height,$source_type);
						if($item['link'] != ''){
							echo "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  title="'.$captions_text.'" alt="'.$item['title'].'"/></a>';
						}
						else{
							echo "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" title="'.$captions_text.'" alt="'.$item['title'].'"/>';
						}
					}
				}else{
					$resize = theme_resize($default_image,$width,$height,$default_source);
					echo "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" alt="default"/>';
				}
			}
			echo '</div>';
			echo '<span class="overlay preload"></span>';
		}
		if(!$nav){
			$this->slideShowNav($items);
		}
		$this->slideshow_Param($slide_options);
	}
	function slideShow_flow($category='',$number='-1',$nav=false , $navNumber= '4') {
		$items = $this->slideShow_getItem($category,$number,'flow');
		$default_image=theme_get_option('slideshow','default_image');
		$default_image=theme_parse_image($default_image,'slide-default.jpg',false);
		$default_source = $default_image['method'];
		$default_image = $default_image['media'];
		$slide_options = array(
			'largeW' => theme_get_option('slideshow', 'flow_largeW'), 
			'largeH' => theme_get_option('slideshow', 'flow_largeH'), 
			'smallW' => theme_get_option('slideshow', 'flow_smallW'), 
			'smallH' => theme_get_option('slideshow', 'flow_smallH'), 
			'offsetSide' => theme_get_option('slideshow', 'flow_offsetSide'),
			'offsetTop' => theme_get_option('slideshow', 'flow_offsetTop'),
			'animSpeed' => theme_get_option('slideshow', 'flow_animSpeed'), 
			'pauseTime' => theme_get_option('slideshow', 'flow_pauseTime'), 
			'pauseOnHover' => theme_get_option('slideshow', 'flow_pauseOnHover'), 
			'easing'=>theme_get_option('slideshow', 'flow_easing'), 
			'navNumber' =>$navNumber,
			'type'=>'flow',
			'symplexnav'=>!$nav,
		);
		$caption = theme_get_option('slideshow', 'flow_captions');
		$caption = $caption ? '':'visibility:hidden;';
		$border_color = theme_get_option('slideshow', 'flow_border_color');
		$border_width = theme_get_option('slideshow', 'flow_border_width');
		$captionPosition = theme_get_option('slideshow', 'flow_captionPosition');
		$captionPosition = empty($captionPosition)?'top':$captionPosition;
		echo '<div id="carousel-container">';
		echo '<div id="carousel" class="carousel">';
		foreach($items as $item) {
			$media_params=$item['media'];
			$url = $default_image;
			$source_type = $default_source;
			if(!empty($media_params['type'])&&$media_params['type']=='image'){
				$url = $media_params['media'];
				$source_type = $media_params['method'];
			}
			echo '<div class="carousel-feature '.$captionPosition.'" style="border-color:'.$border_color.';border-width:'.$border_width.'px">';
			$resize = theme_resize($url,$slide_options['largeW'],$slide_options['largeH'],$source_type);
			if($item['link'] != ''){
				echo  "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" class="carousel-image" src="'.$resize['url'].'" alt="'.$item['title'].'"/></a>';
			}else{
				echo  "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" class="carousel-image" src="'.$resize['url'].'" alt="'.$item['title'].'"/>';
			}
			if(!empty($item['content']))
				echo '<div class="carousel-caption" style="'.$caption.'">'.$item['content'].'</div>';
			echo '</div>';

		}
		echo '</div>';
		echo '</div>';
		if(!$nav)
			$this->slideShowNav($items);
		$this->slideshow_Param($slide_options);
	}
	function slideShowNav($items)
	{
		echo <<<HTML
		<div class="nav-span">
			<div class="wrap-center rel">
			   <ul id="slideshownav" class="fl slideshownav">
HTML;
		foreach ($items as $item) {
			echo ' <li style="display:none"><a href="#">'.$item['title'].'</a></li>';
		}
		echo <<<HTML
			   </ul>
			   <ul id="slidecontrol" class="fr slidecontrol">
					<li>
						<div id="button_pause" class="slide-button-pause" style="display:"><a href="#" id="pauseButton" class="slidepause">Pause Slideshow</a></div>
						<div id="button_play" class="slide-button-play" style="display:none"><a href="#" id="resumeButton" class="slideresume">Resume Slideshow</a></div>
					</li>
					<li class="slidearrow">
						<a href="#" id="slideshowprev" class="slideshowprev">Back</a>
					</li>
					<li class="slidearrow">
						<a href="#" id="slideshownext" class="slideshownext">Prev</a>
					</li>
			   </ul>
			   <div class="clear"></div>
			</div>
		</div> 
HTML;

	}

	function blog_page_navgation(){
		global $wpdb, $wp_query;
		echo '<div class="pagenavi">'."\n";
		if(is_single())
		{
			add_filter('the_title','title_length');
			echo "<div class='post-prev'>";
			previous_post_link('%link','<img src="'.THEME_IMAGES.'/post-prev.png" alt="blog-prev"/>%title');
			echo "</div>";
			echo "<div class='post-next'>";
			next_post_link('%link','%title<img src="'.THEME_IMAGES.'/post-next.png" alt="blog-next">');
			echo "</div>";

		}else{
			$pagenavi_options = array(
				'next_text' => __('Newer Posts',THEME_FRONT_DOMAIN).'<img src="'.THEME_IMAGES.'/post-next.png" alt="blog-next"/>',
				'prev_text' => '<img src="'.THEME_IMAGES.'/post-prev.png" alt="blog-prev"/>'.__('Older Posts',THEME_FRONT_DOMAIN),
				'dotright_text' => __('...',THEME_FRONT_DOMAIN),
				'dotleft_text' => __('...',THEME_FRONT_DOMAIN),
				'num_pages' => 5,
				'use_pagenavi_css' => 0,
			);
			$request = $wp_query->request;
			$paged = '';
			global $wp_version;
			if(is_front_page() && version_compare($wp_version, "3.1", '>=')){//fix wordpress 3.1 paged query
				$paged = (get_query_var('paged')) ?get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
			}else{
				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
			}
			$paged = intval($paged);
			$numposts = $wp_query->found_posts;
			$max_page = intval($wp_query->max_num_pages);
			$pages_to_show = intval($pagenavi_options['num_pages']);
			$pages_to_show_minus_1 = $pages_to_show - 1;
			$half_page_start = floor($pages_to_show_minus_1/2);
			$half_page_end = ceil($pages_to_show_minus_1/2);
			if (empty($paged) || $paged == 0)
				$paged = 1;
			$start_page = $paged - $half_page_start;

			if ($start_page <= 1)
				$start_page = 2;

			$end_page = $paged + $half_page_end;
			if (($end_page - $start_page) != $pages_to_show_minus_1) {
				$end_page = $start_page + $pages_to_show_minus_1;
			}

			if ($end_page >= $max_page) {
				$start_page = $max_page - $pages_to_show_minus_1-1;
				$end_page = $max_page-1;
			}

			if ($start_page <= 1)
				$start_page = 2;
			if ($max_page > 1) {

				previous_posts_link($pagenavi_options['prev_text']);
				next_posts_link($pagenavi_options['next_text'], $max_page);
				if($paged==1)
					echo '<span class="current">1</span>';
				else
					echo '<a href="'.esc_url(get_pagenum_link()).'" class="page" title="1">1</a>';
				if($start_page != 2){
					echo '<span class="extend">...</span>';
				}
				for($i = $start_page; $i  <= $end_page; $i++) {						
					if ($i == $paged) {
						echo '<span class="current">'.$i.'</span>';
					} else {
						echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$i.'">'.$i.'</a>';
					}
				}
				if($end_page < $max_page-1){
					echo '<span class="extend">...</span>';
				}
				if($paged==$max_page)
					echo '<span class="current">'.$max_page.'</span>';
				else
					echo '<a href="'.esc_url(get_pagenum_link($max_page)).'" class="page" title="'.$max_page.'">'.$max_page.'</a>';
				
			}
		}
		echo '</div>'."\n"; 
	}
	function portfolio_list($opts){
		global $wp_filter;
		$the_content_filter_backup = $wp_filter['the_content'];
		extract($opts);
		if($desc_length != 'default' && $advancedesc != 'true'){
			$excerpt_constructor = new Theme_The_Excerpt_Length_Constructor($desc_length);
			add_filter( 'excerpt_length', array($excerpt_constructor,'get_length'));
		}
		$paged = 1;
		$query = array(
				'post_type' => 'portfolio', 
				'orderby'=> $orderby, 
				'order'=> $order,
				'paged'=>$paged
			);
		if($carousel == 'false'){
			if ($nopaging == 'false') {
				if($sortable && $sortabletype == 'all' && !empty($opts['paged']))
					$paged = $opts['paged'];
				else{
					global $wp_version;
					if(is_front_page() && version_compare($wp_version, "3.1", '>=')){//fix wordpress 3.1 paged query
						$paged = (get_query_var('paged')) ?get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
					}else{
						$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
					}
				}
				$query['posts_per_page'] = $max;
				$query['paged'] = $paged;
			}else {
				$query['showposts'] = $max;
			}
		}else{
			$query['showposts'] = -1;
		}
		if($cat != ''){
			global $wp_version;
			if(version_compare($wp_version, "3.1", '>=')){
				$query['tax_query'] = array(
					array(
						'taxonomy' => 'portfolio_category',
						'field' => 'slug',
						'terms' => explode(',', $cat)
					)
				);
			}else{
				$query['taxonomy'] = 'portfolio_category';
				$query['term'] = $cat;
			}
		}
		if($ids){
			$query['post__in'] = explode(',',$ids);
		}
		$portfolio_width = '';
		if($list_width)
			$portfolio_width = 'style="width:'.$list_width.'px;"';
		$r = new WP_Query($query);
		$i = 1;
		$output='';
		if($carousel == 'false')
			$output .= '<ul data-easing="'.$sortableasing.'" class="portfolio-list '.$column_class.'">';
		else
			$output .= '<div class="carousel-wrap">';
		while($r->have_posts()) {
			if($carousel == 'true' && $i%$max == 1)
				$output .= ' <ul class="portfolio-list '.$column_class.'">';
			$r->the_post();
			$terms = get_the_terms(get_the_id(), 'portfolio_category');
			$terms_slug = array();
			if (is_array($terms)) {
				foreach($terms as $term) {
					$terms_slug[] = $term->slug;
				}
			}
			if(empty($terms_slug)){
				$output .= '  <li class="all" '.$portfolio_width.'>';
			} else {
				$output .= '  <li class="'.implode(' ',$terms_slug).'" data-id="id'.get_the_id().'" '.$portfolio_width.'>';
			}
			$width= $size[0];
			$output .= '<div class="portfolio-item" style="'.$item_height.'">';
			$lightbox_size = '';
			$image_title = '';
			if($lightboxtitle != 'none')
				$image_title = get_the_title();
			if (has_post_thumbnail()) {
				$image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'full', true);
				$image_source = get_post_thumbnail_id(get_the_id());
				$source_type = 'form';
				$type = get_post_meta(get_the_id(), '_type', true);
				if($type == 'image'){
					$href =  get_post_meta(get_the_id(), '_image', true);
					if(!empty($href)){
						$href = theme_parse_image($href,null,true);
						$href = $href['media'];
					}else
						$href = $image[0];					
					$icon = 'zoom';
					$rel = 'prettyPhoto';
				}elseif($type == 'video'){
					$href =  get_post_meta(get_the_id(), '_video', true);
					$video_width =  get_post_meta(get_the_id(), '_video_width', true);
					$video_height =  get_post_meta(get_the_id(), '_video_height', true);
					$video_size = '';
					if(!empty($video_width) && is_numeric($video_width)){
						$video_size .= 'width='.$video_width;
					}else{
						$video_size .= 'width='.theme_get_option('portfolio','video_width');
					}

					if(!empty($video_height) && is_numeric($video_height)){
						$video_size = '&amp;height='.$video_height;
					}else{
						$video_size .= '&amp;height='.theme_get_option('portfolio','video_height');
					}
					if(empty($href)){
						$href = $image[0];
					}
					$href=$href.'?'.$video_size;
					$icon = 'play';
					$rel = 'prettyPhoto';
				}elseif($type == 'doc'){
					global $theme_inline_id;
					$theme_inline_id = md5(uniqid(rand()));
					$href="#inline-".$theme_inline_id;
					$content =  get_post_meta(get_the_id(), '_lightbox_content', true);
					$lightbox_width = get_post_meta(get_the_id(), '_lightbox_doc_width', true);
					$lightbox_height = get_post_meta(get_the_id(), '_lightbox_doc_height', true);
					if(empty($lightbox_width) || !is_numeric($lightbox_width)){
						$lightbox_width =theme_get_option('portfolio','lightbox_width');
					}

					if(empty($lightbox_height) || !is_numeric($lightbox_height)){
						$lightbox_height =theme_get_option('portfolio','lightbox_height');
					}
					$content='<div id="inline-'.$theme_inline_id.'" class="hide" style="width:'.$lightbox_width.'px;height:'.$lightbox_height.'px">'.do_shortcode($content).'</div>';
					$icon = 'zoom';
					$rel = 'prettyPhoto';
				}elseif($type == 'iframe'){
					$href =  get_post_meta(get_the_id(), '_lightbox_href', true);
					$lightbox_width = get_post_meta(get_the_id(), '_lightbox_iframe_width', true);
					$lightbox_height = get_post_meta(get_the_id(), '_lightbox_iframe_height', true);
					$lightbox_size = '';
					if(!empty($lightbox_width) && is_numeric($lightbox_width)){
						$lightbox_size .= 'width='.$lightbox_width;
					}else{
						$lightbox_size .= 'width='.theme_get_option('portfolio','lightbox_width');
					}

					if(!empty($lightbox_height) && is_numeric($lightbox_height)){
						$lightbox_size = '&amp;height='.$lightbox_height;
					}else{
						$lightbox_size .= '&amp;height='.theme_get_option('portfolio','lightbox_height');
					}
					$href=$href.'?iframe=true&'.$lightbox_size;
					$icon = 'zoom';
					$rel = 'prettyPhoto';
				}elseif($type == 'link'){
					$link = get_post_meta(get_the_ID(), '_link', true);
					$href = theme_get_superlink($link);
					$link_target = get_post_meta(get_the_ID(), '_link_target', true);
					$link_target = $link_target?$link_target:'_self';
					$icon = 'link';
					$lightbox = '';
					$rel = '';
				}elseif($type == 'gallery'){
					$image_ids_str = get_post_meta(get_the_id(), '_image_ids', true);
					$image_ids = array();
					if(!empty($image_ids_str)){
						$image_ids = explode(',',str_replace('image-','',$image_ids_str));
						$first_id = array_shift($image_ids);
						$image_src = wp_get_attachment_image_src($first_id,'full');
						$href = $image_src[0];
						if($lightboxtitle=='image'){
							$image_title = get_the_title($first_id);
						}
					}else{
						$href =  get_post_meta(get_the_id(), '_image', true);
						if(empty($href)){
							$href = $image[0];
						}
						if($lightboxtitle=='image'){
							$image_title = get_the_title();
						}
					}
					$icon = 'zoom';
					$rel = 'prettyPhoto';
				}else{
					$href = get_permalink();
					$icon = 'doc';
					$rel = 'prettyPhoto';
				}
			}else{
				$type = 'image';
				$image_source = theme_get_option('portfolio','default_portfolio_image');
				$image_source = theme_parse_image($image_source,'post-image-placer.png',true);
				$source_type = $image_source['method'];
				$image_source = $image_source['media'];
				$href = theme_parse_image($image_source,'post-image-placer.png',false);
				$href = $href['media'];
				$icon = 'zoom';
				$rel = 'prettyPhoto';
			}
				
			
				$override_icon = get_post_meta(get_the_ID(), '_icon', true);
				if($override_icon && $override_icon != 'default'){
					$icon = $override_icon;
				}
				$resize = theme_resize($image_source,$size[0],$size[1],$source_type);
				$output .= '<div class="portfolio_image">';
				$output .= '<a class="image_icon_'.$icon.'" style="width:'.$width.'px;height:'.$size[1].'px;" title="'.$image_title.'" href="'.$href.'" '.(isset($link_target)?' target="'.$link_target.'" ':'').' rel="'.$rel.(($type == 'gallery')?'['.strtolower(get_the_title()).']':'').'">';
				$output .= '<img class="image_thumb" id="image'.get_the_id().'" width="'.$size[0].'" height="'.$size[1].'" style="opacity: 0;visibility: visible;" src="'.$resize['url']. '" title="' . get_the_title() . '" alt="' . get_the_title() . '" />';
				$output .= '<span class="overlay preload"></span>';
				$output .= '</a>';
				if($type == 'gallery' && !empty($image_ids)){
					$output .= '<div style="display:none">';
					foreach($image_ids as $image_id){
						$image_src = wp_get_attachment_image_src($image_id,'full');
						if($lightboxtitle=='image'){
							$image_title = get_the_title($image_id);
						}
						$output .= '<a title="'.$image_title.'" href="'.$image_src[0].'" rel="prettyPhoto'.'['.strtolower(get_the_title()).']'.'">gallery-'.get_the_ID().'</a>';
					}
					$output .= '</div>';
				}
				if($type =='doc'){
					$output .= $content;
				}
				$output .= '</div>';

				$output .= '<div class="portfolio_details">';
			if($title == 'true'){
				if($titlelinkable == 'true'){
					$output .= '<div class="portfolio_title"><h4><a href="'.get_permalink().'">' . get_the_title() . '</a></h4></div>';
				}else{
					$output .= '<div class="portfolio_title"><h4>' . get_the_title() . '</h4></div>';
				}
			}
			if($desc == 'true'){
				if($column ==1){
					$output .= '<div class="portfolio_desc">' . apply_filters('the_content',get_the_content()) . '</div>';
				}else{
					if($advancedesc == 'true'){
						remove_filter('get_the_excerpt', 'wp_trim_excerpt');
						$output .= '<div class="portfolio_desc">' . do_shortcode(wpautop(get_the_excerpt())). '</div>';
					}else{
						$output .= '<div class="portfolio_desc">' . get_the_excerpt() . '</div>';
					}
				}
			}
			if(theme_is_enabled(get_post_meta(get_the_id(), '_more', true), $more)){
				$more_link = theme_get_superlink(get_post_meta(get_the_id(), '_more_link', true), get_permalink());
				$more_link_target = get_post_meta(get_the_ID(), '_link_target', true);
				$more_link_target = $more_link_target?$more_link_target:'_self';
				if($moretext !== ''){
					$output .= '<div class="portfolio_more_button"><a class="'.($morebutton=='true'?'button white medium':'').'" href="'.$more_link.'" target="'.$more_link_target.'"><span class="text">'.$moretext.'</span></a></div>';
				}else{
					$output .= '<div class="portfolio_more_button"><a class="'.($morebutton=='true'?'button white medium':'').'" href="'.$more_link.'" target="'.$more_link_target.'"><span class="text">'.wpml_t(THEME_NAME , 'Portfolio More Button Text',theme_get_option('portfolio','more_button_text')).'</span></a></div>';
				}
			}
			$output .= '</div>';
			$output .= '</div>';
			$output .= '</li>';
			if($carousel == 'true' && $i%$max == 0)
				$output .= '</ul>';
			$i++;
		}
		if($carousel == 'false')
			$output .= '</ul>';
		else
			$output .= '</div>';
		$ajax = false;
		if($sortable && $sortabletype=='all')
			$ajax = true;
		if ($nopaging == 'false' && $carousel == 'false') {
			ob_start();
			$this->portfolio_pagenavi('', '', $r, $paged,$ajax);
			$output .= ob_get_clean();
		}
		if($desc_length != 'default' && $advancedesc !='true'){
			remove_filter( 'excerpt_length', array($excerpt_constructor,'get_length'));
		}
		wp_reset_postdata();
		$wp_filter['the_content'] = $the_content_filter_backup;
		return $output;
	}

	function portfolio_pagenavi($before = '', $after = '',$portfolio_query, $paged,$ajax=false) {
		global $wpdb, $wp_query;
		if (is_single())
			return;

		$pagenavi_options = array(
			//'pages_text' => __('Page %CURRENT_PAGE% of %TOTAL_PAGES%',THEME_FRONT_DOMAIN),
			'pages_text' => '',
			'current_text' => '%PAGE_NUMBER%',
			'page_text' => '%PAGE_NUMBER%',
			'first_text' => __('&laquo; First',THEME_FRONT_DOMAIN),
			'last_text' => __('Last &raquo;',THEME_FRONT_DOMAIN),
			'next_text' => __('&raquo;',THEME_FRONT_DOMAIN),
			'prev_text' => __('&laquo;',THEME_FRONT_DOMAIN),
			'dotright_text' => __('...',THEME_FRONT_DOMAIN),
			'dotleft_text' => __('...',THEME_FRONT_DOMAIN),
			'style' => 1,
			'num_pages' => 4,
			'always_show' => 0,
			'num_larger_page_numbers' => 3,
			'larger_page_numbers_multiple' => 10,
			'use_pagenavi_css' => 0,
		);
		$request = $portfolio_query->request;
		$numposts = $portfolio_query->found_posts;
		$max_page = intval($portfolio_query->max_num_pages);

		if (empty($paged) || $paged == 0)
			$paged = 1;
		$pages_to_show = intval($pagenavi_options['num_pages']);
		$larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
		$larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
		$pages_to_show_minus_1 = $pages_to_show - 1;
		$half_page_start = floor($pages_to_show_minus_1 / 2);
		$half_page_end = ceil($pages_to_show_minus_1 / 2);
		$start_page = $paged - $half_page_start;

		if ($start_page <= 0)
			$start_page = 1;

		$end_page = $paged + $half_page_end;
		if (($end_page - $start_page) != $pages_to_show_minus_1) {
			$end_page = $start_page + $pages_to_show_minus_1;
		}

		if ($end_page > $max_page) {
			$start_page = $max_page - $pages_to_show_minus_1;
			$end_page = $max_page;
		}

		if ($start_page <= 0)
			$start_page = 1;

		$larger_pages_array = array();
		if ($larger_page_multiple)
			for($i = $larger_page_multiple; $i <= $max_page; $i += $larger_page_multiple)
				$larger_pages_array[] = $i;
		$style = '';
		if($ajax)
			$style='ajaxpage';
		if ($max_page > 1 || intval($pagenavi_options['always_show'])) {
			$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
			$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
			echo $before . '<div class="pagenavi  '.$style.'">' . "\n";
			switch(intval($pagenavi_options['style'])){
				// Normal
			case 1:
				if (! empty($pages_text)) {
					echo '<span class="pages">' . $pages_text . '</span>';
				}
				
				/*$larger_page_start = 0;
				foreach($larger_pages_array as $larger_page) {
					if ($larger_page < $start_page && $larger_page_start < $larger_page_to_show) {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($larger_page), $pagenavi_options['page_text']);
						echo '<a href="' . esc_url(get_pagenum_link($larger_page)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
						$larger_page_start++;
					}
				}*/
				if($ajax){
					$cat = 'all';
					
					if(isset($portfolio_query->query_vars['tax_query'][0]['terms']) && count($portfolio_query->query_vars['tax_query'][0]['terms'])==1)
						$cat = $portfolio_query->query_vars['tax_query'][0]['terms'][0];
					if ($start_page >= 2 && $pages_to_show < $max_page) {
						$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
						echo '<a href="#'.$cat.'&page=1" data-cat="'.$cat.'" data-page="1" class="first" title="' . $first_page_text . '">' . $first_page_text . '</a>';
						if (! empty($pagenavi_options['dotleft_text'])) {
							echo '<span class="extend">' . $pagenavi_options['dotleft_text'] . '</span>';
						}
					}
					if($paged != 1)
						echo '<a class="previouspostslink" href="#'.$cat.'&action=prev" data-cat="'.$cat.'" data-page="'.($paged-1).'">'.$pagenavi_options['prev_text'].'</a>';
					if($paged != $max_page)
						echo '<a class="nextpostslink" href="#'.$cat.'&action=next" data-cat="'.$cat.'" data-page="'.($paged+1).'">'.$pagenavi_options['next_text'].'</a>';
					for($i = $start_page; $i <= $end_page; $i++) {
						if ($i == $paged) {
							$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
							echo '<span class="current">' . $current_page_text . '</span>';
						} else {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							echo '<a href="#'.$cat.'&page='.$i.'" class="page" data-cat="'.$cat.'" data-page="'.$i.'"title="' . $page_text . '">' . $page_text . '</a>';
						}
					}
					/*$larger_page_end = 0;
					foreach($larger_pages_array as $larger_page) {
						if ($larger_page > $end_page && $larger_page_end < $larger_page_to_show) {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($larger_page), $pagenavi_options['page_text']);
							echo '<a href="' . esc_url(get_pagenum_link($larger_page)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
							$larger_page_end++;
						}
					}*/
					if ($end_page < $max_page) {
						if (! empty($pagenavi_options['dotright_text'])) {
							echo '<span class="extend">' . $pagenavi_options['dotright_text'] . '</span>';
						}
						$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
						echo '<a href="#'.$cat.'&page='.$max_page.'" class="last" data-cat="'.$cat.'" data-page="'.$max_page.'" title="' . $last_page_text . '">' . $last_page_text . '</a>';
					}

				}
				else{
					if ($start_page >= 2 && $pages_to_show < $max_page) {
						$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
						echo '<a href="' . esc_url(get_pagenum_link()) . '" class="first" title="' . $first_page_text . '">' . $first_page_text . '</a>';
						if (! empty($pagenavi_options['dotleft_text'])) {
							echo '<span class="extend">' . $pagenavi_options['dotleft_text'] . '</span>';
						}
					}
					previous_posts_link($pagenavi_options['prev_text']);
					next_posts_link($pagenavi_options['next_text'], $max_page);
					for($i = $start_page; $i <= $end_page; $i++) {
						if ($i == $paged) {
							$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
							echo '<span class="current">' . $current_page_text . '</span>';
						} else {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							echo '<a href="' . esc_url(get_pagenum_link($i)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
						}
					}
					$larger_page_end = 0;
					/*foreach($larger_pages_array as $larger_page) {
						if ($larger_page > $end_page && $larger_page_end < $larger_page_to_show) {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($larger_page), $pagenavi_options['page_text']);
							echo '<a href="' . esc_url(get_pagenum_link($larger_page)) . '" class="page" title="' . $page_text . '">' . $page_text . '</a>';
							$larger_page_end++;
						}
					}*/
					if ($end_page < $max_page) {
						if (! empty($pagenavi_options['dotright_text'])) {
							echo '<span class="extend">' . $pagenavi_options['dotright_text'] . '</span>';
						}
						$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
						echo '<a href="' . esc_url(get_pagenum_link($max_page)) . '" class="last" title="' . $last_page_text . '">' . $last_page_text . '</a>';
					}
				}
				break;
				// Dropdown
			case 2:
				echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" method="get">' . "\n";
				echo '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">' . "\n";
				for($i = 1; $i <= $max_page; $i++) {
					$page_num = $i;
					if ($page_num == 1) {
						$page_num = 0;
					}
					if ($i == $paged) {
						$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
						echo '<option value="' . esc_url(get_pagenum_link($page_num)) . '" selected="selected" class="current">' . $current_page_text . "</option>\n";
					} else {
						$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
						echo '<option value="' . esc_url(get_pagenum_link($page_num)) . '">' . $page_text . "</option>\n";
					}
				}
				echo "</select>\n";
				echo "</form>\n";
				break;
			}
			echo '</div>' . $after . "\n";
		}
	}
	
}
function theme_generator($function){
	global $_themeGenerator;
	if($_themeGenerator==NULL){
		$_themeGenerator = new themeGenerator;
	}
	$args = array_slice( func_get_args(), 1 );
	return call_user_func_array(array( &$_themeGenerator, $function ), $args );
}
