var initFontSizeChanger = function () {
	var baseFontSize = '62.75';
	if (!$.cookie("dffAccessibilityBaseFontSize")) {
		$.cookie("dffAccessibilityBaseFontSize", baseFontSize);
	}
	var baseFontSizeCurrent = $.cookie("dffAccessibilityBaseFontSize");
	if (baseFontSizeCurrent != baseFontSize) {
		$('body').css({ fontSize: baseFontSizeCurrent+'%' });
	}
	
	// Display the buttons
	$('.accessibilityControls').css({ display: 'inline' });
	
	// Initialise the buttons
	$('.accessibilityControls a').each(function () {
		$(this).click(function() {
			switch(this.className) {
				case 'decrease':
					baseFontSizeCurrent = Number(baseFontSizeCurrent) - 5;
					if (baseFontSizeCurrent < 40) {
						baseFontSizeCurrent = 40;
					}
					break;
				case 'increase':
					baseFontSizeCurrent = Number(baseFontSizeCurrent) + 5;
					if (baseFontSizeCurrent > 80) {
						baseFontSizeCurrent = 80;
					}
					break;
				case 'normal':
				default:
					baseFontSizeCurrent = baseFontSize;
					break;
			}
			$.cookie("dffAccessibilityBaseFontSize", baseFontSizeCurrent);
			$('body').css({ fontSize: baseFontSizeCurrent+'%' });
		});
	});
}

var initFlash = function () {
	var validTags = new Array('img');//'div', (Not sure how to get the src / href from div)
	for (var i = 0; i < validTags.length; i++) {
		$(validTags[i] + '.dynamicFlash').each(function () {
			if (!this.id || this.id == '') {
				this.id = 'dynamicFlash_' + i;
			}
			var src = this.src.replace(/\.(gif|jpg|png|jpeg)$/i, '.swf');
			
			// Check if parent is an anchor
			var bannerLink = '';
			var elements = $(this).parents();
			for (var j in elements) {
				if (typeof elements[j] != 'function') {
					if (elements[j].tagName == 'A') {
						bannerLink = elements[j].href;
						break;
					}
				}
			}
			
			
		
			var a = $(this).parent().get(0);
			
			
			$(a).after('<div id="dynamicFlash_div_'+i+'">'+src+'</div>');
			
			var div = $('#dynamicFlash_div_'+i);
			
			var so = new SWFObject(src,'swf_'+i,$(this).width(),$(this).height(),8);
			so.addVariable('bannerLink',bannerLink);
			so.addParam('wmode','transparent');
			if(so.write('dynamicFlash_div_'+i)){
				$(a).remove();
			}
			
			
			
		});
	}
}

var initListTreeExpanding = function () {
	// Quick check to see if component is used
	if ($('dl.listTreeExpanding').length < 1) {
		return false;
	}
	
	$('dl.listTreeExpanding dd').hide();
	$('dl.listTreeExpanding dt').attr('class', 'closed');
	$('dl.listTreeExpanding dt').click(function () {
		if ($(this).next().is('dd')) {
			$(this).next().toggle('fast', function () {
				if ($(this).css('display') == 'none') {
					$(this).prev().attr('class', 'closed');
				} else {
					$(this).prev().attr('class', 'opened');
				}
			});
			return false;
		}
		return true;
	});
}

var initContentPromoBoxTabs = function () {
	// Quick check to see if component is used
	if ($('div.contentPromoBox.withTabs').length < 1) {
		return false;
	}
	
	// List of all the anchor elements
	var contentPromoBoxTabs = new Array();
	
	// A copy of the list of all the anchor elements
	var contentPromoBoxTabsCopy = new Array();
	
	// Load all the links
	$('div.contentPromoBoxTabs a.tab').each(function () {
		var hash = this.hash.match(/^\#(.+)/);
		if (hash) {
			contentPromoBoxTabs.push(hash[1]);
			contentPromoBoxTabsCopy.push(hash[1]);
			$(this).click(function () {
				// Show the tab and hide others
				$('div.contentPromoBoxTabs a.tab').attr('class', 'tab');
				for (var i = 0; i < contentPromoBoxTabs.length; i++) {
					if (contentPromoBoxTabs[i] == hash[1]) {
						$('#' + contentPromoBoxTabs[i]).show();
						$(this).attr('class', 'tab active');
					} else {
						$('#' + contentPromoBoxTabs[i]).hide();
					}
				}
				
				if (window.event) { window.event.cancelBubble = true; }
				return false;
			});
		}
	});
	
	// Attach the detected tab links to the tabs in the order it appears in the html
	$('div.contentPromoBox.withTabs').each(function () {
		$(this).attr('id', contentPromoBoxTabsCopy.shift());
	});
	
	// Hide all the tabs except first one
	for (var i = 1; i < contentPromoBoxTabs.length; i++) {
		$('#' + contentPromoBoxTabs[i]).hide();
	}
}

var initCloseButton = function () {
	// Quick check to see if component is used
	if ($('a.closeButton').length < 1) {
		return false;
	}
	
	$('a.closeButton').css({ display: 'inline' });
	$('a.closeButton').click(function () {
		window.close();
	});
}

var initInputDefaultText = function () {
	// Quick check to see if component is used
	if ($('input[type="text"].defaultText').length < 1) {
		return false;
	}
	
	$('input[type="text"].defaultText').each(function () {
		if ($(this).attr('class').indexOf('defaultTextIdle') == -1) {
			$(this).attr('class', $(this).attr('class') + ' defaultTextIdle');
		}
		
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	$('input[type="text"].defaultText').focus(function () {
		$(this).attr('class', $(this).attr('class').replace(' defaultTextIdle', ''));
		
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('input[type="text"].defaultText').blur(function () {
		if ($(this).attr('class').indexOf('defaultTextIdle') == -1) {
			$(this).attr('class', $(this).attr('class') + ' defaultTextIdle');
		}
		
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
}

var initPrint = function () {
	if (window.print) {
		$('p.print').show();
		$('p.print a').click(function () {
			window.print();
			if (window.event) { window.event.cancelBubble = true; }
			return false;
		});
	}
}

var initIe6Fixes = function () {
	// Fix caching issue in ie6
	document.execCommand("BackgroundImageCache", false, true);
}


var initHorizontalNavigationHovers = function (){

	 $(".navigationHorizontal ul.subnav li").hover(
	 	function (){
	 		$('ul',this).show();
	 	},
	 	function (){
			$('ul',this).hide();
	 	}
	 );
}

/**
* for IE 6 remove the main nav and add it again at the end of the document
* which forces IE to redraw the nav and drop-down menu's after everything else
* which solves the many issues in making sure the drop-downs always appear on
* top of everything els
* 
*/
var initIe6FixHorizontalNavigation = function (){
	//alert($().jquery);
	if ( $.browser.msie ){
		//MICROSOFT IE
		var majorversion = parseInt($.browser.version );
		if(majorversion==6){
		
			// select main things we need
			var nav = $(".navigationHorizontal");
			var pag = $("#page");
			
			// record the position of the navigation element
			var pos = nav.position();
			
			// clone the navigation block
			var nav2 = nav.clone();
			
			// set the position to the same as it was before
			nav2.css({'position':'absolute','top':(pos.top-3)+'px','left':pos.left+'px','width':'960px'});
			
			// add the clone to the end of the page tag
			nav2.appendTo(pag);
			
			// hide the original nav
			nav.hide();

		}
	}
}

//DF Addition for Affiliates and hiding telephone numbers
function checkAffiliates() {
	var custom;
	if($.cookie("customphone")) {
		var custom=$.cookie('customphone');
	}
	else var custom=false;
	if($('div.rightBoxBlue').length<1) {
		return false;
	}
	if(getParameter('customphone') && getParameter('customphone')=='affiliate1') {
		$('div.rightBoxBlue').attr('style','display:none');
		$.cookie("customphone", 'affiliate1', { expires: 30, path: '/', domain: '.sunlifedirect.co.uk' }); //, { expires: 7, path: '/', domain: 'jquery.com', secure: true })
	}
	else {
		if(custom=="affiliate1") {
			$('div.rightBoxBlue').attr('style','display:none');
		}
	}
}

function getParameter(a) {
	tmp=location.search.split(a + "="); //Splits the query string by the parameter + =, e.g getParameter('param1') splits:
																			//e.g. www.sunlifedirect.co.uk/?param1=value1&param2=value2
																			//------------------------------------^ here
	if(tmp[1]) {  tmp=tmp[1].split("\&");  return tmp[0]; } //Splits again at the next ampersand and returns the first in the array.
	else { return false; }
}

function rewriteSillyLinks() {
	var linklist=document.getElementsByTagName('a');		
	var sillylinks="/https://";
	for(i=0;i<linklist.length;i++) {
		mylink=linklist[i].getAttribute('href',2);
		if(mylink!=null) {
			mylinkarr=mylink.split(sillylinks);
			if(mylinkarr.length==2) {
				linklist[i].setAttribute('href','https://' + mylinkarr[1]);
			}
		}
	}
	sillylinks="https://uat-livesite.axa.com/iw-cc/command/";
	for(i=0;i<linklist.length;i++) {
		mylink=linklist[i].getAttribute('href',2);
		if(mylink!=null) {
			mylinkarr=mylink.split(sillylinks);
			if(mylinkarr.length==2) {
				linklist[i].setAttribute('href', mylinkarr[1]);
			}
		}
	}
}

//End DF Additional functions


// Run startup scripts
$(document).ready(function () {
	initIe6FixHorizontalNavigation();
	initHorizontalNavigationHovers();
	initFontSizeChanger();
	initFlash();
	initListTreeExpanding();
	initContentPromoBoxTabs();
	initCloseButton();
	initInputDefaultText();
	initPrint();
	checkAffiliates(); //Added by DF
	rewriteSillyLinks(); //Added by DF
	
	if ($.browser.msie) {
		initIe6Fixes();
	}

	
});
