var divPopupSingleID = "NOS_win";
var MyAccCookie = "MyAccCookie";


function getGlobalPosition(oDiv,global)
{
	var aPos = new Array(0,0);
    do {
		aPos[0] += oDiv.offsetLeft;f
		aPos[1] += oDiv.offsetTop;
    } while(oDiv = oDiv.offsetParent);
	if(global)
	{
		if(window.pageXOffset != undefined)
		{
			aPos[0] -= window.pageXOffset;
			aPos[1] -= window.pageYOffset;
		}
		else
		{
			aPos[0] -= (document.body.scrollLeft == 0 && document.documentElement.scrollLeft)?document.documentElement.scrollLeft:document.body.scrollLeft;
			aPos[1] -= (document.body.scrollTop == 0 && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;
		}
	}
	return aPos;
}

function getAlignment(oDiv,align,global)
{
	var aPos = getGlobalPosition(oDiv,global);
	if(align == null) align = "middle";

	if(align.indexOf('middle') >= 0)		aPos[0] += oDiv.offsetWidth/2;
	else if(align.indexOf('hcenter') >= 0)	aPos[0] += oDiv.offsetWidth/2;
	else if(align.indexOf('right') >= 0)	aPos[0] += oDiv.offsetWidth;

	if(align.indexOf('middle') >= 0)		aPos[1] -= oDiv.offsetHeight/2;
	else if(align.indexOf('vcenter') >= 0)	aPos[1] -= oDiv.offsetHeight/2;
	else if(align.indexOf('bottom') >= 0)	aPos[1] += oDiv.offsetHeight;
	
	return aPos;
}

function openLightPopup(url,w,h,embed,left,top) {
	if(embed == null) embed = true;
	
	myLightWindow.activateWindow({
		href: url,
		type:"external",
		iframeEmbed:embed,
		width:w,
		height:h,
		top:top,
		left:left,
		loadingAnimation:false,
		title:''
	});
	return false;
}
function openMyAccount(obj)
{
	eraseCookie(MyAccCookie);
	return openPopup(obj);
}
function openPopup(obj,url,oParam,id)
{
	var aPos = "top";
	var w = null;
	var h = null;
	var xPos = null;
	var yPos = null;
	var embed = true;
	
	var sUrl = (url != null)?url:obj.href;
	if(sUrl == null) return true;
	
	if(oParam == null && obj != null) oParam = _getParams(obj.getAttribute('params'));
	
	if(oParam != null && oParam.length > 0)
	{
		if(oParam.width != null) w = oParam.width;
		if(oParam.height != null) h = oParam.height;
		if(oParam.top != null) yPos = oParam.top;
		if(oParam.left != null) xPos = oParam.left;
		if(oParam.align != null && obj != null)
		{
			var aPos = getAlignment(obj,oParam.align);
			xPos = aPos[0];
			yPos = aPos[1];
			aPos = [xPos,yPos,oParam.objAlign];
		}
		if(oParam.embed != null) embed = oParam.embed;
	}
	if(id == null) id = (obj != null)?obj.id:(Math.ceil(Math.random()*9999));
	//sUrl += ((sUrl.indexOf("?") > 0)?"&":"?")+getTunnelIDParam()+"&"+getCurrentLocationParam();
	openDivPopup_external(id,(oParam != null)?oParam.style:null,sUrl,aPos);
	
	//resizePopup(getDivPopupID(id)+"_frame",w,h);
	return false;
}
function openPopupSimple(url)
{
	return openDivPopup_external("popup_"+(Math.ceil(Math.random()*9999)),null,url,"top");
}

function resizePopup(id,w,h)
{
	var obj = document.getElementById(id);
	obj.width = w;
	obj.height = h;
}

function getDivPopupID(id)
{
	return "divPopup_"+id;
}
function buildIframeInner(id,style,url)
{
	var name = getDivPopupID(id)+"_frame";
	url += ((url.indexOf("?") > 0)?"&":"?")+getCurrentLocationParam();
	var params = getParamsFromUrl(url);
	if(params["ss"] == null && siteSection != "" && siteSection != null)
	{
		url += ((url.indexOf("?") > 0)?"&":"?")+"ss="+siteSection;
	}
	return '<div class="genericInner_popup"><iframe src="'+url+'" name="'+name+'" id="'+name+'" scrolling="no" class="genericHide_frame" allowtransparency="allowtransparency" marginheight="0" marginwidth="0" frameborder="0"></iframe></div>';
}

function createDivPopup_iframe(id,style)
{
	var tagDiv = document.getElementById(getDivPopupID(id));
	if(tagDiv)
	{
		tagDiv.parentNode.removeChild(tagDiv);
		tagDiv = null;
	}
	
	var flDiv = document.getElementById("divPopup_General");
	if(!flDiv)
	{
		flDiv=document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
		flDiv.setAttribute("id","divPopup_General");
	}
	if(!tagDiv) tagDiv = flDiv.appendChild(document.createElement("div"));
	tagDiv.setAttribute("id",getDivPopupID(id));
	tagDiv = null;
	flDiv = null;
	return getDivPopupID(id);
}
function openDivPopup_externalFixed(id,style,url,position,stackable,modal)
{
	openDivPopup_external(id,style,url,position,stackable,modal);
}
function openDivPopup_external(id,style,url,position,stackable,modal,isFixed)
{
	if(style == null) style = "generic";
	$(window).scrollTop(0);
	var isFF = navigator.userAgent.toLowerCase().indexOf("firefox")!=-1;
	modal = (modal != false && modal != "false");
	stackable = (stackable != false && stackable != "false");
	//position = (!ie6 && position != null && position != "null")?position:'top';
	position = 'top';
	doClosePopup(getDivPopupID(id));

	var currentFlow = document.getElementsByTagName("body")[0].className;
	if(modal && !isFF) document.getElementsByTagName("body")[0].className = (isFF)?"body_noscroll_ff":"body_noscroll";

	var divName = createDivPopup_iframe(id,style);
//	$(function() {
		$("#"+divName).dialog({
			bgiframe: true,
			modal: modal,
			stack:stackable,
			closeOnEscape:false,
			autoResize:true,
			toolTip:true,
			zIndex:10000010,
			dialogClass:"generic_popup",
			position:position
		});
//	});
	var tagDiv = document.getElementById(divName);
	if(url != null) tagDiv.innerHTML = buildIframeInner(id,style,url);
	
	if(modal && !isFF) $("#"+divName).bind('dialogclose', function(event, ui) { document.getElementsByTagName("body")[0].className = currentFlow; });
	//$("#"+divName).bind('dialogclose', function(event, ui) { $("#"+this.id).remove(); $("#"+this.id).dialog('destroy'); });
	tagDiv = null;
	return divName;
}
function changePopup_style(obj,style,position,autoreload,url,tooltip)			/*invoke using "self" from the iframe*/
{
	var id = getIdFromFrame(obj);
	
	$(window).scrollTop(0);
	var fDiv = jQuery.find("#"+id);
	if(fDiv == null || fDiv == "") return;
	style = "generic";
	if(style != null)
	{
		var currentIframe = document.getElementById(obj);
		if(currentIframe != null)
		{
			currentIframe.className = style+"_frame";
			$("#"+id).dialog("option","dialogClass",style+"_popup");
		}
	}
	if(autoreload == true || autoreload == "true")
	{
		/** REFRESH MAIN PAGE **/
		if(url == null || url == 'null') url = homeBase;	/*NOT LOGGED*/
		$("#"+id).bind('dialogbeforeclose', function(event, ui) { changeLocation(url); showBlocker(); return true; });
	}
	if(tooltip == true || tooltip == "true")
	{
		$("#"+id).dialog("option","closeText","noClose");
	}
	if(position != "fixed")
	{
		if(position != null) $("#"+id).dialog("option","position",position);
		else $("#"+id).dialog("option","position",$("#"+id).dialog("option","position"));
	}
	fDiv = null;
}	
function reloadPopup_external(obj,url,style,position)	/*invoke using "self" from the iframe*/
{
	if(url != null)
	{
		var currentIframe = document.getElementById(obj);
		if(currentIframe != null) currentIframe.src = url;
		currentIframe = null;
	}
	if(style != null) changePopup_style(obj,style,position);
}
function redirect(url)
{
	var obj = self;
	callParentFunction("reloadPopup_external('"+obj.name+"','"+style+"','"+position+"','"+autoreload+"')");
}
function showBlocker()
{
	var divName = createDivPopup_iframe("genericBlocker","blocker_box");
	$(function() {
		$("#"+divName).dialog({
			bgiframe:true,
			modal:true,
			stack:false,
			toolTip:true,
			dialogClass:"blocker_box"
		});
	});
	document.getElementsByTagName("body")[0].style.overflow = "hidden";
}
function doClosePopup(id)
{
	var fDiv = jQuery.find("#"+id);
	if(fDiv != null && fDiv != "")
		if($("#"+id).dialog( 'isOpen' ))
		{
			$("#"+id).dialog('close');
		}
	fDiv = null;
}
function doHidePopup(id)
{
	var fDiv = jQuery.find("#"+id);
	if(fDiv != null && fDiv != "")
		if($("#"+id).dialog( 'isOpen' ))
			$("#"+id).dialog('disable');
			
	fDiv = null;
}
function closePopup(id)
{
	doClosePopup(getDivPopupID(id));
}
function getDialog(id)
{
	return $("#"+getDivPopupID(id));
}

$(document).ready(function(){
	var navName = navigator.appName;
	if(navName.toLowerCase().indexOf("explorer") < 0)
	{
		$("input[type=submit],input[type=button],a").focus(function(){$(this).addClass("focused_btn");})
		$("input[type=submit],input[type=button],a").blur(function(){$(this).removeClass("focused_btn");})
	}
});
