// JScript File
var SlideShow = function(){
    //private properties and methods
    var priv = {
	msie6 : jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"],
        imgSize : 0,
	auto_main_image : 6,
	arr : new Array(),
	pointer : -1,
	control : 0,
	folderID : "",
	firstLoad : true,
	timer: {},
        showImage : function(){
	    if(priv.control == 0)
		return;	
	    priv.pointer = priv.pointer + 1;
	    if(priv.pointer >= priv.imgSize)
		priv.pointer = 0;
	    mainImg = jQuery($("#main_images #"+priv.folderID+" img").get(priv.pointer));
	    newSrc = mainImg.attr("load");
	    if(!priv.firstLoad){
		if(mainImg.attr("status") != "1"){
			priv.pointer = priv.pointer - 1;
			if(priv.pointer < 0)
				priv.pointer = 0;
			priv.timer = setTimeout(function() { priv.showImage(); }, 1000);
			return;
		}
	    }
	    $("#main_images #"+priv.folderID+" img").attr("current", "0");
	    $("img#main_image").fadeOut("slow", function(e2){
		$("img#main_image").attr({ 
			src: newSrc
		});
		mainImg.attr("current", "1");
		priv.timer = setTimeout(function() { priv.showImage(); }, priv.auto_main_image * 1000);
	    });
	    priv.firstLoad = false;
        },
	randOrd : function(){
		return (Math.round(Math.random())-0.5); 
	},
	loadNextImage: function(){
		tempPointer = priv.pointer + 1;
		if(tempPointer >= priv.imgSize)
			tempPointer = 0;
		tempImg = jQuery($("#main_images #"+priv.folderID+" img").get(tempPointer));
		tempImg.unbind("load").bind("load",function(){
			$(this).attr("status", "1");
		});
		if(tempImg.attr("status") != "1"){
			tempImg.attr({ 
				src: tempImg.attr("load")
			});
		}
	}
    };
    
    //public methods
    return {
        OnReady    : function(){
		SlideShow.StopSlideShow();
		$("#main_image").hide();
		priv.control = 1;
		priv.firstLoad = true;
		if($("input").index($("#interval")) > 0){ 
			interval = $("input#interval").val();
			if(interval != ""){
				intInterval = parseInt(interval,10);
				priv.auto_main_image = (intInterval != "NaN" ? intInterval : 6);
			}
		}
		$("#main_image").unbind("load").bind("load",function(){
			$("#main_image").fadeIn(500);
			priv.loadNextImage();
		});
		priv.folderID = $("input#image_folder_name").val();
		priv.imgSize = $("#main_images #"+priv.folderID+" img").size();
		currentIndex = $("#main_images #"+priv.folderID+" img").index($("#main_images #"+priv.folderID+" img[current='1']"))
		priv.pointer = currentIndex < 0 ? -1 : (currentIndex - 1);//$("#main_images #"+priv.folderID+" img").index($("#main_images #"+priv.folderID+" img[current='1']")) -1;
		priv.showImage();
        },
	StopSlideShow : function(){
		priv.control = 0;
		clearTimeout(priv.timer);
	}
    };
}();
