﻿(function ($) {
    $.jGrowl = function (m, o) {
        if ($('#jGrowl').size() == 0)
            $('<div id="jGrowl"></div>').addClass($.jGrowl.defaults.position).appendTo('body'); $('#jGrowl').jGrowl(m, o);
    }; $.fn.jGrowl = function (m, o) {
        if ($.isFunction(this.each)) {
            var args = arguments; return this.each(function () {
                var self = this; if ($(this).data('jGrowl.instance') == undefined) { $(this).data('jGrowl.instance', $.extend(new $.fn.jGrowl(), { notifications: [], element: null, interval: null })); $(this).data('jGrowl.instance').startup(this); }
                if ($.isFunction($(this).data('jGrowl.instance')[m])) { $(this).data('jGrowl.instance')[m].apply($(this).data('jGrowl.instance'), $.makeArray(args).slice(1)); } else { $(this).data('jGrowl.instance').create(m, o); } 
            });
        };
    }; $.extend($.fn.jGrowl.prototype, { defaults: { pool: 0, header: '', group: '', sticky: false, position: 'top-right', glue: 'after', theme: 'default', corners: '10px', check: 250, life: 3000, speed: 'normal', easing: 'swing', closer: true, closeTemplate: '&times;', closerTemplate: '<div>[ close all ]</div>', log: function (e, m, o) { }, beforeOpen: function (e, m, o) { }, open: function (e, m, o) { }, beforeClose: function (e, m, o) { }, close: function (e, m, o) { }, animateOpen: { opacity: 'show' }, animateClose: { opacity: 'hide'} }, notifications: [], element: null, interval: null, create: function (message, o) { var o = $.extend({}, this.defaults, o); this.notifications.push({ message: message, options: o }); o.log.apply(this.element, [this.element, message, o]); }, render: function (notification) {
        var self = this; var message = notification.message; var o = notification.options; var notification = $('<div class="jGrowl-notification ui-state-highlight ui-corner-all' +
((o.group != undefined && o.group != '') ? ' ' + o.group : '') + '">' + '<div class="close">' + o.closeTemplate + '</div>' + '<div class="header">' + o.header + '</div>' + '<div class="message">' + message + '</div></div>').data("jGrowl", o).addClass(o.theme).children('div.close').bind("click.jGrowl", function () { $(this).parent().trigger('jGrowl.close'); }).parent(); $(notification).bind("mouseover.jGrowl", function () { $('div.jGrowl-notification', self.element).data("jGrowl.pause", true); }).bind("mouseout.jGrowl", function () { $('div.jGrowl-notification', self.element).data("jGrowl.pause", false); }).bind('jGrowl.beforeOpen', function () { if (o.beforeOpen.apply(notification, [notification, message, o, self.element]) != false) { $(this).trigger('jGrowl.open'); } }).bind('jGrowl.open', function () {
    if (o.open.apply(notification, [notification, message, o, self.element]) != false) {
        if (o.glue == 'after') { $('div.jGrowl-notification:last', self.element).after(notification); } else { $('div.jGrowl-notification:first', self.element).before(notification); }
        $(this).animate(o.animateOpen, o.speed, o.easing, function () {
            if ($.browser.msie && (parseInt($(this).css('opacity'), 10) === 1 || parseInt($(this).css('opacity'), 10) === 0))
                this.style.removeAttribute('filter'); $(this).data("jGrowl").created = new Date();
        });
    } 
}).bind('jGrowl.beforeClose', function () {
    if (o.beforeClose.apply(notification, [notification, message, o, self.element]) != false)
        $(this).trigger('jGrowl.close');
}).bind('jGrowl.close', function () {
    $(this).data('jGrowl.pause', true); $(this).animate(o.animateClose, o.speed, o.easing, function () {
        $(this).remove(); var close = o.close.apply(notification, [notification, message, o, self.element]); if ($.isFunction(close))
            close.apply(notification, [notification, message, o, self.element]);
    });
}).trigger('jGrowl.beforeOpen'); if ($.fn.corner != undefined) $(notification).corner(o.corners); if ($('div.jGrowl-notification:parent', self.element).size() > 1 && $('div.jGrowl-closer', self.element).size() == 0 && this.defaults.closer != false) { $(this.defaults.closerTemplate).addClass('jGrowl-closer ui-state-highlight ui-corner-all').addClass(this.defaults.theme).appendTo(self.element).animate(this.defaults.animateOpen, this.defaults.speed, this.defaults.easing).bind("click.jGrowl", function () { $(this).siblings().children('div.close').trigger("click.jGrowl"); if ($.isFunction(self.defaults.closer)) { self.defaults.closer.apply($(this).parent()[0], [$(this).parent()[0]]); } }); };
    }, update: function () {
        $(this.element).find('div.jGrowl-notification:parent').each(function () { if ($(this).data("jGrowl") != undefined && $(this).data("jGrowl").created != undefined && ($(this).data("jGrowl").created.getTime() + $(this).data("jGrowl").life) < (new Date()).getTime() && $(this).data("jGrowl").sticky != true && ($(this).data("jGrowl.pause") == undefined || $(this).data("jGrowl.pause") != true)) { $(this).trigger('jGrowl.beforeClose'); } }); if (this.notifications.length > 0 && (this.defaults.pool == 0 || $(this.element).find('div.jGrowl-notification:parent').size() < this.defaults.pool))
            this.render(this.notifications.shift()); if ($(this.element).find('div.jGrowl-notification:parent').size() < 2) { $(this.element).find('div.jGrowl-closer').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function () { $(this).remove(); }); } 
    }, startup: function (e) { this.element = $(e).addClass('jGrowl').append('<div class="jGrowl-notification"></div>'); this.interval = setInterval(function () { $(e).data('jGrowl.instance').update(); }, this.defaults.check); if ($.browser.msie && parseInt($.browser.version) < 7 && !window["XMLHttpRequest"]) { $(this.element).addClass('ie6'); } }, shutdown: function () { $(this.element).removeClass('jGrowl').find('div.jGrowl-notification').remove(); clearInterval(this.interval); }, close: function () { $(this.element).find('div.jGrowl-notification').each(function () { $(this).trigger('jGrowl.beforeClose'); }); } 
    }); $.jGrowl.defaults = $.fn.jGrowl.prototype.defaults;
})(jQuery);

/*
*   Get current page name (e.g: home.aspx )
*   Note: ONLY get name of non-url-rewritten pages
*/
function getPageName() {
    var sPathName = window.location.pathname;
    var sPageName = sPathName.substring(sPathName.lastIndexOf('/') + 1);
    return sPageName;
}

/*
*   Get domain name
*/
function getDomainName() {
    var domainName = window.document.domain;
    if (domainName == "localhost")
        domainName = "http://localhost:56121";
    else
        domainName = "http://" + domainName;
    return domainName;
}

function AttachWatermark(controlId, hint) {
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    if ($('#' + controlId).val() != "" && $('#' + controlId).val() != hint) {
        $('#' + controlId).removeClass("watermarkOn");        
    }

    $('#' + controlId).focus(function () {
        $(this).filter(function () {
            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == "" || $(this).val() == hint
        }).removeClass("watermarkOn").val("");
    });
        

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    $('#' + controlId).blur(function () {
        $(this).filter(function () {
            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""
        }).addClass("watermarkOn").val(hint);
    });
}

$(document).ready(function () {

    var currentPageName = getPageName();

    if ($('#ctl00_txtSearchBox').length != 0)
        AttachWatermark("ctl00_txtSearchBox", "Search for products");
    else if ($('#ctl00_ctl00_txtSearchBox').length != 0)
        AttachWatermark("ctl00_ctl00_txtSearchBox", "Search for products");

    if ($('#ctl00_ContentMain_plProducts_msgNoProducts').length != 0) {
        $("#ctl00_ContentMain_SortBy").hide();
    } else {
        $("#ctl00_ContentMain_SortBy").show();
    }

    /* jGrowl notes */
    /* Note: product.aspx, "1 item added" to the basket */
    if ($("#ctl00_ContentMain_hfdAddToBasketStatus").length) {
        if ($("#ctl00_ContentMain_hfdAddToBasketStatus").val() == "add") {
            $.jGrowl(
                "1 item added <br /><br /><a style='color:#525252;'href='" + getDomainName() + "/store/basket.aspx'>Click to checkout now!</a>",
                { header: "My Basket", life: 15000 }
            );
        } 
    }

    if (currentPageName == "basket.aspx") { /* Basket (basket.aspx) */
        /*
        * Checkout step1: Basket
        */
        /* Hide main update button (all product line update buttons call this one) */
        if ($('#ctl00_ContentMain_btnUpdateBasket').length) {
            $('#ctl00_ContentMain_btnUpdateBasket').hide();
        }

        /*
        * Checkout step2: Shipping details
        */
        /* Shipping details watermark */
        if ($('#ctl00_ContentMain_txtPhone').length)
            AttachWatermark("ctl00_ContentMain_txtPhone", "e.g. (03) 1234 5678");

        /* Choose default 1 national delivery option (the first one) */
        if ($('#ctl00_ContentMain_rblNationalDeliveryOptions').length) {
            var isRadioSelected = false;
            for (var i = 0; i < $('#ctl00_ContentMain_rblNationalDeliveryOptions input:radio').length; i++) {
                if ($('#ctl00_ContentMain_rblNationalDeliveryOptions_' + i).attr('checked') == true) {
                    isRadioSelected = true;
                }
            }
            if (!isRadioSelected)
                $('#ctl00_ContentMain_rblNationalDeliveryOptions_0').attr('checked', true);
        }
        /* Choose default 1 international delivery option (the first one) */
        if ($('#ctl00_ContentMain_rblInternationalDeliveryOptions').length) {
            var isRadioSelected = false;
            for (var i = 0; i < $('#ctl00_ContentMain_rblInternationalDeliveryOptions input:radio').length; i++) {
                if ($('#ctl00_ContentMain_rblInternationalDeliveryOptions_' + i).attr('checked') == true) {
                    isRadioSelected = true;
                }
            }
            if (!isRadioSelected)
                $('#ctl00_ContentMain_rblInternationalDeliveryOptions_0').attr('checked', true);
        }

        /*
        * Checkout step3: Order Summary
        */
        /* Add PayPal submission form */
        if ($('#ctl00_ContentMain_rblPaymentOptions').length) {

            $('#ctl00_ContentMain_btnCheckoutOthers').hide();

            if ($.isFunction(addPaypalForm)) {
                addPaypalForm();
            }
        }

        /* Toggle between checkout buttons: (1)PayPal/Credit card or (2)Bank transfer/Cheque/Money order */
        $('#ctl00_ContentMain_rblPaymentOptions input:radio').click(function () {
            /* Paypal/Credit card: Go to paypal payment handling page */
            if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "paypal") {
                $('#ctl00_ContentMain_btnCheckoutOthers').hide();
                $('#ctl00_ContentMain_btnCheckout').show();
            }
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "credit_card") {
                $('#ctl00_ContentMain_btnCheckoutOthers').hide();
                $('#ctl00_ContentMain_btnCheckout').show();
            }
            /* Bank transfer/Cheque/Money order: Send emails to both customer and KidsBags */
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "bank_transfer") {
                $('#ctl00_ContentMain_btnCheckoutOthers').show();
                $('#ctl00_ContentMain_btnCheckout').hide();
            }
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "cheque_money_order") {
                $('#ctl00_ContentMain_btnCheckoutOthers').show();
                $('#ctl00_ContentMain_btnCheckout').hide();
            }
        });

        /* Process checkout */
        $('#ctl00_ContentMain_btnCheckout').click(function (event) {
            event.preventDefault();
            /* Go to paypal */
            if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "paypal") {
                $('#paypalCartUpload').submit();
            }
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "credit_card") {
                $('#paypalCartUpload').submit();
            }
            /* or, Send emails */
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "bank_transfer") {
                __doPostBack("PayByOthers", "");
            }
            else if ($('#ctl00_ContentMain_rblPaymentOptions input:radio:checked').val() == "cheque_money_order") {
                __doPostBack("PayByOthers", "");
            }
        });
    }

});
