﻿function Photofader(nm, mainDiv, imgArr)
{
	this.name	= nm;
	this.imgArr = imgArr;
	this.curImg = 0;
	this.curDiv = 1;
	this.delayInterval = 1;
	var mainDv = document.getElementById(mainDiv);
	document.pfObj = this;
	this.delayInterval = this.imgArr[2];
	document.write("<style type='text/css'>\n");
	document.write("#pf_photo1 img { visibility:hidden; }\n");
	document.write("#pf_photo1 { position:absolute; z-index: 1; }\n");
	document.write("#pf_photo2 { position:absolute; z-index: 0; }\n");
	document.write("</style>");

	this.initImages = function() 
	{
		document.write("<scr");
		document.write("ipt type='text/javascript'>\n");
		for(var i=0; i<this.imgArr.length; i+=3)
		{
			document.write("var img"+i+" = new Image();\n");
			document.write("img"+i+".src = '"+ this.imgArr[i] +"';\n");
		}
		document.write("document.pfObj.start();\n");
		document.write("</scr");
		document.write("ipt>\n");
	}

	this.start = function()
	{		
		var hldr1 = "pf_photo1";
		var hldr2 = "pf_photo2";
		var dv1 = document.createElement("div");
			dv1.id = "pf_photo1";dv1.innerHTML = "<img src='"+ imgArr[0] +"' />";
		var dv2 = document.createElement("div");
			dv2.id = "pf_photo2";
	
		mainDv.appendChild(dv1);
		mainDv.appendChild(dv2);	

		image1 = document.getElementById(hldr1).childNodes[0];		
		setOpacity(image1, 0);
		
		image1.style.visibility = 'visible';
			
		fadeIn(hldr1,0);
			
		preloadBGImages(imgArr);
	}
	
	this.initImages();
}

function preloadBGImages(imgArr){
	for(var i=3; i<imgArr.length; i+=3){
		var nImag = new Image();
		createBGImageSelector(nImag.src);
	}
}

function imageLoadIsComplete(){
	load("sd");
}

var myTimer = "";	
	var imageLoadedTimer = "";			
	function setOpacity(obj, opacity) {
	
		opacity = (opacity == 100)?99.999:opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")";
		obj.style.KHTMLOpacity = opacity/100;
		obj.style.MozOpacity = opacity/100;
		obj.style.opacity = opacity/100;
	}
	
	function manualSetCurImg(ind){
		//alert("--->"+ind);
		clearInterval(myTimer);
		document.pfObj.curImg=(ind*3)-3;
		if(document.pfObj.curImg<0)document.pfObj.curImg=0;
		swapImages();
	}
	
	

	var initialBGShown=false;

	function swapImages(){
		if(document.pfObj.curImg >= document.pfObj.imgArr.length-3)
		document.pfObj.curImg = 0;
		else 
		document.pfObj.curImg=document.pfObj.curImg+3;
		
		
		//alert(document.pfObj.curImg);
		
		if(initialBGShown==false){
			//intMenuControl.closeMenu();
		}
		
		if((document.pfObj.curImg==0)&&(initialBGShown==true)){
			document.pfObj.curImg=document.pfObj.curImg+3;}
			var dvName				= (document.pfObj.curDiv == 1)?"pf_photo2":"pf_photo1";
			var eDivName			= (document.pfObj.curDiv == 1)?"pf_photo1":"pf_photo2";
			document.pfObj.curDiv	= (document.pfObj.curDiv == 1)?2:1;

		var tgtDiv = document.getElementById(dvName);
		var eDiv = document.getElementById(eDivName);
			tgtDiv.innerHTML = "<img src='"+ document.pfObj.imgArr[document.pfObj.curImg] +"' style='visibility:hidden;' />";
			
		var newDelay = document.pfObj.imgArr[(document.pfObj.curImg+2)];
		
			document.pfObj.delayInterval = newDelay;
		var newImageMap =  "#" + document.pfObj.imgArr[(document.pfObj.curImg+1)];
		
		eDiv.style.zIndex = 0;tgtDiv.style.zIndex = 1;
		var img = tgtDiv.childNodes[0];
		initialBGShown=true;
		setOpacity(img, 0);
		img.style.visibility = 'visible';
		
		fadeIn(tgtDiv.id,0);
		
		document.getElementById("mainImgOverlay").useMap=newImageMap; 
	}	
		
	var thumbIndicatorNotSet;
	function fadeIn(objId,opacity) {
		
		
		// Set the indicator on the thumb selector
		//alert(document.pfObj.curImg);
		if(document.pfObj.curImg>1){
			
		}
		else{
			//alert();
			
		}
		if (document.getElementById) {
			obj = document.getElementById(objId).childNodes[0];
			if (opacity < 100) {
			
				if(!thumbIndicatorNotSet){
				setThumbArrowIndicator(document.pfObj.curImg);
				thumbIndicatorNotSet=true;
				}
			
				speed = (speed < 2)?2:speed;
				setOpacity(obj, opacity);
				opacityDif = Math.ceil((100-opacity)/speed);
				opacity += opacityDif;
				myTimer = window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
			} 
			else {
				thumbIndicatorNotSet=false;
				myTimer = setTimeout("swapImages()",document.pfObj.delayInterval*1000);
			}
		}

	}
	
	
	var currentIndicatorIndex = 0;
	function setThumbArrowIndicator(ind){
		
		if(currentIndicatorIndex>1)
		document.getElementById('imgTBSelIndArw_'+currentIndicatorIndex).src="images/blkIndicator.gif";
		
		if(document.pfObj.curImg>1)
		document.getElementById('imgTBSelIndArw_'+document.pfObj.curImg).src="images/whtIndicator.gif";
			
		currentIndicatorIndex = document.pfObj.curImg;
	}
	
		
	var theImage = "";
	
	var thumbIndexCount = 0
	var imageArayIndexCount = 0
	function createBGImageSelector(whichImage){
		//alert();
		thumbIndexCount+=1;
		imageArayIndexCount+=3;
		//alert(thumbIndexCount);
		
		var content = "<div style='float:left;margin-right:12px;'><div style='width:54px;padding-bottom:6px;' align=center><img id='imgTBSelIndArw_"+imageArayIndexCount+"' src='images/blkIndicator.gif' height='0' alt='' border='0'></div><div style='width:54px; height:34px; border-style:solid; border-color:#ffffff; border-width:1px;'  id='tbsel1' onClick='thumbNailItemClick("+thumbIndexCount+")' onMouseOver='tSelect(this,"+thumbIndexCount+");' onMouseOut='tSelectOff(this);'><img src='"+BackgroundImages_thumb[thumbIndexCount-1]+"' alt='' height=34 width=54 border=0></div></div>";
		var newDiv = document.createElement('span');			
		
		newDiv.innerHTML =content;	
		
		if(document.getElementById('thumbSelectorContainer'))
		document.getElementById('thumbSelectorContainer').appendChild(newDiv);
		
	}
