var slideshow={

counter: 0,
repeat: true, //true / false flag to set repeatability of cycle
transition: 'fade', //transition type Ð currently ÔfadeÕ or ÔslideÕ
startState: 1, // 0=no image Ð transition to image 1; 1=1st image displayed from start Ð transition to image 2
startdelay: 3000, //delay before slideshow starts
transitiondelay: 2500, //delay between transition starts
transitiontime: 2000, //time taken by transition Ð should be less than transition delay

start: function() {
	if (document.getElementById("imageBoxSlideshow") && document.getElementById("bannerImages") && $("#bannerImages img").length>0) {

		slideshow.div = $("#imageBoxSlideshow");
		slideshow.imageArray=$("#bannerImages img");
		//set first image
		if (slideshow.startState==1) { 
		slideshow.div.parent().css("background","url("+slideshow.imageArray[0].src+") 0 0 no-repeat"); 
		if (slideshow.imageArray.length>1) slideshow.counter++; 
		}
		if (slideshow.transition=='slide') {
		window.setTimeout("slideshow.slide()",slideshow.startdelay);
		}
		else if (slideshow.transition=='fade') {
		window.setTimeout("slideshow.fade()",slideshow.startdelay);
		}
	}
},
fade: function() {
	var array=slideshow.imageArray;
	var div=$(slideshow.div);
	div.css("opacity",0);
	div.css("backgroundImage","url("+array[slideshow.counter].src+")");
	div.animate({opacity: 1},slideshow.transitiontime,slideshow.next);
},
reset: function(div) {
	var imagebx=document.getElementById("imageBox"); 
	if (slideshow.transition=='slide') {
	div.style.marginLeft=imagebx.clientWidth;
	}
	else if (slideshow.transition=='fade') {
	$(div).css("opacity",0);
	}
},
next: function() {
	var imagebx=document.getElementById("imageBox");
	var slideshw=document.getElementById("imageBoxSlideshow");
	imagebx.style.backgroundImage=slideshw.style.backgroundImage;
	imagebx.style.backgroundPosition="0 0";
	imagebx.style.backgroundRepeat="no-repeat";
	slideshow.reset(slideshw);
	slideshow.counter++;
	
	if (slideshow.counter>=slideshow.imageArray.length && slideshow.repeat==true) {
	slideshow.counter=0;
	}
	else if (slideshow.counter>=slideshow.imageArray.length && slideshow.repeat==false) { 
	return; 
	} 

	if (slideshow.transition=='slide') {
	window.setTimeout("slideshow.slide()",slideshow.startdelay);
	}
	else if (slideshow.transition=='fade') {
	window.setTimeout("slideshow.fade()",slideshow.startdelay);
	}
}
};

//---------------
// PORTFOLIO
//---------------
var configureGallery=function() {
	var slideshowPage = $("#content #ProductDirectory");

	if(slideshowPage.length == 1){
		
		var initialImg = $("#ProductDirectory a.product-thumb")[0];
		var initialImgURL = $("#ProductDirectory a.product-thumb")[0].href;
		
		var initialTitle = $("#ProductDirectory .imageTitle")[0];
		var inititalDetail = $("#ProductDirectory .imageDetail")[0];
		var inititalPrice = $("#ProductDirectory .imagePrice")[0];
		var moreURL = $("#ProductDirectory .imgMore")[0];
		
		//var initialLimited = $(inititalDetail).substr(0,100);
		//console.debug(initialLimited);
		
		$("#imgTitle").html($(initialTitle).text());
		writeCookie("referralProduct",$(initialTitle).text());
		$("#imgDetail").html($(inititalDetail).text());
		$("#imgPrice").html($(inititalPrice).text());
		$("#imgMore a").attr("href",$(moreURL).text());
		
		$("#ProductHero img").attr("src",initialImgURL);
		$(initialImg).addClass("currentThumb");
	
		$("#ProductDirectory a.product-thumb").click(function(){
			
			var imgURL = this.href;
			var origImgURL = $("#ProductHero img").attr("src");
			//console.debug($(".imageTitle",this).text())
			
			$("#imgTitle").html($(".imageTitle", this).text());
			writeCookie("referralProduct",$(".imageTitle", this).text());
			$("#imgDetail").html($(".imageDetail", this).text());
			$("#imgPrice").html($(".imagePrice", this).text());
			
			var moreURL = $(".imgMore",this);
			$("#imgMore a").attr("href",$(moreURL).text());
			
			$("#ProductDirectory a.product-thumb").removeClass("currentThumb");
			$(this).addClass("currentThumb");

			if(imgURL){
				$("#ProductHero").css("background","url("+origImgURL+") no-repeat scroll 0 0");
				loadImage(imgURL);
			};
			
			return false;
		});

	};	
};
var loadImage = function(imgURL) {

		var ProductHero = $("#ProductHero img");
		ProductHero.hide();
		$(ProductHero).load(function() {
			$(this.parentNode).addClass("loading");
			$(this).fadeIn("slow");
		});
		$(ProductHero).attr("src",imgURL);

};


//---------------
// ORDERING
//---------------

function storePageForReferring(){

	var url=window.location.href;
	writeCookie("referralURL",url);
	//writeCookie("referralProduct",name);
}

function loadRequestedPageInfo(){

	var url=readCookie("referralURL");
	if (url)
	{
	
	$("#cartContents").load(url+" #PrintCart",function(){
		var print = $("#cartContents").html();
		print = print.replace(/(<([^>]+)>)/gi,"");
		print = print.replace(/(&nbsp;)/gi," - ");
		$("#cartContents").html(print);
	});
	
	$("#commentsField").load(url+" #comments",function(){
		var output = $("#commentsField").html();
		output = output.replace(/(<([^>]+)>)/gi,"");
		$("#commentsField").html(output);
	});
	
	var FEUusername = jQuery("#FEUusername").text();
	$("#cntnt01moduleform_1 .cartDisplay").load(url+" #FullCart");
	
	$("#FEUusernameVal").val(FEUusername);
	$("#cntnt01moduleform_1 .cartContents").hide();
		
	}
}

function loadProductInfo(){

if($("#fbrp__37").length >= 1){
	var productName=readCookie("referralProduct");
	if (productName){
		$("#fbrp__37").val(productName);
	}
}
};

var readCookie=function(key)
{
    var pageKey,cookie,i,c;
    pageKey=key;
            if (!document.cookie)
            {
                   return false;
            }
   cookie= document.cookie.split(';');
    for (i=0;i<cookie.length;i++)
    {
        c = cookie[i];
       while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(pageKey) == 0){
            cookie= c.substring(pageKey.length+1,c.length);
            return cookie;
       }
    }
    return false;
};
 
var writeCookie=function(key,string)
{
    var pageKey,date,expires;
    pageKey=key;
    date = new Date();
    date.setTime(date.getTime()+(10*24*60*60*1000)); //10 days duration
    expires = "; expires="+date.toGMTString();
    document.cookie = pageKey+"="+string+expires+";path=/;";
};

$(document).ready(function(){
	configureGallery();
	loadRequestedPageInfo();
	loadProductInfo();
	slideshow.start();
});
