/**
 * Handle: slideshow_script    
 * Version: 0.0.1
 * Deps: $jq
 * Enqueue: true
 */

(function($) {
	
	var imgs_array;
	var anchors_array;
	var etiquettes;
	var videos_array;
	var video_count = 0;
	var videoiframe;
	
	var delay_display_start = 1500;
	 
	$.fn.startVideoScript = function(ar) {	
	
		videos_array = $.makeArray(ar);
		
		var self = $(this);
		
		//find the list of the images
		var ul = self.find("ul:first");
		var li = ul.find("li");
		var etiquette = li.find("span:first");
		var anchors = li.find("a");
		var img = li.find("img");
		
		var parent = self.parent();
		videoiframe = $("#video_iframe");

		videoiframe.append('<iframe id="vimeo_wrapper" src="' + videos_array[video_count] +'?title=0&amp;byline=0&amp;portrait=0&amp;color=cd071e" width="828" height="445" frameborder="0"></iframe>');	
		
		timer = setInterval(start,delay_display_start);		
		
		//anchors array
		anchors_array = $.makeArray(anchors);			
		//images array
		imgs_array = $.makeArray(img);
		//etiquettes array
		etiquettes =  $.makeArray(etiquette);
		
		//per default		
		et_selected = jQuery(etiquettes [0]);
		et_selected.fadeTo("slow",0);	
						
		if(imgs_array.length>0) {
			addThbsEvents();			
			
            var ul = $('ul',self);
            
            var itemsWidth = ul.innerWidth() - (self.outerWidth()-20);
						
			if (ul.innerWidth() > (self.outerWidth() + 20)) {			
				$('.slider').slider({				
					min: 0,
					max: itemsWidth,
					handle: '.handle',
					stop: function (event, ui) {
						ul.animate({'left' : ui.value * -1}, 500);
					},
					slide: function (event, ui) {					
						ul.css('left', ui.value * -1);
					}
				});
			}
		}
				
		function addThbsEvents() {
			$("#thbs_wrapper img").each(function() {
					$(this).bind ("click",function() {							
						video_counter = parseInt($(this).attr("id"));
						$('#vimeo_wrapper').remove();
						videoiframe.append('<iframe id="vimeo_wrapper" src="' + videos_array[video_counter] +'?title=0&amp;byline=0&amp;portrait=0&amp;color=cd071e" width="614" height="330" frameborder="0"></iframe>');	
						
						})
					
						
					});
					
					$("#thbs_wrapper img").each(function() {
					$(this).bind ("mouseover",function() {		
					
						video_count = parseInt($(this).attr("id"));					
							var et = $(etiquettes[video_count]);
							et.fadeTo("slow",1);
						})
					
						
					});
					
					$("#thbs_wrapper img").each(function() {
					$(this).bind ("mouseout",function() {		
						video_count = parseInt($(this).attr("id"));					
							var et = $(etiquettes[video_count]);
							et.fadeTo("slow",0.25);	
						})					
						
					});					
		}
		
		function start() {
			clearInterval(timer);		
			videoiframe.fadeTo(250, 1);
		}
	}	
	
}) (jQuery);


	
	
	
