/*
 * .JS
 */
$(document).ready(function() {
	$("html").addClass("JS");
});

/*
 * FadeIn #background. Does not work well with sIFR.
 */
$(document).ready(function() {
	$("#background").css("opacity","0").css("top","-4848px");
	$("#background").fadeTo(1234, 1).css("top","0");
});

/*
 * Our campagings title for sIFR
 */
$(document).ready(function() {
	$("#navigation ul li a:first-child").wrap("<span class='title'></span>");
});

/*
 * Discover Chivas Studio
 */

$(document).ready(function() {
	$("#discover li").hover(function(){
		$('#extra').addClass('dN');
		$(this).find('img').css("opacity", "0.5");
		$(this).find('div.li-text').addClass("visible");
	},function(){
		$('#extra').removeClass('dN');
		$(this).find('img').css("opacity","1");
		$(this).find('div.li-text').removeClass("visible");
	});
});

/*
 * Pop overlays.
 */
var popList;

$(document).ready(function() {
    $(".pop").css("opacity", "0.0").css("z-index", "-1").css("top", "-4848px"); ;

    if (popList != null) {
        for (i = 0; i < popList.length; i++) {
            $('#' + popList[i] + '_show').click(function(event) {
                $(event.currentTarget.hash).css("z-index", "101").fadeTo("fast", 1.0).css("top", "0").css("visibility", "visible");
                return false;
            });

            $('#' + popList[i] + '_close').click(function(event) {
                $(event.currentTarget.hash).css("opacity", 0.0).css("z-index", "-1").css("visibility", "hidden"); ;
                $(event.currentTarget.hash).removeClass("reading");
                return false;
            });
        }
    }
});

/*
* Hidden .more links when active.
*/

$(document).ready(function() {
    $(".navigation li a.active").parent().find(".more").css("display", "none");
});

/*
* Read more.
*/
$(document).ready(function() {
    $(".makingChivas .process .more").css("opacity", 0);

    $(".makingChivas .readMore a").click(function(event) {
        var elementName;
        if (event.srcElement == null)
            elementName = event.currentTarget.hash;
        else
            elementName = event.srcElement.hash;
        
        $(elementName + " .more").fadeTo("fast", 1);
        $(this).parent().fadeTo("fast", 0);
        $(elementName).addClass("reading");
        return false;
    });
    $(".makingChivas .readLess a").click(function(event) {
        var elementName;
        if (event.srcElement == null)
            elementName = event.currentTarget.hash;
        else
            elementName = event.srcElement.hash;

        $(elementName + " .more").fadeTo("fast", 0);
        $(elementName + " .readMore").fadeTo("fast", 1);
        if ($.browser.msie && $.browser.version <= 7)
            $(elementName + " .readMore").css("height", "100px");
        $(elementName).removeClass("reading");
        return false;
    });
});

$(document).ready(function() {
    $(".ourRange .events .readMore a").click(function() {
        $(this).parent().parent().parent().parent().addClass("reading");
        return false;
    });
    $(".ourRange .edition .readMore a").click(function() {
        $(this).parent().parent().parent().parent().addClass("reading");
        return false;
    });
});

/*
* IE6's awesome CSS implementation.
*/

$(document).ready(function() {
    $("#makingChivas ul.navigation li:first-child").css("background", "none");
    $("#ourEvents ul.navigation li:first-child").css("background-image", "none");
});

/*
 * Hover for IE6 / general active.
 */
$(document).ready(function() {
    $("#information ul li").hover(function() {
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
});

$(document).ready(function() {
    var el = $(".pop div ul li.defaultText");
    
    if (!el.hasClass("defaultText")) 
    {        
        el = $(".pop div ul li:first-child");
    }
    el.addClass("active");

    $(".pop div ul li").mouseover(function() {
        $(this).parent().find("li.active").removeClass("active");
        $(this).addClass("active");
    });
    $(".pop div ul li").mouseout(function() {
        $(this).parent().find("li.active").removeClass("active");
        var el = $(".pop div ul li.defaultText");
        if (el == null)
            el = $(".pop div ul li:first-child");
        el.addClass("active");
    });
});
$(document).ready(function() {
    $("#map ul li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });
});

/*
* Drinks hover state / tooltip.
*/
$(document).ready(function() {
    if ($('#nav-drinks').length > 0) {
        $("#nav-drinks .drink a").after("<div></div>").next("div").css("opacity", "0");
        $("#nav-drinks .drink a").hover(function() {
            $(this).next("div").fadeTo("fast", 1);
        }, function() {
            $(this).next("div").fadeTo("fast", 0);
        });
        $('"#nav-drinks .drink a').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            fade: 250
        });
    }
});

/*
 * Launch links with a class of popup in a new window
 */
$(document).ready(function() {
	$('a.popup').click(function() {
		window.open(this.href, 'chivasPrint', 'menubar=yes,width=650,height=500,toolbar=no,scrollbars=yes');
		return false;
	});
});

/*
 * For sIFR (?!)
 */
$(document).ready(function() {
	$(".pop div ul li dt").addClass("dt");
	$(".pop div ul li dd").addClass("dd");
});

/*
 * Coda Slider Effect
 * http://jqueryfordesigners.com/coda-slider-effect/
 *
 * Copyright (c) 2008 Remy Sharp
 * http://remysharp.com/about/
 * 
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 */

// when the DOM is ready...
$(document).ready(function() {

    if ($('#content') != null && $('#content').length > 0) {
        $('#content .scrollContainer div.panel').ifixpng();
        $('#content .scroll-nav .scroll-frame').ifixpng();
        $('#content .scroll-nav .highlight-frame').ifixpng();
    }
    
    SetupSlider('nav-drinks', 6, true, '', '.navigation');
	SetupSlider('drinks', 1, false, '-1000px', '.nav-scroll');
	SetupSlider('makingChivas', 1, false, '-1000px', '.navigation');
	SetupSlider('extra', 1, false, '', '.scroll-nav');
	SetupSlider('mediaView', 1, false, '', '.scroll-nav');
	SetupSlider('newsItems', 3, false, '', '.scroll-nav');	

	// show and hide carousel controls on mouse over/out
	
	//	$('#extra').mouseleave(function(){ $('#scroll-container').animate({top:"380px"},300); });
});

// News page Scroll Pane
$(document).ready(function() {
	$('#content .scrollable').jScrollPane({ showArrows: false, scrollbarWidth: 6, arrowSize: 16, animateInterval: 50 });
});

$(document).ready(function() {
    $('#content .xform tr td select').each(function(intIndex) {
        AddMissingClassToType(this, null, "dropdownlist");
    });
    $('#content .xform tr td input').each(function(intIndex) {
        AddMissingClassToType(this, null, "textbox");
        AddMissingClassToType(this, "checkbox", "checkbox");
        AddMissingClassToType(this, "submit", "button");
    });
    $('#content .xform tr td textarea').each(function(intIndex) {
        AddMissingClassToType(this, null, "textbox");
    });    
});

function AddMissingClassToType(el, inputType, cssClass) {
    if (el.getAttribute('type') == inputType)
        $(el).addClass(cssClass);
}

function SetupSlider(baseTag, panelsDisplayed, nextPrevOnly, leftOffset, navigationClass) {

    if ($('#' + baseTag) != null) {
        var $panels = $('#' + baseTag + ' .scrollContainer > div');

        if ($panels.length > 0) {
            var $container = $('#' + baseTag + ' .scrollContainer');

            // if false, we'll float all the panels left and fix the width 
            // of the container
            var horizontal = true;

            // float the panels left if we're going horizontal
            if (horizontal) {
                $panels.css({
                    'float': 'left',
                    'position': 'relative' // IE fix to ensure overflow is hidden
                });

                if (leftOffset != '' && !location.hash)
                    $panels.css('left', leftOffset);

                // calculate a new width for the container (so it holds all panels)
                // you have to add them all seperately in case they dont have the same offsetWidth (e.g. padding on last-child)
                var offsetWidthCalc = 0;
                for (var i = 0; i < $panels.length; i++)
                    offsetWidthCalc += $panels[i].offsetWidth;

                $container.css('width', offsetWidthCalc);
            }

            // collect the scroll object, at the same time apply the hidden overflow
            // to remove the default scrollbars that will appear
            var $scroll = $('#' + baseTag + ' .scroll').css('overflow', 'hidden');

            // apply our left + right buttons if more than 1 image
            if ($panels.length > 1)
            {
                $scroll
                    .before('<div class="scrollButtons left"></div>')
				    .after('<div class="scrollButtons right"></div>');

                var containerTop = $('#scroll-container').css("top");
                
                var hoverIntentConfig = {
                    sensitivity: 30, // number = sensitivity threshold (must be 1 or higher)    
                    interval: 100, // number = milliseconds for onMouseOver polling interval    
                    over: function() { $('#scroll-container').animate({ top: "-=160px" }, 300); }, // function = onMouseOver callback (REQUIRED)    
                    timeout: 100, // number = milliseconds delay before onMouseOut
                    out: function() { $('#scroll-container').animate({ top: containerTop }, 300); } // function = onMouseOut callback (REQUIRED)
                };

                $('#extra').hoverIntent(hoverIntentConfig);
            }
        //.before('<img class="scrollButtons left" src="/img/navigation-prev_amber.png" />')
        //.after('<img class="scrollButtons right" src="/img/navigation-next_amber.png" />');



            // handle nav selection
            function selectNav() {
                $(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');
            }

            $('#' + baseTag + ' ' + navigationClass).find('a').click(selectNav);

            // go find the navigation link that has this target and select the nav
            function trigger(data) {
                var el = $('#' + baseTag + ' ' + navigationClass).find('a[href$="' + data.id + '"]').get(0);
                selectNav.call(el);
            }

            if (window.location.hash) {
                trigger({ id: window.location.hash.substr(1) });
            } else {
                $('ul'+navigationClass+' a:first').click();
            }

            // offset is used to move to *exactly* the right place, since I'm using
            // padding on my example, I need to subtract the amount of padding to
            // the offset.  Try removing this to get a good idea of the effect
            var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0) * -1;
            //alert('offset: ' + offset);
            var scrollOptions = {
                target: $scroll, // the element that has the overflow

                // can be a selector which will be relative to the target
                items: $panels,

                navigation: navigationClass+' a',

                // selectors are NOT relative to document, i.e. make sure they're unique
                prev: 'div.left',
                next: 'div.right',

                // allow the scroll effect to run both directions
                axis: 'xy',

                onAfter: trigger, // our final callback

                offset: offset,
                
                panelsDisplayed: panelsDisplayed,

                // duration of the sliding effect
                duration: 500,

                // easing - can be used with the easing plugin: 
                // http://gsgd.co.uk/sandbox/jquery/easing/
                easing: 'swing'
            };

            // apply serialScroll to the slider - we chose this plugin because it 
            // supports// the indexed next and previous scroll along with hooking 
            // in to our navigation.
			$('#' + baseTag).serialScroll(scrollOptions);

            if (nextPrevOnly) {
                scrollOptions.duration = 1;
                $.localScroll.modifiedHash(scrollOptions, baseTag);
            }
            else {
                // now apply localScroll to hook any other arbitrary links to trigger 
                // the effect
                $.localScroll(scrollOptions);

                // finally, if the URL has a hash, move the slider in to position, 
                // setting the duration to 1 because I don't want it to scroll in the
                // very first page load.  We don't always need this, but it ensures
                // the positioning is absolutely spot on when the pages loads.
                scrollOptions.duration = 1;
                $.localScroll.hash(scrollOptions);
            }
			
			$('html, body').animate({scrollTop:0}, 1);



			
        }
    }
}