//HEADER JS - v3.0 -km 06/2006
var dmn =  'http://' + location.hostname;
var domain = dmn;

var loc = new String(location);

//var ref = document.referrer;

//get rid of this once everything is switched over
var msnTrail = Get_Cookie('msnTrail');

var onLRW = (loc.contains('lifetimetv.com/lrw'));
var onLMN = (loc.contains('lifetimetv.com/lmn'));
var onBoards = (loc.contains('boards.lifetimetv.com'));
var onSurvey = (loc.contains('surveyor.lifetimetv.com'));
var onSearch = (loc.contains('googlebot.lifetimetelevision.com'));
var wideSite = false;

var wideSiteList = ['.com/breastcancer'];

//if on an external site, change all links to www.lifetimetv.com
if ( (onBoards) || (onSurvey) || (onSearch) ) { domain='http://www.lifetimetv.com'; }


/*
	This list determines which button is highlighted when you reach a  particular "section." 
	Add urls to each array with a comma and they will be added to that list for that button. -km 6/2006
*/	
var navRules = 
	{		
		'schedule' : 
			[ 'lifetimetv.com/shows/tvschedule/' ],
			
		'movies' :
			[ 'lifetimetv.com/movies/' ],
			
		'games' :
			[ 'lifetimetv.com/games/' ],
			
		'sweeps' : 
			[ 'lifetimetv.com/sweeps/' ],
			
		'video' : 
			[ 'lifetimetv.com/videolounge/' ],
			
		'mobile' : 
			[ 'lifetimetv.com/mobile/' ],
			
		'shop' :
			[ 'lifetimetv.com/movies/entertain/' ],
			
		'boards' :
			[ 'boards.lifetimetv.com/' ],
			
		'diethealth' :
			[ 'lifetimetv.com/reallife/df/' ],
			
		'lovesex' :
			[ 'lifetimetv.com/reallife/relation/'],
			
		'astrology' :
			[ 'lifetimetv.com/astro/' ]
	};

//find out what the highlighted button should be
var navButton = getTopNavState(); //'movies';	
var hdrTabsPath = domain + '/images/common/headertabs/';

var headerHTML = [];	
	
//ADVERTISEMENT
headerHTML.push(
	'<div id="adcontainer">',
	'	<div id="widerightad"><%widerightad%></div>',
	'	<div id="advertisement">',
	'		<oascall />',
		'<\/div>',
	'<\/div>',
	'<div id="navdivider"><\/div>'
);

//NAVIGATION
headerHTML.push(
	'<div id="menu">',
	'	<h1><a href="<%domain%>/index.php"><img src="<%domain%>/images/common/hdr_logo_mystory.gif" alt="My story on Lifetime" title="My story on Lifetime" border="0" /></a><\/h1>',
	'	<ul id="nav">',
	'	  <li><a href="<%tvschedulelink%>" class="btn1"><img src="<%domain%>/images/common/headertabs/schedule_off.gif" name="tab_schedule" alt="Schedule" title="Schedule" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/movies/index.php" class="btn2"><img src="<%domain%>/images/common/headertabs/movies_off.gif" name="tab_movies" alt="Movies" title="Movies" border="0" /><\/a><\/li>',
	'	  <li><a href="javascript:launchVideoLounge()" class="btn3"><img src="<%domain%>/images/common/headertabs/video_off.gif" name="tab_video" alt="Video" title="Video" border="0" /><\/a><\/li>',
	'	  <li><a href="http://boards.lifetimetv.com/" class="btn4"><img src="<%domain%>/images/common/headertabs/boards_off.gif" name="tab_boards" alt="Boards" title="Boards" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/reallife/df/index.php" class="btn5"><img src="<%domain%>/images/common/headertabs/diethealth_off.gif" name="tab_diethealth" alt="Diet & Health" title="Diet & Health" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/reallife/relation/index.html" class="btn6"><img src="<%domain%>/images/common/headertabs/lovesex_off.gif" name="tab_lovesex" alt="Love & Sex" title="Love & Sex" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/astro/index.html" class="btn7"><img src="<%domain%>/images/common/headertabs/astrology_off.gif" name="tab_astrology" alt="Astrology" title="Astrology" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/games/index.php" class="btn8"><img src="<%domain%>/images/common/headertabs/games_off.gif" name="tab_games" alt="Games" title="Games" border="0" /><\/a><\/li>',		
	'	  <li><a href="<%domain%>/sweeps/index.html" class="btn9"><img src="<%domain%>/images/common/headertabs/sweeps_off.gif" name="tab_sweeps" alt="Sweeps" title="Sweeps" border="0" /><\/a><\/li>',  
	'	  <li><a href="<%domain%>/mobile/index.php" class="btn10"><img src="<%domain%>/images/common/headertabs/mobile_off.gif" name="tab_mobile" alt="Mobile" title="Mobile" border="0" /><\/a><\/li>',
	'	  <li><a href="<%domain%>/movies/entertain/index.php" class="btn11"><img src="<%domain%>/images/common/headertabs/shop_off.gif" name="tab_shop" alt="Shop" title="Shop" border="0" /><\/a><\/li>',
	'	<\/ul>',
	'<\/div>',
	'<script type="text/javascript">setTopNavState();<\/script>',
	'<div id="navdivider2"><\/div>'
);

//PERSONALIZATION

headerHTML.push(
	'<div id="subnav">',
	'	<ul id="member_options">',
	'		<li class="personalization"><%personalization%><\/li>',
	'		<li class="memberlink">| <%accessmsg%><\/li>',
	'		<li class="memberlink">| <%action%><\/li>',
	'	<\/ul>',
	'	<form name="gs" method="GET" action="http://googlebot.lifetimetelevision.com/search" onSubmit="if((this.q.value==\'<%searchstr%>\')||(this.q.value==\'\')){return false;}" class="formstyle">',
	'		<div id="searchbox">',
	'			<input value="date:D:L:d1" name="sort" type="hidden">',
	'			<input value="xml_no_dtd" name="output" type="hidden">',
	'			<input value="lifetimetv" name="site" type="hidden">',
	'			<input value="UTF-8" name="ie" type="hidden">',
	'			<input value="UTF-8" name="oe" type="hidden">',
	'			<input value="<%googlexsl%>" name="client" type="hidden">',
	'			<input value="<%googlexsl%>" name="proxystylesheet" type="hidden">',
	'			<input type="text" id="searchterm" name="q" size="22" value="<%searchstr%>" onclick="if(this.value==\'<%searchstr%>\'){this.value=\'\'}" />',
	'			<input type="image" id="go" name="Go" src="<%domain%>/images/common/hdr_btn_go.gif" />',
	'		<\/div>'	,
	'	<\/form>',
	'	<div id="widerightshim"></div>',
	'	<form name="showsform" class="formstyle">',
	'		<select id="pulldown" name="showlist" onChange="changeURL(this)">',
	'			<option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Shows -- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/option>',
	'			<%pulldown%>',
	'		<\/select>',
	'	<\/form>',
	'<\/div>',
	'<div id="navdivider3"><\/div>'
);

//run thru rules, find out what the right button to highlight is
function getTopNavState()
{
	var loc = new String(location);
	var btnName = new String();
	
	for (btn in navRules)
	{
		var thisURLList = navRules[btn];
		
		for (i=0;i<thisURLList.length;i++)
		{
			if (loc.indexOf(thisURLList[i])!=-1)
			{
				btnName = btn;
				break;
			}
		}
	}

	return btnName;
}		

//set that button to be the active button
function setTopNavState()
{
	if (typeof(navButton)!="undefined" && navButton!="")
	{
		document.images['tab_'+navButton].src = hdrTabsPath + navButton + '_on.gif';
	}
}		

function drawHeader()
{	
	//configure member links messages
	var memberlinks = {
		'loggedOut' : 
			{
			'personalization'		: 'Become a Member!', 
			'accessmsg' 			: '&nbsp; <a href="<%domain%>/community/members/member_intro.html?rsrc=sn" title="Register Now">Register Now<\/a>',
			'action' 					: '&nbsp; <a href="<%domain%>/cgi/community/members/authentication.pl" title="Log In">Log In<\/a>'
			},
						
		'loggedIn' : 
			{
			'personalization'		: 'Welcome, <%username%>',
			'accessmsg' 			: '&nbsp; <a href="<%domain%>/community/members/exclusive/index.php" title="Member Center">Member Center<\/a>',
			'action' 					: '&nbsp; <a href="<%domain%>/cgi/community/members/authentication.pl?lg_action=reset_lc" title="Log Out">Log Out<\/a>'
			}
	}
	
	var pulldown = new String();
	
	//this fills in the pulldown html
	for (obj in headerLinks)
	{
		if (headerLinks[obj]['label'] == '')
		{
			var optionTmpl = new Class_HTMLTemplate(['<option>'+divider+'<\/option>']);
		}
		else 
		{
			var optionTmpl = new Class_HTMLTemplate(['<option value="<%domain%><%linkurl%>"><%linklabel%><\/option>']);
			optionTmpl.addParams( { 'domain' : domain, 'linkurl' : headerLinks[obj]['url'], 'linklabel' : headerLinks[obj]['label'] } );
		}
		optionTmpl.fillTemplate();
		pulldown+=optionTmpl.text;
	}
	
	//different OAS position call for different sites
	var oastag = ( onLMN || onLRW ) ? 
		'<script>OAS_AD(\'Top\')<\/script>' :  '<script>OAS_AD(\'Top1\')<\/script>';

	//run thru each line in the headerHTML and make substitutions where necessary
	for (line in headerHTML)
	{
		if (headerHTML[line].contains('<oascall')) 
		{ 
			document.write(oastag); 
		}
		else 
		{
			//added this bit in to deal with mac IE problems. - TFC 05/25/05
			if (line == 'push')  { headerHTML[line] = ''; }
		}
		
		//search and replace thru a template class
		var l = new Class_HTMLTemplate([headerHTML[line]]);

		l.addParams(
			{ 
			//Change link content based on login state
			'personalization' :  
				(userProfile['loggedIn']==1) ? memberlinks['loggedIn']['personalization'] : memberlinks['loggedOut']['personalization'],
			'accessmsg' : 
				(userProfile['loggedIn']==1) ? memberlinks['loggedIn']['accessmsg'] : memberlinks['loggedOut']['accessmsg'],
			'action' : 
				(userProfile['loggedIn']==1) ? memberlinks['loggedIn']['action'] : memberlinks['loggedOut']['action'],
				
			'domain' : (onBoards || onSurvey || onSearch) ? 'http://www.lifetimetv.com' : domain,
			'tvschedulelink' : 'javascript:location=\''+domain+getTVScheduleLink()+'\'',
			'pulldown' : pulldown,
			'searchstr' : ' SEARCH',
			'googlexsl' : 'ltv_frontend', //(dev) ? 'http://www.lifetimetv.com/google/google_ltv_dev.xsl' : 'ltv_frontend',
			'username' : userProfile['firstname'],
			'widerightad' : '<a href="http://www.lifetimetv.com/breastcancer/sbcfl.php"><img src="http://www.lifetimetv.com/images/breastcancer/widerightad.jpg" class="widerightimg" border=0 /></a>'
			}
		);
		
		//replace tokens
		l.fillTemplate();
		
		//once the sub-ins are done, print the line of HTML to the browser
		l.printTemplate();
	}		
	
	//hacks for safari
	if (is_safari)
	{
		//searchbox
		document.getElementById("searchterm").style.height = "17px";
	}
	
	/* everything below this line is for our site which has the wrong doctype (none) rigtht now.
	*
	*	This is the doctype we need:
	*	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	*	<html xmlns="http://www.w3.org/1999/xhtml">
	*/

	if (is_fox || is_ie && is_win)
	{
		document.getElementById("searchterm").style.height = "17px";
	}

	if (is_fox && is_win)
	{
		document.getElementById("adcontainer").style.width = "758px";
		document.getElementById("menu").style.width = "758px";
		document.getElementById("subnav").style.width = "758px";
	}
	//for ie 7. ugh
	if (is_ie || agt.contains('msie 7'))
	{
		document.getElementById("adcontainer").style.width = "760px";
		document.getElementById("menu").style.width = "760px";
		document.getElementById("subnav").style.width = "760px";
		document.getElementById("searchterm").style.height = "17px";
	}

	/*** WIDE SITE ***/
	
	for (site in wideSiteList)
	{
		//if the location contains one of the url snippets in the list, make it wide!
		if (loc.contains(wideSiteList[site]))
		{
			wideSite = true;
			break;
		}	
	}

	//these are the rules for adjusting the header to the wider version (990px)
	if (wideSite)
	{
		//stretch all rows
		document.getElementById('adcontainer').style.width = "988px";
		document.getElementById('menu').style.width = "988px";
		document.getElementById('navdivider').style.width = "990px";
		document.getElementById('navdivider2').style.width = "990px";
		document.getElementById('navdivider3').style.width = "990px";
		document.getElementById('subnav').style.width = "988px";
		document.getElementById("searchterm").style.height = "13px";
		
		if (is_safari)
		{
			//searchbox
			document.getElementById("searchterm").style.height = "17px";
		}
		
		if (is_fox && is_win)
		{
			document.getElementById("adcontainer").style.width = "988px";
			document.getElementById("menu").style.width = "988px";
			document.getElementById("subnav").style.width = "988px";
		}
		
		if (is_ie || agt.contains('msie 7'))
		{
			document.getElementById("adcontainer").style.width = "988px";
			document.getElementById("menu").style.width = "988px";
			document.getElementById("subnav").style.width = "988px";
		}

		//insert top right ad
		document.getElementById('widerightad').style.display = 'inline';
		document.getElementById('widerightshim').style.display = 'inline';	
	}
}


