/*global YAHOO */
// @todo remove these "global" functions

/* This first inline block is to replace browser hawk with client-side browser detection.*/
var BROWSER_ISMAC = (navigator.appVersion.indexOf("Windows") === -1);
var BROWSER_ISPC = (navigator.appVersion.indexOf("Windows") !== -1);
var PLATFORMNAME = "";
var BROWSER_NAME = navigator.appName.toLowerCase();
var IS_IE6 = (navigator.appVersion.indexOf("MSIE 6") !== -1);
var IS_IE7 = (navigator.appVersion.indexOf("MSIE 7") !== -1);
var gIVI_section = "";
var isIE = navigator.appName === "Microsoft Internet Explorer";

if(BROWSER_NAME === "microsoft internet explorer"){
	BROWSER_NAME = "explorer";
}

if(BROWSER_ISPC){
	PLATFORMNAME = "pc";
}else if(BROWSER_ISMAC){
	PLATFORMNAME = "mac";
}else{
	PLATFORMNAME = "unix";
}



function findPos(obj) {
	var curleft = 0; 
	var curtop = 0;
	while( obj !== null)
	{
		curleft += obj.offsetLeft ;
		curtop += obj.offsetTop ;
		obj = obj.offsetParent ;
	}
	
	return [curleft,curtop];
}

function findPos_n(oObj, bUseParent){
	if(typeof(oObj) === "string") {
	oObj = document.getElementById(oObj);
	}
    var b = document.getElementsByTagName("body")[0];
    var sl = document.documentElement.scrollLeft;
    var rect;
    var r;
    var st = document.documentElement.scrollTop;
    if(document.getBoxObjectFor){
        r = document.getBoxObjectFor(oObj);
        rect = {l:r.x,t:r.y,r:r.width+r.x,b:r.y+r.height};
    } else{
        if(oObj.getBoundingClientRect){
	        r = oObj.getBoundingClientRect();
	        rect = {l:r.left-2+sl,t:r.top-2+st,r:r.right-2+sl,b:r.bottom-2+st};
        } else {
	        var l = oObj.offsetLeft;
	        var t = oObj.offsetTop;
	        rect = {l:l,t:t,r:l+oObj.offsetWidth,b:t+oObj.offsetHeight};
	        var p = oObj.offsetParent;
	        if(bUseParent){ p = oObj.parentNode;}
	        while(p){
		        l = p.offsetLeft;
		        t = p.offsetTop;
		        if(l){
		        rect.l += l;
		        rect.r += l;
		        }
		        if(t){
		        rect.t += t;
		        rect.b += t;
		        }
		        if(bUseParent){
		        p = p.parentNode;
		        } else {
		        p = p.offsetParent;
		        } 
	        }
        }
    }
    return rect;
}



function getPositionLeft(This){
	var el = This;
	var pL = 0;
	while(el){pL+=el.offsetLeft;el=el.offsetParent;}
	return pL;
}
function getPositionTop(This){
	var el = This;var pT = 0;
	while(el){pT+=el.offsetTop;el=el.offsetParent;}
	return pT;
}

function OpenBox(oObj, sAdvantage, sBrand, dArrowPos){
	if(!sBrand) {
		sBrand = "Ford";
	}
	
	if(!dArrowPos) {
		dArrowPos = 60 / 182;
	}
	var $Region = YAHOO.util.Region;
	var popDivContainer = document.getElementById("popDivContainer");
	//var arrowPos = 60 / 182;  // where the arrow on the box is located (60 px from top)
	if (document.getElementById("popupDiv")){
		popDivContainer.removeChild(document.getElementById("popupDiv"));
	}

	var s2div = document.createElement('div');
	s2div.setAttribute('id', 'popupDiv');
	s2div.style.position='absolute';
	s2div.style.display = 'block';
	s2div.innerHTML = "<table cellspacing=0 cellpadding=0 border=0><tr id='popupDivTitle'><td>" + sBrand + " Advantage </td><td align=right id='popupDivClose'>Close</td><tr><tr><td colspan=2><div id='popupDivContent' class='font'><br>" + sAdvantage + "</div></td><tr></table>";

	document.getElementById("popDivContainer").appendChild(s2div);
	
	YAHOO.util.Event.addListener(document.getElementById("popupDivClose"),"click", function() {
		document.getElementById("popDivContainer").removeChild(document.getElementById("popupDiv"));
	});

	var imgRect = $Region.getRegion(oObj);
	var boxRect = $Region.getRegion(s2div);
	var popRect = $Region.getRegion(popDivContainer);

	// get the width and height from the browser, it may have changed
	s2div.style.left = (imgRect.right - popRect.left) + "px";
	s2div.style.top = (imgRect.top - popRect.top) - ((boxRect.height  * dArrowPos) - (imgRect.height / 2)) + "px"; // may need to add padding

}
