(function(){
	var SS = {
		browser: navigator.appName,
		version: parseFloat(navigator.appVersion),	
		show_console: false,
		images : [],
		display_for : 20000,
		timer : 0,
		timerStop : 0,
		current : -1,
		start_width : 0,
		playing: true,
		animate : function(){
			if (SS.show_console) {console.log("function: animate");}
			if(SS.current == -1){
				var _img = jQuery("#ss_container img");
				var _link = jQuery("#ss_link");
				SS.images.unshift({"src" : _img.attr("src"), "alt" : _img.attr("alt"), "href" : _link.attr("href"), "caption" : _link.text(), "loaded" : true});
				SS.current++;
			}
			SS.load_images();
			//SS.add_bar();
			SS.start_slideshow();

			jQuery("#ss_stop_animation").click(function(e){
				e.preventDefault();
				jQuery(this).blur();
				if(jQuery(this).text() == "Stop Animation"){
					SS.stop_slideshow();
				}
				else
				{
					SS.playing = true;
					jQuery(this).text("Stop Animation");
					SS.start_slideshow();
				}
			});
		},
		load_images: function(){
			if (SS.show_console) {console.log("function: load_images");}
			forEach(SS.images, SS.load);
		},
		load: function(image_obj){
			if (SS.show_console) {console.log("function: load");}
			var img = new Image();
			img.onload = function(){
				image_obj.loaded = true;
			}
			img.src=image_obj.src;
		},
		change: function(){
			if (SS.show_console) {console.log("function: change");}
			
			SS.current++;
		
			if(SS.current == SS.images.length){
				SS.current = 0;
			}
			var img_obj = SS.images[SS.current];
			
			if(img_obj.loaded){
				SS.show(img_obj);
			} else {
				var img = new Image();
				img.onload = function(){
					SS.show(img_obj);
				}
				img.src= img_obj.src;
			}
			
			
		},
		show : function(img){
		if (SS.show_console) {console.log("function: show");}
		SS.pause_slideshow();
		jQuery("#ss_container").fadeOut(1000, function(){

				jQuery("#ss_container img").attr("src", img.src);
				jQuery("#ss_container img").attr("alt", img.alt);
				jQuery("#ss_link").attr("href", img.href);				
				jQuery("#ss_container").fadeIn(1000, function(){
					jQuery("#ss_timer").width(SS.start_width);
					SS.start_slideshow();
				});
			});
		},

		add_navigation: function(){
			if (SS.show_console) {console.log("function: add_navigation");}
			jQuery("#ss_container").hover(
				function(){
					jQuery("#ss_next,#ss_prev").show();
					jQuery(this).addClass("hover");SS.pause_slideshow();},
				function(){
					jQuery("#ss_next,#ss_prev").hide();
					jQuery(this).removeClass("hover");SS.start_slideshow();});

			jQuery("#ss_container .overlay").fadeTo("fast", 0.70);
			jQuery("#ss_next,#ss_prev").hide();

			jQuery("#ss_prev .link").click(function(e){
				e.preventDefault();
				jQuery(this).blur();
				if(SS.current > 0){
					SS.current--;
				} else {
					SS.current = SS.images.length -1;
				}


				SS.stop_slideshow();
				var img_obj = SS.images[SS.current];
				SS.show(img_obj);
			});
			jQuery("#ss_next .link").click(function(e){
				e.preventDefault();
				jQuery(this).blur();
				if(SS.current < SS.images.length-1){
					SS.current++;
				} else {
					SS.current = 0;
				}
				SS.stop_slideshow();
				var img_obj = SS.images[SS.current];
				SS.show(img_obj);
			});
		},
		animate_bar: function(){
			if (SS.show_console) {console.log("function: animate_bar");}
			var inc = (100/SS.display_for) * SS.start_width
			new_width = jQuery("#ss_timer").width() - inc;
			jQuery("#ss_timer").width(new_width);
		},
		stop_slideshow: function(){
			if (SS.show_console) {console.log("function: stop_slideshow");}
			SS.playing = false;
			jQuery("#ss_timer").width(SS.start_width)
			jQuery("#ss_stop_animation").text("Start Animation");
			SS.pause_slideshow();
		},
		pause_slideshow: function(){
			if (SS.show_console) {console.log("function: pause_slideshow");}
			window.clearInterval(SS.timerStop);
			window.clearTimeout(SS.timer);
		},
		start_slideshow: function(){
			if (SS.show_console) {console.log("function: start_slideshow");}
			if(SS.playing){
				jQuery("#ss_timer").width(SS.start_width);
				//SS.timerStop = window.setInterval(SS.animate_bar, 100);
				SS.timer = window.setTimeout(SS.change, SS.display_for);
			}
		}
	}
	jQuery.noConflict();
	jQuery(function(){
		SS.images = slideshow.image_list;
		SS.display_for = slideshow.display_for * 1000;
		SS.animate();
		//SS.add_navigation();
				
	});
})()

