function prinav () {
	$('#nav > li').hover(
	
		function(){$(this).addClass('hovered'); $('#nav > #' + $(this).attr('id') + ' > ul').css('left','0');}, 
		function(){$(this).removeClass('hovered'); $('#nav > #' + $(this).attr('id') + ' > ul').css('left','-999em');}
	);
	
    $('#nav > li > ul > li.popout').hover(
		function() {$(this).addClass('hover');},
	 	function() {$(this).removeClass('hover');
    });	// This removes the need for HTC fix csshover.htc
	
	$('#nav > li > a').focus(
		function(){$(this).parent().addClass('hovered');}
	);
	$('#nav > li > a').blur(
		function(){$(this).parent().removeClass('hovered');}
	);
		
	$('#nav > li > ul > li > a').focus(
		function(){$(this).parent().parent().parent().addClass('hovered');}
	);
	$('#nav > li > ul > li > a').blur(
		function(){$(this).parent().parent().parent().removeClass('hovered'); $('#nav > #' + $(this).attr('id') + ' > ul').css('left','-999em');}
	);
	$('#nav > li > ul > li.popout > a').blur(
		function (){
			$(this).parent().removeClass('popoutfocus'); // removes the grey line in the li		
			}
			
	);
	$('#nav > li > ul > li.popout > a').focus(
		function(){
			$(this).parent().parent().find('li ul.popoutfocus').removeClass('popoutfocus'); //This is what removes the ul (it bloody works)
			$(this).next().addClass('popoutfocus'); //This adds what is removed above to the current ul
			$(this).parent().css('position','relative'); //This puts the UL next to the parent li
			$(this).parent().addClass('popoutfocus'); // adds the grey line in the li
			}
	);
	
	
	
	
	// Vertically align the text if its spreads onto 2 lines
	
	$('#nav > li > a').each ( function(){
				var getHeight = $(this).height();
				if(getHeight >= 20){
					$(this).css('margin-top', '9px')
				}
			}
		);
				
}

//For c118 and c170


function hideMyAcc() {
	$('#accountinfo > dt.exp').addClass('expander');
	$('#accountinfo > dt.expander').addClass('closed');
	$('#accountinfo > dd').hide();
//	$('#accountinfo > dt.expander > a').removeAttr('href');
	$('#accountinfo > dt.expander.closed').css('cursor','pointer').click(function () {
		$(this).next('dd').toggle('slow').siblings('dd:visible').slideUp('normal');
		$(this).toggleClass('closed').siblings('dt.expander').addClass('closed');
		return false;
	});
	return false;
	
//	$('#accountinfo > dt.close').css('cursor','pointer').click(function () {
//		$(this).siblings('dd:visible').slideUp('fast');
//		$(this).slideUp('slow');
//		$('#accountinfo > dt').removeClass('opened');
//	});
};

//function gamepromo() {
//	$('.iwggamelist > li').hover(function (){
		//$(this).children('img').slideToggle('normal');
//	});
//};


function popupwindow() {
	$('.popup').attr("target","_blank");
}

function showprintbtn() {
	$('.printlink').css('display','inline');
	$('h2 > .primary').css('display','inline');
}

function showDDbreakdown(){
	$('#openSchedule').css('display', 'inline');
}

function moveflashcontainer(){
	//$('#flashContainer').css('display', 'block');
	//$('#contentwrapper').css('background', 'none'); <--- THIS HIDES BACKGROUND IMAGE FOR TRANSPARENT FLASH
	//$('#content').css('position', 'relative');
	//$('#content').css('overflow', 'visible');
}

function insertAnchorTags(parentClassName, href, anchorText, tabindex) {
    var elements = getElementsByClassName(document, "*", parentClassName);
    for ( var i= 0 ; i < elements.length; i++ ) {
    	while(elements[i].firstChild) {
			elements[i].removeChild(elements[i].firstChild);
		}
	
		eAnchor = document.createElement("a");
		eAnchor.setAttribute("href",href);
		if (tabindex != null) { eAnchor.setAttribute("tabindex",tabindex + i); }
		eAnchor.appendChild(document.createTextNode(anchorText));
    	elements[i].appendChild(eAnchor);

	}
}

function getElementsByClassName(oElm, strTagName, strClassName) {
  var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  strClassName = strClassName.replace(/\-/g, "\\-");
  var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  var oElement;
  for(var i=0; i<arrElements.length; i++){
    oElement = arrElements[i];      
    if(oRegExp.test(oElement.className)){
      arrReturnElements.push(oElement);
    }   
  }
  return (arrReturnElements);
}
function checkEmptyHeaders() {
		$('.c84 .header h2 a').each(function(index) {
			if($(this).text() == "")
			{
				$(this).parent().parent().css('display','none');
				$(this).parent().parent().next().css('border-top','1px solid #dedede');
			}
		})
}

function printpage(){
	$('span.printlink').html('<a href="javascript:window.print()"><img src="player/g/bg_a_print.gif" />Print this page</a>');
}

// This must be a path to a blank image. That's all the configuration you need.
if (typeof blankImg == 'undefined') var blankImg = 'player/g/blank.gif';

function pngFix()
{
	if ($.browser.msie && $.browser.version < 7) {
	$('img.ie6').each(function(index) {
		var comp = "";
		var substring = $(this).attr('class');
		var result = substring.split(/\W+/);
		for (var i = 0; i < result.length; i++)
		{
			if (result[i].match(/^c\d/))
			{
				comp = result[i];
			}
		}
		var src = $(this).attr('src').replace('player/i/logo/', 'player/i/logo/'+comp+'_');
			$(this).attr({
				'src': src
			});
	})
	$('img').each(function(index) {
		var src = $(this).attr('src').replace('.png', '.gif');
			$(this).attr({
				'src': src
			});
	})

	}
}


function sifrCheck()
{
	if ($("h2.dreamrg").length > 0) {
		var addPaths = '<link rel="stylesheet" href="player/styles/sifr/sIFR-screen.css" type="text/css" media="screen"/><link rel="stylesheet" href="player/styles/sifr/sIFR-print.css" type="text/css" media="print"/><script src="player/scripts/3l/sifr/sifr.js" type="text/javascript"></script><script src="player/scripts/3l/sifr/sifr-debug.js" type="text/javascript"></script><script src="player/scripts/3l/sifr/sifr-config.js" type="text/javascript"></script>';
		$('head').append($(addPaths));
	}
}

function inputRollover()
{
	$('input').filter(function()
	{
		return this.type.match('image');
	}).each(function()
	{
		if((this.id != "iAC-accredited") && (this.id != "addToBasketFirst")) // non-Camelot image
		{
			$(this).hover(
			function()
			{
				this.src = this.src.replace(/.gif/,"_rollover.gif");
			},
			function()
			{
				this.src = this.src.replace(/_rollover.gif/,".gif");
			});
		}
	});
}

function hideh2()
{
	$('#infopage h2').addClass("hide");
}

function buttonRollover()
{
	if ($.browser.msie && $.browser.version < 7)
	{
		$('button.primary').hover(
		function()
		{
			if (($(this).parent().parent().attr('class') != "step5") && ($(this).parent().parent().attr('class') != "playslipoverflow"))
			{
				$(this).css('backgroundImage', 'url(player/g/buttons/btn_pri_rollover.gif)');
				$(this).find('span').css('backgroundImage', 'url(player/g/buttons/btn_pri_rollover_rhs.gif)');
			}
		},
		function()
		{
			if (($(this).parent().parent().attr('class') != "step5") && ($(this).parent().parent().attr('class') != "playslipoverflow"))
			{
				$(this).css('backgroundImage', 'url(player/g/buttons/btn_pri_inactive.gif)');
				$(this).find('span').css('backgroundImage', 'url(player/g/buttons/btn_pri_inactive_rhs.gif)');
			}
		});
		$('button.primarySmall').hover(
		function()
		{
			$(this).css('backgroundImage', 'url(player/g/buttons/btn_pri_rollover.gif)');
			$(this).find('span').css('backgroundImage', 'url(player/g/buttons/btn_pri_rollover_rhs.gif)');
		},
		function()
		{
			$(this).css('backgroundImage', 'url(player/g/buttons/btn_pri_inactive.gif)');
			$(this).find('span').css('backgroundImage', 'url(player/g/buttons/btn_pri_inactive_rhs.gif)');
		});
		$('button.gobutton').hover(
		function()
		{
			$(this).css('backgroundImage', 'url(player/g/buttons/btn_square_go_rollover.gif)');
		},
		function()
		{
			$(this).css('backgroundImage', 'url(player/g/buttons/btn_square_go.gif)');
		});
	}
}

function NeedsMouseOver()
{
		$(".needsmouseover").css("cursor", "pointer");	
		$(".needsmouseover").hover(
			function()
			{
					// If IE6 swap out the logos and adjust the settings of the background
					// IE6 doesnt support css :hover so we need Javascript to do this
					if ($.browser.msie && $.browser.version < 7) {
						myString = new String($(this).attr("class"))
						if (myString.indexOf("hero_hh_template_button") != -1 ) { //Not false				
							var theimg = $(this).find('img');
							// Some buttons dont have an inline image so check for this before the replace
							if ($(theimg).attr('src') != null) {
								var origsrc = $(theimg).attr('src');
								var src = $(theimg).attr('src').replace('player/i/promos_heroes/', 'player/i/promos_heroes/ie6over_');
								$(theimg).attr({'src': src});
							}
						}					
					$(this).css("background-position", "bottom left");
					}

				// Underline the hyperlink on mouse over of the outer div container
				$(this).find("a").css("text-decoration", "underline").css("color", "#fff");
				$(this).find("p").css("color", "#fff");					
			},
			function()
			{
					if ($.browser.msie && $.browser.version < 7) {
						myString = new String($(this).attr("class"))
						if (myString.indexOf("hero_hh_template_button") != -1 ) { //Not false					
							var theimg = $(this).find('img');
							if ($(theimg).attr('src') != null) {
								var origsrc = $(theimg).attr('src');
								var src = $(theimg).attr('src').replace('player/i/promos_heroes/ie6over_', 'player/i/promos_heroes/');
								$(theimg).attr({
									'src': srcs
								});
							}
						}					
					$(this).css("background-position", "top left");
					}

				$(this).find("a").css("text-decoration", "none").css("color", "#333");
				$(this).find("p").css("color", "#333");					
			}
		);
		// Get the href of the a link and apply it to the outer div
		$(".needsmouseover").click(function(){
			window.location = $(this).find("a").attr("href");
		});				
}




function london2012(){
	var countdown = new Object();
	
	countdown.today = new Date();
	countdown.todayEpoch = countdown.today.getTime();

	countdown.targetDate = new Date("27 July, 2012");
	countdown.targetEpoch = countdown.targetDate.getTime();

	countdown.daysLeft = Math.ceil((countdown.targetEpoch - countdown.todayEpoch) / (60*60*24) / 1000);

	countdown.display = function() {
		$('<div class="countdown"><span class="daysleft">'+countdown.daysLeft+'</span><span class="daystogo">Days to go</span></div>').insertBefore('.thumbs');
	}
	
	countdown.display();
	
	
	// Apply countdown bg
	
	var charLength = $('.london2012 .countdown .daysleft').text().length;
	
	$('.london2012 .countdown').addClass('bg'+ charLength);
	
	
	// Image viewer

	$('.thumbs a').bind('click', function(){
		

	var imageurl = $(this).attr('href');
	var headertext = $(this).find('img').attr('alt');
	
	$(this).parent().parent().parent().find('.large').attr('src', imageurl);
	$(this).parent().parent().parent().find('.heading_bg p').text(headertext);
		
		return false;
		
	});
	
	
	
	

	


}

$(document).ready(function() {
	hideMyAcc();
	prinav();
	popupwindow();
	showprintbtn();
	showDDbreakdown();
	moveflashcontainer();
	pngFix();
	checkEmptyHeaders();
	printpage();
	sifrCheck();
	inputRollover();
	hideh2();
	buttonRollover();
	NeedsMouseOver();
	
	
	if ($(".london2012").length > 0) {
	london2012();
	}
	
});
