/*!
 * slideViewer 1.2
 * Examples and documentation at: 
 * hhttp://www.gcmingati.net/wordpress/wp-content/lab/jquery/imagestrip/imageslide-plugin.html
 * 2007-2010 Gian Carlo Mingati
 * Version: 1.2.0 (17-FEBRAURY-2010)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Requires:
 * jQuery v1.4.1 or later, jquery.easing.1.2
 * 
 */

//var j = 0;
jQuery.fn.slideView = function(settings) {
	return this.each(function(){
		var container = jQuery(this);
		var photos = container.find("ul.photos");
		var pictWidth = 272;
		var pictHeight = photos.find("img").height();
		var pictEls = photos.find("li").size();
		var stripViewerWidth = pictWidth*pictEls;
		var offset = 7;
		var counter = 0;
		var timerInterval;
		photos.width(stripViewerWidth);
		//container.each(function(i) {
		container.find("div.mask").after("<ul class=\"controls\"><\/ul>");
		var controls = container.find("ul.controls");
		
		controls.append("<li class='arrows L'><a class='hide' href='#'>&lt;<\/a><\/li>");
											
		photos.find("li").each(function(n) {
			jQuery(this).hide();
			controls.append("<li><a class='num' title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");												
		});
		
		container.find("div.mask").click(function () {	
			document.location.href = photos.find("li:visible a").attr("href");
		});
		
		controls.append("<li class='arrows G'><a class='hide' href='#'>&gt;<\/a><\/li>");
		
		controls.append("<li class='button'><a class='hide' href='#'>Play<\/a><\/li>");
		controls.find("li.button a").bind("click", function(event){
			if (jQuery(this).html() == "Play") {
				pause();
			} else {
				play();
				jQuery(this).removeClass("pause").addClass("play");
				jQuery(this).html('Play');
			}
			event.preventDefault();
		});																		
		
		function gotoPhoto(z) {
			counter = z;
			controls.find("li.on").removeClass("on");
			controls.find("li:nth-child(" + (z+1) + ")").addClass("on");
			photos.find("li:visible").fadeOut();
			photos.find("li:nth-child(" + z + ")").fadeIn();
		}
		
		function nextPhoto() {
			if (counter < pictEls) {
				counter++;
			} else {
				counter = 1;
			}
			gotoPhoto(counter);
		}
		
		function play() {
			timerInterval = setInterval(nextPhoto, 3000);
			controls.find("li.button").removeClass("pause").addClass("play").find("a").html('Play');
		}
		
		function pause() {
			clearInterval(timerInterval);
			controls.find("li.button").removeClass("play").addClass("pause").find("a").html('Pause');			
		}
		
		controls.find(".arrows").each(function(z) {
			jQuery(this).bind("click", function(event){
				var gotoPos = counter;
				switch (z) {
					case 0:
						if (gotoPos > 1) gotoPos -= 1;
						break;
					case 1:
						if (gotoPos < pictEls) gotoPos += 1;
						break;
				}
				gotoPhoto(gotoPos);
				event.preventDefault();
			});
		});
		
		controls.find("a.num").each(function(z) {
			jQuery(this).bind("click", function(event){
				gotoPhoto(z+1);
				pause();
				event.preventDefault();
			});
		});
		controls.find("li:nth-child(2)").addClass("on");
		
		play();
	});
};
