//  Force getElementById
if(!document.getElementById) {
	if(document.all) {
		document.getElementById = function() {
			if(typeof document.all[arguments[0]] != "undefined") {
				return document.all[arguments[0]];
			} else { return null; }
		}
	} else if(document.layers) {
		document.getElementById = function() {
			if(typeof document[arguments[0]] != "undefined") {
				return document[arguments[0]];
			} else { return null; }
		}
	}
}

//********************************************************************************************//
var imBusy = 0;
alphaKEY = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
divKEY = new Array("NULL", "P","P","P","P","P","P","O","O","O","O","C","C","C","C","W","W","W","W","W","W");
secKEY = new Array("NULL", "1","2","3","4","5","6","1","2","3","4","1","2","3","4","1","2","3","4","5","6");
var Num1 = 0;
var ex;
var imageID = "ID";
var autoFlip = false;
var cycleNUM = -1;
var onFrame = 0;
var bufferTO = -1;
var bufferFROM = -1;

var xPos = 0;
var yPos = 0;
var resizeTimer;
var scale = 100;
var step = 0;

var xDistance = 0;
var yDistance = 0;

var x1 = 0;
var y1 = 0;
var x2 = 0;
var y2 = 0;
var winPad = 50;

var slideTimer;
var step2 = 0;
var h = 0;
var x = 0;
var slidePos = 0;
var slideDis = 300;
var divHeight = 0;
var divCursor = 'auto';
var bufferID = 0;
var xBuffer = 0;
var disTemp = 0;
var bufferDis = 0;
var captionText = " ";
var imageBUFFER = 0;
var sectionBUFFER = 0;

MOVE_COORDS_PLUS = new Array(0, 1, 4, 7, 11, 17, 23, 30, 38, 47, 56, 66, 75, 84, 92, 99, 100);
MOVE_COORDS_MINUS = new Array(100, 99, 92, 84, 75, 66, 56, 47, 38, 30, 23, 17, 11, 7, 4, 1, 0);

var frameTotal = MOVE_COORDS_PLUS.length;

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// GET PAGE SIZES ARRAY

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// GET PAGE SCROLL

function getPageScroll(){

	var yScroll;
	var xScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	if (self.pageXOffset) {
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// LAUNCH POP UP

function setOpacity(value) {
	document.getElementById('IMAGECONT').style.display = (value == 0)? 'none': 'block';
	document.getElementById('SMOKE').style.display = (value == 0)? 'none': 'block';
	document.getElementById('SMOKE').style.opacity = value/10;
	document.getElementById('SMOKE').style.filter = 'alpha(opacity=' + value*10 + ')';
}

function getImage(tempOn, id) {
	//alert(tempOn+" / "+id);
	document.getElementById('IMAGECONT').innerHTML = "";
	setOpacity(7);
	new Ajax.Updater('IMAGECONT', 'detail.php?id='+id+'&on='+tempOn, {onComplete:function(){ showImage(); }});
}

function showImage() {
	var imageCont = document.getElementById('IMAGECONT');
	var imageSrc = document.getElementById('IMAGE');
	var arrayPageScroll		= getPageScroll();
	var arrayPageSize		= getPageSize();
	imageCont.style.left = arrayPageScroll[0] + (arrayPageSize[2]/2);// - (imageSrc.width/2);
	imageCont.style.top = arrayPageScroll[1] + (arrayPageSize[3]/2);// - (imageSrc.height/2);
	imageCont.width = imageSrc.width;
	//alert(arrayPageScroll[0]);
}

function hideImage() {
	setOpacity(0);
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

function setCaption(caption) {
	captionText = caption;
}

function zoomInit(pFROM, pTO){
	if (imBusy == 0){
		if (pFROM != pTO){
			zoomTo(pFROM, pTO);
		}
	} else {
		//store command to execute later
		bufferTO = pTO;
		bufferFROM = pFROM;
	}
	if (pFROM != pTO){
		imBusy = 1;
		//alert("1:"+imBusy);
	}
}

function zoomTo(pFROM, pTO) {

		bufferFROM = -1;
		bufferTO = -1;
//alert("From:"+pFROM+" and going to:"+pTO);
	if (step < frameTotal){
		x1 = document.getElementById('B' + pFROM).style.left;
		y1 = document.getElementById('B' + pFROM).style.top;
		x2 = document.getElementById('B' + pTO).style.left;
		y2 = document.getElementById('B' + pTO).style.top;
		
		x1 = x1.replace(/px/gi,"");
		y1 = y1.replace(/px/gi,"");
		x2 = x2.replace(/px/gi,"");
		y2 = y2.replace(/px/gi,"");
		
		x1 = x1.replace(/pt/gi,"");
		y1 = y1.replace(/pt/gi,"");
		x2 = x2.replace(/pt/gi,"");
		y2 = y2.replace(/pt/gi,"");
		
		x1 -= winPad;
		y1 -= winPad;
		x2 -= winPad;
		y2 -= winPad;

		//alert("From:"+x1+", "+y1+" and going to:"+x2+", "+y2);
		
		xDistance = x2 - x1;
		yDistance = y2 - y1;
		
		//alert("xDistance:"+xDistance+"         yDistance:"+yDistance);
		
		if (xDistance < 0){
			xPos = parseInt((MOVE_COORDS_MINUS[step]/100) * Math.abs(xDistance)) + parseInt(x2);
		} else {
			xPos = parseInt((MOVE_COORDS_PLUS[step]/100) * Math.abs(xDistance)) + parseInt(x1);
		}
		
		if (yDistance < 0){
			yPos = parseInt((MOVE_COORDS_MINUS[step]/100) * Math.abs(yDistance)) + parseInt(y2);
		} else {
			yPos = parseInt((MOVE_COORDS_PLUS[step]/100) * Math.abs(yDistance)) + parseInt(y1);
		}
		
		//alert("xPos:"+xPos+" + yPos:"+yPos);
	
		//window.scrollBy(xPos, yPos)
		//window.scrollTo(xPos, yPos)

		document.getElementById('BX').style.left = xPos + 49;
		document.getElementById('BX').style.top = yPos + 13;
		//alert(document.getElementById('BX').style.left);

		window.scroll(xPos, yPos)
		
		step++;
		zoomTimer = setTimeout("zoomTo("+pFROM+", "+pTO+")", 50);
	} else{
		zoomStop();
		onFrame = pTO;
		if (autoFlip == true){
			autoFliper(onFrame);
		}
	}
}

function zoomStop(){
	if (zoomTimer) clearTimeout(zoomTimer);
	step = 0;
	imBusy = 0;
	cycleNUM = -1;
	//alert("2:"+imBusy);
}

function stopFliper(){
	if(autoFlip == true){
		autoFlip = false;
	} else {
		autoFlip = true;
		autoFliper(onFrame);
	}
}
/*
function autoFliper(divID){
	if (autoFlip == true){
	ex = document.getElementById('B' + divKEY[divID] + '_' + secKEY[divID] + '_' + alphaKEY[(cycleNUM + 1)] );
	//alert("ex:" + ex + "    :    " + 'B' + divKEY[divID] + '_' + secKEY[divID] + '_' + alphaKEY[(cycleNUM + 1)]);
		if(ex){
			cycleNUM++;
		} else {
			cycleNUM = 0;
		}
		divCursor = document.getElementById('B' + divKEY[divID] + "_" + secKEY[divID] + "_" + alphaKEY[cycleNUM]).style.cursor;
		if (divCursor == "s-resize"){
			imageID = divKEY[divID] + "_" + secKEY[divID] + "_" + alphaKEY[cycleNUM];
			//alert(imageID);
			slideInit(imageID);
		}
	}
}
*/

function nextImage(sectionON, numberIMAGES){
//alert(sectionBUFFER + "/" + imageBUFFER);
	if (imBusy == 0){
		if (sectionON == sectionBUFFER){
			imageBUFFER--;
			if (imageBUFFER < 1){
				imageBUFFER = numberIMAGES;
			}
			
			slideInit(sectionBUFFER + "_" + imageBUFFER, "X", "X", "X");
		} else {
			imageBUFFER = numberIMAGES;
			sectionBUFFER = sectionON;
			slideInit(sectionBUFFER + "_" + numberIMAGES, "X", "X", "X");
		}
	}
}

function prevImage(sectionON, numberIMAGES){
//alert(sectionBUFFER + "/" + imageBUFFER);
	if (imBusy == 0){
		if (sectionON == sectionBUFFER){
			imageBUFFER++;
			if (imageBUFFER > numberIMAGES){
				imageBUFFER = 1;
			}
			
			slideInit(sectionBUFFER + "_" + imageBUFFER, "X", "X", "X");
		} else {
			imageBUFFER = 1;
			sectionBUFFER = sectionON;
			slideInit(sectionBUFFER + "_" + numberIMAGES, "X", "X", "X");
		}
	}
}

function slideInit(divID, folderID, numberID, pictureID){
	if (imBusy == 0){
		divCursor = document.getElementById(divID).style.cursor;
		if (divCursor == "s-resize" || folderID == "X"){
			if(bufferID != 0){
				//xBuffer = document.getElementById(bufferID).style.left;
				xBuffer = document.getElementById(bufferID).style.top;
				xBuffer = xBuffer.replace(/px/gi,"");
				xBuffer = xBuffer.replace(/pt/gi,"");
				slideClose(bufferID, divID);
				bufferID = divID;
			} else {
				bufferID = divID;
				//x = document.getElementById('B' + divID).style.left;
				x = document.getElementById(divID).style.top;
				x = x.replace(/px/gi,"");
				x = x.replace(/pt/gi,"");
				
				//for auto
				//sectionBUFFER = folderID + "_" + numberID;
				//imageBUFFER = pictureID;
				
				slideDiv(divID);
				
			}
		} else{
			//alert(divCursor);
			popImage(folderID + "/" + numberID + "/" + pictureID + "_L.jpg", pictureID + "_L.jpg", captionText);
			slideStop();
		}
	} else {
		imBusy = 1;
	}
}

function slideDiv(divID){
	imBusy = 1;
	
	if(step2 == 0){
		disTemp = document.getElementById(divID).style.top;
		disTemp = disTemp.replace(/px/gi,"");
		disTemp = disTemp.replace(/pt/gi,"");
		slideDis = 383 - disTemp;
		//alert("OPEN:"+disTemp);			
	}
	bufferDis = slideDis;
	
	divCursor = document.getElementById(divID).style.cursor;
	if (step2 < frameTotal){
		//document.getElementById('B' + divID).style.top;
		slidePos = parseInt((MOVE_COORDS_PLUS[step2]/100) * Math.abs(slideDis)) + parseInt(x);

		//document.getElementById('B' + divID).style.left = slidePos;
		document.getElementById(divID).style.top = slidePos;
		
		h = document.getElementById(divID).style.height;
		h = h.replace(/px/gi,"");
		h = h.replace(/pt/gi,"");
		divHeight = parseInt(h) + 1;
		document.getElementById(divID).style.height = divHeight;

		step2++;
		slideTimer = setTimeout("slideDiv('"+divID+"')", 50);
	} else{
		document.getElementById(divID).style.cursor = "pointer";
		slideStop();
		if (autoFlip == true){
			setTimeout("autoFliper(onFrame);",3000);
		}
	}
}

function slideClose(bufferID, divID){
	imBusy = 1;
	//slideDis = bufferDis;
	//alert("CLOSE");
	if (step2 < frameTotal){

		slidePos = parseInt(xBuffer) - parseInt((MOVE_COORDS_PLUS[step2]/100) * Math.abs(slideDis));
		//alert("array:"+(MOVE_COORDS_PLUS[step2]/100)+"slideDis:"+slideDis+" xBuffer:"+xBuffer);

		//document.getElementById('B' + bufferID).style.left = slidePos;
		document.getElementById(bufferID).style.top = slidePos;
		
		h = document.getElementById(bufferID).style.height;
		h = h.replace(/px/gi,"");
		h = h.replace(/pt/gi,"");
		divHeight = parseInt(h) - 1;
		document.getElementById(bufferID).style.height = divHeight;
		
		step2++;
		slideTimer = setTimeout("slideClose('"+bufferID+"', '"+divID+"')", 50);
	} else{
		document.getElementById(bufferID).style.cursor = "s-resize";
		if (slideTimer) clearTimeout(slideTimer);
		step2 = 0;
		//x = document.getElementById('B' + divID).style.left;
		x = document.getElementById(divID).style.top;
		x = x.replace(/px/gi,"");
		x = x.replace(/pt/gi,"");
		slideDiv(divID);
	}
}

function slideStop(){
	if (slideTimer) clearTimeout(slideTimer);
	step2 = 0;
	imBusy = 0;
	if (bufferTO > 0 && bufferFROM > 0){
		zoomTo(bufferFROM, bufferTO);
	}
	//alert("4:"+imBusy);
	
}






function zoomMisc(pFROM, pTO) {

		bufferFROM = -1;
		bufferTO = -1;
//alert("From:"+pFROM+" and going to:"+pTO);
	if (step < frameTotal){
		x1 = document.getElementById('B' + pFROM).style.left;
		y1 = document.getElementById('B' + pFROM).style.top;
		x2 = document.getElementById('B' + pTO).style.left;
		y2 = document.getElementById('B' + pTO).style.top;
		
		x1 = x1.replace(/px/gi,"");
		y1 = y1.replace(/px/gi,"");
		x2 = x2.replace(/px/gi,"");
		y2 = y2.replace(/px/gi,"");
		
		x1 = x1.replace(/pt/gi,"");
		y1 = y1.replace(/pt/gi,"");
		x2 = x2.replace(/pt/gi,"");
		y2 = y2.replace(/pt/gi,"");
		
		x1 -= winPad;
		y1 -= winPad;
		x2 -= winPad;
		y2 -= winPad;

		//alert("From:"+x1+", "+y1+" and going to:"+x2+", "+y2);
		
		xDistance = x2 - x1;
		yDistance = y2 - y1;
		
		//alert("xDistance:"+xDistance+"         yDistance:"+yDistance);
		
		if (xDistance < 0){
			xPos = parseInt((MOVE_COORDS_MINUS[step]/100) * Math.abs(xDistance)) + parseInt(x2);
		} else {
			xPos = parseInt((MOVE_COORDS_PLUS[step]/100) * Math.abs(xDistance)) + parseInt(x1);
		}
		
		if (yDistance < 0){
			yPos = parseInt((MOVE_COORDS_MINUS[step]/100) * Math.abs(yDistance)) + parseInt(y2);
		} else {
			yPos = parseInt((MOVE_COORDS_PLUS[step]/100) * Math.abs(yDistance)) + parseInt(y1);
		}
		
		//alert("xPos:"+xPos+" + yPos:"+yPos);
	
		//window.scrollBy(xPos, yPos)
		//window.scrollTo(xPos, yPos)

		//alert(document.getElementById('BX').style.left);

		window.scroll(xPos, yPos)
		
		step++;
		MiscTimer = setTimeout("zoomMisc("+pFROM+", "+pTO+")", 50);
	} else{
		MiscStop();
		onFrame = pTO;
	}
}
function MiscStop(){
	if (MiscTimer) clearTimeout(MiscTimer);
	step = 0;
	imBusy = 0;
	cycleNUM = -1;
	//alert("2:"+imBusy);
}


function overviewENLARGE(pictureSRC){
		popImage("OVERVIEW/" + pictureSRC, "Overview Image", captionText);
}

/***********************************************
* Drag and Drop Script: © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var dragobject={
	z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
	initialize:function(){
		document.onmousedown=this.drag
		document.onmouseup=function(){this.dragapproved=0
	}
},
	drag:function(e){
		var evtobj=window.event? window.event : e
		this.targetobj=window.event? event.srcElement : e.target
		if (this.targetobj.className=="drag"){
		this.dragapproved=1
		
		if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
		if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
		
		var tester = this.targetobj.style.left
		var tester1 = tester.substr(0,(tester.length - 2))
		//alert(tester1)
		var ax = tester1 * 1
		
		if(ax <= 600){
			
			this.offsetx=parseInt(this.targetobj.style.left)
			this.x=evtobj.clientX
		} else {
			//alert(ax)
		}
		
		var tester = this.targetobj.style.top
		var tester2 = tester.substr(0,(tester.length - 2))
		//alert(tester.substr(0,(tester.length - 2)))
		//alert(this.targetobj.style.left)
		var ay = tester2 * 1
		
		if(ay <= 600){
			this.offsety=parseInt(this.targetobj.style.top)
			this.y=evtobj.clientY
			
		} else {
			//alert(ay)
		}
		
		if (evtobj.preventDefault)
		evtobj.preventDefault()
		document.onmousemove=dragobject.moveit
		
	}
},
	moveit:function(e){
		var evtobj=window.event? window.event : e
		if (this.dragapproved==1){
			
			twin = this.targetobj.id;
			twin = twin.replace(/T/gi,"");

				this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
				document.getElementById(twin).style.left=((this.offsetx+evtobj.clientX-(this.x+150))*10+150)+"px"

				this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
				document.getElementById(twin).style.top=((this.offsety+evtobj.clientY-(this.y+50))*10+50)+"px"
			 
			return false
		}
	}
}

dragobject.initialize()

