var bigWidth; //width of the zoom image 
var smallWidth; // width of zoomed image
var bigHeight; // height of the zoom image
var bigX = 0; // zoom image x position
var bigY = 0; // zooom image y position
var smallHeight;
var bigBoxWidth;
var bigBoxHeight;
var factorY;
var factorX;
var easing = 0.18;
var zoomInterval = 0; 
var mouseX = 0;
var mosueY = 0;
var sHeight;
var elements;
var scrollBorder;
var thumbsPos = new Array();
var thumbsPosDown = new Array();
var imgAmount;
var i = 0;
var g = 92*5;
var idc = 0;
var delay = 0;
var rollOverInetrval;
var hideInterval;
var str =  new String(window.location);
var zoomImageShown = false;
var nr;
var issueNumber;
var pageNumber;
var images;
var scrollHeight; 


function initProductPage()
{
	sHeight = 92;
	document.body.onmousemove=setMouseCoords;
	//elements = countElements("thumbnails-mask","img");
	scrollHeight = sHeight * elements ; 
	//document.getElementById("thumbnails-loop").style.top = scrollHeight+'px';

	smallWidth = 230;
	smallHeight = 345;

	bigBoxWidth = document.getElementById('zoom-box').offsetWidth;
	bigBoxHeight = document.getElementById('zoom-box').offsetHeight;

	//document.getElementById('zoom-image').style.left = -450 + 'px';
	//document.getElementById('zoom-image').style.top = -800 + 'px';
	

	YAHOO.util.Event.addListener("up-arrow", "click", moveDown);
	YAHOO.util.Event.addListener("down-arrow", "click", moveUp);

	//zoom onload vars 
	var oUpArrow = document.getElementById("up-arrow");
	var oDownArrow = document.getElementById("down-arrow");

	//countsImages ('thumbnails-mask');
	document.getElementById('medium-image').onclick=showBig;
	document.getElementById('large-image').onmouseover=clearIntrv;

	document.getElementById('large-image').onmouseout=hideBig;
	//document.getElementById('large-image').onclick=showBig;
	bigWidth = document.getElementById('zoom-image').offsetWidth ;
	bigHeight = document.getElementById('zoom-image').offsetHeight ;
	
	
	
 

}







function countsImages (element_id)
{
	images = document.getElementById(element_id).getElementsByTagName('img');

	imgAmount = (images.length -1);
	idcc = (images.length -1);
	//var images2 = document.getElementById('thumbnails-loop').getElementsByTagName('img');
	scrollBorder = (sHeight * 3 ) - (sHeight*images.length);

	for(var d=0;d<images.length;d++)
	{
		
		
		
		thumbsPos.push(sHeight*(d-1));
		
		thumbsPosDown.unshift(sHeight*(d-1));
		
		document.getElementById(images[d].id).style.top = thumbsPos[d]+'px';
		YAHOO.util.Event.addListener(images[d].id, "click", moveUp);
		//YAHOO.util.Event.addListener(images2[d].id, "click", moveUp);
		
		
		
		
	}
}





function setMouseCoords(e)
{
	var ie = document.all?true:false;
	if(!e)
	{
		   e = window.event;
	}
	if(e.pageX || e.pageY)
	{
		   mouseX = e.pageX;
		   mouseY = e.pageY;
		
	
	
	}
	else if(e.clientX || e.clientY)
	{		
	   mouseX = YAHOO.util.Event.getPageX(e); 
	   mouseY = YAHOO.util.Event.getPageY(e); 	
	}
}


//show zoom image 
function showBig()
{
clearIntrv();	
//document.getElementById('sizing').style.visibility = 'hidden';
	
	document.getElementById('zoom-image').style.visibility = 'visible';
	document.getElementById('zoom-box').style.visibility = 'visible';
	document.getElementById('bastards').style.visibility = 'hidden';
	
	largeImageShown = true;

	zoomInterval = setInterval("animateBigImage()",20);
	
}



 
function prepViewLargeImageLink(product_id, image_type, channel)
{
    var fullsize_link = document.getElementById('full-size-image-link');
    fullsize_link.onclick = function()
    {
        launchPopUp('/' + channel + '/ViewFullSizeImage.ice?productID=' + product_id + '&imageType=' + image_type, 580,760);
    }
}

function loadBig(img,prodId,channel) {

        prepViewLargeImageLink(prodId, img, channel)
        document.getElementById("medium-image").src =   img + "";
		//document.getElementById("medium-image").src =  "/images/Michelle%20Zebra%20Bikini.JPG";
        loadZoomImage(img,prodId);
 
    }


function loadZoomImage(img,prodId){
	
		
		 
	    document.getElementById("zoom-image").src =  "/images/"+prodId;
		//document.getElementById("zoom-image").src = "/images/Michelle%20Zebra%20Bikini.JPG";
	
}




function clearIntrv(){
	clearTimeout(hideInterval);
		
}



function hideBig(){
	
	hideInterval = setTimeout("hideDelay()",250);
	
}






function hideDelay(){
	
	if(document.getElementById('zoom-image').style.visibility != 'hidden')
	{
		
		clearInterval (zoomInterval);	
		document.getElementById('zoom-image').style.visibility = 'hidden';
		document.getElementById('zoom-box').style.visibility = 'hidden';
		document.getElementById('bastards').style.visibility = 'visible';
			
	}

	
}







function hideBigBox()
{	
	document.getElementById('zoom-image').style.visibility = 'hidden';
	document.getElementById('bastards').style.visibility = 'visible';
}


//zoom function 
function animateBigImage()
{
	var xPos =  mouseX;
	var yPos =  mouseY;	
	var bigXPos = YAHOO.util.Dom.getX('zoom-image');
	var bigYPos = YAHOO.util.Dom.getY('zoom-image');

	var largeXOffset = YAHOO.util.Dom.getX('medium-image');
	var largeYOffset = YAHOO.util.Dom.getY('medium-image');

	//var bigXPos = document.getElementById('zoom-image').offsetLeft;
	//var bigYPos = document.getElementById('zoom-image').offsetTop;	

	factorX = (bigWidth- bigBoxWidth)/smallWidth;
	factorY = (bigHeight-bigBoxHeight)/smallHeight ;	

	var targetX = 0 - (factorX * (xPos-largeXOffset));
	var targetY = 0 - (factorY * (yPos-largeYOffset));		

	targetX = Math.round(targetX);
	targetY = Math.round(targetY);

	bigX +=  ((targetX-bigX)*easing); 
	bigY +=  ((targetY-bigY)*easing);	

	bigY = Math.round(bigY);
	bigX = Math.round(bigX);
	
	document.getElementById("zoom-image").style.left  = bigX + 'px';
	document.getElementById("zoom-image").style.top = bigY + 'px';
 	//var move = new YAHOO.util.Anim('bImg', {top: { to: bigY }, left:{to:bigX}}, 1 ,YAHOO.util.Easing.easeOut); 
}

//scroll fucntionality 

function countElements(container_id, tag)
{
	var container = document.getElementById(container_id);
	var elem_count = container.getElementsByTagName(tag).length;
	return elem_count;
}



 
 
function moveDown(e)
{		
	if(idc>0)
	{
		idc--;		
	}		
	else
	{
		idc=images.length-1;
	}	
	var tempPoss = thumbsPos.shift();
	thumbsPos.push(tempPoss);
		document.getElementById(images[idc].id).style.top = -92+'px';	
	
	for(var d=0;d<images.length;d++)
	{
	
			
				
		
		
			

		
	
				var moves = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeOutStrong); 		
		moves.animate();
				
				
				
		
	}
	 

 }
 
 


 
 
//move thumbnails up 
function moveUp(e) 
{ 				
	if(document.getElementById(images[idc].id).offsetTop<92) 
	{		
		document.getElementById(images[idc].id).style.top = 368+'px';
	}
	if(idc<imgAmount)
	{						
		idc++;
	}
	else 
	{
		idc=0;
	}
		
	var tempPos = thumbsPos.pop();
	thumbsPos.unshift(tempPos);
	
	for(var d=0;d<images.length;d++)
	{	
		var move = new YAHOO.util.Anim(images[d].id, {top: {to: thumbsPos[d]}} ,  0.6 ,YAHOO.util.Easing.easeOutStrong); 		
		move.animate();	
	}
	
}