
/* check you are on plc staging or live */
var thisURL = document.location.href.toLowerCase();
if((thisURL.indexOf("btplc.bt.unitech.net") != -1) || (thisURL.indexOf("btplc.com") != -1))
{

/* Top Nav load START */

var strUrl = "/imports/menu.aspx";		
var strPath = document.location.pathname.toLowerCase();
if(strPath.indexOf("/thegroup") == 0)
	strUrl += "?parent=0";
else if(strPath.indexOf("/sharesandperformance") == 0)
	strUrl += "?parent=1";
else if(strPath.indexOf("/news") == 0)
	strUrl += "?parent=2";
else if(strPath.indexOf("/societyandenvironment") == 0 || strPath.indexOf("/responsiblebusiness") == 0)
	strUrl += "?parent=3";
else if(strPath.indexOf("/innovation") == 0)
	strUrl += "?parent=4";
else if(strPath.indexOf("/careercentre") == 0)
	strUrl += "?parent=5";	
else if(strPath.indexOf("/btlondon2012") == 0)
	strUrl += "?parent=6";	

// make the ajax request right away
$.ajax({
url: strUrl,
  success: function(data) {
	$(document).ready(function(){  // as soon as page has loaded, insert into ul#nav
		$("ul#nav").html(data);		
	});
  }
});

/* Top Nav load END */


/* Footer load */
var strFooterUrl = "/footer.xml";

$.ajax({
url: strFooterUrl,
dataType: "html",
 success: function(data) {
	$(document).ready(function(){  // as soon as page has loaded, insert into footer
		$("div#footerContent").html(data);	
		$("#shareWithLink").hover(
			function(){ $("#share_popup ul#share").show(); },  
			function(){ $("#share_popup ul#share").hide(); }           
		);
		$("ul#share").hover(
			function(){ $("#share_popup ul#share").show();  },
			function(){ $("#share_popup ul#share").hide();  }
		);
	});
  }
});

/* Footer load end */

}


$(document).ready(
function(){ 
	//Search box (autocomplete etc.)

	$("#searchtext").val("Search");
	$("#searchtext").focus(function () { if($("#searchtext").val() == "Search") { $("#searchtext").val(""); } });
	//$("#searchtext").autocomplete("/search/autocomplete_ajax.cfm");
	$("#searchgo").click(function () { if($("#searchtext").val() == "Search") { $("#searchtext").val(""); } });
	$("#searchgo").keydown(function () { if($("#searchtext").val() == "Search") { $("#searchtext").val(""); } });

	//$("#searchtextbox").autocomplete("/search/autocomplete_ajax.cfm");
	//$("#simpleSearchBox").autocomplete("/search/autocomplete_ajax.cfm");

	// hide toggle divs on faqs page 
	$('.toggle').hide();

	// capture clicks on the faqs toggle links
	$('a.toggleLink').click(function() {
		// change the link depending on whether the element is shown or hidden
		if($(this).attr("id") == "selectedQuestion")
		{
		   $(this).removeAttr("id");
		}
		else
		{
		   $(this).attr("id", "selectedQuestion");
		}
		// toggle the display
		$(this).next('.toggle').toggle('fast');
		 
		// return false so any link destination is not followed
		return false;
	});

	//if coming back from the feedback page then load the thank you page
	if (document.location.href.indexOf('?feedbackmsg=thankyou') > -1) {
		//wait for the content to load first and then replace with custom message
		setTimeout(function() {     $("#theboardtext").html("<h1>Thank You</h1><p>Thank you for your time in filling out the Feedback form.<br></p>", null ); }, 500);
	}
});

function LoadBoardContent(link) {
	var board = $("#theboardtext");
	var pageUrl = link.attr("href");

	$.ajax({
		url: pageUrl,
		success: function(data) {
			var centreColumn = $("#centre_column", data);
			if (centreColumn.length == 1) {
				data = centreColumn;
				//alert('using centre column');
			}
			board.html(data);
			
			var links = $("a", board);

			//alert('Links: ' + links.length);

			links.click(function() {
				//alert('link clicked');
				LoadBoardContent($(this));
				return false;
			});
		}
	});

	return false;
}

/* Add web trends js */
/*
if(thisURL.indexOf("btplc.com") != -1)
{
document.write('<script type="text/javascript" src="/templates/js10/sdctag1.js"></script>');
document.write('<script type="text/javascript" src="/templates/js10/sdctag2.js"></script>');
}*/

/* Fontchanger */

var expdate = new Date ();
expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now
var font;
var colour;
var domain = document.domain;

init();
	
function init()
{	
	font = "";
	colour = "";
	
	if(getFontCookie()!="" || getColourCookie()!="")
	{
	    if(getFontCookie()!="")
	    {
	       font = getFontCookie();
	    }
	    if(getColourCookie()!="")
	    {
	       colour = getColourCookie();
	    }
	    /*setActiveStyleSheet(colour+font);*/
		setActiveStyleSheet(font);
	}
}

function setCookie(name, fontsize, expires)
{ 
   if(getColourCookie()!="")
   {
	    colour = getColourCookie();
   }
   else
   {
       colour = 'white';
   }
   
   if(fontsize == '')
   {
      fontsize = 'small';
   }
   
   font = fontsize;
   submit();
   location.reload();
}

function submit()
{
   createFontCookie(font);
   createColourCookie(colour);
}

function addStylesheet(title)
{
  document.write("<link type='text/css' rel='alternate stylesheet' href='/Templates/css10/PLCGeneral"+ title +".css' title='"+ title +"'/>");
}

function setActiveStyleSheet(title) {
   addStylesheet(title);
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
	   a.rel = "alternate stylesheet";
       if(a.getAttribute("title") == title)
		{
			a.disabled = false;
			a.rel = "stylesheet";
		}
     }
   }
}

function createFontCookie(fontvalue) {
	setNewCookie("fontsize", fontvalue);
}

function createColourCookie(colourvalue) {
	setNewCookie("fontcolor", colourvalue);
}

function setNewCookie(name, value)
{
   var expdate = new Date ();
   expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now
   if(domain == 'btplc.bt.unitech.net')
   {
     //Staging cookie string
     document.cookie = name + "=" +value+";expires=" +expdate.toGMTString() +"; path=/";
   }
   else
   {
     //Live cookie string
     document.cookie = name + "=" +value+";expires=" +expdate.toGMTString() +"; domain=.btplc.com; path=/";
   }
  
}

function getFontCookie() {
		return getCookie("fontsize");  		
}

function getColourCookie() {
		return getCookie("fontcolor");  		
}
function getCookie(cookieName) {
		// document.cookie gives us a list of all the cookies from this domain
		// we need to find just the cookie we are interested in
		// take a substring between "cookiename=" and ";" - this is the value of the cookie
		// this function will return either the value of the cookie, or a null value if it's not there
		var cookiename = cookieName;
  		var nameStr = cookiename + "=";
  		var maxLen = document.cookie.length
  		var i = 0
  		while (i < maxLen) {
    			var j = i + nameStr.length
    			if (document.cookie.substring(i,j) == nameStr) {
       				var cookieEnd = document.cookie.indexOf(";",j);
			
       				if (cookieEnd == -1) {
	          				cookieEnd = document.cookie.length;
       				}

					a=document.cookie.substring(j,cookieEnd);
				       return unescape(document.cookie.substring(j,cookieEnd));
    			}
    		i++
  		}
  		return "";
}
	
// do they accept cookies?
// simply drop a test cookie and try to retrieve it. if it's not there, they don't take cookies

function createTestCookie() {
	setNewCookie("test", '1');
}
	
function getTestCookie() {
		return getCookie("test");  		
}
	
function checkCookieAccept() { 
	
	createTestCookie();

	if (getTestCookie()=="test") { 
		return 1;
	}
	else { 
		return 0;
	}
}
