/*
	Version 1.27
	Dependencies: 
	3tm.common.js
	3tm.funky.js
*/

function infuseHome(card,phonesafe,yourlaw,homeprot) {
	drawInfusionControls(homeprot);
  funky = new Funky( 'funky', SITEROOT + 'img/' );
	funky.init();
	fuse = funky.infusion( 'fader', 700, 5000, true, true, true);
	fuse.imgs = card? [ 'card/fader1.jpg', 'card/fader2.jpg', 'card/fader3.jpg' ]:[ 'fader1.jpg', 'fader2.jpg', 'fader3.jpg' ];
	fuse.alts = card? [ 'Don\'t be a victim of card fraud', 'Protect your cards if they are lost or stolen', 'Fraud on lost & stolen cards totalled 47.9 million pounds in 2009' ] :[ 'Every year in the UK ID fraud affects over 1.8 million people' , 'It\'s your name. Don\'t let it fall into the wrong hands', 'No gimmicks. No free trials. Just expert help with looking after your identity'];
	if(phonesafe) {
		fuse.imgs = [ 'php/fader1.jpg', 'php/fader2.jpg', 'php/fader3.jpg' ];
		fuse.alts = [ 'Over 850,000 Brits experienced mobile phone theft in 2009', 'Don\'t panic if an accident happens', 'On average it takes 6 hours to report a mobile phone lost or stolen']
	}
	if(yourlaw) {
		fuse.imgs = [ 'ylhp/fader1.jpg', 'ylhp/fader2.jpg', 'ylhp/fader3.jpg' ];
		fuse.alts = [ 'Need impartial, legal advice but don\'t know where to turn?', 'Life\'s unpredictable. But don\'t worry', 'Facing Redundancy? Moving home? Wrangles with retailers?']
	}	
	if(homeprot) {
		fuse.imgs = [ 'homeprot/fader1.jpg', 'homeprot/fader2.jpg' ];
		fuse.alts = [ 'CPP Home Emergency','CPP Gas Central Heating Protection' ];
		fuse.callback = changePrice;
	}	
	fuse.par.infusion_start( fuse );
}

function drawInfusionControls(homeprot) {
	var s = '<img src="' + SITEROOT + 'img/fc1_on.gif" alt="1" id="fc1" onclick="funky.infusion_goto(fuse,0)" />';
	s += 		'<img src="' + SITEROOT + 'img/fc2.gif" alt="2" id="fc2"  onclick="funky.infusion_goto(fuse,1)" />';
	if(!homeprot)s +=    '<img src="' + SITEROOT + 'img/fc3.gif" alt="3" id="fc3"  onclick="funky.infusion_goto(fuse,2)" />';
	present( 'infusionControls' ).innerHTML = s;
}

function changePrice(inview) {
	present('rof1').style.display = inview == 0 ? 'block' : 'none';
	present('rof2').style.display = inview == 1 ? 'block' : 'none';	
}

/* Carousel */

function drawCarouselControls() {

var s = '<div><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,0)" class="caon">1</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,1)" class="">2</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,2)" class="">3</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,3)" class="">4</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,4)" class="">5</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,5)" class="">6</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,6)" class="">7</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,7)" class="">8</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,8)" class="">9</span><span onmouseout=\'if(this.className!="caon")this.className=""\' onmouseover=\'if(this.className!="caon")this.className="cahover"\' onclick="funky.carousel_moveBy(1,9)" class="">10</span><div class="clearBoth"></div></div>';

present ( 'carouselnumbers' ).innerHTML = s;

var s = '<div class="carouselLeft"><span class="off" onmouseover="if(this.className!=\'off\')this.className=\'hover\'" onmouseout="if(this.className!=\'off\')this.className=\'\'" id="carouselLeft" onclick="funky.carousel_click(0,1)"></span></div><div class="carouselRight"><span onmouseover="if(this.className!=\'off\')this.className=\'hover\'" onmouseout="if(this.className!=\'off\')this.className=\'\'" id="carouselRight" onclick="funky.carousel_click(1,1)"></span></div>'

present ( 'carouselArrows' ).innerHTML = s;
}

function makeCarouselWork() {
		drawCarouselControls();
		var obj = present( 'obj'); 
		lis = obj.getElementsByTagName('li');
		var ul = lis[0].parentNode;
		var finalHeight = 200;
		for ( var i = 0; i < lis.length; i++ ) {
			lis[i].style.position = 'absolute';
			lis[i].style.display = 'block';
			if ( lis[i].offsetHeight > finalHeight ) {
				finalHeight = lis[i].offsetHeight;
			}
		}
		ul.style.height = finalHeight + 'px';
		ul.style.zIndex = '0';
		ul.style.position = 'relative';
		lis = null;
		funky.carousel( obj, 190, 1, 1, true, 3000, 50, false, false, 'numberChanger(@inview@);', 200, true, 4, .30, 1.45 );
}

//carousel update numbers for HP testimonial
function numberChanger(x) {
	var numb = present("carouselnumbers").getElementsByTagName("span");
	for (var i = 0; i < numb.length; i++) {
		numb[i].className = i == x ? "caon" : "";
	}
	present ('carouselLeft').className = x==0 ? 'off' : present ('carouselLeft').className == 'hover' ? 'hover' : '';
	present ('carouselRight').className = x==9 ? 'off' : present ('carouselRight').className == 'hover' ? 'hover' : '';
}


/* For Flight Promo version of Card home only */

//reveal imported from old microsite then updated
//get heights and then hide all
function Od (ele) {
	return document.getElementById(ele);
}
function processallis(tid) { //tid is typesOfIdFraud
	allis = Od('reveal').getElementsByTagName('*');
	if(allis!==null)
	var shtml='';
	for(var i=0;i<allis.length;i++) {
		if(allis[i].tagName!==null) {
			if(allis[i].tagName.toLowerCase()=='li') {
				if(navigator.userAgent.indexOf('Chrome')<0)allis[i].firstChild.nextSibling.oh = allis[i].firstChild.nextSibling.offsetHeight; //variation factor
				allis[i].className='';
				var atag = allis[i].firstChild.firstChild;
				if(!tid)atag.firstChild.className = 'jStrong';
				var span = document.createElement ('span');
				span.innerHTML = 'Open';
				span.className = 'openclose';
				if(tid)
					atag.insertBefore(span,atag.lastChild);
				else
					atag.appendChild(span);
				if(!tid) {
					span = document.createElement('span');
					span.className = 'clearBoth';
					atag.appendChild ( span );
				}
			}
			if(allis[i].tagName.toLowerCase()=='a'&&allis[i].parentNode.parentNode.tagName.toLowerCase()=='li') {
				if(tid)
					allis[i].onclick = new Function('this.blur();reveal(this.parentNode.nextSibling,null,true);return false;');
				else
					allis[i].onclick = new Function('this.blur();reveal(this.parentNode.nextSibling);return false;');
				allis[i].href='javascript:void(0);';
			}
			if(allis[i].tagName.toLowerCase()=='div') {
				if(allis[i].oh!=null&&allis[i].oh>=10) {
					if(allis[i].oh>=10) allis[i].style.height='0px';
					allis[i].style.paddingBottom='0px';
					allis[i].style.overflow='hidden';
				}
			}
		}
	}
	if(present('openAll'))present('openAll').style.display = 'block';
}
//click event
CTEL = null; //Current Transitioning Element
SHOWALL = true;

function updateRevealAllDiv() {
	var lis = present('reveal').getElementsByTagName('li');
	SHOWALL = true;
	for(var i=0;i<lis.length;i++) {
		if(lis[i].className == 'active') SHOWALL = false;
	}	
	present('openAll').className = SHOWALL ? '' : 'close';
	present('openAll').innerHTML = SHOWALL ? 'Open all' : 'Close all';
}

function revealAll() {
	var show = SHOWALL;
	var lis = present('reveal').getElementsByTagName('li');
	for(var i=0;i<lis.length;i++) {
		lis[i].className = show ? 'active' : '';
		lis[i].firstChild.nextSibling.style.height = show ? 'auto' : '0px';
		lis[i].firstChild.nextSibling.style.paddingBottom = show ? '15px' : '0px';
		lis[i].firstChild.lastChild.previousSibling.innerHTML = show ? 'Close' : 'Open';

	}
	if(present('openAll'))updateRevealAllDiv() ;
}
function reveal(click,lind,tid) {
	//_trackEvent(EventCat, 'Clicked', click.parentNode.firstChild.firstChild.innerHTML);
	if(CTEL!=null) return;
	if (click.parentNode.className=='active') {
		if(click.oh<10||click.oh==null)click.parentNode.className = ''; else click.parentNode.className='active tempP';
		click.parentNode.firstChild.firstChild.lastChild.previousSibling.innerHTML = 'Open'
	}
	else { 
		if(typeof _gaq != 'undefined') {
				_gaq.push(['_trackEvent', EventCat, 'Clicked', click.parentNode.firstChild.firstChild.firstChild.innerHTML]);
		}
		click.parentNode.className = 'active';
		click.parentNode.firstChild.firstChild.lastChild.previousSibling.innerHTML = 'Close';
		click.style.paddingBottom = '0px';
	}
	if(click.oh>=10) {
		//do height transition
		CTEL = click;
		CTEL.ch = click.parentNode.className=='active'?0:CTEL.oh;
		heightChanger(click.parentNode.className=='active'?1:-1,lind==null?'':lind);
	} 
	if(present('openAll'))updateRevealAllDiv();
}
function heightChanger(d,lind) {
	CTEL.ch = CTEL.ch+(d*15);
	if(CTEL.ch<0&&d==-1)CTEL.ch = 0;
	if(CTEL.ch>CTEL.oh && d==1)CTEL.ch=CTEL.oh;
	CTEL.style.height=CTEL.ch+'px';
	if((CTEL.ch>0&&d==-1) || (CTEL.ch<CTEL.oh && d==1))
		setTimeout("heightChanger(" + d + ",'" + lind + "')", 50);
	else {  if(d==-1)CTEL.parentNode.className='';CTEL = null; 
		if(lind!=''&&d==-1) {
			LIs[lind].el.style.backgroundColor=LIs[lind].off?"":"#666666";
			LIs[lind].el.firstChild.style.color=LIs[lind].off?"":"#FFFFFF"; 
		} 
	}
}

/* End for flight promo version of card home*/
