$(document).ready(function() {

	$.localScroll();
	
       
	//GLOBAL NAVIGATION
	
	$("#global-navigation a").click(
		function() {
			$("#global-navigation a").removeClass("current");
			$(this).addClass("current");
		}
			
	);


	//PERSONALITY CHARACTER SLIDESHOW
	var slideshow_count = $("#characters-slideshow li").length;
	var slideshow_current = 1;
	
	$("#feature-" + slideshow_current).show();
	$("#bg-feature-" + slideshow_current).show();
	
	function slideshow_run() {
		
		$("#feature-" + slideshow_current).fadeOut(7000);
		$("#bg-feature-" + slideshow_current).fadeOut(7000);
		
		slideshow_current++;
		
		if(slideshow_current > slideshow_count) {
			slideshow_current = 1;
		}
		
		$("#feature-" + slideshow_current).fadeIn(6000);
		$("#bg-feature-" + slideshow_current).fadeIn(6000);
		
	}
	
	if($("#characters-slideshow li").length > 0) {
		$("#characters-slideshow").everyTime(6000, slideshow_run);
	}



//PERSONALITY CHARACTER SLIDESHOW
	var slideshow_two_count = $("#hero-slide li").length;
	var slideshow_two_current = 1;
	
	$("#product-" + slideshow_two_current).show();
	$("#product-feature-" + slideshow_two_current).show();
	
	function slideshow_two_run() {
		
		$("#product-" + slideshow_two_current).fadeOut(5000);
		$("#product-feature-" + slideshow_two_current).fadeOut(5000);
		
		slideshow_two_current++;
		
		if(slideshow_two_current > slideshow_two_count) {
			slideshow_two_current = 1;
		}
		
		$("#product-" + slideshow_two_current).fadeIn(4000);
		$("#bg-feature-" + slideshow_two_current).fadeIn(4000);
		
	}
	
	if($("#hero-slide li").length > 0) {
		$("#hero-slide").everyTime(5000, slideshow_two_run);
	}
       
	

	//TABBED NAVIGATION
    var current_tab = "tab-1";
    var tab_i = 1;
    $("#content-tab-1").show();
	$("#range-1").show();
	$("#range-1-product-1").show();
	$("#range-2-product-1").show();
	
    $(".sub-nav li a").click(
		function() {
		    var tab_id = $(this).attr("id");
			var this_range = $(this).attr("rel");

		    if (current_tab != tab_id) {

		        $(".sub-nav li").each(
					function() {
					    var this_tab_id = $(this).children("a").attr("id");
						
					    if (current_tab == this_tab_id) {
					        $("#content-" + current_tab).fadeOut(300);
					        $("#content-" + tab_id).animate({ marginLeft: 0 }, 300).fadeIn(300);
					        $("#" + current_tab).removeClass("current");
							current_tab = tab_id;
					        $("#" + tab_id).addClass("current");
							
							showRange(this_range);
							currentItem = 1;
							rangeNavigationInit(current_range);

					        return false;
					    }
					}
				);

		    }

		    return false;
		}
	);
	


	//RANGE NAVIGATION
	current_range = "range-1";
	currentItem = 1;
	rangeNavigationInit(current_range);
	
	$("#ct-next a").click(
		
		function() {
			
			var totalItems = $("#" + current_range + " li").length;
			$("#" + current_range + "-product-" + currentItem).fadeOut(300);
			currentItem < totalItems ? currentItem++ : currentItem = 1;
			
			$("#" + current_range + "-product-" + currentItem).animate({ marginLeft: 0 }, 300).fadeIn(300);
				rangeNavigationInit(current_range);
			//$("li").animate({ marginLeft: 0 }, 300).fadeIn(300);
			
			return false;
		}
	);
	
	$("#ct-previous a").click(
		
		function() {
			
			var totalItems = $("#" + current_range + " li").length;
			
			
				$("#" + current_range + "-product-" + currentItem).fadeOut(300);
				currentItem > 1 ? currentItem-- : currentItem = totalItems;
				$("#" + current_range + "-product-" + currentItem).animate({ marginLeft: 0 }, 300).fadeIn(300);
				rangeNavigationInit(current_range);
			
			//$("li").animate({ marginLeft: 0 }, 300).fadeIn(300);
			
			return false;
		}
	);
	
	//OVERLAYS
	function showOverlay(url) {$("#overlays").load(url);}

        $(".email-signup-link").click(
		function() {
			showOverlay(this.href);
			return false;
		}
	);
	
	$(".privacy-link").click(
		function() {
			showOverlay(this.href);
			return false;
		}
	);



	$("a").click(
		function() { 
			if(this.href.match("/news/")) {
				var show=$("#show").val();
				if(show>'"') {
					if(!this.href.match("show=all")) this.href=this.href+"?show="+show;
				}
			}
		}
	);
	

	
		
});

//RANGE NAVIGATION
	
function showRange(thisRange) {
	
	$("#" + current_range).fadeOut(300, function () {
		$("#" + current_range + " li").hide();
		$("#" + current_range + "-product-1").show();
	});
	
	$("#" + thisRange).animate({ marginLeft: 0 }, 300).fadeIn(300);
	
	current_range = thisRange;
	
}

function resetImages(thisRange) {
	
	$("#" + current_range + li).hide();
	
}

function rangeNavigationInit(thisRange) {
	
	var totalItems = $("#" + thisRange + " li").length;
	
	$("#controls p.list").text(currentItem + " of " + totalItems);
	
}


//ACTION TRACKING
	function trackAction(action, title, media, type) {
	
	
	var track_path = "/" + action + "/" + title;
	
	if(media) track_path += "/" + media;
	if(type) track_path += "/" + type;
	
	
	track_path = unescape(track_path);
	pageTracker._trackPageview(track_path);
}


String.prototype.prepare = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
	

function emailAddressValid(emailAddress) {
	var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return(emailFilter.test(emailAddress));
}

function postCodeValid(postCode) {
	var postCodeFilter = /^((GIR 0AA|gir 0aa)|[A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw]) [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2})$/i;
	return(postCodeFilter.test(postCode));
}



