var closeSuccessPopup = 1500;
var tryCallParentFunction = 500;
var tryOpenPopup = 100;
var trySwfObject = 1000;

function getIdFromFrame(obj)
{
	return obj.substring(0, obj.length-6);
}
function proxy_receiver(call)
{
	eval(call);
}
function proxy_error_receiver(call)
{
	setTimeout(function() { callParentFunction(call); }, tryCallParentFunction);
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
function getSenderUrl() {
	var parentLoc = getParentLocation();
	var sUrl = (parentLoc.indexOf("https:") == 0)?homeSSL:homeBase;
	sUrl += "jsp/common/proxyTunnel.jsp";
	return sUrl;
}
function parentEnabled()
{
	return true;
}
function callParentFunction(call)
{
	callParentFunction_tunnel(call);
}
function callParentFunction_tunnel(call)
{
	try
	{
		var objMovie = thisMovie("swfProxy");
		if(objMovie != null)
		{
			if(objMovie.send != null && objMovie.send(call) == "success") return;
		}
	}
	catch(ev)
	{
	}
	
	setTimeout(function() { callParentFunction(call); }, tryCallParentFunction);
}

function getGlobalPosition(oDiv,global)
{
	var aPos = new Array(0,0);
    do {
		aPos[0] += oDiv.offsetLeft;
		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 openError()
{
	openDivPopup_external("popup_error","error_frame","signin/error.html","center");
}
function openToolTip(oDiv,msg,style,position,align,timer)
{
	var id = "divname_"+Math.floor(10000*Math.random());
	var aPos = getAlignment(oDiv,align);
	var xPos = aPos[0];
	var yPos = aPos[1];

	if(position == null) position = "middle";

	
	var timerId = -1;
	var msgBox = showMessage(msg,style,[xPos,yPos,position],false,true,id);	
	if(timer != null && timer.constructor == Number)
		timerId = setTimeout(function() { closeMessage(id) }, timer);

	msgBox.onmouseout = function() { closeMessage(id); clearTimeout(timerId); };
	
	return msgBox;
}
function updatePopup(style,position,autoreload,url,tooltip)
{
	var call = "changePopup_style('"+self.name+"'";
	call += (style != null)?(",'"+style+"'"):",null";
	call += (position != null)?(",'"+position+"'"):",null";
	call += (autoreload != null)?(",'"+autoreload+"'"):",null";
	call += (url != null)?(",'"+url+"'"):",null";
	call += (tooltip != null)?(",'"+tooltip+"'"):",null";
	call += ")";
	callParentFunction(call);
}

function onResizeIframe(size){
	var call = "onResizeParentIframe("+size+"";
	call += ")";
	callParentFunction(call);
}

function autoClosePopup(obj)
{
	var id = getIdFromFrame(obj);
	
	callParentFunction("doClosePopup('"+id+"')");
}
function autoRedirect(url)
{
	callParentFunction("changeLocation('"+url+"')");
}
function timerCloseFunction(timer)
{
	setTimeout(function() { autoClosePopup(self.name); }, timer);
}
function timerRedirectFunction(timer,url)
{
	setTimeout(function() { autoRedirect(url); }, timer);
}
function onRollOver(obj)
{
	if(obj.className.indexOf("_btn_over") < 0 && obj.className.indexOf("_btn") > 0)
	{
		var classSplit = obj.className.split("_btn");
		obj.className = classSplit.join("_btn_over");
	}
}
function onRollOut(obj)
{
	if(obj.className.indexOf("_btn_over") > 0)
	{
		var classSplit = obj.className.split("_btn_over");
		obj.className = classSplit.join("_btn");
	}
}

/** POINT HISTORY **/
function onNext()
{
	submitChange(1);
}
function onBack()
{
	submitChange(-1);
}
function onChange()
{
	submitChange(0);
}
function submitChange(delta)
{
	var objHid = document.getElementById("change");
	objHid.value = delta;
	document.forms[0].submit();
}

function openPopupBlog(id, style, url, position, stackable, modal){
	openDivPopup_externalFixed(id,style,url,position,stackable,modal);
}
function openPopup(id, style, url, position, stackable, modal){
	var call = "openDivPopup_externalFixed('"+id+"'";
	call += (style != null)?(",'"+style+"'"):",null";
	call += (url != null)?(",'"+url+"'"):",null";
	call += (position != null)?(",'"+position+"'"):",null";
	call += (stackable != null)?(",'"+stackable+"'"):",null";
	call += (modal != null)?(",'"+modal+"'"):",null";
	call += ")";
	callParentFunction(call);
	return true;
}
function autoOpenPopup(popup,id)
{
	if(id != null && id != 'null' && id != '') doClosePopup(id);
	eval(popup+"()");
}
function onOpenPopup(popup,bClose)
{
	var callFnc = "";
	if(bClose) callFnc = "autoOpenPopup('"+popup+"','"+getIdFromFrame(self.name)+"');";
	else callFnc = popup+"()";
	
	callParentFunction(callFnc);
	return false;
}


function eventPush(obj, event, handler) {
	if (obj.addEventListener) {
		obj.addEventListener(event, handler, false);
	} else if (obj.attachEvent) {
		obj.attachEvent('on'+event, handler);
	}
}

/* ERRORS */
function genericError(type)
{
	if(type != "session")
	{
		type = "error";
	}
	document.location.href = homeBase+"/home.do?deeplink="+type;
}

function callGenericError(type)
{
	callParentFunction("genericError('"+type+"')");
}
function disableButton(btn)
{
	document.getElementById(btn).disabled='disabled';
}

function initTemplate01(div,path,file,xmlUrl,height,width,wmode,static)
{
	setTimeout(function() { doInitTemplate01(div,path,file,xmlUrl,height,width,wmode,static); }, trySwfObject);
}
function doInitTemplate01(div,path,file,xmlUrl,height,width,wmode,static)
{
	var objDiv = document.getElementById(div)
	if(objDiv)
	{
		if(height == null) height = "528";
		if(width == null) width = "708";
		if(wmode == null) wmode = 'transparent';
		if(path.indexOf("http") < 0) path = homeBase+path;
		file = path+file;
		if(xmlUrl != null && xmlUrl != "" && xmlUrl.indexOf("http") < 0) xmlUrl = homeBase+xmlUrl;
		
		if(static != true) file += "?rnd="+(Math.random()*100000);
		
		var flashvars = '';
		flashvars='homeBase='+homeBase;
		if(xmlUrl != null && xmlUrl != "")
			flashvars += '&xmlUrl='+xmlUrl;

		var params = {
		  base: path,
		  bgcolor: "#000000",
		  allowScriptAccess: "always",
		  allowFullScreen: "true",
		  wmode: wmode,
		  flashvars:flashvars
		};
		var attributes = {
		  id: "swf_"+div,
		  name: "swf_"+div
		};
		
		swfobject.embedSWF(file, div, width, height, "9.0.0",homeBase+"swf/expressInstall.swf", null, params, attributes);
	}
	else
	{
		initTemplate01(div,path,file,xmlUrl,height,width,wmode,static);
	}
}

function onGoToBlog() {
	callParentFunction("goToAnchor('blog_div')");
}
function goToAnchor(name) {
	if(name == null) name = "";
	var currentHref = window.location.href;
	window.location.href = currentHref.substr(0, currentHref.lastIndexOf("#")) + "#"+name;
}
function scrollView(id)
{
	document.getElementById(id).scrollIntoView(false); return false;
}
function detectBrowser(type)
{
	var user = navigator.userAgent+"";
	return (user.toLowerCase().indexOf(type) >= 0);
}
function changeLocation(url)
{
	if(url == null) url = window.location.href+"";
	if(url.indexOf("#") > 0) url = url.substring(0,url.indexOf("#"));
	var ref = window.location.href+"";
	if(ref.indexOf("#") > 0) ref = ref.substring(0,ref.indexOf("#"));
	if(detectBrowser("safari") && url == ref && window.location.reload != null)
		window.location.reload();
	else if(window.location.assign != null)
		window.location.assign(url);
	else
		window.location.href = url;
}
function printIFrame(name)
{
	window.frames[name].focus();
	window.frames[name].print();
}
function checkRemember()
{
	var user = readCookie("rememberUser");
	if(user != null)
	{
		var obj = document.getElementById("email");
		obj.value = user;
		obj = document.getElementById("remember");
		obj.checked = "checked";
	}
}
function rememberUser()
{
	var obj = document.getElementById("remember");
	if(obj.checked == true || obj.checked == "true")
	{
		obj = document.getElementById("email");
		createCookie("rememberUser",obj.value);
	}
	else
	{
		eraseCookie("rememberUser");
	}
}
function initFocus()
{
	self.focus();
	document.forms[0].elements[0].focus();
}

/** Widgets Update **/
function callSimpleWS(sUrl)
{
	if($('#tunnel_frame').length <= 0)
	{
		$('body').append("<div id='tunnel_frame' style='visibility:hidden; height:0px; width:0px; display:block; position:absolute; top:0px; left:0px;'></div>");
	}
	var sFrameID = "tunnel_frame_"+Math.floor(Math.random()*999999);
	$('#tunnel_frame').append('<iframe id="'+sFrameID+'" width="0" height="0" src="'+sUrl+'" style="visibility:hidden;display:none;" ></iframe>'); 
}
function userLogin()
{
	//$('body').append("<div class='tunnel_loggedin'>Logged In</div>");
	callSimpleWS(homeSSL + "w/s/sp.do");
}
function userLogout()
{
	//$('body').append("<div class='tunnel_loggedout'>Logged Out</div>");
	callSimpleWS(homeSSL + "w/s/rp.do");
}

/** TUNNEL FUNCTIONS **/
function getProxyUrl(secure) {
	var sUrl = (secure)?homeSSL:homeBase;
	sUrl += "jsp/common/proxyTunnel.jsp";
	return sUrl;
}
function callFunction_tunnel(call,secure)
{
	var loc = window.location.href+"";
	var sSrv = (secure)?"https:":"http:";
	
	if(loc.indexOf(sSrv) == 0)
	{
		try
		{
			proxy_receiver(call);
		}
		catch(ev)
		{
		}
	}
	try
	{
		var sUrl = getProxyUrl(secure);
//		sUrl += "?rnd="+Math.floor(Math.random()*999999);
		sUrl += "?task="+encodeURIComponent(call);
		if($('#tunnel_frame').length <= 0)
		{
			$(document).append("<div id='tunnel_frame' style='visibility:hidden; height:0px; width:0px; display:block; position:absolute; top:0px; left:0px;'></div>");
		}
		var sFrameID = "tunnel_frame_"+Math.floor(Math.random()*999999);
		$('#tunnel_frame').append('<iframe id="'+sFrameID+'" width="0" height="0" src="'+sUrl+'" style="visibility:hidden;display:none;" ></iframe>'); 
		return;
	}
	catch(ev)
	{
	}
	
	setTimeout(function() { callParentFunction(call); }, tryCallParentFunction);
}
