var martin_debug = (top.location.href.indexOf("?martin=true") != -1) ? true : false;
var primary_navigation = new Array();
var debug = false;
var quicklinks_fade_hex = "#EB3645";
var quicklinks_fade_initial_hex = "#D7D7D7";
var quicklinks_fade_duration = 750; //in milliseconds, so 3/4 of a second
var allowed_quicklink_items = 20; //maximum number of quicklink items allowed (per user)

//added 26-07-2007, Martin Burford, Modem Media
var viewing_homepage = (top.location.href.split("/")[top.location.href.split("/").length-1] == "home.html") ? true : false;
var running_from = (document.domain == "mburfordlon02") ? "development" : "live";
var path_prefix_images = (viewing_homepage) ? "" : "../";
var path_prefix_global = (viewing_homepage) ? "global/" : "../global/";

//overwrite path prefix when running in development mode
if(running_from == "development"){path_prefix_images = "/SWIFA/";}
if(running_from == "development"){path_prefix_global = "/global/";}

//interactive overlib layers
var overlib_delay = 150;

var overlib_add_to_my_quicklinks = '';
var overlib_add_to_my_quicklinks_offsetx = 20;
var overlib_add_to_my_quicklinks_offsety = -65;

overlib_add_to_my_quicklinks += '<div class="overlay">';
overlib_add_to_my_quicklinks += '	<h4 class="firstItem">Add to My Quicklinks</h4>';
overlib_add_to_my_quicklinks += '	<p class="cornered"><a href="javascript:void(nd())">Close</a></p>';
overlib_add_to_my_quicklinks += '	<p>Click this link to save the current page to your Quicklinks list. You can access your favourites using the My Quicklinks panel in the top-right corner of each page.</p>';
overlib_add_to_my_quicklinks += '	<p class="lastItem"><a href="#">Find out more</a></p>';
overlib_add_to_my_quicklinks += '</div>';

var overlib_quicklinks_help = '';
var overlib_quicklinks_help_offsetx = 15;
var overlib_quicklinks_help_offsety = -65;

//updated 19-06-2007, Martin Burford, Modem Media
overlib_quicklinks_help += '<div class="overlay">';
overlib_quicklinks_help += '	<h4 class="firstItem">My Quicklinks</h4>';
overlib_quicklinks_help += '	<p class="cornered"><a href="javascript:void(nd())">Close</a></p>';
overlib_quicklinks_help += '	<p>Quickly get to your favourite pages by adding them to My Quicklinks.</p>';
overlib_quicklinks_help += '	<p class="lastItem"><a href="' + path_prefix_global + 'help_what_is_my_quicklinks.html">Tell me more</a></p>';
overlib_quicklinks_help += '</div>';

var overlib_yes_no = '';
var overlib_yes_no_offsetx = -20;
var overlib_yes_no_offsety = -55;

overlib_yes_no += '<div class="overlay">';
overlib_yes_no += '	<h4 class="firstItem">Lorem ipsum</h4>';
overlib_yes_no += '	<p class="cornered"><a href="javascript:void(nd())">Close</a></p>';
overlib_yes_no += '	<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>';
overlib_yes_no += '	<p class="button footer lastItem">';
overlib_yes_no += '		<a href="#"><img src="' + path_prefix_images + 'images/buttons/no_F8F8F8.gif" alt="No" /></a>';
overlib_yes_no += '		<a href="#"><img src="' + path_prefix_images + 'images/buttons/yes_F8F8F8.gif" alt="Yes" /></a>';
overlib_yes_no += '	</p>';
overlib_yes_no += '</div>';

//quicklinks related
var no_records = true;
var quicklinks_page_already_stored = "This page is already being stored within My Quicklinks";

function lockout_navigation_item(element){
	element.onmouseout = function(){
		return;
	}
}

//global function for primary navigation rollover actions
function highlight_primary_navigation(mode,element){
	var active_element = "";
	var active_element_colour = "";
	
	for(i=0; i<=primary_navigation.length-1; i++){
		//identify the colour of the active navigation element
		if(primary_navigation[i][0] == element.id){
			active_element = primary_navigation[i][0];
			active_element_colour = primary_navigation[i][1];
			break;
		}
	}
	
	if(mode == "over"){		
		//handle exception for news and blogs item being the active navigation element
		if(element.id == "nav9"){
			element.className = "listLast selected";
			element.parentNode.className = "selected";
		}
		else{
			element.className = "selected";
		}
	}
	else{		
		//reset UL background if moused out from the news and blogs navigation element
		if(element.id == "nav9"){
			element.className = "listLast" + active_element_colour;
			element.parentNode.className = "";
		}
		else{
			element.className = active_element_colour;
		}
	}
	if(debug){window.status = active_element + " - " + active_element_colour;}
}

//global function for grabbing the object handle of any page element
function get_element(id){
	return document.getElementById(id);
}

function load_navigation_page(url){
	window.open(url);
}

//switch the display state of the footer (side by side paragraph) sub-content
function toggle_footer(){
	var content_parent = get_element("footer").getElementsByTagName("P")[0];
	var content_child = get_next_sibling(content_parent);
	
	var content_mode = (content_child.style.display == "block") ? "closed" : "open";
	
	//change display type
	content_child.style.display = (content_child.style.display == "block") ? "none" : "block";
	
	//set background image based on display state identified above
	content_parent.style.background = "url(" + path_prefix_images + "images/global/footer_arrow_" + content_mode + ".gif) no-repeat 0px 12px";	
}

//function which fixes the issue whereby Firefox identifies line breaks as an objects next sibling
function get_next_sibling(start){
    end = start.nextSibling;

    while(end.nodeType!=1){
        end = end.nextSibling;
    }
    return end;
}

//handle the lack of margin support for absolutley positioned elements (within relative parent) (IE only, all versions)
function check_browser(){
	//append top margin on for IE browsers only
	if((navigator.userAgent.toLowerCase().indexOf("windows") != -1) && (navigator.userAgent.toLowerCase().indexOf("msie") != -1)){
		offset = offset + 10;
	}
}

//position the quicklinks element absolutely
function position_quicklinks(){
	var offset_marker = get_element("offset_marker");

	offset = get_offset_top(offset_marker);
	browser = check_browser();

	get_element("client_quicklinks").style.top = offset + 9 + "px";
		
	//the remainder of the calls in this function are essentially for the global page onload
	show_hide_quicklinks(get_element("quicklinks").getElementsByTagName("H3")[0],"");
}

//find the offsetTop for the quicklinks elment
function get_offset_top(obj){
	return obj.offsetTop;
}

//if javascript is available, auto-hide the quicklinks element after page load
function show_hide_quicklinks(obj,tag_type){
	if(tag_type == "link"){
		parentObj = get_element("quicklinks");
	}
	else{
		//parentObj = obj.parentNode;
		parentObj = get_element("quicklinks");
	}
	
	if(parentObj.className != "selected"){
		parentObj.className = "selected";
		parentObj.getElementsByTagName("DIV")[0].style.display = "block";
		parentObj.getElementsByTagName("H3")[0].style.background = "url(" + path_prefix_images + "images/quicklinks_top_selected.gif) no-repeat left top";
	}
	else{
		parentObj.className = "unselected";
		parentObj.getElementsByTagName("DIV")[0].style.display = "none";
		parentObj.getElementsByTagName("H3")[0].style.background = "url(" + path_prefix_images + "images/quicklinks_top.gif) no-repeat left top";
	}
}

//place all elements identified by the same classname into a global array
function get_elements_by_class_name(str_class, tag, obj_element) {
	tag = tag || "*";
	obj_element = obj_element || document;
	
	var obj_collection = obj_element.getElementsByTagName(tag);
	
	if(!obj_collection.length &&  tag == "*" &&  obj_element.all) obj_collection = obj_element.all;
	
	var arr = new Array();
	var delimeter = str_class.indexOf('|') != -1  ? '|' : ' ';
	var classes = str_class.split(delimeter);
	
	for(var i=0, j=obj_collection.length; i<j; i++){
		var obj_classes = obj_collection[i].className.split(' ');
		if(delimeter == ' ' && classes.length > obj_classes.length) continue;
	
		var c = 0;
		comparisonLoop:
	
		for(var k=0, l=obj_classes.length; k<l; k++){
			for (var m=0, n=classes.length; m<n; m++){
				if(classes[m] == obj_classes[k]) c++;
				if((delimeter == '|' && c == 1) || (delimeter == ' ' && c == classes.length)){
					arr.push(obj_collection[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}

//quicklinks functions
function delete_quicklinks_page(cookie_page_id){
	var cookie_contents_string = read_cookie_quicklinks();
	
	if(cookie_contents_string.indexOf(cookie_page_id) != -1){
		cookie_contents_string = cookie_contents_string.replace(cookie_page_id + "|","");
		
		//having deleted the necessary cookie value, rebuild the dynamic quicklinks menu
		var expiration = new Date();
		expiration.setDate(expiration.getDate() + 365);
		document.cookie = cookie_contents_string + ((365 == null) ? "" : "; expires="+expiration.toGMTString() + ";path=/");

		//hide any open overlib layers
		nd();
		
		//rebuild the dynamic quicklinks menu
		build_quicklinks();
	}
	
	//fix to ensure a double-click on the Quicklinks header isn't required
	show_hide_quicklinks(this,'');
}

//simple function which returns whether another item can be added to the quicklinks menu
function quicklinks_counter(){
	var cookie_contents_string = read_cookie_quicklinks();	
	
	cookie_contents_string = cookie_contents_string.substr(0,cookie_contents_string.length-1);
	cookie_contents_string = cookie_contents_string.substr(12,cookie_contents_string.length-11);

	if(cookie_contents_string.split("|").length < allowed_quicklink_items){
		return true;
	}
	else{
		return false;
	}
}

//dependant on how many quicklinks are currently being stored, display sub-content in 1 of 2 ways
function build_quicklinks(){
	var cookie_contents_string = "";
	var quicklinks_counter = 0;
	var quicklinks_offset = 12;
	var current_page_id = "";
	var is_site_homepage = false;
	var is_site_homepage_array = new Array();

	cookie_contents_string = read_cookie_quicklinks();
	quicklinks_counter = cookie_contents_string.split("|").length -1;
	cookie_contents_string = cookie_contents_string.replace("quicklinks=","");
	is_site_homepage_array = top.location.href.split("/");
	is_site_homepage = (is_site_homepage_array[is_site_homepage_array.length-1] == "home.html") ? true : false;
		
	var cookie_contents_array = cookie_contents_string.split("|");
	var quicklinks_html = "";
	var li_class = "listFirst";
	
	//no quicklinks have been added
	if(quicklinks_counter == 0){
		//updated 09-07-2007, Martin Burford, Modem Media
		//auto-hide the overlib is toggling the quicklinks menu
		quicklinks_html += '<h3 id="quicklinks_trigger" onclick="javascript:void(nd());show_hide_quicklinks(this,\'\')" class="fade-fff600">';
		quicklinks_html += '	My Quicklinks (0)';
		quicklinks_html += '	<a href="' + path_prefix_global + 'help_what_is_my_quicklinks.html" title="What is My Quicklinks">';
		quicklinks_html += '		<img id="offset_marker" src="' + path_prefix_images + 'images/question_mark.gif" alt="Quicklinks Help" onmouseover="return overlib(overlib_quicklinks_help, FULLHTML, TEXTSIZE, \'11px\', BELOW, WRAP, STICKY, HAUTO, DELAY, overlib_delay)" onmouseout="return nd()" />';		
		quicklinks_html += '	</a>';
		quicklinks_html += '</h3>';
		quicklinks_html += '<div id="client_quicklinks">';
		quicklinks_html += '	<p>Save pages that you use frequently here whenever you see this icon:</p>';
		quicklinks_html += '	<p class="instruction">Add this page to My Quicklinks</p>';
		quicklinks_html += '	<p><a href="' + path_prefix_global + 'help_what_is_my_quicklinks.html">Tell me more</a></p>';
		quicklinks_html += '	<p class="bar"><a href="javascript:show_hide_quicklinks(null,\'link\')">Close</a></p>';
		quicklinks_html += '</div>';
	}
	//quicklinks have been added
	else{
		//updated 09-07-2007, Martin Burford, Modem Media
		//auto-hide the overlib is toggling the quicklinks menu
		quicklinks_html += '<h3 id="quicklinks_trigger" onclick="javascript:void(nd());show_hide_quicklinks(this,\'\')" class="fade-fff600">';
		quicklinks_html += '	My Quicklinks (<span>' + quicklinks_counter + '</span>)';
		quicklinks_html += '	<a href="' + path_prefix_global + 'help_what_is_my_quicklinks.html" title="What is My Quicklinks">';
		quicklinks_html += '		<img id="offset_marker" src="' + path_prefix_images + 'images/question_mark.gif" alt="Quicklinks Help" onmouseover="return overlib(overlib_quicklinks_help, FULLHTML, TEXTSIZE, \'11px\', BELOW, WRAP, STICKY, HAUTO, DELAY, overlib_delay)" onmouseout="return nd()" />';
		quicklinks_html += '	</a>';
		quicklinks_html += '</h3>';
		quicklinks_html += '<div id="client_quicklinks">';
		quicklinks_html += '	<ul>';
		for(i=1; i<=quicklinks_counter; i++){
			current_page_id = cookie_contents_array[i-1].replace(" ","");

			//change class if the first item has already been added
			if(i>1){li_class = "";}
			
			quicklinks_html += '		<li class="' + li_class + '"><a href="' + lookup_cookie_page_id(current_page_id,"url") + '?from=quicklinks" class="web" title="Load ' + lookup_cookie_page_id(current_page_id,"display_text") + '">' + lookup_cookie_page_id(current_page_id,"display_text") + '</a>&nbsp;<a href="javascript:delete_quicklinks_page(\'' + current_page_id + '\')" title="delete"><img src="' + path_prefix_images + 'images/cross.gif" alt="" onmouseover="return overlib(\'Delete\', LEFT, TEXTSIZE, \'11px\', WIDTH, 32, OFFSETX, 13)" onmouseout="return nd()" /></a></li>';
		}
		
		quicklinks_html += '	</ul>';
		quicklinks_html += '	<p class="bar clear"><span><a href="javascript:delete_all_quicklinks()">Delete All</a></span>&nbsp;<a href="javascript:show_hide_quicklinks(null,\'link\')">Close</a></p>';
		quicklinks_html += '</div>';
	}

	get_element("quicklinks").innerHTML = quicklinks_html;
	parentObj.getElementsByTagName("H3")[0].style.background = "url(" + path_prefix_images + "images/quicklinks_top.gif) no-repeat left top";
}

//read current contents from quicklinks
function read_cookie_quicklinks(){
	var cookie_contents_array = document.cookie.split(";");
	var cookie_contents_string = "";
	
	for(i=0; i<=cookie_contents_array.length-1; i++){
		if(cookie_contents_array[i].indexOf("quicklinks=") != -1){
			cookie_contents_string = cookie_contents_array[i];
			break;
		}
	}

	return cookie_contents_string;
}

//set cookie, either as a new value, or appending to an existing set of quicklinks
function set_cookie(value,expiry_days){
	var cookie_contents_array = document.cookie.split(";");
	var cookie_contents_string = "quicklinks=";
	var exit_function = false;
	var added = false;
	
	for(i=0; i<=cookie_contents_array.length-1; i++){
		if(cookie_contents_array[i].indexOf("quicklinks=") != -1){
			//only allow processing to continue if page requested isn't already being stored in client cookie
			if(cookie_contents_array[i].indexOf(value) != -1){
				alert(quicklinks_page_already_stored);
				exit_function = true;
				break;
			}
			else{
				cookie_contents_string = cookie_contents_array[i];
				break;
			}
		}
	}
	
	//continue only if page being added is not already being stored within the client cookie
	if(!exit_function){
		if(cookie_contents_string.length > 0){
			no_records = false;
		}
		
		//append new value to existing cookie key value(s)
		cookie_contents_string += value + "|";
		
		//add new cookie content
		var expiration = new Date();
		expiration.setDate(expiration.getDate() + expiry_days);
		document.cookie = cookie_contents_string + ((expiry_days == null) ? "" : "; expires="+expiration.toGMTString() + ";path=/");

		//enable fade in/out to indicate a client-side change		
		added = true;		
	}

	//rebuild the dynamic quicklinks menu
	build_quicklinks();	
	
	//indicate on screen that the user's actions have added a new item to the quicklinks
	if(added){
		yellow_fade.fade_element("quicklinks_trigger", 30, quicklinks_fade_duration, quicklinks_fade_initial_hex, quicklinks_fade_hex);
		setTimeout("yellow_fade.fade_element('quicklinks_trigger', 30, " + quicklinks_fade_duration + ", '" + quicklinks_fade_hex + "', '" + quicklinks_fade_initial_hex + "')",quicklinks_fade_duration);
		
		//hide any overlib elements onmouseout of currently selected "add to quicklinks" item
		nd();
		
		//added 19-06-2007, Martin Burford, Modem Media
		//fix to ensure a double-click on the Quicklinks header isn't required
		show_hide_quicklinks(this,'');
		show_hide_quicklinks(this,'');
	}
}

function delete_all_quicklinks(){
	if(confirm("Are you sure you want to delete all items from My Quicklinks?")){
		//add new cookie content
		var expiration = new Date();
		expiration.setDate(expiration.getDate() + 0);
		document.cookie = "quicklinks=" + ((0 == null) ? "" : "; expires="+expiration.toGMTString() + ";path=/");	
		
		//hide any open overlib layers
		nd();
	
		//rebuild the dynamic quicklinks menu
		build_quicklinks();
		
		//added 19-06-2007, Martin Burford, Modem Media
		//fix to ensure a double-click on the Quicklinks header isn't required
		show_hide_quicklinks(this,'');		
	}
}

//identify which type of browser is being run
function browser_type(){
	switch(navigator.appName){
		case "Microsoft Internet Explorer":
			return "ie";
			break;
		case "Netscape":
			//this covers both Netscape and more importantly, Firefox
			return "netscape";
			break;
		case "Opera":
			return "opera";
			break;
	}
}

//if a h4 animated toggle group has a link, this will ensure the link is only loaded when the group is expanded
function toggle_header_link(obj,url){
	var content_display = obj.parentNode.className.replace(" ","");
	
	switch(content_display){
		case "contracted":
			var new_window = window.open(url,'popup');	
			
			if(window.focus){
				new_window.focus();
			}
			return false;
			
			break;
	}
}

//added 22-06-2007, Scott Livingstone
//function to make external links open in a new window - replacing target="_blank"
function externalLinks(){
	if(!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for(var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

//updated 19-06-2007, Martin Burford, Modem Media
//useful function to use for testing the limit of quicklink items possible from one user event
function autofill_quicklinks(){
	alert("Populating quicklinks to maximum of " + allowed_quicklink_items);
	
	set_cookie('0-0_home',365);
	set_cookie('1-0_news_blogs',365);
	set_cookie('1-1_news',365);
	set_cookie('1-2_blogs',365);
	set_cookie('1-3_techtalk',365);
	set_cookie('2-0_retirement',365);
	set_cookie('2-1_personal_pensions',365);
	set_cookie('2-1-1_personal_pensions_features',365);
	set_cookie('2-1-2_personal_pensions_available_funds',365);
	set_cookie('2-1-3_personal_pensions_detailed_information',365);
	set_cookie('2-1-4_personal_pensions_online_illustration',365);
	set_cookie('2-1-5_personal_pensions_apply_online',365);
	set_cookie('2-2_stakeholder_pensions',365);
	set_cookie('2-2-1_stakeholder_pensions_features',365);
	set_cookie('2-2-2_stakeholder_pensions_available_funds',365);
	set_cookie('2-2-3_stakeholder_pensions_detailed_information',365);
	set_cookie('2-2-4_stakeholder_pensions_online_illustration',365);
	set_cookie('2-2-5_stakeholder_pensions_apply_online',365);
	set_cookie('2-3_retirement_account',365);
	set_cookie('2-3-1_retirement_account_what_does_it_do',365);
}
