var flckrPage;
var twitterPage;
var rssPage;
//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;
		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.style,sUrl,aPos);
	
	resizePopup(getDivPopupID(id)+"_frame",w,h);
	return false;
}

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.do?deeplink="+type;
}

function callGenericError(type)
{
	callParentFunction("genericError('"+type+"')");
}
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);
	}
}

/** MY FT **/
var wMin = 956;
function popupFile(fileName, w, h)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,resizable=0,menubar=0,toolbar=0,location=0' ;
	title = 'FullThrottle' + Math.floor(Math.random()*1000000+1);
	win = window.open(fileName, title, winprops);
	if(win) {
		win.focus();
		if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}
}
		
		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;
		}
/** UPDATE SOCIAL **/

function updateMyFTCategory(id)
{
	var obj;
	var url = "";
	var data = new Object();
	url = homeBase+"viewMyFTContent.do";
	switch(id)
	{
		case 'motors':
			//url = "viewMyFTContent_Sample1.html"
			//url = "viewMyFTContent_error.jsp";
			data.categoryId = 1;
			break;
		case 'making':
			//url = "viewMyFTContent_Sample2.html"
			data.categoryId = 2;
			break;
		case 'sound':
			//url = "viewMyFTContent_Sample3.html"
			data.categoryId = 3;
			break;
	}
	

	if(data.categoryId != null)
	{
		document.getElementById("motors_tab").className = "";
		document.getElementById("making_tab").className = "";
		document.getElementById("sound_tab").className = "";
	
		var obj = document.getElementById(id+"_tab");
		obj.className = "selected";
		$("#contentTab_div").load(url, data);
	}
	return false;
}

function updateSocialArea(id, page, index)
{
	var url = "";
	var pageSize = 3;
	var data = new Object();
	switch(id)
	{
		case 'flckr':
			pageSize = 12;
			url = homeBase+"viewFlickrContent.do";
			break;
		case 'facebook':
			url = homeBase+"viewFbWallEntries.do";
			break;
		case 'twitter':
			pageSize = 5;
			url = homeBase+"viewTwitterContent.do";
			break;
		case 'rss':
			id += "_"+index;
			data.rssIndex = index;
			url = homeBase+"viewRSSContent.do";
			break;
	}
	//url -> Service url
	//Page size -> number of items requested
	//page -> page requested
	data.pageSize = pageSize;
	data.pageNum  = page;
	if(url != null && url != "")
	{
		//new Ajax.Updater(id+"_div", url, { method: 'post', parameters:data });
		$("#"+id+"_div").load(url, data);
	}
}

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;
}

function onChangePassword(obj) {
	var box = document.getElementById("change_box");
	if(obj.className == "open")
	{
		obj.className = "close";
		box.className = "box visible";
	}
	else
	{
		obj.className = "open";
		box.className = "box hidden";
	}
}

function initMyFT(id) {
	var hashID = unescape(self.document.location.hash.substring(1));
	switch(hashID)
	{
		case 'motors':
		case 'making':
		case 'sound':
			break;
		default: hashID = "";
	}
	updateMyFTCategory((hashID != "")?hashID:id);
	
	//Setup
	showSwf('myft_setup',homeBase+'swf/ft_switch.swf',null,null,null,112,239);
}
function openMyFTSetup() {
	//return openPopup(document.getElementById('myft_setup'),'../../jsp/myft/myftSetup.html',{width:818,height:540,style:'myft_setup'})
	return openPopup(document.getElementById('myft_setup'),homeBase+'viewSwitchContent.do',{width:818,height:540,style:'myft_setup'})
}
function initEvents(id) {
	
	var url = "";
	var data = new Object();
	switch(id)
	{
		case 'home':
			url = homeBase+"jsp/events/featured_events.html";
			break;
		case 'events':
			url = homeBase+"jsp/events/events_list.html";
			break;
	}
	if(url != null && url != "")
	{
		$("#events_div").load(url, data);
	}
}
function initHome() {
	//Splash
	showSwf('splash_swf',homeBase+'swf/home/ft_hmain.swf');
	//Main
	showSwf('banner_main',homeBase+'swf/home/ft_eventsmain.swf',null,{link:homeBase+'events.do'});
	//Left
	showSwf('banner_1',homeBase+'swf/home/ft_ftvbanner.swf',null,{link:homeBase+'fttv.do'});
	//Right
	//showSwf('banner_2',homeBase+'swf/home/ft_buybannerfla.swf',null,{link:homeBase+'promos.do'});
}
function initMap() {
	//map
	showSwf('map_div',homeBase+'swf/ft_map.swf',{base:homeBase+'swf/'});
}
function initProducts() {
	//products
	showSwf('products_swf',homeBase+'swf/products/main.swf',{base:homeBase+'swf/products/'});
}
function initTV() {
	//products
	showSwf('tv_swf',homeBase+'swf/fttv.swf');
}

/** SIMPLE **/
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;
}