//global variable to only show the bincular iframe 
//when the user's cursor is over the binocular icon.
var BinoInprocess = false;
//global variable to the iframe that contains the bino IMG tag
var binoIfDoc = null;	 //onload of iframe sets this variable.

//initializes iframe variables
function initBinoIf(ifDoc) {	
	binoIfDoc = ifDoc;	//only happens once.
}

//preps the iframe by setting the x and y coordinate, 
//sets the width and height of the iframe and
//loads the binocular image into the iframe
function loadBinoImg(BinocParam, ItemOrd) {
	if( binoIfDoc == null ) return;	//has not been initialized

	var browserHeight=window.innerHeight;
	if(browserHeight==null){
		browserHeight=document.body.offsetHeight
	}
	var PreviewHeight=260;
	var PreviewWidth=346;
	if(browserHeight<=600){
		PreviewHeight=180;
		PreviewWidth=240;
	}
	
	var Iframe = document.getElementById('binocCover');	
	var eResultTitle = document.getElementById("resultTop"+ItemOrd);
	if( oBrowser.isMacIE() == true )
	{
		Iframe.style.top = getTopPosition(eResultTitle, PreviewHeight, ItemOrd) + 100;
	}
	else
	Iframe.style.top = getTopPosition(eResultTitle, PreviewHeight, ItemOrd);
	Iframe.style.left = oCoor.getLeft(eResultTitle.getElementsByTagName("A")[0]);

	setImg(	BinocParam, Iframe, PreviewWidth, PreviewHeight );
}

//gets the y coordinate of the binocular iframe
function getTopPosition(eResultTitle, PreviewHeight, ItemOrd) {
	var bAdjust = ( eResultTitle.tagName == "TD" );
	
	var CurrentTop=document.body.scrollTop;	
	var top =  oCoor.getTop(eResultTitle) - PreviewHeight;
	if(bAdjust == true) top = top + 15;
	
	if(top<CurrentTop){
		var resultBottom = document.getElementById("resultBottom"+ItemOrd)
		top = oCoor.getTop(resultBottom) + resultBottom.offsetHeight;
		if(bAdjust == true) top = top - 15;
	}
	
	return top;
}		
			
//Sets the binocular image in the binocular iframe
function setImg(BinocParam, Iframe, PreviewWidth, PreviewHeight) {
	BinoInprocess = true;
	var BinocURL='http://binoculars.wc.ask.com/binocl_get?';
	
	var ifDoc = getIfDoc();
	ifDoc.body.innerHTML = "<img src=\""+BinocURL+BinocParam+"\" onload=\"parent.showBino();\" width=\""+PreviewWidth+"\" height=\""+PreviewHeight+"\"/>";
}

function getIfDoc() {
	if( document.all ) {
		return document.frames['binocCover'].document;
	} else {
		return document.getElementById('binocCover').contentDocument;
	}
}

//gets the IMG tag object in the iframe
function getBinoImgObj() {
	return binoIfDoc.getElementsByTagName("IMG")[0];
}

//show the binocular iframe
function showBino() {
	if( BinoInprocess == true ) {		
		var Iframe = document.getElementById('binocCover');
		var oImg = getBinoImgObj();
		
		//bug on mozilla, not refreshing properly, so set image size after image is loaded
		Iframe.style.width = oImg.width;
		Iframe.style.height = oImg.height;
	}
}

//hide the binocular iframe
function hideBino() {
	BinoInprocess = false;
	
	var ifDoc = getIfDoc();
	ifDoc.body.innerHTML = "";
	
	var Iframe = document.getElementById('binocCover');
	Iframe.style.width = 1;
	Iframe.style.height = 1;
}