jQuery.fn.radioImages = function () {
    var $this = this;
    $this.find('input:radio').hide().filter(':checked').parents('li:first').addClass('selected');
    $this.find('input:radio').change(function () {
        $this.find('li.selected').removeClass('selected');
        $this.find('li:has(input:checked)').addClass('selected');
    });
    if ($.browser.msie) {
        $this.find('li').click(function (e) {
            $(this).parent().find('li.selected').removeClass('selected');
            $(this).addClass('selected');
            $(this).find('input:radio').attr('checked', 'checked');
            e.preventDefault();
        });
    }
    return this;
};

$(document).ready(function () {
    if (typeof (theForm) === "undefined" || (typeof (Page_ValidationActive) !== "undefined" && !Page_ValidationActive)) {
        return;
    }
    if (typeof (Page_Validators) === "undefined" || Page_Validators.length == 0) {
        return;
    }
    var validators = Page_Validators;
    $(validators).each(function () {
        var val = $(this);
        if (val.css('visibility') === 'hidden') {
            val.hide().css({ visibility: 'visible' });
        }
    });
    window.ValidatorUpdateDisplay = function (val) {
        val.style.display = val.isvalid ? "none" : "block";
    }
});

$(document).ready(function () {
    $("#jquery_jplayer").jPlayer({
        ready: init,
        customCssIds: true,
        preload: "auto",
        oggSupport: true,
        swfPath: "/Content/javascript"
    });

    var audioDir = "/Content/audio/";

    var enabled = $.cookie('sound_enabled') !== "false";

    function playSound(file) {
        if (!enabled) return;
        $("#jquery_jplayer").jPlayer("setFile", file + ".mp3", file + ".ogg").jPlayer("play");
    }

    function stopSound() {
        $("#jquery_jplayer").jPlayer("pause");
    }

    function enableSound() {
        $.cookie('sound_enabled', null, { path: '/', domain: '.beano.com' });
        enabled = true;
    }

    function disableSound() {
        stopSound();
        $.cookie('sound_enabled', 'false', { expires: 365, path: '/', domain: '.beano.com' });
        enabled = false;
    }

    function setText() {
        if (enabled) {
            $('#sound-control a').html('<span /> Turn Sound Off');
        } else {
            $('#sound-control a').html('<span /> Turn Sound On');
        }
        Cufon.replace('#sound-control a', { fontFamily: 'CreativeBlock BB', hover: true });
    }

    function init() {
        $("a[data-splat]").each(function () {
            var audioFile = audioDir + $(this).data('splat');
            $(this).hover(function () {
                playSound(audioFile);
            }, function () {
                stopSound();
            });
        });

        $('#sound-control a').click(function () {
            if (enabled) {
                disableSound();
            } else {
                enableSound();
            }
            setText();
            return false;
        });
        setText();
    }
});

/* Featured */
$(function () {
    $("div.slidetabs").tabs(".images > div.images-holder", {
        effect: 'fade',
        fadeInSpeed: 1000,
        fadeOutSpeed: 1000,
        rotate: true
    }).slideshow({ autoplay: true, interval: 5000, clickable: false });
});

/* listers */
$(function () {
    // custom easing called "custom"
    $.easing.custom = function (x, t, b, c, d) {
        var s = 1.70158;
        if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
        return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    }

    $("div.scrollable").scrollable({ easing: 'custom', speed: 700, circular: true });
    $("div.product-scrollable").scrollable({ easing: 'custom', speed: 700, circular: true });
    $(".overlay-wrapper a").hover(function () {
        $(".overlay").hide();
        $(this).children(".overlay").fadeTo(400, 1);
    }, function () { $(this).children(".overlay").hide(); });
});

/* Ticki */
(function ($) {
    var theCharacterTimeout = 50;
    var theStoryTimeout = 5000;
    var theWidgetOne = "_";
    var theWidgetTwo = "-";
    var theWidgetNone = "";
    var theLeadString = "";
    var theItemCount = 0;

    // Ticker startup
    function startTicker() {
        // Define run time values
        theCurrentStory = -1;
        theCurrentLength = 0;
        theItemCount = theSummaries.length;
        // Locate base objects
        if (document.getElementById) {
            theAnchorObject = document.getElementById("tickerAnchor");
            runTheTicker();
        }
        else {
            document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
            return true;
        }
    }
    // Ticker main run loop
    function runTheTicker() {
        var myTimeout;
        // Go for the next story data block
        if (theCurrentLength == 0) {
            theCurrentStory++;
            theCurrentStory = theCurrentStory % theItemCount;
            theStorySummary = theSummaries[theCurrentStory].replace(/&quot;/g, '"');
            thePrefix = "<span class=\"tickls\">" + theLeadString + "</span>";
        }
        // Stuff the current ticker text into the anchor
        theAnchorObject.innerHTML = thePrefix +
theStorySummary.substring(0, theCurrentLength) + whatWidget();
        // Modify the length for the substring and define the timer
        if (theCurrentLength != theStorySummary.length) {
            theCurrentLength++;
            myTimeout = theCharacterTimeout;
        }
        else {
            theCurrentLength = 0;
            myTimeout = theStoryTimeout;
        }
        //Cufon.refresh('.ticki');
        // Call up the next cycle of the ticker
        setTimeout(runTheTicker, myTimeout);
    }
    // Widget generator
    function whatWidget() {
        if (theCurrentLength == theStorySummary.length) {
            return theWidgetNone;
        }

        if ((theCurrentLength % 2) == 1) {
            return theWidgetOne;
        }
        else {
            return theWidgetTwo;
        }
    }

    $(function () {
        if (!$('body').hasClass('no-ticki')) {
            startTicker();
        }
    });
})(jQuery);

/**Retro Decade Nav **/
$(function() {
	/**
	* for each menu element, on mouseenter, 
	* we enlarge the image, and show both sdt_active span and 
	* sdt_wrap span. If the element has a sub menu (sdt_box),
	* then we slide it - if the element is the last one in the menu
	* we slide it to the left, otherwise to the right
	*/
    $('#sdt_menu > li').bind('mouseenter',function(){
		var $elem = $(this);
		$elem.find('img')
				.stop(true)
				.animate({
				'width':'100px',
				'height':'100px',
				'left':'0px'
				},400,'easeOutBack')
				.andSelf()
				.find('.sdt_wrap')
				.stop(true)
				.animate({'top':'140px'},500,'easeOutBack')
				.andSelf()
				.find('.sdt_active')
				.stop(true)
				.animate({'height':'100px'},300,function(){
			    var $sub_menu = $elem.find('.sdt_box');
			    if($sub_menu.length){
				var left = '100px';
				if($elem.parent().children().length == $elem.index()+1)
				left = '-100px';
				$sub_menu.show().animate({'left':left},200);
			}	
		});
	}).bind('mouseleave',function(){
		var $elem = $(this);
		var $sub_menu = $elem.find('.sdt_box');
		if($sub_menu.length)
			$sub_menu.hide().css('left','0px');
					
		$elem.find('.sdt_active')
				.stop(true)
				.animate({'height':'0px'},300)
				.andSelf().find('img')
				.stop(true)
				.animate({
				'width':'0px',
				'height':'0px',
				'left':'50px'},400)
				.andSelf()
				.find('.sdt_wrap')
				.stop(true)
				.animate({'top':'80px'},500);
	});
});
