/*
* jw-core.js
*
* Contains core utility classes for use with JohnnieWalker.com
*/
/* Declare Johnnie Walker namespace for site Globals */
var JohnnieWalker = {

    hideUI: function() {
        $('li.dob').slideUp(500);
        $('.enter .button').slideUp(300);
        $('.rememberMe input, .rememberMe span').slideUp(100);
    },

    showUI: function() {
        $('li.dob').slideDown(400);
        $('.enter .button').slideDown(500);
        $('.rememberMe input, .rememberMe span').slideDown(600);
    },
    
    firstHideUI: function() {
        $('li.dob').css('display', 'none');
        $('.enter .button').css('display', 'none');
        $('.rememberMe input, .rememberMe span').css('display', 'none');
    }

};

$(document).ready(function() {
    /* Show UI */
    JohnnieWalker.showUI();
    
	/* Add a events to add selected state for fieldsets on the Registration page */
	$(".registrationContainer fieldset").click(function() {
		$(this).parent().find("fieldset.current").removeClass('current');
		$(this).addClass('current');
	});

	$(".registrationContainer fieldset").keyup(function() {
		$(this).parent().find("fieldset.current").removeClass('current');
		$(this).addClass('current');
	});

	/* Clear the search field upon click */
	$(".field").click(function() {
		this.value = "";
	});

	/* Hover effect for IE6s benefit */
	var hoverButtonFix = $(".button");
	hoverButtonFix.mouseover(function(event) {
		$(this).children().addClass("hover");
	}).mouseout(function() {
		$(this).children().removeClass("hover");
	});

	/* Used for the expanding divs on the blends pages */
	/* Only have more button if JS is on */
	$(".expando").addClass("json");

	var expandoContent = $("div.expandoContent");
	if (!expandoContent.hasClass("hidden")) {
		expandoContent.addClass("hidden");
	}

	$(".js-button").addClass("hidden");


	/* Click event for the expando buttons ; reveal content and hide more button */
	$(".expando").click(function(event) {
		event.stopPropagation();
		$(this).prev().toggleClass('hidden');
		$(this).toggleClass('hidden');
		return false;
	});

	/* Temporary hook to allow people to return from whence they came */
	$(".windowHistoryMinusOne").click(function() {
		history.go(-1);
	});

	/* auto tabbing on Age gateway */
	if ($('body').hasClass("age-gateway")) {
	    $('.age-gateway :text').autotab_magic();
	}

	$(".title").keypress(function(e) {
		if (e.keyCode == 13) return false;
	});

	$(".regselect").keypress(function(e) {
		if (e.keyCode == 13) return false;
	});
	
	/* Centering Secondary Navigation */
	positionSecondaryNavigation = function() {
		/* Hide this in javascript.aspx, so that there is no flicker on placement */
		$(".subNavWrapper").css("visibility", "visible");
		var mNavItemPos = $(".mainNav li.selected").offset().left;
		var mNavCent = $(".mainNav li.selected").outerWidth() / 2;
		var sNavPos = $(".subNav").offset().left;
		var sNavWidth = $(".subNav").outerWidth();
		var sNavCent = sNavWidth / 2;
		var moveSubNav = (mNavItemPos + mNavCent - sNavPos - sNavCent);
		var leftEdgeOfNav = $(".navigation").offset().left;
		var rightEdgeOfNav = leftEdgeOfNav + $(".navigation").width();
		var leftEdgeOfSNav = sNavPos + moveSubNav;
		var rightEdgeOfSNav = leftEdgeOfSNav + sNavWidth;
		if (rightEdgeOfSNav >= rightEdgeOfNav) {
			moveSubNav = moveSubNav - (rightEdgeOfSNav - rightEdgeOfNav);
		}
		if (leftEdgeOfNav >= leftEdgeOfSNav) {
			moveSubNav = 0;
		}
		$(".subNav").css("left", moveSubNav + 'px');
	}
	positionSecondaryNavigation();
	/* On change of the country selector in the gateway focus on the first date field */
	$(".countrySelector").change(function(event) {
		$(".day").focus();
	});

    /* Check for FB and add _blank as a target */
    $("a[href='http://www.facebook.com/JohnnieWalker']").attr('target', '_blank');

    /* Check for Itunes link and add _blank as a target */
    $("a[href='http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=258125787']").attr('target', '_blank');



});
