<?php
/**
 * Slider Widget Class
 *
 * @since 2.8.0
 */
class Theme_Widget_Slider extends WP_Widget {

	function Theme_Widget_Slider() {
		$widget_ops = array('classname' => 'widget_slider', 'description' => __( "Slideshow widget", THEME_ADMIN_DOMAIN) );
		$this->WP_Widget('slider', THEME_SLUG.' - '.__('Slideshow', THEME_ADMIN_DOMAIN), $widget_ops);
		$this->alt_option_name = 'widget_slider';
	
		if ('widgets.php' == basename($_SERVER['PHP_SELF'])) {
			add_action( 'admin_print_scripts', array(&$this, 'add_admin_script') );
		}
	}
	function add_admin_script(){
		wp_enqueue_script('jquery-chosen');
		wp_enqueue_script( 'slider-chosen', THEME_ADMIN_ASSETS_URI . '/js/slider-chosen.js', array('jquery'));
	}

	function getItems($source,$number){
		$items = array();
		foreach($source as $key => $value){
			if($key == 's'){
				$query = array( 
					'post_type' => 'slideshow', 
					'showposts'=>$number, 
					'orderby'=>'menu_order', 
					'order'=>'ASC',
				);

				if($value != 'all'){
					global $wp_version;
					if(version_compare($wp_version, "3.1", '>=')){
						$query['tax_query'] = array(
							array(
								'taxonomy' => 'slideshow_category',
								'field' => 'slug',
								'terms' => $value
							)
						);
					}else{
						$query['taxonomy'] = 'slideshow_category';
						$query['term'] = $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' => $link,
						'link_target' => $link_target,
						'content'=>get_the_content(),
					);
					$items[] = $res;
				}
			}elseif($key == 'b'){
				$query = array( 
					'post_type' => 'post', 
					'showposts'=>$number, 
					'orderby'=>'date', 
					'order'=>'DESC',
					'meta_key'=>'_thumbnail_id',
				);
				if($value != 'all'){
					$value = implode(',',$value);
					$query['category_name'] = $value;
				}

				$loop = new WP_Query($query);
				while ( $loop->have_posts() ) {
					$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'];
					}
					$loop->the_post();
					$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;
				}
			}elseif($key == 'p'){
				$query = array( 
					'post_type' => 'portfolio', 
					'showposts'=>$number, 
					'orderby'=>'menu_order', 
					'order'=>'ASC',
				);
				if($value != 'all'){
					global $wp_version;
					if(version_compare($wp_version, "3.1", '>=')){
						$query['tax_query'] = array(
							array(
								'taxonomy' => 'portfolio_category',
								'field' => 'slug',
								'terms' =>  $value
							)
						);
					}else{
						$query['taxonomy'] = 'portfolio_category';
						$query['term'] =  $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',
						'content'=>get_the_excerpt(),
					);
					$items[] = $res;
				}

			}
		}
		return $items;
	}
	function widget($args, $instance) {
		extract( $args );
		$title  = apply_filters( 'widget_title', $instance['title'] );
		$source = $instance['source'];
		$number = is_numeric($instance['number']) ? $instance['number'] : 6;
		$per = is_numeric($instance['per']) ? $instance['per'] : 3;
		$delay = is_numeric($instance['delay']) ? $instance['delay'] : 1500;
		$speed = is_numeric($instance['speed']) ? $instance['speed'] : 500;
		$width = is_numeric($instance['width']) ? $instance['width'] : 180;
		$height = is_numeric($instance['height']) ? $instance['height'] : 100;
		$animate = !empty($instance['animate']) ? $instance['animate'] : 'fade';
		$easing = !empty($instance['easing']) ? $instance['easing'] : 'swing';
		$type = !empty($instance['type']) ? $instance['type'] : 'image';
		$items = array();
		if(! empty($source) && is_array($source)){
			foreach($source as $value){
				$value = split('\|',$value);
				if(empty($value[1])){
					$items[$value[0]] = 'all';
					break;
				}else
					$items[$value[0]][] = $value[1];
			}
			$items = $this->getItems($items,$number);
		}
		echo $before_widget;
		echo '<div class="widget-slider-panel">';
		echo '<div class="widget-slider-title">';
		if ( $title )
			echo $before_title . $title . $after_title;
		echo '</div>';
		echo '<div class="widget-slider-control">';
		echo '<div class="widget-slider-forward">&gt;</div><div class="widget-slider-divider"></div><div class="widget-slider-back">&lt;</div>';
		echo '</div>';
		echo '<div class="clear"></div>';
		echo '</div>';
		echo '<div class="widget-slider-content" data-delay="'.$delay.'" data-speed="'.$speed.'" data-animate="'.$animate.'" data-easing="'.$easing.'">';
		foreach($items as $key =>$item){
			if($key % $per == 0)
				echo '<ul class="widget-slider-list post-list">';
			echo '<li class="widget-slider-item">';
			$url = THEME_IMAGES_DIR.'/slide-default.jpg';
			$source_type = 'dir';
			$media_params=$item['media'];
			if(!empty($media_params['type'])&&$media_params['type']=='image'){
				$url = $media_params['media'];
				$source_type = $media_params['method'];
			}
			
			$resize = theme_resize($url,$width,$height,$source_type);
			echo '<a target="'.$item['link_target'].'" href="'.$item['link'].'">';
			echo '<img class="thumbnail" width="'.$resize['width'].'" height="'.$resize['height'].'" src="'.$resize['url'].'" alt="'.$item['title'].'"/>';
			echo '</a>';
			if($type == 'html'){
				echo '<a target="'.$item['link_target'].'"href="'.$item['link'].'">'.$item['title'].'</a>';
				echo $item['content'];
			}
			echo '<div class="clear"></div>';
			echo '</li>';
			if($key % $per == ($per-1) || $key == count($items))
				echo '</ul>';
		}	
		echo '</div>';		
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = (int) $new_instance['number'];
		$instance['per'] = (int) $new_instance['per'];
		$instance['source'] = $new_instance['source'];
		$instance['type'] = $new_instance['type'];
		$instance['animate'] = $new_instance['animate'];
		$instance['easing'] = $new_instance['easing'];
		$instance['delay'] = (int)$new_instance['delay'];
		$instance['speed'] = (int)$new_instance['speed'];
		$instance['width'] = (int)$new_instance['width'];
		$instance['height'] = (int)$new_instance['height'];
		
		return $instance;
	}

	function form( $instance ) {
		
		$title = ! empty($instance['title']) ? esc_attr($instance['title']) : 'SlideShow';
		$number = ! empty($instance['number']) ? (int)esc_attr( $instance['number'] ) : 5;
		$per = ! empty($instance['per']) ? (int)esc_attr( $instance['per'] ) : 1;
		$source = ! empty($instance['source']) ? $instance['source'] : array();
		$type = isset($instance['type']) ? esc_attr( $instance['type'] ) : 'image';
		$easing = ! empty($instance['easing']) ? esc_attr( $instance['easing'] ) : 'swing';
		$animate = isset($instance['animate']) ? esc_attr( $instance['animate'] ) : 'fade';
		$delay = ! empty($instance['delay']) ? (int)esc_attr( $instance['delay'] ) : 1500;
		$speed = ! empty($instance['speed']) ? (int)esc_attr( $instance['speed'] ) : 500;
		$width = ! empty($instance['width']) ? (int)esc_attr( $instance['width'] ) : 180;
		$height = ! empty($instance['height']) ? (int)esc_attr( $instance['height'] ) : 100;

?>
		<script type="text/javascript">
		/*function selectInit(){
			jQuery("select.chosen").chosen();
			jQuery(".chzn-container").css('width','218px');
			jQuery(".chzn-drop").css('width','218px');
			jQuery(".search-field input").css('width','218px');
			jQuery(".chzn-container .chzn-results").css("max-height",'190px');
		}
		jQuery(document).ready(function(){
			selectInit();
		});*/
		</script>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', THEME_ADMIN_DOMAIN); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>

		<p><label for="<?php echo $this->get_field_id('source'); ?>"><?php _e('Select source of slideshow:', THEME_ADMIN_DOMAIN); ?></label>
		<select data-placeholder="Select Source.." class="social_icon_select_sites widefat" name="<?php echo $this->get_field_name('source'); ?>[]" multiple="multiple" style="height:auto;width:218px">
<?php
		echo '<optgroup label="SlideShow Category">';
		echo '<option value="s" '.(in_array('s', $source)? 'selected="selected"':'').'>(s) All</option>';
		$entries = get_terms('slideshow_category','orderby=name&hide_empty=0&suppress_filters=0');
		foreach($entries as $key => $entry) {
			$key = 's|'.$entry->slug;
			echo '<option value="' . $key . '" '.(in_array($key, $source)? 'selected="selected"':'').'>';
			echo '(s)' .$entry->name;
			echo '</option>';
		}
		echo '</optgroup>';

		echo '<optgroup label="Posts Category">';
		echo '<option value="p" '.(in_array('p', $source)? 'selected="selected"':'').'>(p) All</option>';
		$entries = get_categories('orderby=name&hide_empty=0');
		foreach($entries as $key => $entry) {
			$key = 'b|'.$entry->slug;
			echo '<option value="'. $key . '" '.(in_array($key, $source)? 'selected="selected"':'').'>';
			echo '(b)' .$entry->name;
			echo '</option>';
		}
		echo '</optgroup>';	

		echo '<optgroup label="Portfolios Category">';
		echo '<option value="p" '.(in_array('p', $source)? 'selected="selected"':'').'>(p) All</option>';
		$entries = get_terms('portfolio_category','orderby=name&hide_empty=0&suppress_filters=0');
		foreach($entries as $key => $entry) {
			$key = 'p|'.$entry->slug;
			echo '<option value="'. $key . '" '.(in_array($key, $source)? 'selected="selected"':'').'>';
			echo '(p)' .$entry->name;
			echo '</option>';
		}
		echo '</optgroup>';	
?>
		</select></p>
		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of sliders to show :', THEME_ADMIN_DOMAIN); ?></label>
		<input style="float:right" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

		<p><label for="<?php echo $this->get_field_id('per'); ?>"><?php _e('Number of sliders per page :', THEME_ADMIN_DOMAIN); ?></label>
		<input style="float:right" id="<?php echo $this->get_field_id('per'); ?>" name="<?php echo $this->get_field_name('per'); ?>" type="text" value="<?php echo $per; ?>" size="3" /></p>

		<p><label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Slider Type:', THEME_ADMIN_DOMAIN); ?></label>
		<select id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" class="widefat" style="width:100%;">
			<option value="image" <?php selected($type, 'image'); ?>>Only Image</option>
			<option value="html" <?php selected($type, 'html'); ?>>With Content</option>
		</select>
		</p>

		<p><label for="<?php echo $this->get_field_id('animate'); ?>"><?php _e('Slider Animate:', THEME_ADMIN_DOMAIN); ?></label>
		<select id="<?php echo $this->get_field_id( 'animate' ); ?>" name="<?php echo $this->get_field_name( 'animate' ); ?>" class="widefat" style="width:100%;">
			<option <?php selected($animate, 'all'); ?> value="all">Random</option>
			<option <?php selected($animate, 'blindX'); ?> value="blindX">BlindX</option>
			<option <?php selected($animate, 'blindY'); ?> value="blindY">BlindY</option>
			<option <?php selected($animate, 'blindZ'); ?> value="blindZ">BlindZ</option>
			<option <?php selected($animate, 'cover'); ?> value="cover">Cover</option>
			<option <?php selected($animate, 'uncover'); ?> value="uncover">Uncover</option>
			<option <?php selected($animate, 'curtainX'); ?> value="curtainX">CurtainX</option>
			<option <?php selected($animate, 'curtainY'); ?> value="curtainY">CurtainY</option>
			<option <?php selected($animate, 'fade'); ?> value="fade">Fade</option>
			<option <?php selected($animate, 'fadeout'); ?> value="fadeout">FadeOut</option>
			<option <?php selected($animate, 'fadeZoom'); ?> value="fadeZoom">FadeZoom</option>
			<option <?php selected($animate, 'growX'); ?> value="growX">GrowX</option>
			<option <?php selected($animate, 'growY'); ?> value="growY">GrowY</option>
			<option <?php selected($animate, 'scrollUp'); ?> value="scrollUp">ScrollUp</option>
			<option <?php selected($animate, 'scrollDown'); ?> value="scrollDown">ScrollDown</option>
			<option <?php selected($animate, 'scrollLeft'); ?> value="scrollLeft">ScrollLeft</option>
			<option <?php selected($animate, 'scrollRight'); ?> value="scrollRight">ScrollRight</option>
			<option <?php selected($animate, 'scrollHorz'); ?> value="scrollHorz">ScrollHorz</option>
			<option <?php selected($animate, 'scrollVert'); ?> value="scrollVert">ScrollVert</option>
			<option <?php selected($animate, 'shuffle'); ?> value="shuffle">Shuffle</option>
			<option <?php selected($animate, 'slideX'); ?> value="slideX">Shuffle</option>
			<option <?php selected($animate, 'slideY'); ?> value="slideY">SlideY</option>
			<option <?php selected($animate, 'toss'); ?> value="toss">Toss</option>
			<option <?php selected($animate, 'turnUp'); ?> value="turnUp">TurnUp</option>
			<option <?php selected($animate, 'turnDown'); ?> value="turnDown">TurnDown</option>
			<option <?php selected($animate, 'turnLeft'); ?> value="turnLeft">TurnLeft</option>
			<option <?php selected($animate, 'turnRight'); ?> value="turnRight">TurnRigh</option>
			<option <?php selected($animate, 'wipe'); ?> value="wipe">Wipe</option>
			<option <?php selected($animate, 'zoom'); ?> value="zoom">Zoom</option>
		</select>
		</p>
		<p><label for="<?php echo $this->get_field_id('easing'); ?>"><?php _e('Animate Easing:', THEME_ADMIN_DOMAIN); ?></label>
		<select id="<?php echo $this->get_field_id( 'easing' ); ?>" name="<?php echo $this->get_field_name( 'easing' ); ?>" class="widefat" style="width:100%;">
			<option selected="selected" value="swing">swing</option>
			<option <?php selected($easing, 'linear'); ?> value="linear">linear</option>
			<option <?php selected($easing, 'jswingeY'); ?> value="jswingeY">jswing</option>
			<option <?php selected($easing, 'easeInQuad'); ?> value="easeInQuad">easeInQuad</option>
			<option <?php selected($easing, 'easeOutQuad'); ?> value="easeOutQuad">easeOutQuad</option>
			<option <?php selected($easing, 'easeInOutQuad'); ?> value="easeInOutQuad">easeInOutQuad</option>
			<option <?php selected($easing, 'easeInCubic'); ?> value="easeInCubic">easeInCubic</option>
			<option <?php selected($easing, 'easeOutCubic'); ?> value="easeOutCubic">easeOutCubic</option>
			<option <?php selected($easing, 'easeInOutCubic'); ?> value="easeInOutCubic">easeInOutCubic</option>
			<option <?php selected($easing, 'easeInQuart'); ?> value="easeInQuart">easeInQuart</option>
			<option <?php selected($easing, 'easeOutQuart'); ?> value="easeOutQuart">easeOutQuart</option>
			<option <?php selected($easing, 'easeInOutQuart'); ?> value="easeInOutQuart">easeInOutQuart</option>
			<option <?php selected($easing, 'easeInQuint'); ?> value="easeInQuint">easeInQuint</option>
			<option <?php selected($easing, 'easeOutQuint'); ?> value="easeOutQuint">easeOutQuint</option>
			<option <?php selected($easing, 'easeInOutQuint'); ?> value="easeInOutQuint">easeInOutQuint</option>
			<option <?php selected($easing, 'easeInSine'); ?> value="easeInSine">easeInSine</option>
			<option <?php selected($easing, 'easeOutSine'); ?> value="easeOutSine">easeOutSine</option>
			<option <?php selected($easing, 'easeInOutSine'); ?> value="easeInOutSine">easeInOutSine</option>
			<option <?php selected($easing, 'easeInExpo'); ?> value="easeInExpo">easeInExpo</option>
			<option <?php selected($easing, 'easeOutExpo'); ?> value="easeOutExpo">easeOutExpo</option>
			<option <?php selected($easing, 'easeInCirc'); ?> value="easeInCirc">easeInCirc</option>
			<option <?php selected($easing, 'easeOutCirc'); ?> value="easeOutCirc">easeOutCirc</option>
			<option <?php selected($easing, 'easeInOutCirc'); ?> value="easeInOutCirc">easeInOutCirc</option>
			<option <?php selected($easing, 'easeInElastic'); ?> value="easeInElastic">easeInElastic</option>
			<option <?php selected($easing, 'easeOutElastic'); ?> value="easeOutElastic">easeOutElastic</option>
			<option <?php selected($easing, 'easeInBack'); ?> value="easeInBack">easeInBack</option>
			<option <?php selected($easing, 'easeOutBack'); ?> value="easeOutBack">easeOutBack</option>
			<option <?php selected($easing, 'easeInOutBack'); ?> value="easeInOutBack">easeInOutBack</option>
			<option <?php selected($easing, 'easeInBounce'); ?> value="easeInBounce">easeInBounce</option>
			<option <?php selected($easing, 'easeOutBounce'); ?> value="easeOutBounce">easeOutBounce</option>
			<option <?php selected($easing, 'easeInOutBounce'); ?> value="easeInOutBounce">easeInOutBounce</option>
		</select>
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'delay' ); ?>"><?php _e( 'Delay Time :', THEME_ADMIN_DOMAIN ); ?></label>
		<input id="<?php echo $this->get_field_id( 'delay' ); ?>" style="float:right" name="<?php echo $this->get_field_name( 'delay' ); ?>" type="text" value="<?php echo $delay; ?>" size="5" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'speed' ); ?>"><?php _e( 'Animate Speed :',THEME_ADMIN_DOMAIN ); ?></label>
		<input id="<?php echo $this->get_field_id( 'speed' ); ?>" style="float:right" name="<?php echo $this->get_field_name( 'speed' ); ?>" type="text" value="<?php echo $speed; ?>" size="5"/>
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e( 'Image Width :',THEME_ADMIN_DOMAIN ); ?></label>
		<input id="<?php echo $this->get_field_id( 'width' ); ?>" style="float:right" name="<?php echo $this->get_field_name( 'width' ); ?>" type="text" value="<?php echo $width; ?>" size="5"/>
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php _e( 'Image Height :',THEME_ADMIN_DOMAIN ); ?></label>
		<input id="<?php echo $this->get_field_id( 'height' ); ?>" style="float:right" name="<?php echo $this->get_field_name( 'height' ); ?>" type="text" value="<?php echo $height; ?>" size="5"/>
		</p>
<?php
	}
}
