$(document).ready(function () {

    $('#homePanelOne, #homePanelTwo, #homeImagePanels .one,  #homeImagePanels .two').hover(

    function () {
        $(this).children('.bg').stop().fadeTo("slow",1);
    }, function () {
        $(this).children('.bg').stop().fadeTo("slow",0.65);
    });

    // hover styles to improve menu behaviour/IE6 support
    $("#primaryNav > li, body#CaskPage .carousel li, #ExpressionPage .viewMore, #CaskPage .viewMore, input[type='submit']").hover(
    function () {
        $(this).addClass("hover");
    }, function () {
        $(this).removeClass("hover");
    });

    // prevent tooltips on main nav
    $("#primaryNav a").attr("title", "");

    // Disable logo and home menu option when already on homepage.
    $('#HomePage #header #logo a, #HomePage #primaryNav a[href="/"]').removeAttr('href');

    // add hook for Javascript-dependent styles
    $("html").addClass("js-ok");

    // stop opening of new window on Twitter share click
    $(".addthis_button_twitter").click(function () {
        return false;
    });

    $("body#DistilleryGatewayPage ol.our-tours li:last-child").addClass("last-child");

    // client side implementation to hide "you're too young" panel
    $('body#AgeCheckPage input.closeSorryButton').click(function () {
        $('.ageCheckPanel').removeClass('hidden');
        $('.underAgePanel').addClass('hidden');
        return false;
    });

    $("body#DistillationGatewayPage #subNav li.first").hover(

    function () {
        $(this).addClass("first-hover");
    }, function () {
        $(this).removeClass("first-hover");
    });

    $("body#DistillationGatewayPage #subNav li:eq(1)").hover(

    function () {
        $(this).addClass("second-hover");
    }, function () {
        $(this).removeClass("second-hover");
    });

    $("body#DistillationGatewayPage #subNav li.last").hover(

    function () {
        $(this).addClass("third-hover");
    }, function () {
        $(this).removeClass("third-hover");
    });

    $("body#CaskPage #caskExpressions li:first-child").addClass("first-child");

    $(".extra-text").hide();

    $("a.popup-text-expand").click(function () {
        $(this).toggleClass("popup-text-expand, popup-text-collapse");
        $(this).siblings(".extra-text").slideToggle();
        return false;
    });

    // Apply smooth scrolling to bookmark click event (and offset slightly so that target is not right at top of page).
    $("#makingWhiskyGatewayPage a.next").click(function (event) {
        Chunk.ScrollToBookmarkAnchor(this, event, -25);
    });
	$("#tripleDistillationGatewayPage a.long").click(function (event) {
        Chunk.ScrollToBookmarkAnchor(this, event, -25);
    });
    $("#WhatsOnGatewayPage a.jump").click(function (event) {
        Chunk.ScrollToBookmarkAnchor(this, event, -25);
    });

    // add shadows to product range listing bottles
    if ($("body#RangePage").length) {
        $("ul#rangeExpressionNav li").append('<div class="shadow"></div>');
        $("ul#rangeExpressionNav li:first-child, ul#rangeExpressionNav li:nth-child(5n+1)").addClass("edge-l");
        $("ul#rangeExpressionNav li:last-child, ul#rangeExpressionNav li:nth-child(5n)").addClass("edge-r");
        $("ul#rangeExpressionNav li:only-child").addClass("edge-both");
    }

    // Add Gallery to Tours & Meetings detail page
    $('#slideshow').coinslider({
        width: 643,
        height: 394,
        spw: 1,
        sph: 5,
        effect: 'rain',
        navigation: false,
        delay: 5000,
        sDelay: 200
    });

	// Reveal more Expressions on Cask page
    if ($('#caskExpressions ul li#9').length) {
        initialHeight = $('#caskExpressions ul').height();
        cutoffPoint = $('#caskExpressions ul li#9');
        expressionListCutoff = cutoffPoint.position();
        $('#caskExpressions ul').height(expressionListCutoff.top + 12);
        $("#CaskPage .viewMore").show();
        $("#CaskPage .viewMore").toggle(function () {
            $(this).addClass("open");
            $("#CaskPage .show").hide();
            $("#CaskPage .hide").show();
            $('#caskExpressions ul').animate({
                height: initialHeight
            });
        }, function () {
            $(this).removeClass("open");
            $("#CaskPage .show").show();
            $("#CaskPage .hide").hide();
            $('#caskExpressions ul').animate({
                height: expressionListCutoff.top + 12
            });
        });
    }

	// Reveal more Serves on Expression page
    $("#ExpressionPage .viewMore").show();
    $("#ExpressionPage .viewMore").click(function () {
        $(this).toggleClass("open");
        $('#ExpressionPage .alternateServes').slideToggle();
        $("#ExpressionPage .show").toggle();
        $("#ExpressionPage .hide").toggle();
    });

    if (Chunk.Exists('body#HomePage') && Chunk.HasFlash() && Chunk.Home.IsPlayAnimation()) {
        Chunk.Home.HideFeaturedPanels();
        Chunk.Home.SetAnimationHasPlayed();
    }

    if (Chunk.Exists('body#HomePage') && Chunk.HasFlash()) {
        $('body#HomePage').css('background-image', 'none');
    }
});

Chunk = {};
Chunk.Home = {};

Chunk.Exists = function (selector) {
    return $(selector).length > 0;
};

Chunk.NotExists = function (selector) {
    return $(selector).length === 0;
};

Chunk.HasFlash = function () {
    return swfobject.hasFlashPlayerVersion("9.0.0.0");
};

// Scrolls to the bookmark location (plus optional offset) targeted by the provided link.
Chunk.ScrollToBookmarkAnchor = function (linkClicked, linkClickEvent, offsetFromTarget) {
    linkClickEvent.preventDefault();
    if (!offsetFromTarget) {
        offsetFromTarget = 0; // If we don't have an offset, use 0;
    }
    var bookmarkName = linkClicked.hash.substr(1); // Chop off leading hash symbol from bookmark target.
    var bookmarkLocator = 'a[name="' + bookmarkName + '"]'; // Locate the target bookmark element by its name attribute.
    var bookmarkElementPositionOffset = $(bookmarkLocator).offset(); // Find offset of target bookmark.
    var scrollToPosition = 0; // Scroll to top by default if we don't have an offset.
    if (bookmarkElementPositionOffset) {
        scrollToPosition = bookmarkElementPositionOffset.top + offsetFromTarget;
    }
    $('html,body').animate({
        scrollTop: scrollToPosition
    }, 'slow');
};

Chunk.Home.SetAnimationHasPlayed = function () {
    $.cookie('skipFlash', 'true');
};

Chunk.Home.IsPlayAnimation = function () {
    return $.cookie('skipFlash') === null;
};

Chunk.Home.HideFeaturedPanels = function () {
    $('body#HomePage #mainContent').hide();
};

Chunk.Home.FadeOutFeaturedPanels = function () {
    $('body#HomePage #mainContent').fadeOut('fast');
};

Chunk.Home.FadeInFeaturedPanels = function () {
    $('body#HomePage #mainContent').fadeIn('slow');
    sIFR.replace(gothamLight, {
        selector: 'body#HomePage h2',
        css: ['.sIFR-root { text-transform: uppercase; }', '.sIFR-root a { color: #494948; text-decoration: none; }', '.sIFR-root a:hover { color: #494948; text-decoration: none; }'],
        wmode: 'transparent',
        fitExactly: true,
        tuneWidth: 1
    });
};

var isFireFox = new Boolean();
if (navigator.userAgent.indexOf("Firefox") !== -1) {
	if (navigator.platform === "MacIntel" || navigator.platform === "MacPPC") {
		isFireFox = true;
	} else {
		isFireFox = false;
	}
} else {
	isFireFox = false;
}

function isEven(value) {
	if (value % 2 === 0) {
		return true;
	} else {
		return false;
	}
}
var width;
window.onresize = function () {
	if (isFireFox) {
		width = window.innerWidth;
		if (isEven(width)) {
			self.resizeTo(width - 1, window.outerHeight);
		}
	}
};

