var flckrPage;
var twitterPage;
var rssPage;
var homeBase;
var homeSSL;
//set ie6 png fix image path
//clear=homeBase+"images/clear.gif"; //path to clear.gif
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 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 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;
}


/* ERRORS */
function genericError(type)
{
	if(type != "session")
	{
		type = "error";
	}
	document.location.href = homeBase+"/home?deeplink="+type;
}

function callGenericError(type)
{
	callParentFunction("genericError('"+type+"')");
}

/** MY FT **/		
function onResize()
{
	var w = document.documentElement.clientWidth;
	var h = document.documentElement.clientHeight;
	var obj = document.getElementById("header");
	if(w < wMin)
	{
		window.scrollTo((wMin-w)/2.0,(document.all)?document.body.scrollTop:window.pageYOffset);
		if(!is_ie) document.body.style.backgroundPosition = "-165px 0px";
		if(is_ie6) obj.style.width = wMin+"px";
	}
	else
	{
		document.body.style.backgroundPosition = "top";
		if(is_ie6) obj.style.width = "100%";
	}
	return false;
}

function showImgPopup(obj,url)
{
	$.imgbox( $(obj), {
		'speedIn'		: 0,
		'speedOut'		: 0,
		'alignment'		: 'center',
		'overlayShow'	: true,
		'allowMultiple'	: false,
		'url'			: url
	} );
	return false;
}

function parseHtml(url) {
	var nHtml = 0;
	var nAux = 0;
	var sText = url;
	while(nHtml < sText.length-1 && (nHtml = sText.indexOf("http://",nHtml)) >= 0)
	{
		nAux = sText.indexOf(" ",nHtml);
		if(nAux < 0) nAux = sText.length-1;
		var s1 = sText.slice(0, nHtml);
		var s2 = sText.slice(nHtml, nAux);
		var s3 = (nAux < sText.length-1)?sText.slice(nAux):"";
		s2 = "<a href='"+s2+"' target='_blank'>"+s2+"</a>";
		nAux = s1.length + s2.length;
		sText = s1+s2+s3;
		nHtml = nAux;
	}
	return sText;
}


/** SIMPLE IFRAME HEADER **/
function getSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {w:myWidth,h:myHeight};
}
function onResizeWin() {
	var oSize = getSize();
	
	var h = $('#header_simple').attr('offsetHeight');
	if(navigator.appName.indexOf("Microsoft")!=-1)
	{
		var w = $('#header_simple').attr('offsetWidth');
		document.getElementById('externalFrame').style.width = ((oSize.w < w)?w:oSize.w)+"px";
	}
	document.getElementById('externalFrame').style.height = (oSize.h-h)+"px";
	return true;
}
function setSimpleWindow()
{
	onResizeWin();
	if(navigator.appName.indexOf("Microsoft")!=-1)
	{
		window.attachEvent("onresize", onResizeWin);
		$('#externalFrame').bind('load',onResizeWin);
	}
	else
	{
		$(window).bind('resize',onResizeWin);
		$('#externalFrame').attr('scrolling','no');
		$('#externalFrame').attr('verticalscrolling','no');
	}
}
function removeHeader() {
	changeLocation($('#externalFrame').attr('src'));
	return false;
}
/** MY COKE WRAPPER **/
function onMyCokeResize(){
	preventInject();
	var padding = parseInt($("body").css('paddingTop'));
	if(padding != $("#mycoke_header").height())
		$("body").css('paddingTop',$("#mycoke_header").height());
}
function onMyCokeMenu(id){
		/**
		 * This javascript fragment allows to execute valid js code
		 * when it is included in anchor "href" property.
		 * For javascript it must start with "javascript:" directive 
		 */
		jQuery('document').ready(function(){
			jQuery(id).each(function(){
				toURL = jQuery(this).attr('href');
				if(toURL.indexOf('javascript:') == 0){
					var sScript = toURL.substring(11);
					//jQuery(this).attr('onclick',toURL);
					jQuery(this).click(function () { 
					  eval(sScript);
					});
					jQuery(this).attr('href','javascript:void(0);');
				}
			});
		});
}

//REFERAL
function getHost(fullURL)
{
	if(fullURL == null) return null;
	var re = new RegExp('^[(?:f|ht)tp(?:s)?\://]*[www.]*([^./]*)', 'im');
	var matched = fullURL.match(re);
	if(matched != null)
		return matched[1].toString();
	else
		return fullURL;
}

function getReferal(tag)
{
	var referer = document.referrer;
	if(tag != null)
	{
		var myTag = getFromParam(tag);
		if(myTag!=null) referer =  myTag;
	}
	referer = getHost(referer);
	return referer;
}
function checkProperReferal(refTarget)
{
	var referal = getReferal();
	if(referal == null || referal=='') referal = getHost(readCookie("mycokeRefer"));
	if(refTarget == null) refTarget = (homeBase != null)?homeBase:(window.location.href+"");
	return referal!=null && referal!='' && refTarget.indexOf(referal) >= 0;
}

//CCMETRO
function getActiveX(name)
{
	var bExists = false;
	try
	{
		var XLApp = new ActiveXObject( name );
		bExists = ( XLApp )?true:false;
		//document.write(name+": "+bExists+" ["+XLApp+"]<br/>");
	}
	catch(ev)
	{
		//document.write(name+": Error: ["+ev.message+"]<br/>");
	}
	return bExists;
}
function isThereClientInstalled()
{
    try {
		return (getActiveX("There.Launcher") || getActiveX("There.VoiceTrainer"));
    } catch( error ) {
    }
	return false;
}

function browserIsSupported()
{
  var agent = navigator.userAgent.toLowerCase();
  var major = parseInt(navigator.appVersion);
  var minor = parseFloat(navigator.appVersion);

  var isOpera = agent.indexOf("opera") != -1;
  var isIe = !isOpera && ( agent.indexOf("msie") != -1 );
  var isIe3 = isIe && ( major < 4 ) && ( agent.indexOf("msie 6") == -1 );
  var isIe4 = isIe && ( major == 4 ) && ( agent.indexOf("msie 4") != -1 );
  var isIe5Up = isIe && !isIe3 && !isIe4;

  return isIe5Up;
}

var bCheckCCMetroState = false;
var CCMetroState = null;
var idCCMetroObj = null;
function readyCCMetroState(state)
{
	CCMetroState = state;
	if(bCheckCCMetroState)
	{
		setCCMetroBtns();
	}
}
function setCCMetroBtns(id) {
	if(id != null) idCCMetroObj = id;
	bCheckCCMetroState = (CCMetroState == null);
	setCCMetroBtnsCall(idCCMetroObj,CCMetroState);
}
function setCCMetroBtnsCall(id, status) {
	var obj = document.getElementById(id);
	if(browserIsSupported() && isThereClientInstalled())
	{
		if(status == "2" || status == "3")
		{
			//OPEN THERE'S
			obj.href = homeSSL + "startCCMetroRegistration.do?from=internal";
			obj.onclick=null;
			obj.target = "_blank";
			obj.className = "btn_go_cc";
			//BLANK
		}
		else
		{
			//CCMETRO DOWN
			obj.className = "btn_sorry_cc";
			obj.onclick=returnFalse;
			obj.href = "";
		}
	}
	else
	{
		bCheckCCMetroState = false;
		//GO TO DOWNLOAD
		obj.className = "btn_download_cc";
		obj.href = homeSSL+"jsp/accountmgt/ccmetro/ccMetroDownload.jsp?simple=true";
	}
}

/* TRACKING */
function myCokeTracking(tag)
{
	if(tag == null || tag == '' || tag == 'null') return;
	if(typeof doMyCokeTracking != "undefined")
	{
		$(window).ready(function(){
			doMyCokeTracking(tag);
		});
	}
}
function onLoadVideo(path,w,h)
{
	var id = "swfPlayer";
	var objMovie = thisMovie(id);
	if(objMovie != null)
	{
		if(objMovie.loadVideo != null)
			objMovie.loadVideo(path,w,h);
	}
	return false;
}
function onVideoComplete()
{
	//CALLBACK
}

//TEST COOKIES
function testCookies()
{
	$(document).ready( function() {
		createCookie("test", "ok");
		
		if(readCookie("test") == null) {
			$("body").append('<div id="noscript"></div><div id="blocker"></div>');
		}
		
		eraseCookie("test");
	});
}

function setCount()
{
	var count = readCookie("mycoke_count");
	var time = readCookie("mycoke_count_time");
	var curTime = new Date().getTime();
	
	if(time == null || isNaN(time)) time = curTime;
	
	if(count != "" && count != null)
	{
		if((parseInt(time) + (30 * 60 * 1000)) <= curTime) count = 1;
		else count = parseInt(count)+1;
	}
	else count = 1;
	
	time = curTime;
	
	document.cookie = "mycoke_count="+count;
	document.cookie = "mycoke_count_time="+time;
	
	$('meta[name=WT.z_counter]').attr("content",count+"");
}