var frameCnt  = 0;
var frameLoaded = 0;
var thumbCnt  = 6;
var tarray = new Array;
var iThumbArray = new Array;


function getDocHeight() {

  var winHeight = 0;

  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    winHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winHeight = document.body.clientHeight;
  }

  return winHeight;
}

function getDocWidth() {

  var winWidth = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winWidth = document.body.clientWidth;
  }

  return winWidth;
}

function ThumbItemNames() {

	for(i=0; i < thumbCnt; i++)
		thumbItem[i] = "Thumb" + i;

}



function Init() {

	ThumbItemNames();
	
	var tmpVal = getDocHeight();
	
	newH = Math.round((tmpVal / 2) - 100);
	
	tmpStr = newH + "px";
	
	document.getElementById("waitBox").style.top = tmpStr;

	
	var tmpVal = getDocWidth();
	
	newW = Math.round((tmpVal / 2) - 50);
	
	tmpStr = newW + "px";
	
	document.getElementById('waitBox').style.left = tmpStr;
	
	
	iThumbArray = new Array(totalImgs);
	iarray      = new Array(totalImgs);
	tarray      = new Array(totalImgs);
	ititles     = new Array(totalImgs);

	for(x = 0; x < totalImgs; x++) {
		iarray[x] = new Image();
		tarray[x] = new Image();
	}
	
	InitFirstPage();
	
	FirstPage();
	
	for(x = thumbCnt; x < totalImgs; x++)
		iThumbArray[x] = LoadThumb(x);
	
	InitRemainder(12);

}

function InitFirstPage(){

	if(totalImgs < thumbCnt)
		thumbCnt = totalImgs;

	for(y = 0; y < thumbCnt; y++){
		iThumbArray[y] = LoadThumb(y);
	}
	
	for(y = 0; y < thumbCnt; y++){
		LoadPic(y);
	}
}

function InitRemainder(maxNo) {

	
	if(maxNo > totalImgs)
		maxNo = totalImgs;
	
	for(x = thumbCnt; x < maxNo; x++)
		LoadPic(x);

	if(x == totalImgs)
		frameLoaded = 1;

}

function LoadPic(imgID) {

	var str = "";

	var imgTag = ImageArray[imgID].split("<")[2]

	var imagePath = new String(imgTag.split("'")[1]);
	
	iarray[imgID].src = imagePath;
	
	var imgTitle = new String(imgTag.split("'")[3]);
		
	var altTag = imgTitle.replace(/-+/g," "); 

	if(altTag != "undefined")
		ititles[imgID] = altTag;
	else
		ititles[imgID] = "";
}


function LoadThumb(imgID) {

	var str = "";

	var imgTag = ImageArray[imgID].split("<")[2]

	var imagePath = new String(imgTag.split("'")[1]);
	
	//Thumb nail path
	
	var tmpThumbDir = new String(imagePath);
	
	rExp = /Pics/gi;
	
	thumbDir = tmpThumbDir.replace(rExp, "Thumbs");
	
	tarray[imgID].src = thumbDir;
	
	var tmpStr = new String(imgTag.split("'")[3]);

	rExp = /_/gi;

	var iwidth  = new String(imgTag.split("'")[5])

	var iheight = new String(imgTag.split("'")[7])
	
	if(iheight > 58) {
	
        	var fscale = Math.min(102/iwidth, 58/iheight, 1);
		var iw = Math.round(fscale * iwidth);
		var ih = Math.round(fscale * iheight);

   		if(isNaN(iw))
   			iw = 58;

   		if(iw > 102)
   			iw = 58;

   		if(isNaN(ih))
    			ih = 58;

    		if(ih > 56)
   			ih = 52;
   			
   		var hdiff = 58 - ih;
		var newH = Math.round(hdiff / 2);

   	} else {
   		iw = iwidth;
   		ih = iheight;
   		
   		var hdiff = 58 - ih;
		var newH = Math.round(hdiff / 2);
   	}

   	
   	var attribs = "<a href=#  onClick='LoadImage(" + imgID + ")' >";
   	str = attribs + " <img src='" + thumbDir + "' width=\"" + iw + "px\" height=\"" + ih + "px\" border=\"0px\" vspace=\"" + newH + "px\"  ></a>";
   	
   	href = str;
   	   	
   	return(href);
   	
}


function RtnFirstImgHeight() {
	
	var iheight = 0;

	var imgTag = ImageArray[0].split("<")[2]

	iheight = new String(imgTag.split("'")[7]);
	
	return(iheight);
}


function RtnFirstImgWidth() {

	var iwidth = 0;

	var imgTag = ImageArray[0].split("<")[2]

	iwidth  = new String(imgTag.split("'")[5]);
	
	return(iwidth);
}


function FirstPage() {

	document.getElementById("waitBox").style.visibility = "visible";
		
	var foreground = document.getElementById('loadarea');
	
	foreground.style.visibility = "hidden";
	
	foreground.src = iarray[0].src;

  	foreground.vspace = 0;
  	   	
     	var iwidth =  RtnFirstImgWidth();
      	var iheight = RtnFirstImgHeight();
      	
      	var fscale = Math.min(997/iwidth, 500/iheight, 1);
      	
    	var iw = Math.round(fscale * iwidth);
 	var ih = Math.round(fscale * iheight);

   	if((foreground.width > 997) || (foreground.height > 500)) {
  		 
 		foreground.width = iw;
 		foreground.height = ih;	
 		
 		iheight = ih;
 	}
 	
      	var hdiff = 500 - iheight;
     	var newH = Math.round(hdiff / 2);
     	    	
   	foreground.vspace = newH;	

	for(i=0; i < 6; i++) {

		document.getElementById(thumbItem[i]).innerHTML = iThumbArray[i];
	}
	
	iloaded = iarray[0].complete;
	
  	if(iloaded == true) {
  		document.getElementById("waitBox").style.visibility = "hidden";
  		document.getElementById('loadarea').style.visibility = "visible";
  	} else {
  		HideWaitBox(0, 1);
  	}
  	
  	str = "<div id='ititle' >" + ititles[0] + "</div>";
	
	document.getElementById('ititle').innerHTML = str;
}

function FadeInImage(foregroundID,i,backgroundID)
{

	var foreground = document.getElementById(foregroundID);
	
	foreground.style.visibility = "hidden";
	
  	foreground.vspace = 0;
  	 	
   	foreground.width = iarray[i].width;
   	foreground.height = iarray[i].height;
   	
      	var iwidth = iarray[i].width;
      	var iheight = iarray[i].height;
      	var fscale = Math.min(997/iwidth, 500/iheight, 1);
      	
   	var iw = Math.round(fscale * iwidth);
 	var ih = Math.round(fscale * iheight);

   	if((iarray[i].width > 997) || (iarray[i].height > 500)) {
  		 
 		foreground.width = iw;
 		foreground.height = ih;
 		
 	}
 	
   
     	var iw = Math.round(fscale * iwidth);
 	var ih = Math.round(fscale * iheight);
 		
  	var hdiff = 500 - ih;
  	var newH = Math.round(hdiff / 2);
  	foreground.vspace = newH;
 		
  	foreground.src = iarray[i].src;
  	
  	iloaded = iarray[i].complete;
  	
  	if(iloaded == true) {
  		document.getElementById("waitBox").style.visibility = "hidden";
  		document.getElementById('loadarea').style.visibility = "visible";
  	} else {
  		HideWaitBox(i, 1);
  	}
  	
  	str = "<div id='ititle' >" + ititles[i] + "</div>";
	
	document.getElementById('ititle').innerHTML = str;
}

function HideWaitBox(i, iCount) {
	
	iloaded = iarray[i].complete;
	
	if(iCount == 10)
		iloaded = true;

	if(iloaded == true) {
		document.getElementById("waitBox").style.visibility = "hidden";
		document.getElementById('loadarea').style.visibility = "visible";
	}
	else {
		iCount++;
		self.setTimeout("HideWaitBox("+ i +"," + iCount + ")", 500);
	}

}

function LoadImage(i) {

	document.getElementById("waitBox").style.visibility = "visible";
		
	FadeInImage('loadarea',i,'MainPic');
}


function ThumbsLeft() {

	if(frameCnt > 0) {
	
		frameCnt--;

		iCount = frameCnt * thumbCnt;

		for(i=0; i < thumbCnt; i++) {
			document.getElementById(thumbItem[i]).innerHTML = iThumbArray[iCount];
			iCount++;
		}
	}
}


function ThumbsRight() {

	if(frameCnt < totframes) {
	
		frameCnt++;
		
		var iCount = frameCnt * thumbCnt;

		if(frameCnt == totframes) {	
		
			y = totalImgs - iCount;

			for(i= 0; i < y; i++) {
				// document.getElementById(thumbItem[i]).setAttribute(thumbItem[i], iThumbArray[iCount])
				document.getElementById(thumbItem[i]).innerHTML = iThumbArray[iCount];
				iCount++;
			}

			for(i= y; i < thumbCnt; i++) {
				document.getElementById(thumbItem[i]).innerHTML = "";
			}

		} else {
			for(i= 0; i < thumbCnt; i++) {
				document.getElementById(thumbItem[i]).innerHTML = iThumbArray[iCount];
				iCount++;
			}
		}
		
		if(frameLoaded == 0) {
		
			imgBuffer = (frameCnt * thumbCnt) + thumbCnt;
		
			if(imgBuffer > totalImgs)
				imgBuffer = totalImgs;
		
			InitRemainder(imgBuffer);
		}
	}
}


var message="Function Disabled!";

function StopRightClick(){
	if (event.button==2){
		alert(message);
		return false;
	}
}


if (document.all&&!document.getElementById){
	document.onmousedown=StopRightClick;
}


document.oncontextmenu=new Function("alert(message);return false")






