$(document).ready(function(){
						       
	//Create destination switch
	 $('.desrarea .images img').hide();
	 //$('.desrarea .images img.iaustralia_new_zealand_holidays').fadeIn();

    $('#regionselect').click(function(){
        
		var selected = $(this).find(':selected');
        $('.regionDetail').fadeOut();
		$('.desrarea .images img').fadeOut();
		
        var count = $('.countrylist').length;
		var selectedValue = $(this).val();
        $('.countrylist').slideUp(function(){
            if(!--count) {
               $('.'+selected.val()).slideDown(function(){
					$('img.i' + selectedValue).fadeIn();
				});
            }       
        });
    });
	
	$('#schools-hdr').show();	
		$('#home-cta-tabs li').click(function() {
								
			$('#home-cta-tabs li').removeClass('current');
			$(this).addClass('current');
			var id = $("li.current a").attr('href');
			$("#home-cta-content-wrap > div").fadeOut(500).hide();
			$(id).fadeIn();
			return false;
			
	});
	$('.tab-text').animate({
		opacity: 1,
		marginLeft: '+=166'
	}, 2000);

	// Update Cookie when region link clicked
	$('.update_cookie').live('click', function(e){
		
		e.preventDefault();
		
		var url = $(this).attr('href');
		
		$.post('http://www.thestc.co.uk/fdm/region', {
			url: url
		}, function(data) {
			if ( data )
			{
				var b = '0';
				var pairs = document.cookie.split(";");
				for ( var i=0; i<pairs.length; i++ )
				{
					var pair = pairs[i].split("=");
					if ( pair[0].indexOf("region-") != -1 ) eraseCookie( pair[0] );
				}
				createCookie( 'region-'+data, 'checked' );
				window.location = url;
			}
		});
		
	});

	// Update Cookie (from front page)
	$('.update_cookie_frontpage').click(function(e) {
		
		e.preventDefault();
		
		var url = $(this).attr('href');
		
		$(".tabcontent select option:selected").each(function() {
			
			if ( $(this).val() )
			{
				//alert( $(this).parent().attr('id')+' - '+$(this).val());
				createCookie( $(this).parent().attr('id'), $(this).val() );
			}
		});

		window.location = url;
		
	});
	
	// Clear Cookie contents
	$('#clear_cookie').click(function(e) {
		
		e.preventDefault();
		
		var pairs = document.cookie.split(";");
		for ( var i=0; i<pairs.length; i++ )
		{
			var pair = pairs[i].split("=");
			eraseCookie( pair[0] );
		}
		
		createCookie( 'schools_submenu', 'open' );
		createCookie( 'col_id_2', 'schools_submenu' );
		
		window.location = 'http://www.thestc.co.uk/destinations';
	
	});
	


	// Image Slideshow //
    $('.slideshow').cycle({
        fx: 'fade',
		timeout: 6000,
        speed: 2000,
		pager: '.switcher',
        pagerAnchorBuilder: pagerFactory
    });

    function pagerFactory(idx, slide) {
        var s = idx > 2 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };

	// Tabbed Search Box //
	if ( readCookie( 'schools_submenu' ) == null )
	{
		createCookie( 'schools_submenu', 'open' );
		createCookie( 'col_id_2', 'schools_submenu' );
	}

    $('.tab_btn').each(function() {
		
		if ( readCookie( $(this).attr('id') ) == 'open' )
		{
			$(this).addClass('active');

			$('.row').css("display", "block");
			if ( $(this).attr('id') == 'schools_submenu') $('#familyage_row, #activities_row').css("display", "none");
			if ( $(this).attr('id') == 'families_submenu') $('#schoolage_row, #subject_row').css("display", "none");
			if ( $(this).attr('id') == 'holidays_submenu') $('#schoolage_row, #familyage_row, #subject_row').css("display", "none");
		}

	});
	
	$('.tab_btn').click(function() {
		
		var submenu_id = $(this).attr('id');
		
		$('.tab_btn').removeClass("active");
		$(this).addClass('active');
		
		$('.tabcontent').slideUp('fast');
		
		$('.row').fadeIn('slow');
		
		if ( submenu_id == 'schools_submenu' )
		{
			$('#familyage_row, #activities_row').fadeOut('fast').children().val('');
			createCookie( 'col_id_11', '' );
			createCookie( 'col_id_12', '' );
		}
		if ( submenu_id == 'families_submenu' )
		{
			$('#schoolage_row, #subject_row').fadeOut('fast').children().val('');
			createCookie( 'col_id_9', '' );
			createCookie( 'col_id_10', '' );
		}
		if ( submenu_id == 'holidays_submenu' )
		{
			$('#schoolage_row, #familyage_row, #subject_row').fadeOut('fast').children().val('');//
			createCookie( 'col_id_9', '' );
			createCookie( 'col_id_10', '' );
			createCookie( 'col_id_12', '' );
		}

		$('.tabcontent').slideDown({easing:'easeInBack', duration:'slow'});

		createCookie( 'schools_submenu', 'closed' );
		createCookie( 'families_submenu', 'closed' );
		createCookie( 'holidays_submenu', 'closed' );
		createCookie( 'col_id_2', submenu_id );
		createCookie( submenu_id, 'open' );
		
		return false;
	});

    $('.trigger').each(function() {
		
		var select_id = $(this).attr('id');
		
		if ( readCookie( select_id ) != null ) $(this).val( readCookie( select_id ) );
	
	});

	$('.trigger').change(function() {
		
		var select_id = $(this).attr('id');
		
		createCookie( select_id, $(this).val() );
		
		$('.dsfeed').fadeOut('fast', function() {
			$('#loading').slideDown('fast');
		});
		
		$.post('http://www.thestc.co.uk/fdm', {
			cookieStr: getCookies(),
			tagdata: $('#fdm_tmpl').html()
		}, function(data) {
			
				$('.dsfeed').empty().append(data).slideDown('slow');
				$('#loading').slideUp('fast');
		
		});//, 'json'
		
		return false;
	});
	
	//  Accordion
	heightArray = new Array();
	$("dl.v_show_hide dd").each(function(i) {
	 theHeight = $(this).height();
	 heightArray[i] = theHeight;
	});

	$("dl.v_show_hide dd").hide();
	$("dl.v_show_hide dt").click(function () {
	 $(this).next("dd").css({height: heightArray[$("dl.v_show_hide dt").index(this)]});
	 $(this).next("dd").slideToggle("slow")
		.siblings("dd").slideUp("slow");
	});
	
	$('.casestudies dt:first-child').trigger('click');
	
	// Search Box Accordion //
	if ( readCookie( 'regions_submenu' ) == null ) createCookie( 'regions_submenu', 'open' );

    $('.slider').each(function() {
		
		if ( readCookie( $(this).attr('id') ) == 'open' )
			$(this).delay(800).slideDown({easing:'easeOutExpo', duration:700}).parent().addClass('active');
			
	});
	
	$('.accordion li a').click(function() {
		
		$(this).next().slideToggle({easing:'easeInQuad', duration:350}).parent().toggleClass('active');
		
		var submenu_id = $(this).next().attr('id');
		
		if ( readCookie( submenu_id ) == 'open' ) createCookie( submenu_id, 'closed' );
		else createCookie( submenu_id, 'open' );
		
		return false;
	});
	
	$('.slider input').each(function() {
		
		var checkbox_id = $(this).attr('id');
		var checkbox_name = $(this).attr('name');
		
		if ( readCookie( checkbox_id ) == 'checked' )
			$(this).prop("checked", true);
		else if ( readCookie( checkbox_name ) == $(this).val() )
			$(this).prop("checked", true);
	
	});
	
	if ( readCookie( 'region_topic' ) != '' )
	{
		$('#dsregion_'+readCookie( 'region_topic' )).slideDown('fast');
	}
	
	$('.slider div').live('click', function() {		
		
		var checkbox_id = $(this).next().attr('id');
		var checkbox_name = $('#'+checkbox_id).attr('name');
		var checkbox_type = $('#'+checkbox_id).attr('type');
		
		if ( checkbox_type == 'checkbox' )
		{
			if ( readCookie( checkbox_id ) == 'checked' ) createCookie( checkbox_id, 'unchecked' );
			else createCookie( checkbox_id, 'checked' );
		}
		if ( checkbox_type == 'radio' ) createCookie( checkbox_name, $('#'+checkbox_id).val() );
		
		$('.dsfeed').fadeOut('fast', function() {
			$('#loading').fadeIn('fast');
		});
		$('#loading').fadeIn('fast');
		//alert(getCookies());
		
		// Update region topic
		updateRegionTopic();
		
		$.post('http://www.thestc.co.uk/fdm', {
			cookieStr: getCookies(),
			tagdata: $('#fdm_tmpl').html()
		}, function(data) {
			
			$('#loading').fadeOut('fast');
			$('.dsfeed').empty().append(data).fadeIn('slow');
		
		});
		
		return false;
		
	});
	
	$.post('http://www.thestc.co.uk/fdm', {
		cookieStr: getCookies(),
		tagdata: $('#fdm_tmpl').html()
	}, function(data) {
		
		updateRegionTopic();
		$('.dsfeed').empty().append(data).slideDown('slow');
		$('#loading').slideUp('fast');
		
	});
	
	// Scroll Pagination
	$(window).scroll(function(){
		if ( $(window).scrollTop()+550 >= $(document).height() - $(window).height() )
		   lastPostFunc();
	});
	
	createCookie( 'working', 'false' );
	
	function lastPostFunc() 
	{ 
		$('#loading_pagination').fadeIn('fast');
		
		if ( readCookie( 'working' ) == 'true' ) return false;
		else createCookie( 'working', 'true' );
		
		$.post('http://www.thestc.co.uk/fdm', {
			cookieStr: getCookies(),
			tagdata: $('#fdm_tmpl').html(),
			lastid: $(".hentry:last").attr("id")
		}, function(data){
			
			if ( data != '<div class="no-results">No Results</div>' )
			{
				$(".dsfeed .hentry:last").after(data);	
				$('#loading_pagination').fadeOut('slow');
			} else 
				$('#loading_pagination').remove();
			
			createCookie( 'working', 'false' );
			
		});
	};  

	
	// jCaousel /
    $('#mycarousel').jcarousel({
    	wrap: 'circular',
		animation: 750
//		buttonNextHTML: null,
//		buttonPrevHTML: null
//		auto: 1
    });
	
	$('.jcarousel-container').hover(function(){
			$(this).stop().animate({ opacity: '1'}, 300);
		},
		function(){
			$(this).stop().animate({ opacity: '0.5'}, 300);													   	
		});													   


	// Pointy Pointy //
	$('.taggedlink').hover(function() {
		$(':last-child',this).animate({ right: '12'}, 100);	
		},
		function(){
		$(':last-child',this).animate({ right: '16'}, 100);
	});

	$('.tabset>li>a').click(function(){
		var $tab;
		$(this).closest('.tabset').find('>li>a.active').removeClass('active');
		$(this).addClass('active');
		$tab = $($(this).attr('href'));
		$tab.siblings().hide();
		$tab.find('>div').show();
		$tab.fadeIn();
		return false;
	});
	if(location.hash) {
		$('a[href$=' + location.hash +']').addClass('active');
		$('#tabbedContent').each(function(){
			$(this).find("section#" + location.hash.substr(1)).fadeIn();
		});
	} else {
		$('.tabset>li:first-child a').addClass('active');
		$('#tabbedContent').each(function(){
			$(this).find(':first-child').fadeIn();
		});
	}
	
	function createCookie( name, value, days )
	{
		var days = '1';
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie( name )
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function eraseCookie( name )
	{
		createCookie( name, '', -1 );
	}
		
	function getCookies()
	{
		var pairs = document.cookie.split(";");
		var cookieStr = '';
		for ( var i=0; i<pairs.length; i++ )
		{
			var pair = pairs[i].split("=");
			cookieStr += '&'+pair[0]+'='+pair[1];
		}
		return cookieStr;
	}			
		
	function updateRegionTopic()
	{
		var b = '0';
		var pairs = document.cookie.split(";");
		for ( var i=0; i<pairs.length; i++ )
		{
			var pair = pairs[i].split("=");
			if ( pair[0].indexOf("region-") != -1 )
				if ( pair[1] == 'checked' )
				{
					b++;
					var region_arr = pair[0].split("-");
					var region = region_arr[1];
				}
		}
		if ( b == 1 )
		{
			$('.dstopic').fadeOut('fast');
			$('#dsregion_'+region).slideDown('fast');
			createCookie( 'region_topic', region );
			
		} else {
			$('.dstopic').slideUp('fast');
			eraseCookie( 'region_topic' );
		}
	}			
	
});


