function youTubeScroller(container,isFirefox){
	this.isFirefox = isFirefox;
	this.container = container;
	this.videoWidth = 317;
	this.videoCount = $(this.container + ' UL LI').length;
	this.windowWidth = 3;
	this.stepCount = this.videoCount - this.windowWidth;
	this.pointer = 0;
	this.offset = 0;
	this.minOffset = 0;
	this.maxOffset = -(this.stepCount * this.videoWidth);
	this.animating = false;
	
	$(this.container + ' UL').css({width: (this.videoCount * this.videoWidth) + 'px'});
	if(this.isFirefox){
		$('DIV.viewList OBJECT').addClass('hide');
		$('DIV.viewList OBJECT').slice(0,3).removeClass('hide');
	}
}

youTubeScroller.prototype.doScroll = function(delta,updateSlider){
	var _self = this;
	if(this.isFirefox){
		var range = (delta > 0 ) ? delta : -delta;
		var lower,upper;
		if(delta > 0){
			lower = this.pointer;
			upper = this.pointer + range;
		}else{
			lower = this.pointer + 3 - range;
			upper = this.pointer + 3;
		}
	
		$('DIV.viewList OBJECT').slice(lower,upper).addClass('hide');
		this.pointer += delta;
		var slidePX = -delta * this.videoWidth;
		this.offset += slidePX;
	
		if(updateSlider)
			$('#slider').slider('value',this.pointer);
	
		if(!this.animating){
			this.animating = true;
			$(this.container + ' UL').animate({'left':_self.offset + 'px'},function(){_self.animating = false; $('DIV.viewList OBJECT').slice(_self.pointer,_self.pointer + 3).removeClass('hide');});
		}
	}else{
		_self.pointer += delta;
		var slidePX = -delta * _self.videoWidth;
		_self.offset += slidePX;
	
		if(updateSlider)
			$('#slider').slider('value',_self.pointer);
	
		if(!_self.animating){
			_self.animating = true;
			// $(this.container + ' UL').css({'left':_self.offset + 'px'});
			// _self.animating = false;
			$(this.container + ' UL').animate({'left':_self.offset + 'px'},function(){_self.animating = false; });
		}
	}
};

function howToGooSteps() {
    this.currentStep = 0;
    this.list = $('OL.erSteps');
    this.steps = this.list.find('LI');
    this.stepCount = this.steps.length;
    this.stepWidth = 640;
    this.stepheight = 480;

    this.install();
};
howToGooSteps.prototype.template = function() { };
howToGooSteps.prototype.install = function() {
    var self = this;

    this.steps.find('A').click(function(e) {
        var linkClass = $(this).attr('class');

        switch (linkClass) {
            case 'next': 
                {
                    self.steps.eq(self.currentStep).hide();
                    self.currentStep++;
                    self.steps.eq(self.currentStep).show();
                    e.preventDefault();
                } break;
            case 'previous': 
                {
                    self.steps.eq(self.currentStep).hide();
                    self.currentStep--;
                    self.steps.eq(self.currentStep).show();
                    e.preventDefault();
                } break;
            case 'play': { } break;
            case 'print': { } break;
        }
    });
};

/*
------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

$(document).ready(function() {
    var isFirefox = false;

    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
    // Is this a version of Mozilla?
    if ($.browser.mozilla) {

        //Is it Firefox?
        if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
            isFirefox = true;
        }
    }

    // embed countdown
    var parameters = { "allowFullScreen": "true", "allowscriptaccess": "always", "wmode": "transparent" };
    var variables = {};
    // swfobject.embedSWF( '/assets/swf/CadburyClock2010.swf','flashCounter', 360,130, '9.0.28', false, variables, parameters );
    swfobject.embedSWF('/assets/swf/cadbury_clock.swf', 'flashCounter', 360, 130, '9.0.28', false, variables, parameters);

    // your country needs goo
    var parameters = { "allowFullScreen": "true", "allowscriptaccess": "always", "wmode": "transparent" };
    var variables = {};
    // swfobject.embedSWF( '/assets/swf/CadburyClock2010.swf','flashCounter', 360,130, '9.0.28', false, variables, parameters );
    swfobject.embedSWF('/assets/swf/Comic.swf', 'ycngFlash', 639, 481, '9.0.28', false, variables, parameters);

    // egg-mented reality page
    var flashvars = { sendToFriendUrl: 'eggmented-s2f.aspx', enterCompUrl: 'cadbury-creme-egg-codes.aspx?code=BX1390Y', numLoops: '2', width: 800, height: 600 };
    //var flashvars = { sendToFriendUrl: 'http://www.cremeegg.co.uk', enterCompUrl: 'http://www.cremeegg.co.uk', numLoops: '2', width: 800, height: 600 };

    var parameters = { "allowFullScreen": "true", "allowscriptaccess": "always", "wmode": "transparent" };
    //var variables = {};
    swfobject.embedSWF('/Shell.swf', 'fp_container', 800, 600, '9.0.0', "expressInstall.swf", flashvars, parameters);
    //swfobject.embedSWF('assets/swf/Comic.swf', 'fp_container', 640, 480, '9.0.28', false, variables, parameters);
    //swfobject.embedSWF("Shell.swf", "altContent", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params);
    /*
    */

    $('DIV.yourPrize,UL.eggBands,DIV.prizesPage').pngFix();

    // 'Go' button rollovers
    /*
    $('P.goLink')
    .mouseover( function(){
    $(this).addClass('goLink_on');
    } )
    .mouseout( function(){
    $(this).removeClass('goLink_on');
    } );
    */

    $('DIV.socialMediaLinks P A')
	.mouseover(function() {
	    $('DIV.socialMediaLinks P').addClass('on');
	})
	.mouseout(function() {
	    $('DIV.socialMediaLinks P').removeClass('on');
	});

    $('UL.headerLinks LI A')
	.mouseover(function() {
	    var oldClass, newClass;
	    var index = $('UL.headerLinks LI').index($(this).parent('LI:eq(0)'));
	    oldClass = $('UL.headerLinks LI').eq(index).attr('class');
	    if (oldClass.indexOf('_on') == -1) {
	        $('UL.headerLinks LI').eq(index).addClass(oldClass + '_hover').removeClass(oldClass);
	    }
	})
	.mouseout(function() {
	    var oldClass, newClass;
	    var index = $('UL.headerLinks LI').index($(this).parent('LI:eq(0)'));
	    oldClass = $('UL.headerLinks LI').eq(index).attr('class');
	    if (oldClass.indexOf('_on') == -1) {
	        $('UL.headerLinks LI').eq(index).addClass(oldClass.substr(0, (oldClass.length - 6))).removeClass(oldClass);
	    }
	});

    $('SPAN.falseLabelDOB').click(function(e) {
        $('SELECT#day').focus();
        e.preventDefault();
    });

    // home page tabs
    $('DIV.tabContent').not('DIV.tabContent:eq(0)').addClass('hiddenPanel');
    $('UL.tabControl LI A').click(function(e) {
        var minorTabs = jQuery('UL.tabControl LI');
        var minorPanels = jQuery('DIV.tabContent');
        var thisParent = jQuery(this).parent('LI');
        var thisClass = thisParent.attr('class');
        var clickedTab = minorTabs.index(thisParent);

        if (thisClass.indexOf('_on') == -1) {
            for (index = 0; index < minorTabs.length; index++) {
                var newClass;
                thisItem = minorTabs.eq(index);
                nClass = jQuery(thisItem).attr('class');

                if (nClass.indexOf('_on') != -1) {
                    newClass = nClass.substr(0, (nClass.length - 3));
                    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
                    minorPanels.eq(index).addClass('hiddenPanel');
                }
                if (clickedTab == index) {
                    newClass = nClass + '_on';
                    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
                    minorPanels.eq(index).removeClass('hiddenPanel');
                }

            }
        }
        e.preventDefault();
    });

    // on the box tabs
    $('DIV.otbTabContent').not('DIV.otbTabContent:eq(0)').addClass('hiddenPanel');
    $('DIV.otbTabs DIV A').click(function(e) {
        var minorTabs = jQuery('DIV.otbTabs DIV');
        var minorPanels = jQuery('DIV.otbTabContent');
        var thisParent = jQuery(this).parent('H3').parent('DIV');
        var thisClass = thisParent.attr('class');
        var clickedTab = minorTabs.index(thisParent);

        if (thisClass.indexOf('_on') == -1) {
            for (index = 0; index < minorTabs.length; index++) {
                var newClass;
                thisItem = minorTabs.eq(index);
                nClass = jQuery(thisItem).attr('class');

                if (nClass.indexOf('_on') != -1) {
                    newClass = nClass.substr(0, (nClass.length - 3));
                    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
                    minorPanels.eq(index).addClass('hiddenPanel');
                }
                if (clickedTab == index) {
                    newClass = nClass + '_on';
                    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
                    minorPanels.eq(index).removeClass('hiddenPanel');
                }
            }
        }

        e.preventDefault();
    });

    //$('DIV.otbTabs DIV:eq(1) A').click();
    $('DIV.otbTabs DIV:eq(0) A').click();

    $('DIV.ggTabs DIV A').click(function(e) {
        //e.preventDefault();
    });

    // goo games tabs
    $('DIV.ggTabContent').not('DIV.ggTabContent:eq(0)').addClass('hiddenPanel');
    $('DIV.ggTabs DIV A.otbLink').click(function(e) {
        var minorTabs = jQuery('DIV.ggTabs DIV');
        var minorPanels = jQuery('DIV.ggMain DIV.ggTabContent');
        var thisParent = jQuery(this).parent('H3').parent('DIV');
        var clickedTab = minorTabs.index(thisParent);

        minorTabs.removeClass('on');
        minorTabs.eq(clickedTab).addClass('on');

        minorPanels.addClass('hiddenPanel');
        minorPanels.eq(clickedTab).removeClass('hiddenPanel');

        e.preventDefault();
    });
    $('DIV.ggTabs DIV:eq(0)').addClass('on');

    /*
    // goo games tabs
    $('DIV.ggTabContent').not('DIV.ggTabContent:eq(0)').addClass('hiddenPanel');
    $('DIV.ggTabs DIV A').click(function(e){
    var minorTabs = jQuery('DIV.ggTabs DIV');
    var minorPanels = jQuery('DIV.ggTabContent');
    var thisParent = jQuery(this).parent('H3').parent('DIV');
    var thisClass = thisParent.attr('class');
    var clickedTab = minorTabs.index( thisParent );
		
		if(thisClass.indexOf('_on') == -1){
    for(index = 0;index < minorTabs.length; index++){
    var newClass;
    thisItem = minorTabs.eq(index);
    nClass = jQuery(thisItem).attr('class');
		
				if(nClass.indexOf('_on') != -1){
    newClass = nClass.substr(0,(nClass.length - 3));
    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
    minorPanels.eq(index).addClass('hiddenPanel');
    }
		
				if(clickedTab == index){
    newClass = nClass + '_on';
    minorTabs.eq(index).addClass(newClass).removeClass(nClass);
    minorPanels.eq(index).removeClass('hiddenPanel');
    }
    }
    }
    e.preventDefault();
    });
    $('DIV.ggTabs DIV:eq(0) A').click();
    */

    // youTube video scroller
    var thisScroller = new youTubeScroller('DIV.viewList', isFirefox);

    $('DIV.controlPanel DIV A').click(function(e) {
        var action = $(this).parent('DIV').attr('class');

        switch (action) {
            case 'previous':
                {
                    move = (thisScroller.pointer > 0) ? -1 : 0;
                } break;
            case 'next':
                {
                    move = (thisScroller.pointer < thisScroller.stepCount) ? 1 : 0;
                } break;
        }
        if (move != 0) { thisScroller.doScroll(move, true); }
        e.preventDefault();
    });

    if ($('#slider').length != 0) {
        $('#slider').slider({
            value: 0,
            min: 0,
            max: thisScroller.stepCount,
            step: 1,
            // slide: function(event, ui){ console.log("slide:: ui.value:[%s]",ui.value); },
            // stop: function(event, ui){ console.log("stop:: ui.value:[%s]",ui.value); },
            change: function(event, ui) {
                if (typeof event.originalEvent == 'object' && thisScroller.pointer != ui.value) {
                    var difference = ui.value - thisScroller.pointer; // positive value = scroll to right 'previous' action
                    thisScroller.doScroll(difference, false);
                } else {
                }
            }
        });
    }

    // external site notification overlay
    var shroud = $('#mhShroud');
    var overlay = $('#mhOverlay');
    var hideSelect = false;

    $('UL.socialMediaLinks LI A,UL.footerSocialMediaLinks LI A,A.externalLink').click(function(e) {
        var externalLink = $(this).attr('href');

        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body", "html").css({ height: "100%", width: "100%" });
            $("html").css("overflow", "hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                hideSelect = true;
                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
            }
        }

        overlay.css({ marginLeft: '-288px', width: '572px' });
        if (!(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
            overlay.css({ marginTop: '-229px' });
        }

        $('#mhOverlay A.leave').attr({ 'href': externalLink });
        shroud.show();
        overlay.show();
        e.preventDefault();
    });

    // close overlay
    $('#mhOverlay A').click(function(e) {
        var linkClass = $(this).attr('class');
        if (linkClass == 'return') {
            e.preventDefault();
        }
        if (linkClass != 'leave') {
            shroud.hide();
            overlay.hide();
            if (hideSelect)
                $('#TB_HideSelect').hide();
        }
    });

    // apply sifr headers for YouTube videos and blog posts
    if (typeof sIFR == "function") {

        // This is the preferred "named argument" syntax
        sIFR.replaceElement(named({ sSelector: "H1.sifrReplace", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#05e4fd", sBgColor: "#9827b0", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));

        // sIFR.replaceElement(named({sSelector:"H2.sifrReplace", sFlashSrc:"/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor:"#ffea06", sBgColor:"#9827b0",sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0}));
        sIFR.replaceElement(named({ sSelector: "H3.sifrReplace", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#FFFFFF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));
        sIFR.replaceElement(named({ sSelector: "H3.sifrGGReplace", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#FFFFFF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));

        sIFR.replaceElement(named({ sSelector: "H2.sifrPostHeader", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#E60A6B", sBgColor: "#9827b0", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));


        sIFR.replaceElement(named({ sSelector: "P.count", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#FFFFFF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));
        sIFR.replaceElement(named({ sSelector: "SPAN.normal", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#FFFFFF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));
        sIFR.replaceElement(named({ sSelector: "SPAN.digits", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#27A1FF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 0, nPaddingBottom: 0 }));

        sIFR.replaceElement(named({ sSelector: "DIV.blank P SPAN.alpha", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#F3E909", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: 1, nPaddingBottom: 1 }));

        sIFR.replaceElement(named({ sSelector: "DIV.blank P SPAN.beta", sFlashSrc: "/assets/swf/sifr_Frankenfurter_EF_solid.swf", sColor: "#FFFFFF", sLinkColor: "#FFFFFF", sBgColor: "#332A86", sHoverColor: "#FFFFFF", sWmode: "transparent", nPaddingTop: -15, nPaddingBottom: -15 }));




        // This is the older, ordered syntax
        // sIFR.replaceElement("h5#pullquote", "tradegothic.swf", "#000000", "#000000", "#FFFFFF", "#FFFFFF", 0, 0, 0, 0);
    }

    var enableYouTubeVideos = true; // used to disable flash videos while building/testing pages - needed to due MASSIVE CPU useage when displaying 12-odd flash videos in one page
    if (enableYouTubeVideos) {
        // load YouTube movies on homepage, 'on the box' page
        var parameters = { "allowFullScreen": "true", "allowscriptaccess": "always", "wmode": "transparent" };
        var variables = {};
        $('DIV.youtubeEmbed').each(function() {
            var flashID = $(this).attr('id');
            var flashURL = $(this).text();
            swfobject.embedSWF(flashURL, flashID, 312, 200, '9.0.28', false, variables, parameters);
        });

        $('DIV.otbTabContent DIV').each(function() {
            var flashID = $(this).attr('id');
            var flashURL = $(this).text();
            if (flashURL.indexOf('http') != -1)
                swfobject.embedSWF(flashURL, flashID, 640, 385, '9.0.28', false, variables, parameters);
        });

        $('DIV.ggTabContent DIV').each(function() {
            var flashID = $(this).attr('id');
            var flashURL = $(this).text();
            if (flashURL.indexOf('http') != -1)
                swfobject.embedSWF(flashURL, flashID, 640, 385, '9.0.28', false, variables, parameters);
        });
    }
    if ($('DIV.erGameSteps').length != 0) {
        var stepsPage = new howToGooSteps();
    }

    $('A.egPDFLink').click(function(e) {
        window.open($(this).attr('href'));
        e.preventDefault();
    });
});

