// JScript File
var Gallery = function(){
    //private properties and methods
    var priv = {
	msie6 : jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"],
        imgSize : 0,
	pointer: 0,
	locked : false,
	auto_main_image : 0,
	carousel : null,
	firstLoad : true,
	timer : {},
        showImage : function(){
	    if(priv.locked || priv.auto_main_image == 0)
		return;
	    priv.pointer = priv.pointer + 1;
	    if(priv.pointer >= priv.imgSize)
		priv.pointer = 0;
	    mainImg = jQuery($("ul#mycarousel li").get((priv.pointer - 1))).find("img");
	    newSrc = mainImg.attr("src").replace("umbraco/ImageGen.ashx?image=/", "").replace("&width=49&height=38&constrain=true", ""); //  .replace("49", "930").replace("38", "482");
	    //alert(newSrc + "     #main_images img[load='"+newSrc+"']     " + $("#main_images img[load='"+newSrc+"']").size());
	    if(!priv.firstLoad){
	    	if($("#main_images img[load='"+newSrc+"']").attr("status") != "1"){
			priv.pointer = priv.pointer - 1;
			if(priv.pointer < 0)
				priv.pointer = 0;
			priv.timer = setTimeout(function() { priv.showImage(); }, 1000);
			return;
	    	}
	    }
	    $("#gallery_main_img").fadeOut("slow", function(e2){
		$("#gallery_main_img").attr({ 
			src: newSrc
		});
		priv.timer = setTimeout(function() { priv.showImage(); }, priv.auto_main_image * 1000);
	    });
	    priv.firstLoad = false;
        },
	loadNextImage: function(){
		tempPointer = priv.pointer + 1;
		if(tempPointer >= priv.imgSize)
			tempPointer = 0;
		targetImg = jQuery($("ul#mycarousel li").get((tempPointer - 1))).find("img");
		tempSrc = targetImg.attr("src").replace("umbraco/ImageGen.ashx?image=/", "").replace("&width=49&height=38&constrain=true", ""); //  .replace("49", "930").replace("38", "482");
		tempImg = $("#main_images img[load='"+tempSrc+"']");
		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(){
		Gallery.StopGallery();
		$("ul#mycarousel li:last").css('width', '49px');
		$("#gallery_main_img").hide();
		if(priv.msie6){
			mainSrc = $("#gallery_main_img").attr('load');
			mainSrc = mainSrc.replace("umbraco/ImageGen.ashx?image=/", "").replace("&width=930&height=482&constrain=true", "");
			$("#gallery_main_img").attr('src', mainSrc);
			$("#mycarousel li").each(function(){
				thumbSrc = $(this).find('img').attr('load');
        			thumbSrc = thumbSrc.replace("/umbraco/ImageGen.ashx?image=", "");
				thumbSrc = thumbSrc.replace("&width=49&height=38&constrain=true", "");
				$(this).find('a').attr("href", "javascript:Gallery.ClickImage('"+thumbSrc+"');");
				$(this).find('img').attr('src', thumbSrc);
			});
		}
		else{
			//$("#gallery_main_img").attr('src', $("#gallery_main_img").attr('load'));
			$("#mycarousel li").each(function(){
				$(this).find('img').attr('src', $(this).find('img').attr('load'));
			});
		}
            	$("#mycarousel").jcarousel({scroll: parseInt($('input#scroll').val())});
		$("#mycarousel").css('visibility', 'visible');
		/*auto: $('input#auto').val(),
		wrap: 'circular',
		initCallback: Gallery.MyCarousel_InitCallback,
		scroll: parseInt($('input#scroll').val()),
		itemVisibleInCallback: {onBeforeAnimation: Gallery.MyCarousel_ItemVisibleInCallback},
        	itemVisibleOutCallback: {onAfterAnimation: Gallery.MyCarousel_ItemVisibleOutCallback}*/
        	$("#gallery_main_img").unbind("load").bind("load",function(){
			$("#gallery_main_img").fadeIn("slow");
			priv.loadNextImage();
		});
		//$('#mycarousel').Draggable({
		//	insideParent : true,
		//	axis:	'horizontally',
		//	grid:		[74,0]
		//	//onSlide : function(){
		//	//	alert('aa');
		//	//}
		//});
		//$("ul#mycarousel li a").unbind("click").bind("click", priv.clickImage);
		priv.auto_main_image = parseInt($('input#auto_main_image').val());
		priv.locked = false;
		priv.pointer = 0;
		priv.firstLoad = true;
		priv.imgSize = $("ul#mycarousel li").size();
		priv.showImage();
        },
        MyCarousel_InitCallback : function(carousel){
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});

		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
        },
	MyCarousel_ItemVisibleInCallback : function(carousel, item, i, state, evt){
		// The index() method calculates the index from a
    		// given index who is out of the actual item range.
		var idx = carousel.index(i, mycarousel_itemList.length);
		carousel.add(i, Gallery.MyCarousel_GetItemHTML(mycarousel_itemList[idx - 1]));
	},
	MyCarousel_ItemVisibleOutCallback : function(carousel, item, i, state, evt){
		carousel.remove(i);
	},
	/**
	 * Item html creation helper.
	 */
	MyCarousel_GetItemHTML : function(item){
		if(priv.msie6){
			item.url = item.url.replace("/umbraco/ImageGen.ashx?image=", "").replace("&amp;width=49&amp;height=37&amp;constrain=true", "");
		}
		return '<a href="javascript:Gallery.ClickImage(\''+item.url+'\');">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a><img src="' + item.url + '" width="49" height="37" alt="' + item.title + '" />';
	},
	ClickImage : function(newSrc){
		newSrc = newSrc.replace("umbraco/ImageGen.ashx?image=/", "").replace("&width=49&height=38&constrain=true", "");//replace("49", "930").replace("38", "482");
		if($("#gallery_main_img").attr("src") == newSrc)
			return;
		priv.locked = true;
		$("#gallery_main_img").fadeOut("slow", function(e2){
			$("#gallery_main_img").attr({ 
				src: newSrc
			});
		});
	},
	SetCarousel : function(carousel){
		priv.carousel = carousel;
	},
	StopGallery : function(){
		clearTimeout(priv.timer);
	}
    };
}();
