/* Pull-down menu javascript
Version 1.6
Coded By Javid Ali for 3rd Time Media
Copyright (c) 2010

Picks up menu and allows for arrow. Includes styling of menu for pd functioning. 
However, Arrow Y position and submenu X & Y positions has to be CSSd relative to li tags. Arrow class is: 'pdarrow' and pdarrow.gif is image
Class 'menu_ignore' to be used for excluding LIs as part of it's classes
*/

//styles to override non-js menu
//level 2 tabs background images
document.write('<style type="text/css"> .tabs .tabHasSubs .tabOn  span.left { background: url("img/tabs/tab.active.gif") center 30px no-repeat; }  .tabs .tabHasSubs span.right {  background: url("img/tabs/hassubs.gif") top right no-repeat; } .tabs .tabHasSubs .tabOn span.right { color: #FFF; background: url("img/tabs/hassubs.gif") top right no-repeat; }  .tabs .tabHasSubs span.left { background: none; } ');
//lower tabs
document.write('.nojstabs{display:none}</style>');




var menu_fads = new FadeList();
function menu_eventiser(menuid,arrow,zind,imgsrc) { // Read menu ul and process events and objects - id of ul,show arrow?,default z index
	var root = present(menuid);
	if(!root||!GoodBrowser) return;
	var mencountsub, mencount,men,zind,tmpstrm,mencountsubdiv,arrowml;
	pdmitems = new Array();
	zind=zind?zind:1;
	men = root.childNodes;
	//loop through ul
	mencount = 0; //counter for actual array of top lis
	mencountsub; //temp variable to hold child menu
	for(var i=0;i<men.length;i++) {
		if(men[i].nodeType==1)
			if(men[i].tagName.toLowerCase()=='li'&&men[i].className.indexOf('menu_ignore')<0) {
				men[i].style.position = 'relative';
				mencountsub = men[i].getElementsByTagName('ul');
				men[i].style.zIndex=zind;
				mencountsub = mencountsub.length>0?mencountsub[0]:false;
				mencountsubdiv=false;
				if(mencountsub) { //add nested div and extra arrow 
					tmpstrm = mencountsub.innerHTML;
					arrowml=arrow?'<img src="' + imgsrc + 'pdarrow.gif" alt="V" id="pdarrow' + mencount + '" class="pdarrow">':'';
					tmpstrm = arrowml + '<div>' + tmpstrm + '</div>';
					mencountsub.innerHTML = tmpstrm;
					mencountsubdiv = mencountsub.getElementsByTagName('li')[0].parentNode;
					IE?mencountsubdiv.style.filter = 'alpha(opacity=0)':mencountsubdiv.style.opacity = '0';
					mencountsub.style.position = 'absolute';
					//style the arrow
					if(arrow) {
						present("pdarrow"+mencount).style.position='absolute';
						present("pdarrow"+mencount).style.left=Math.floor((men[i].offsetWidth/2)-(present("pdarrow"+mencount).offsetWidth/2)-2) + 'px';
					} 
				} 
					
				//set object
				pdmitems.push({li:men[i],div:mencountsubdiv,wid:men[i].offsetWidth,ope:false,cn:men[i].className});//add to object array
				
				//set events
				men[i].onmouseover = mencountsub.onmouseover = new Function('menu_show(' + mencount + ',true,false,' + zind + ');');
				men[i].onmouseout = mencountsub.onmouseout =  new Function('menu_show(' + mencount + ',false,false,' + zind + ');');
			
				//increment actual array counter
				mencount++;				
			} //is li tag and not ignored

	} //for loop
} //function

function menu_show(ind,ope,timed,zind,aid) { 	//open or close a menu

	//Arrow
	if(!mtVar(present("pdarrow" + ind)))menu_addArrow(ind,ope);		
	
	//check for submenu, otherwise leave it
	if(!pdmitems[ind].div)return;
		
	//Switch classes on top level li tag - move this up to change class regardless of sub menu items.
	pdmitems[ind].li.className=ope?pdmitems[ind].cn + ' menuon':pdmitems[ind].cn;
	
	//Ensure consistency over time to avoid flicker
	if(typeof pdmitems[ind].menutimer!=='undefined')clearTimeout(pdmitems[ind].menutimer);
	
	//avoid flicker
	if(pdmitems[ind].ope!=ope) timed=false; //reset timer if there was a change to ensure consistency
	pdmitems[ind].ope=ope;
	if(!timed) {
		pdmitems[ind].menutimer = setTimeout('menu_show(' + ind + ',' + ope + ',' + true + ',' + zind + ',"' + aid + '")',200);
		return;
	}
	
	//Avoid flicker by making sure of not doubling at any single stage (redraw)
	if(ope==pdmitems[ind].displaying) return;
	pdmitems[ind].displaying = ope;
	

	
	//Put it on top
	for(var i=0;i<pdmitems.length;i++)
		pdmitems[i].li.style.zIndex=i==ind?'50':zind;
	
	//fade open/close menu if time delay done and still in position
	menu_fads.addconc(pdmitems[ind].div,ope?1:-1,300,0,100,0,!ope?'pdmitems[' + ind + '].div.style.display="none";pdmitems[' + ind + '].div.parentNode.style.display="none";':'');
	
	//Avoid getting locked because of display none, and makesure active menu is showing.
	if(ope) {
		pdmitems[ind].div.parentNode.style.display='block'; //ensure its showing
		pdmitems[ind].div.style.display='block'; //ensure its showing
		for(var i=0;i<menu_fads.fades[0].length;i++)//Cancel display:none attributes of active fades with same object
		if(menu_fads.fades[0][i].obj==pdmitems[ind].div)menu_fads.fades[0][i].func=menu_doNothing;
	}
	menu_fads.start();
}
function menu_addArrow(ind,show) {
	var pdar = present('pdarrow' + ind);
	if(!pdar)return;
	pdar.style.display=show?'block':'none';
}
function menu_doNothing(){}
function recordOutboundLink(link, label) {
	try {
    
		_gaq.push(['_trackEvent', 'CTA Clicks - ID', THISPAGE, label]);
		setTimeout('_gaq.push(["_link", "' + link.href + '"])', 100); //linker version
		//setTimeout('document.location="' + link.href + '"', 100); //standard version
		
		//GWO conversion
		//var _gaq = _gaq || []; //Causes error
			//_gaq.push(['gwo._setAccount', 'UA-10006919-31']); 
			//_gaq.push(['gwo._setDomainName', '.cpp.co.uk']); 
			//_gaq.push(['gwo._trackPageview', '/1415362695/goal']); 
		//_gaq.push(['gwo._setAllowLinker', true]);
		//_gaq.push(['gwo._setAllowHash', false]);
		//End gwo	

	}catch(err){ document.location = link.href; }
}

function recordOutboundLinkCard(link, label) {
	amount = link.toString().indexOf('term=2')>-1?'85':'35';
  try {
    _gaq.push(['_trackEvent', 'CTA Clicks - Card - ' + amount, THISPAGE, label]);


		//GO Conversion script - applies to all links for CTA test
		_gaq.push(['gwo._setAccount', 'UA-10006919-31']);
		_gaq.push(['gwo._setAllowLinker', true]);
		_gaq.push(['_setDomainName', '.cpp.co.uk']);
		_gaq.push(['gwo._setAllowHash', false]);
		_gaq.push(['gwo._trackPageview', '/2816190410/goal']);

		setTimeout('_gaq.push(["_link", "' + link.href + '"])', 100); //linker version
		//setTimeout('document.location = "' + link.href + '"', 100); //standard version
  }catch(err){ document.location = link.href; }
}

function recordOutboundLinkAccount(label) {
	try {
    _gaq.push(['_trackEvent', 'CTA Clicks - Account Login', THISPAGE, label]);
  }catch(err){  }
}

function recordOutboundLinkAccountLink(link, label) { 
	try {
		_gaq.push(['_trackEvent', 'CTA Clicks - Account Login', THISPAGE, label]);
		setTimeout('_gaq.push(["_link", "' + link.href + '"])', 100); //linker version
		//setTimeout('document.location="' + link.href + '"', 100); //standard version
	}catch(err){ document.location = link.href; }
}

function recordOutboundLinkSocial(link, label) {
	try {
    _gaq.push(['_trackEvent', 'Footer Links - Social Media', THISPAGE, label]);
		window.open(link.href, 'OutboundWindow', 'location=1,status=1,scrollbars=1, width=980,height=700,menubar=1,resizable=1,toolbar=1');
  }catch(err){  }
}


//Search Form functions

function cppSearchSubmit() {
	document.location.href='search/?gs=' + present('cppSearchField').value;
}

/* Header ticker */
TICKLOCKFADE = TICKLOCKSLIDE = false;
CURRENTSLIDE=1;
TICKAUTOTIMER = 0;
TICKAUTO = true;
function parseTicker() {
		//set fader events
		infuseHeader();
}

function infuseHeader () {
  TICKLOCKFADE = true;
	funkyh = new Funky( 'funkyh', 'img/ticker/' );
	funkyh.init();
	var fadeTime = 700;
	
	fuseh1 = funkyh.infusion( 'tickFader1', fadeTime);
	fuseh1.imgs = [ 'ticker1.gif', 'ticker1.on.gif'];
	fuseh1.alts = [ '', '' ]; 
	present('tickFader1').onclick = new Function ('TICKAUTO = false; changeTicker(1)');

	fuseh2 = funkyh.infusion( 'tickFader2', fadeTime);
	fuseh2.imgs = [ 'ticker2.gif', 'ticker2.on.gif'];
	fuseh2.alts = [ '', '' ];
	present('tickFader2').onclick = new Function ('TICKAUTO = false; changeTicker(2)');

	fuseh3 = funkyh.infusion( 'tickFader3', fadeTime);
	fuseh3.imgs = [ 'ticker3.gif', 'ticker3.on.gif'];
	fuseh3.alts = [ '', '' ];
	present('tickFader3').onclick = new Function ('TICKAUTO = false; changeTicker(3)');

	fuseh4 = funkyh.infusion( 'tickFader4', fadeTime);
	fuseh4.imgs = [ 'ticker4.gif', 'ticker4.on.gif'];
	fuseh4.alts = [ '', '' ];
	present('tickFader4').onclick = new Function ('TICKAUTO = false; changeTicker(4)');

	fuseh1.callback = fuseh2.callback = fuseh3.callback = fuseh4.callback = function() { TICKLOCKFADE = false; };
	
	funkyh.infusion_start( fuseh1 );
	funkyh.infusion_start( fuseh2 );
	funkyh.infusion_start( fuseh3 );
	funkyh.infusion_start( fuseh4 );
	funkyh.infusion_goto( fuseh1, 1);
	
	//IE bug fix
	fuseh1.htimgs[0].style.height = fuseh1.htimgs[1].style.height = fuseh2.htimgs[0].style.height = fuseh2.htimgs[1].style.height = fuseh3.htimgs[0].style.height = fuseh3.htimgs[1].style.height = fuseh4.htimgs[0].style.height = fuseh4.htimgs[1].style.height = '62px';
	
	TICKAUTOTIMER = setTimeout('changeTicker(2)', 5000);
}

function changeTicker(tickerNo) {
	clearTimeout(TICKAUTOTIMER);
	if(TICKLOCKFADE||TICKLOCKSLIDE) return;
	if(CURRENTSLIDE==tickerNo) return;
	TICKLOCKFADE = TICKLOCKSLIDE = true;
	
	
	funkyh.infusion_goto(funkyh.obj[CURRENTSLIDE-1],0);
	funkyh.infusion_goto(funkyh.obj[tickerNo-1],1);
	slideTicker((240*tickerNo)-960, tickerNo);
	var next = tickerNo < 4 ? tickerNo + 1 : 1;
	if(TICKAUTO) TICKAUTOTIMER = setTimeout('changeTicker(' + next + ')', 5000);
}

function slideTicker(pos,c) {
	var cpos = (240*CURRENTSLIDE)-960;
	cpos < pos ? doSlideTickerForward(cpos,pos) : doSlideTickerBackward(cpos,pos);
	if(cpos == pos) TICKLOCKSLIDE = false;
	CURRENTSLIDE = c;
}

function doSlideTickerForward(cpos,targ) {
	if(cpos < targ) cpos += 20;
	if(cpos > targ) cpos = targ;
	present('tickSlider').style.left = cpos + 'px';
	if(cpos == targ) TICKLOCKSLIDE = false;
	else 	setTimeout('doSlideTickerForward(' + cpos + ',' + targ + ')',20);
}

function doSlideTickerBackward(cpos, targ) {
	if(cpos > targ) cpos -= 20;
	if(cpos < targ) cpos = targ;
	present('tickSlider').style.left = cpos + 'px';
	if(cpos == targ) TICKLOCKSLIDE = false;
	else 	setTimeout('doSlideTickerBackward(' + cpos + ',' + targ + ')',20);
}

//Secure account login
var DOCLOSE = true;
function secureAccountHolderLinkClick() {
	secureAccountOpen();
	setTimeout('DOCLOSE=false',10);
	document.getElementsByTagName('body')[0].onclick = secureAccountBodyClick;
}

function secureAccountBodyClick() {
	DOCLOSE = true;
	setTimeout('secureAccountClose()',50);
}

function secureAccountButtonClick() {
	setTimeout('DOCLOSE=true',20);
}

function secureAccountDivClick() {
	setTimeout('DOCLOSE=false',10);
}

function secureAccountCloseClick() {
	setTimeout('DOCLOSE=true',20);
}

fads = new FadeList();
function secureAccountOpen() {
	var obj = present('secureAccount');
	IE? obj.style.filter = 'alpha(opacity=0)' : obj.style.opacity = '0';
	obj.style.display = 'block';
	fads.addconc(obj,1,200,0,100);
	fads.start();
	recordOutboundLinkAccount('Login Open');
}

function secureAccountClose() {
	if(!DOCLOSE) return;
	document.getElementsByTagName('body')[0].onclick = null;
	fads.addconc(present('secureAccount'),-1,200,0,100,null,null,null,true);
	fads.start();
	recordOutboundLinkAccount('Login Close');
}

function showOpening(el, on) { //used in footer ie6 bug fix
	if(!ie6)return;
	el.getElementsByTagName('span')[0].style.display = on? 'block' : 'none';
}
