// Radio Images
jQuery.fn.radioImages = function () {
    var $this = this;
    $this.addClass('radio-images');
    $this.find('input:radio').filter(':checked').parents('li:first').addClass('selected');
    $this.find('label').click(function () {
        $this.find('li.selected').removeClass('selected');
        $this.find('li:has(input:checked)').addClass('selected');
    });
    // Dumb ass IE fixing
    if ($.browser.msie) {
        var width = $this.find('label img').width();
        var height = $this.find('label img').height();
        $this.find('label').each(function () {
            var image = $(this).find('img');
            $('<span class="target" />').css({ width: width + 'px', height: height + 'px', background: 'url(' + image.attr('src') + ') no-repeat' }).attr('title', image.attr('alt')).insertBefore(image);
            image.remove();
        });
    } else {
        $this.find('img').addClass('target');
    }
    return this;
};

// Fixing WebForms validation
$(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";
    }
});

// Featured gallery
$(document).ready(function () {
    if ($("#slideshow-featured .images > *").length > 1) {
        var effect = $.browser.msie && parseInt($.browser.version) < 9 ? "default" : "fade";
        $("div.slidetabs").tabs(".images > div", {
            effect: effect,
            fadeInSpeed: 2000,
            fadeOutSpeed: 2000,
            rotate: true
        }).slideshow({ autoplay: true, interval: 10000, clickable: false });
    } else {
        $(".images :first-child").show();
    }
});

// Search and comments
$(document).ready(function () {
    $('#search').submit(function () {
        $(this).find(':submit').attr('disabled', 'disabled');
    });

    var now = new Date();
    var midnightLastNight = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var midnightTwoNightsAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
    $('.comment-date-time[rel!=""]').each(function () {
        var $this = $(this);
        var date = new Date($this.attr('rel'));
        if (date > midnightTwoNightsAgo) {
            var daySignifier = date > midnightLastNight ? "Today" : "Yesterday";
            var hour = (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()) + 1;
            var minute = date.getMinutes();
            var ampm = date.getHours() > 12 ? "pm" : "am";
            var text = daySignifier + " at " + hour + ":" + minute + ampm;
            $this.text(text);
        }
    });
});

// Fallback support for placeholder attribute
jQuery(function () {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if ('placeholder' in test) jQuery.support.placeholder = true;
});
$(function () {
    if (!$.support.placeholder) {
        var active = document.activeElement;
        $('input:text[placeholder]').focus(function () {
            if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('input:text').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function () { $(this).val(''); });
        });
    }
});

// Stockists
$(function () {
    $('a.stockists').fancybox({ width: 850, height: 600, type: 'iframe' });
});
