// JavaScript navigation for time pages - Kelvin Owers/Steve Holmes

// bottomFillercanbeNull(onlyusedforlongpages)
// selElementcanbeNull

// Booleans to say whether nodes with children are expanded or not 
var isBroadband = false;
var isLivebox;
var isBBExplained;
var isDialup;
var isLandingPage;
var isUnique;
var isMobileBroadband; // New immediately following Livebox Mini; aka "Internet Everywhere"

// Strings for assigning CSS classes - the 'notSelected' is required as we need to have a class value which
// is over written by Selected, as this is the only way to add styling to only the Selected element.
var broadband;
var bbStarter;
var bbSelect;
var bbMax;
var unique;
var uniquePhones;
var uniquePlans;
var uniqueBroadband;
var uniqueQuestions;
var uniqueDemo;
var mobileBroadband; // New immediately following Livebox Mini; aka "Internet Everywhere"
var compareall;
var bbExplained;
var speedDemo;
var swtch;
var livebox;
var dialup;
var bb4business;
var xboxAccredited;
var storeLocator;

function cleanUp() {
	// Booleans to say whether nodes with children are expanded or not 
	isBroadband       = false;
	isLivebox         = false;
	isBBExplained     = false;
	isDialup          = false;
	isLandingPage     = false;
	isUnique          = false;
	isMobileBroadband = false;

	// Strings for assigning CSS classes -the 'notSelected' is required as we need to have a class value which
	// is over written by Selected, as this is the only way to add styling to only the Selected element.
    broadband   		= 'notSelected';
    bbStarter   		= 'notSelected';
    bbSelect    		= 'notSelected';
    bbMax       		= 'notSelected';
    unique				= 'notSelected';
    uniquePhones 		= 'notSelected';
    uniquePlans 		= 'notSelected';
    uniqueBroadband 	= 'notSelected';
    uniqueQuestions 	= 'notSelected';
    uniqueDemo			= 'notSelected';
	mobileBroadband     = 'notSelected'; // New immediately following Livebox Mini; aka "Internet Everywhere"
    compareall  		= 'notSelected';
    bbExplained 		= 'notSelected';
    speedDemo   		= 'notSelected';
    swtch       		= 'notSelected';
    livebox     		= 'notSelected';
    dialup      		= 'notSelected';
	bb4business         = 'notSelected';
	xboxAccredited      = 'notSelected';
	storeLocator        = 'notSelected';

	debug('all items unselected');
}

var openWindows=new Array();

function navPop(theURL,theWidth,theHeight,theScroll,theResize) {
	// For accessibility reasons allow scrolling and resizing of windows (e.g. if user increases text size)
	// unless otherwise specified this is a renamed version of orangePop(from time_nav_buttons), and is used
	// for links in Orange Shop which generate a confirmation popup
	if (!theScroll) { theScroll = "yes"; }
	if (!theResize) { theResize = "yes"; }

	var theProperties = "width="+theWidth+",height="+theHeight+",scrollbars="+theScroll+",resizable="+theResize+",top=10,left=10";
	orangePopWin      = window.open(theURL,"navPop",theProperties);
	openWindows[0]    = orangePopWin;

	if (window.focus) { orangePopWin.focus(); }
}
	
function setNavVar(selParent, selElement, isLandingPage) {
	debug("Calling setNavVar");

	// reset selected states
	cleanUp();

	if (!isLandingPage) { isLandingPage = false; }

	debug("setNavVar isLandingPage: "+isLandingPage);

	// This list should contain all potential parents, i.e. all nodes which have at least one child.
	debug("setNavVar: switch on selParent = " + selParent);
	switch(selParent) {
		case'broadband':
			debug("setNavVar: broadband = expanded\nsetNavVar: isBroadband = true");
			broadband   = 'expanded';
			isBroadband = true;
			break;				
		case'unique':
			debug("setNavVar: unique = expanded\nsetNavVar: isUnique = true");
			unique = 'expanded';
			isUnique = true;
			break;
		case'bbExplained':
			debug("setNavVar: bbExplained = expanded\nsetNavVar: isBBExplained = true");
			if (!isLandingPage) {
				broadband   = 'expanded';
				isBroadband = true;
			} else {
				bbExplained   = 'expanded';
				isBBExplained = true;
			}
			break;
		case'livebox':
			debug("setNavVar: livebox = expanded\nsetNavVar: isLivebox = true");
			livebox   = 'expanded';
			isLivebox = true;
			break;
	}

	// Having established the IMMEDIATE parent of the current element, there may be other ancestors which need
	// to be highlighted. The following code defines the hierarchy of the tree.
	debug("setNavVar: switch on selElement = " + selElement);
	switch(selElement) {
		case 'broadbandaccess':
			debug("setNavVar: broadband = selected\nsetNavVar: isBroadband = true");
			broadband   = 'selected';
			isBroadband = true;
			break;
		case 'bbStarter':
			debug("setNavVar: bbStarter = selected");
			bbStarter = 'selected';
			break;
		case 'bbSelect':
			debug("setNavVar: bbSelect = selected");
			bbSelect = 'selected';
			break;
		case 'bbMax':
			debug("setNavVar: bbMax = selected");
			bbMax = 'selected';
			break;
		case 'unique':
			debug("setNavVar: unique = selected");
			unique = 'selected';
			break;
		case 'uniquePhones':
			debug("setNavVar: uniquePhones = selected");
			uniquePhones='selected';
			isUnique= true;
			break;
		case 'uniquePlans':
			debug("setNavVar: uniquePlans = selected\nsetNavVar: isUnique = true");
			uniquePlans='selected';
			isUnique= true;
			break;
		case 'uniqueBroadband':
			debug("setNavVar: uniqueBroadband = selected\nsetNavVar: isUnique = true");
			uniqueBroadband='selected';
			isUnique= true;
			break;
		case 'uniqueQuestions':
			debug("setNavVar: uniqueQuestions = selected\nsetNavVar: isUnique = true");
			uniqueQuestions='selected';
			isUnique= true;
			break;
		case 'uniqueDemo':
			debug("setNavVar: uniqueDemo = selected\nsetNavVar: isUnique = true");
			uniqueDemo='selected';
			isUnique= true;
			break;
		case 'livebox':
			debug("setNavVar: livebox = selected\nsetNavVar: isLivebox = true");
			livebox   = 'selected';
			isLivebox = true;
			break;
		case 'compareall':
			debug("setNavVar: compareall = selected");
			compareall = 'selected';
			break;
		case 'bbExplained':
			debug("setNavVar: bbExplained = selected\nsetNavVar: isBBExplained = true");
			bbExplained   = 'selected';
			isBBExplained = true;
			break;
		case 'speedDemo':
			debug("setNavVar: speedDemo = selected\nsetNavVar: isBBExplained = true");
			speedDemo   = 'selected';
			bbExplained   = 'expanded';
			isBBExplained = true;
			break;
		case 'swtch':
			debug("setNavVar: swtch = selected");
			swtch = 'selected';
			break;
		case 'dialup':
			debug("setNavVar: dialup = selected\nsetNavVar: isDialup = true");
			dialup   = 'selected';
			isDialup = true;
			break;
		case 'bb4business':
			bb4business = 'selected';
			break;
		case 'xboxAccredited':
			xboxAccredited = 'selected';
			break;
		case 'storeLocator':
			storeLocator = 'selected';
			break;
	}

	// Lowest level nodes first	
	// if (isLivebox) { bbUnlimited = 'expanded'; isBBUnlimited=true; }
	// ...then their parents
	if (isUnique) {
		broadband   = 'expanded';
		isBroadband = true;
	}/* else if (isBBExplained) {
		if (isLandingPage) {
			bbExplained = 'selected';
		} else {
		}
	}*/ // ...etc(add more as required)

	// Determine which is the absolute parent node, and shade all children in light grey
	if (isBroadband) {
		broadband += " masterParent";
	} else if (isBBExplained) {
		if (isLandingPage) {
			bbExplained += " masterParent";
		}
	} // else if (isDialup) { dialup += " masterParent"; } // ...etc(add more as required)

	// There is no default case for this switch statement. Consequently, any value passed as 'selElement' which
	// is not specified above simply falls through, and no subsection link in the left-nav is highlighted. This
	// is applicable to /time/max.htm and /time/speed.htm for example, where we pass the values 'max' and 'speed'
	// as 'selElement'. That said, there are no entries in the left-nav applicable to these two pages anyway at
	// present. If and when these become available, we will need to add cases to this switch to cater for them.
	// For now, we should continue to call setNavVar() via writeTimeNav() with logically correct parameters, as
	// we do for /time/max.htm and /time/speed.htm. The switch statement will then just fall through.
}

function writeTimeNav(selParent, selElement, bottomFiller, isLandingPage) {
	debug("Calling writeTimeNav");
	document.write(createNav(selParent, selElement, bottomFiller, isLandingPage));
}

function createNav(selParent, selElement, bottomFiller, isLandingPage){	
	debug("Calling createNav");
	if (!isLandingPage) { isLandingPage = false; }

	debug("createNav isLandingPage: "+isLandingPage);
	debug('leftNav: '+selParent+' '+selElement+' '+bottomFiller+' '+isLandingPage);

	setNavVar(selParent,selElement,isLandingPage);

	var n = '';
	n += '<div class="invisibletitle"><a href="#content" title="SkipNavigation">skip navigation</a></div>';
	n += '<div id="dnav">';
	n += '<div class="msiefix">';
	n += timeNav(isLandingPage);
	n += '</div>';
	n += '<div class="baseline"></div>';

	if (isMac&&isIE) {}
	else { n += filler(bottomFiller); }

	n += '</div>';
	n += '<div id="content" class="invisibletitle">start of page content</div>';

	debug(n);
	return n;
}

function timeNav(isLandingPage){
	var o = '';
	o += '<ul class="level_1">';

	debug("timeNav: isLandingPage "+isLandingPage);

	if (isLandingPage) {
		o+='<li class="'+broadband+'"><div><a href="/time/broadbandaccess/">broadband</a></div></li>';
		if (isBroadband) {
			o+='<ul class="level_2">';
				o+='<li class="'+bbStarter+'"><div><a href="/time/broadbandstarter/">Home Starter</a></div></li>';
				o+='<li class="'+bbSelect+'"><div><a href="/time/broadbandselect/">Home Select</a></div></li>';
				o+='<li class="'+bbMax+'"><div><a href="/time/broadbandmax/">Home Max</a></div></li>';
				o+='<li class="'+compareall+'"><div><a href="/time/compare/">compare broadband packages</a></div></li>';
				o+='<li class="'+bbExplained+'"><div><a href="/time/broadbandexplained/">broadband explained</a></div></li>';
				if (isBBExplained) {
					o+='<ul class="level_3">';
						o+='<li class="'+speedDemo+'"><div><a href="/time/speeddemo/">speed demo</a></div></li>';
					o+='</ul>'; // Close BBExplained
				}
				o+='<li class="'+swtch+'"><div><a href="/time/switch.htm">switching broadband</a></div></li>';
				o+='<li class="'+livebox+'"><div><a href="/time/livebox/">Livebox</a></div></li>';
		}
		o+='</ul>'; // Close Broadband

		// Added this condition specifically for the dial-up section of left nav, in WC/WLR. Not clear
		// over the reasons how or why this works, but it seems to do the trick.
		if (!isBroadband) {
			o+='<ul class="level_1">'; // open others
		}

		o+='<li class="'+dialup+'"><div><a href="/time/dialup/">dial-up</a></div></li>';
		o+='</ul>'; // Close Everything!
	}
	// Full left nav
	else {
		o+='<li class="external"><div><a href="http://shop.orange.co.uk/shop/mobile">mobile phones &amp; plans</a></div></li>';
		o+='<li class="'+broadband+'"><div><a href="/time/broadbandaccess/">broadband</a></div></li>';
		if (isBroadband) {
			o+='<ul class="level_2">';
				o+='<li class="'+bbStarter+'"><div><a href="/time/broadbandstarter/">Home Starter</a></div></li>';
				o+='<li class="'+bbSelect+'"><div><a href="/time/broadbandselect/">Home Select</a></div></li>';
				o+='<li class="'+bbMax+'"><div><a href="/time/broadbandmax/">Home Max</a></div></li>';
				o+='<li class="'+bb4business+'"><div><a href="/time/businessbroadband/">broadband for business</a></div></li>';
				o+='<li class="'+compareall+'"><div><a href="/time/compare/">compare broadband packages</a></div></li>';
				o+='<li class="'+bbExplained+'"><div><a href="/time/broadbandexplained/">broadband explained</a></div></li>';
			if (isBBExplained) {
				o+='<ul class="level_3">';
					o+='<li class="'+speedDemo+'"><div><a href="/time/speeddemo/">speed demo</a></div></li>';
				o+='</ul>';//Close BBExplained
			}
				o+='<li class="'+swtch+'"><div><a href="/time/switch.htm">switching broadband</a></div></li>';
				o+='<li class="'+mobileBroadband+'"><div><a href="/time/redirect/redirect=ext&linkfrom=Time_broadbandaccess&link=Internet_Everywhere_find_out_more&linkto=shop.orange.co.uk/shop/mobile_broadband?WT.svl=204">mobile broadband</a></div></li>';
				o+='<li class="'+livebox+'"><div><a href="/time/livebox/">Livebox</a></div></li>';
				o+='<li class="'+xboxAccredited+'"><div><a href="/time/xboxlive/">Xbox LIVE accredited</a></div></li>';

			if (isUnique) {
				o+='<ul class="level_3">';
					o+='<li class="'+uniquePhones+'"><div><a href="/unique/phones.htm">phones</a></div></li>';
					o+='<li class="'+uniquePlans+'"><div><a href="/unique/plans.htm">plans</a></div></li>';
					o+='<li class="'+uniqueBroadband+'"><div><a href="/unique/unique_broadband.htm">broadband</a></div></li>';
					o+='<li class="'+uniqueQuestions+'"><div><a href="/unique/faqs.htm">questions</a></div></li>';
					o+='<li class="'+uniqueDemo+'"><div><a href="/unique/demo.htm">demo</a></div></li>';
				o+='</ul>'; // Close Unique
			}
			o+='</ul>'; // Close Broadband
		}

			o+='<li class="'+dialup+'"><div><a href="/time/dialup/">dial-up</a></div></li>';
			o+='<li class="external"><div><a href="http://www.orangeaccessories.co.uk/">home phones &amp; accessories</a></div></li>';
			o+='<li class="external"><div><a href="http://shop.orange.co.uk/shop/show/stores/">store locator</a></div></li>';
		o+='</ul>'; // Close Everything!
	} // end of non-landing page nav
	return o;
}

function filler(bottomFiller) {
	var x = '<div style="height:5em;text-align:center;padding-top:1em;"></div>';

	// case statement left in for legacy reasons, but this should be called with a height values in ems (or pixels)
	switch (bottomFiller) {
		case'reasonable':
			x += '<div style="height:10em;"></div>';
			break;
		case'medium':
			x += '<div style="height:30em;"></div>';
			break;
		case'large':
			x += '<div style="height:40em;"></div>';
			break;
		case'huge':
			x += '<div style="height:50em;"></div>';
			break;
		case'hugePlus':
			x += '<div style="height:60em;"></div>';
			break;
		default:
			x += '<div id="dnavBottomFiller" style="height:'+bottomFiller+';"></div>';
			break;
	}

	return x;
}