var curImg = 0;
var timerId = -1;
var intervalf = 3000;
var intervalm = 4500;
var intervals = 6000;
var interval = intervalm;
var imgIsLoaded = false;
var arrPreload = new Array();
var _PRELOADRANGE = 5;
var currentImage;

function replaceNum(myinput, token, newstr)
{
    var input = myinput;
    var output = input;
    var idx = output.indexOf(token);
    if (idx > -1) 
	{
		output = input.substring(0, idx);
		output += newstr;
		output += input.substr(idx+token.length);
    }
    return output;
}

function changeSpeed(sidx)
{
    switch (sidx) 
	{
		case 0: interval = intervalm; break;
		case 1: interval = intervals; break;
		default: interval = intervalf;
    }
    if (timerId != -1) 
	{
		window.clearInterval(timerId);
		timerId = window.setInterval("forward();", interval);
    }
}

function preloadRange(intPic,intRange) {
	for (var i=intPic;i<intPic+intRange && i<numImgs; i++) {
		arrPreload[i] = new Image();
  	    arrPreload[i].src = imageSrcArray[i];
	} 
	return true;
}

function imgLoadNotify()
{
    imgIsLoaded = true;
}

function changeSlide()
{
   if (document.all)
	{
		//document.all.imgs.style.filter="blendTrans(duration=1)";
    	//document.all.imgs.filters.blendTrans.Apply();
    }
    var copyrights="";
    var title="";
	var details="";
	var title="";
	var downloadLink="";
	document.getElementById("imgtd").style.backgroundImage='url('+imageSrcArray[curImg]+')';
    var img = "<img src=\""+thumbImageSrcArray[curImg]+"\" alt=\""+imageNameArray[curImg]+"\" border=0>";
	if(imageCopyrightArray[curImg].length>1)
	{
	  copyrights= "<i>Image Credit/Copyright: " + imageCopyrightArray[curImg] + "</i>";	 
	}
	title=imageNameArray[curImg];
	details=imageDescArray[curImg];
	if(parseInt(imageDpiArray[curImg])>=mindpi  && diArray[curImg] )
	{
	   if(isUserLoggedIn) 
	   {
		 if(di)
		 downloadLink= "<a href=\"downloadimg.asp?img=" + idsArray[curImg] + "\" style=\"font-size:11px;\">Download high resolution image</a> <span class=\"smallfont\">(" + imageSrcArray[curImg].substring(imageSrcArray[curImg].lastIndexOf(".")+1,imageSrcArray[curImg].length).toUpperCase() + ", "+ imageDimensionsArray[curImg] +", "+ imageDpiArray[curImg] + " DPI, " + imageSizesArray[curImg]+ ")</span> ";	
		 else 
		 downloadLink= "High resolution image available. To download for editorial use, <a href=\"editprofile.asp\"><span class=\"normalfont\">update your profile</span></a>.";	
	   }
	   else
	   {
		   downloadLink= "High resolution image available for download: Please <a href=\"login.asp\"><span class=\"normalfont\">login</span></a> or <a href=\"register.asp\"><span class=\"normalfont\">register</span></a> for access.";	
	   }
	}
	var pnumLine = "<font class=\"smallfont\">";
    pnumLine += replaceNum(SHOWINGSTRING, "%slideNum", eval(curImg+1));
    pnumLine += "</font>";
	document.getElementById("thumb").innerHTML=img;
	document.getElementById("pem").innerHTML=pnumLine;
	document.getElementById("imgtitle").innerHTML=title;
	if(details.length==0)
	document.getElementById("details").style.display='none';
	else
	document.getElementById("details").style.display='inline';
	
	document.getElementById("details").innerHTML=details;
    document.getElementById("downloadlink").innerHTML=downloadLink;
	document.getElementById("copyrights").innerHTML=copyrights;
    if (document.all) 
	{
		//document.all.imgs.filters.blendTrans.Play();
	}
    currentImage= document.createElement('img');

	//currentImage=new Image()
	currentImage.onload=function(){adjustImageFrame()};
	currentImage.src=imageSrcArray[curImg];

}
 function adjustImageFrame()
  {
     var height,width
	 height=maxHeight;
	 width=maxWidth;
	 //get main image height
	if(currentImage.height>0)
	 height=currentImage.height
	 //get main image width
	if(currentImage.width>0)
	width=currentImage.width
	//alert(currentImage.width+"x" + currentImage.height);
	document.getElementById("maintable").style.width = width + "px";
	document.getElementById("maintable").style.height = height + "px";
	document.getElementById("imgtable").style.height = (height-32) + "px";
	/*document.getElementById("tblimg").style.width=width+ "px";
	document.getElementById("tblimg").style.height=height+ "px";
	document.getElementById("img").style.width=width;+ "px"
	document.getElementById("imgframe").style.width=width;
    document.getElementById("imgframetop").style.width = (width-32) + "px";
	document.getElementById("imgframebottom").style.width =(width-32) + "px";
	document.getElementById("imgframecontents").style.width =width+ "px";
	document.getElementById("imgframecontents").style.height =(height-32)+ "px";*/
}
function start()
{
	if (!arrPreload[curImg+1])
	{
		imgIsLoaded = false;
		imgIsLoaded = (curImg+_PRELOADRANGE<numImgs)?preloadRange(curImg+1,_PRELOADRANGE):preloadRange(curImg+1,numImgs-curImg-1);
	}    
	if (imgIsLoaded) 
	{
		curImg=numImgs;
		changeSlide();
	}
}

function forward()
{
	if (!arrPreload[curImg+1])
	{
		imgIsLoaded = false;
		imgIsLoaded = (curImg+_PRELOADRANGE<numImgs)?preloadRange(curImg+1,_PRELOADRANGE):preloadRange(curImg+1,numImgs-curImg-1);
	}    
	if (imgIsLoaded) 
	{
		curImg++;
		if (curImg >= numImgs)
		{
				curImg=numImgs-1;
				finish();
		}
		else
		{
			changeSlide();
		}
	}
}
function move(no)
{
	curImg=no 
	changeSlide();
	finish();
	return false;
}

function rewind()
{
	curImg--;
	if (curImg < 0)
	{
		curImg=0;
		finish();
	}
	else
		changeSlide();
}

function stop()
{
    window.clearInterval(timerId);
    timerId = -1;
    imgIsLoaded = true;
}

function play()
{
    if (timerId == -1) 
		timerId = window.setInterval('forward();', interval);
}

function finish()
{
	stop();
}




