/*Copyright © 2007 RedZee Search, All rights reserved.*/

// framebusters
// onbeforeunload="_onbeforeunload();"
var canUnload = true;
function _onbeforeunload(evt) {
	var msg = "Please press CANCEL to KEEP your results.";
	if (!canUnload) {
		if (evt) {
			evt.returnValue = msg;
		}
		else if (window.event) {
			window.event.returnValue = msg;
		}
	}
	canUnload = true; // just in case
}

function _onloadcomplete() {
	// alert("@debug::_onloadcomplete():SANITY");
	canUnload = true;
}
function _onreadystatechange() {
	// alert("@debug::_onreadystatechange():SANITY");

	// pvwimg = window.event.srcElement;
	if (window.event) {
		// alert("@debug::_onreadystatechange(): "+window.event.srcElement.readyState);
		if ("complete" == window.event.srcElement.readyState) {
			canUnload = true;
		}
	}

}

var TAB_HTML = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr height=\"20px\"><td width=\"15px\" style=\"background-image: url(/images/left.jpg);\"></td><td align=\"center\" valign=\"middle\" style=\"background-image: url(/images/middle.jpg);\"><a href=\"#\" onclick=\"vt(TDX);\" style=\"font-family:Tahoma;font-size:9pt;color:#333333;text-decoration:none;\">TDSC</a></td><td align=\"center\" valign=\"middle\" width=\"15px\" style=\"background-image: url(/images/x.jpg);\"><a href=\"#\" onclick=\"rt(TDX);\" style=\"font-family:Tahoma;font-size:9pt;color:#333333;text-decoration:none;\">&nbsp;&nbsp;</a></td></tr></table>";
var TAB_HEIGHT = 26;
var nxtdx=0; function getNextTdx() { return nxtdx++; }

function createTab(imglm) {
	var tdx = getNextTdx();

	var newTabHtml = TAB_HTML;
	newTabHtml = newTabHtml.replace(/TDX/g, tdx+"");
	newTabHtml = newTabHtml.replace(/TDSC/g, imglm.resDspurl);

	var tabTable = document.getElementById("res_tabs");
	var tabCell = tabTable.rows[0].insertCell(-1);
	tabCell.vAlign = "top";
	tabCell.innerHTML = newTabHtml;
	tabCell.tdx = tdx;

	var tabFrame = createFrame(imglm);
	tabFrame.id = "res_frame_"+tdx;
	tabCell.tabFrameId = tabFrame.id;

	return tdx;
}

function createFrame(imglm) {
	var frameWidth = getScrWidth();
	frameWidth += "px";
	var frameHeight = getScrHeight();
	frameHeight -= TAB_HEIGHT;
	frameHeight += "px";

	var tabFrame = document.createElement("IFRAME");
	tabFrame.style.position = "absolute";
	tabFrame.style.display = "none";
	tabFrame.style.width = frameWidth;
	tabFrame.style.height = frameHeight;
	tabFrame.style.top = TAB_HEIGHT+"px";
	tabFrame.style.left = "0px";
	tabFrame.style.zIndex = 1000;
	tabFrame.frameBorder = "0";
	tabFrame.src = imglm.resTargetUrl;

	// _onloadcomplete
	tabFrame.onload = _onloadcomplete;
	tabFrame.onreadystatechange = _onreadystatechange;

	document.body.appendChild(tabFrame);

	return tabFrame;
}

function getTabCell(tdx) {
	var tabTable = document.getElementById("res_tabs");
	for (var cdx=1,len=tabTable.rows[0].cells.length;len>cdx;cdx++) {
		var tabCell=tabTable.rows[0].cells[cdx];
		if (tdx==tabCell.tdx-0) {
			return tabCell;
		}
	}
	return null;
}

function view(imglm) {
	canUnload = false;
	document.getElementById("res_tabs").style.display = "inline";
	var tdx = createTab(imglm);
	vt(tdx);
}

function t0() {
	hideTabs();
	nav(0);
        _isOpenedTab = 0;
}

function vt(tdx) {
	var tabCell = getTabCell(tdx);
	if (null == tabCell) return;

	hideAll();
	hideTabs();

	// @todo
	// tabCell.style.backgroundColor = "#FFFFCC";
	var tabCellTable = tabCell.getElementsByTagName("TABLE")[0];
	tabCellTable.rows[0].cells[0].style.backgroundImage = "url(/images/left2.jpg)";
	tabCellTable.rows[0].cells[1].style.backgroundImage = "url(/images/middle2.jpg)";
	tabCellTable.rows[0].cells[2].style.backgroundImage = "url(/images/x2.jpg)";

	document.getElementById(tabCell.tabFrameId).style.display = "inline";
        _isOpenedTab = 1;
}

function hideTabs() {
	var tabTable = document.getElementById("res_tabs");
	for (var cdx=1,len=tabTable.rows[0].cells.length;len>cdx;cdx++) {
		var tabCell=tabTable.rows[0].cells[cdx];

		// @todo
		// tabCell.style.backgroundColor = "#FFFFFF";
		var tabCellTable = tabCell.getElementsByTagName("TABLE")[0];
		tabCellTable.rows[0].cells[0].style.backgroundImage = "url(/images/left.jpg)";
		tabCellTable.rows[0].cells[1].style.backgroundImage = "url(/images/middle.jpg)";
		tabCellTable.rows[0].cells[2].style.backgroundImage = "url(/images/x.jpg)";

		document.getElementById(tabCell.tabFrameId).style.display = "none";
	}
}

function rt(tdx) {
	var tabCell = getTabCell(tdx);
	if (null == tabCell) return;
	var tabTable = document.getElementById("res_tabs");
	var tabFrame = document.getElementById(tabCell.tabFrameId);

	document.body.removeChild(tabFrame);
	tabTable.rows[0].removeChild(tabCell);
	if (1 == tabTable.rows[0].cells.length) {
		document.getElementById("res_tabs").style.display = "none";
	}
	t0();
}
