// ZOOM Script
var steps = 0;

var zoomOrigW  = new Array(); var zoomOrigH  = new Array();
var zoomOrigX  = new Array(); var zoomOrigY  = new Array();
var myWidth = 0, myHeight = 0, myScroll = 0; myScrollWidth = 0; myScrollHeight = 0;
var minBorder      = 90;
var int;
var zoomCurrent = 0;
var zoomSteps      = 15;   // Number of zoom animation frames
var zoomTime = 5;
var zoomStartW = 0,zoomStartH = 0,zoomStartX = 0, zoomStartY = 0;
var zoomChangeW = 0, zoomChangeH = 0;
//var zoomDivName = "zoomContainer";
var zoomDivName = "TB_window";

function zoom(zoomDiv){
	
	getSize();
	startW = 50;
	startH = 12;
	startPos = findElementPos(zoomDiv);
	zoomStartW = startW;
	zoomStartH = startH;
	
	

	hostX = startPos[0];
	hostY = startPos[1];

	zoomStartX = hostX;
	zoomStartY = hostY;


	// Store original position in an array for future zoomOut.
	//zoomOrigW[theID] = startW;
	//zoomOrigH[theID] = startH;
	//zoomOrigX[theID] = hostX;
	//zoomOrigY[theID] = hostY;

	// Now set the starting dimensions

	//$j("#"+zoomDivName).style.width = startW + 'px';
	//$j("#"+zoomDivName).style.height = startH + 'px';
	//$j("#"+zoomDivName).style.left = hostX + 'px';
	//$j("#"+zoomDivName).style.top = hostY + 'px';
	
	$j("#"+zoomDivName).width(startW)
	$j("#"+zoomDivName).height(startH)
	$j("#"+zoomDivName).css({ position:"absolute", top:hostY, left:hostX }); 
	
	endW = $j("#TB_window").width();
	endH = $j("#TB_window").height();

	sizeRatio = endW / endH;
	if (endW > myWidth) {
		endW = myWidth;
		endH = endW / sizeRatio;
	}
	if (endH > myHeight) {
		endH = myHeight;
		endW = endH * sizeRatio;
	}

	zoomChangeX = ((myWidth / 2) - (endW / 2) - hostX);
	zoomChangeY = (((myHeight / 2) - (endH / 2) - hostY) + myScroll);
	zoomChangeW = (endW - startW);
	zoomChangeH = (endH - startH);


	$j("#"+zoomDivName).show();
	var zoomdiv =  $j("#"+zoomDivName);
	//var int=self.setInterval("clock()",50)	
	int = setInterval("zoomStep()",5);
	
	//int = setInterval("zoomElement('"+zoomdiv+"', "+zoomCurrent+", "+startW+", "+zoomChangeW+", "+startH+", "+zoomChangeH+", "+hostX+", "+zoomChangeX+", "+hostY+", "+zoomChangeY+", "+steps+", "+includeFade+", "+fadeAmount+", 'zoomDoneIn(zoomID)')", zoomTime);

}

function zoomStep(){
	//var t=new Date()
	if(steps >= 15){
		clearInterval(int);
		steps = 0 ;
		//$j("#"+zoomDivName).hide();		
		//$j("#TB_window").fadeIn();

	}else{
		//alert(steps);
		//$j("#disp").html(steps)
		//document.getElementById('disp').innerHTML = steps; 
		zoomCurrent = steps;
		moveW = cubicInOut(zoomCurrent, zoomStartW, zoomChangeW, zoomSteps);
		moveH = cubicInOut(zoomCurrent, zoomStartH, zoomChangeH, zoomSteps);
		moveX = cubicInOut(zoomCurrent, zoomStartX, zoomChangeX, zoomSteps);
		moveY = cubicInOut(zoomCurrent, zoomStartY, zoomChangeY, zoomSteps);
		//alert("moveY :"+moveY+" , zoomCurrent : "+zoomCurrent+",zoomStartY : "+zoomStartY+" , zoomChangeY : "+zoomChangeY+" , zoomSteps : "+zoomSteps)
	
		//document.getElementById(zoomdiv).style.left = moveX + 'px';
		//document.getElementById(zoomdiv).style.top = moveY + 'px';
		//zoomimg.style.width = moveW + 'px';
		//zoomimg.style.height = moveH + 'px';
		
		setOpacity(zoomCurrent * 7, "TB_window")
		
		$j("#"+zoomDivName).css({ position:"absolute", top:moveY, left:moveX}); 
		$j("#"+zoomDivName).width(moveW)
		$j("#"+zoomDivName).height(moveH)	
		
		
		//$j("#"+zoomDivName).width(steps)
		//$j("#"+zoomDivName).height(steps)
		//$j("#"+zoomDivName).css({ position:"absolute", top:steps, left:steps}); 
		steps = steps+1;

	}
}


function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}

function getSize() {

	// Window Size

	if (self.innerHeight) { // Everyone but IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myScroll = document.documentElement.scrollTop;
	} else if (document.body) { // Other IE, such as IE7
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		myScroll = document.body.scrollTop;
	}

	// Page size w/offscreen areas

	if (window.innerHeight && window.scrollMaxY) {	
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // All but Explorer Mac
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		myScrollHeight = document.body.offsetHeight;
	}
}

// Utility: Math functions for animation calucations - From http://www.robertpenner.com/easing/
//
// t = time, b = begin, c = change, d = duration
// time = current frame, begin is fixed, change is basically finish - begin, duration is fixed (frames),

function linear(t, b, c, d)
{
	return c*t/d + b;
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function cubicIn(t, b, c, d) {
	return c*(t/=d)*t*t + b;
}

function cubicOut(t, b, c, d) {
	return c*((t=t/d-1)*t*t + 1) + b;
}

function cubicInOut(t, b, c, d)
{
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}

function bounceOut(t, b, c, d)
{
	if ((t/=d) < (1/2.75)){
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)){
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)){
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}

function setOpacity(opacity, theID) {

	var object = document.getElementById(theID).style;

	// If it's 100, set it to 99 for Firefox.

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.9999; } // This is majorly awkward
	}

	// Multi-browser opacity setting

	object.filter = "alpha(opacity=" + opacity + ")"; // IE/Win
	object.opacity = (opacity / 100);                 // Safari 1.2, Firefox+Mozilla

}

