$(document).ready(function() {

	$('.sitepromo').hover(function(){
		videoLoader($('input.video_url',this).val());
	});

	$('#awards_promo h3, #speedtest_promo h3').clone().addClass('hover').prependTo('#awards_promo, #speedtest_promo');
	$('#awards_promo, #speedtest_promo').hover(function(){
		$(this).addClass('hover').find('.new_button').removeClass('disabled_button').addClass('red_big');
		$(this).find('.new_button').addClass('hover');
	},function(){
		$(this).removeClass('hover').find('.new_button').removeClass('red_big').addClass('disabled_button');
		$(this).find('.new_button').removeClass('hover');
	});
	
	$('#home_box2 h2').click(function(){$('#home_box2 #checker_form #postcode').focus();});
/*
	$('#checker_form').submit(function(){
		//Save entered postcode in cookie
		var location = $('#checker_form input#postcode').val();
		var options = { path: '/', expires: 10 };
		$.cookie('location',location, options);
	});
*/
	var in_progress = false;
	$('<li id="divider"></li>').insertAfter($('#site_nav_sections .site_nav_section .section_content.last').parent());
	$('#site_nav_sections .site_nav_section .section_content').hide();
	$('#site_nav').prepend('<div id="selected_slide"></div>');
	$('#site_nav_sections .site_nav_section .section_content').eq(0).show();

	if ($('.selected').length==0) {
		$('#site_nav_sections .site_nav_section > h3:not(.selected)').click(function(){
			$('#site_nav_sections .site_nav_section > h3').removeClass('selected');
			$(this).addClass('active');
			switchSection($(this).next('.section_content'));
		});
	}
	
//Slide show initialisation
	var cur_slide = $('#site_nav_sections .site_nav_section .section_content').eq(0);
	fluidContent(cur_slide);
	var rotation;
	var initial_position = 50;
	var position = initial_position;
	var slide_unit = 50;

	function fluidContent(content) {
		if (!$.browser.msie || $.browser.version > 6) {
			var content_height = $(content).innerHeight();
			var site_nav_height=500
			if (content_height>400) {site_nav_height = content_height+100}
			$('#site_nav').css('height',site_nav_height);
		}
	}

	function switchSection(section){
		$('#site_nav_sections .site_nav_section .section_content').fadeOut('fast').next('.types_all').fadeOut();
		
		$(section).fadeIn(function(){
			$('#site_nav_sections .site_nav_section > h3').removeClass('selected');
			$(section).prev('h3').removeClass('active').addClass('selected');
		}).next('.types_all').fadeIn();
		$('#selected_slide').fadeOut();
		fluidContent(section);
	}

	function sectionsSlideshow() {
		$('#selected_slide').css('top',position+'px').show();
		cur_slide.fadeOut().next('.types_all').fadeOut();
		$('#site_nav_sections h3').removeClass('selected');
		if (cur_slide.is('.last')) {
			cur_slide = $('#site_nav_sections .site_nav_section .section_content').eq(0);
			cur_slide.fadeIn().next('.types_all').fadeIn();
			$('#selected_slide').animate({top: initial_position}, 100,function(){in_progress=false;});
			position = initial_position;
		}
		else {
			cur_slide = cur_slide.parent().next().find('.section_content');
			cur_slide.fadeIn().next('.types_all').fadeIn();
			$('#selected_slide').animate({top: position+slide_unit}, 300,function(){in_progress=false;});
			position=position+slide_unit;
		}
		fluidContent(cur_slide);
	}
	
	if (!$.browser.msie || $.browser.version > 7) {
		$('#site_nav').hover(function(){clearInterval(rotation);});
		$('#site_nav').click(function(){clearInterval(rotation);});
	}
	
	$("#speedtest_promo_big").click(function() {
		window.location=$(this).find('a:first').attr('href');
	});
	
	$('.tools .sitepromo').not('.tools .sitepromo.pc, .tools .sitepromo.sw').click( function() {
		window.location=$(this).find('a.tool_link').attr('href');
	});
	
	$('.videos .sitepromo').click( function() {
		$(this).click(function(){$("a#open_video").click();});
	});
	
	// EXPLORE SECTION HOVER EFFECTS //
	$('.tools .sitepromo, .videos .sitepromo').hover(
		function () { $(this).next('li').addClass('sep_bottom').end().prev('li').addClass('sep_top'); },
		function () { $(this).next('li').removeClass('sep_bottom').end().prev('li').removeClass('sep_top'); }
	);
	
	// OPEN POSTCODE CHECKER //
	$('.sitepromo.pc:not(.open)').live('click', function () {
		$(this).addClass('open');
		$(this).find('.pc_intro').hide();
		$('.error_message').hide();
		$('div#postcode_slide').show();
	});

	// CLOSE POSTCODE CHECKER //
	$('.sitepromo #postcode_slide span.cancel a').live('click', function () {
		$('div#postcode_slide').hide();
		$('.sitepromo.pc span.pc_intro').show();
		$(this).parent().parent().parent().removeClass('open');
	});
	
	// OPEN SWITCH ISP FORM //
	$('.sitepromo.sw:not(.open)').live('click', function () {
		$(this).addClass('open');
		$(this).find('.sw_intro').hide();
		$('div#switch_slide').show();
	});

	// CLOSE SWITCH ISP FORM //
	$('.sitepromo #switch_slide span a').live('click', function () {
		$('div#switch_slide').hide();
		$('.sitepromo.sw .sw_intro').show();
		$(this).parent().parent().parent().removeClass('open');
	});

});
