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

(function($) {
	
	var flashdetected = "false"; 
	var images_array;
	var thbs_array;	
	var timer;
	var counter = 0;
	var img_selected;
	var showThbs;
	var currentThb;
	var thbs_array = [];
			
	$.fn.startJsSlideshow = function(param) {	
	
		if ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("Mac") == -1 && navigator.appVersion.indexOf("3.1") == -1) || (navigator.plugins && navigator.plugins["Shockwave Flash"]) || navigator.plugins["Shockwave Flash 2.0"]) {
	    	flashSupported = true;
		} else {
	   		flashSupported = false;
		}
	
		var self = $(this);
		
		//find the list of the images
		var ul = self.find("ul:first");
		
		if (flashSupported==false) {
			appendlis(param);
		} else {
			if(jQuery.browser.mobile) appendlis(param);
		}
				
		var li = ul.find("li");
		var img = li.find("img");
		
		//images array
		images_array = $.makeArray(img);
		
				
		//find the list of the thumbnails
		var thbs_content = $("#thbs_wrapper");
		thbs_array = $.makeArray(thbs_content.find("img"));
	
		if(thbs_array.length>0) {
			
			addThbsEvents();
						
            var ul = $('ul',thbs_content);
            
            var itemsWidth = ul.innerWidth() - thbs_content.outerWidth() + 22;
						
			if(ul.innerWidth() >  thbs_content.outerWidth() - 22) {
				$('.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);
					}
				});
			}
		}
		//left button
		jQuery('#arrow_slideshow_left').click(function() {
			if (flashSupported==true) {
				document.getElementById("slideshowcontainer").move_backward();
			} else {
				click_left() 
			}
			jQuery('#pause_slideshow').css("visibility","visible");
			jQuery('#play_slideshow').css("visibility","hidden");
			jQuery('#play_slideshow').css("left","0px");
		});
		//right button
		jQuery('#arrow_slideshow_right').click(function() {
			if (flashSupported==true) {
				document.getElementById("slideshowcontainer").move_forward();
			} else {
				click_left() 
			}
			jQuery('#pause_slideshow').css("visibility","visible");
			jQuery('#play_slideshow').css("visibility","hidden");
			jQuery('#play_slideshow').css("left","0px");
		});					
		//pause button
		jQuery('#pause_slideshow').click(function() {
			jQuery('#pause_slideshow').css("visibility","hidden");
			jQuery('#play_slideshow').css("visibility","visible");
			jQuery('#play_slideshow').css("left","931px");
			if (flashSupported==true) document.getElementById("slideshowcontainer").not_move();
			else stop_slideshow();
		});	
		
		jQuery('#play_slideshow').click(function() {
			jQuery('#pause_slideshow').css("visibility","visible");
			jQuery('#play_slideshow').css("visibility","hidden");
			jQuery('#play_slideshow').css("left","0px");
			if (flashSupported==true) document.getElementById("slideshowcontainer").again_move();
			else play_slideshow();
		});
		
		if(images_array.length>0) {		
			img_selected = jQuery(images_array[0]);
			img_selected.fadeTo("slow", 1);
			timer = setInterval(loop,5000);	
		}
		
		function appendlis(param) {
			var i = 0;
			while(i<param.length) {
				var url = param[i];
				ul.append('<li><img src='  + url + ' width="944" height="445"></img></li>');
				i++;
			}
		}
		
		function loop() {	
			
			if (thbs_array.length>0) currentThb.attr('class','unactive');
			counter++;				
			if (counter == images_array.length) counter = 0;
			img_selected.fadeTo("slow", 0);			
			img_selected = jQuery(images_array[counter]);
			if (thbs_array.length>0) currentThb = $(thbs_array[counter]);
			if (thbs_array.length>0) $(thbs_array[counter]).attr('class','active');
			img_selected.fadeTo("slow", 1);	
					
		}
		
		function stop_slideshow() {
			clearInterval(timer);
		}
		
		function play_slideshow() {
			timer = setInterval(loop,5000);	
		}
		
		function click_right() {
			clearInterval(timer);
			if (thbs_array.length>0) currentThb.attr('class','unactive');
			counter++;			
			if (counter == images_array.length) counter = 0;
			img_selected.fadeTo("slow", 0);			
			img_selected = jQuery(images_array[counter]);
			if (thbs_array.length>0) currentThb = $(thbs_array[counter]);
			if (thbs_array.length>0) $(thbs_array[counter]).attr('class','active');
			img_selected.fadeTo("slow", 1);					
			timer = setInterval(loop,5000);				
		}
		
		function click_left() {			
			clearInterval(timer);	
			if (thbs_array.length>0) currentThb.attr('class','unactive');	
			counter--;
			if (counter == -1) counter = images_array.length-1;			
			img_selected.fadeTo("slow", 0);			
			img_selected = jQuery(images_array[counter]);
			if (thbs_array.length>0) currentThb = $(thbs_array[counter]);
			if (thbs_array.length>0) $(thbs_array[counter]).attr('class','active');
			img_selected.fadeTo("slow", 1);			
			timer = setInterval(loop,5000);					
		}
		
		function addThbsEvents() {
			$("#thbs_wrapper img" ).each(				
				function(){
					thbs_array.push($(this));
					$(this).attr('class','unactive');
					if ($(this).attr("id") == "0") {
						$(this).attr('class','active');
						currentThb = $(this);
					} else {
						$(this).attr('class','unactive');
					}
					$(this).bind ("click",function() {	
							if ($(this).attr("class")!= "active") {
								$(this).attr('class','active');
								currentThb.attr('class','unactive');
								currentThb = $(this);
								if (flashSupported==true) {									
									 document.getElementById("slideshowcontainer").go_to_picture($(this).attr("id"));
								} else {
									clearInterval(timer);
									counter = parseInt($(this).attr("id"));
									img_selected.fadeTo("slow", 0);
									img_selected = jQuery(images_array[counter]);
									img_selected.fadeTo("slow", 1);	
									timer = setInterval(loop,5000);
								}
							}
						});
					});
				}
	}	
	
	$.fn.loopImg = function(param) {
		currentThb.attr('class','unactive');
		currentThb = $(thbs_array[param]);
		currentThb.attr('class','active');		
	};	
		
}) (jQuery);


	
	
	
