﻿(function($) {

    function CreateFadePopup(popupelement, openelements, closeelements, focuselement, speed) {
        $(openelements).click
        (function() {
            if ($(openelements).hasClass("active")) {
                popupelement.fadeOut(speed);
                $(openelements).removeClass("active");
                return false;
            }
            popupelement.fadeIn(speed);
            $(openelements).addClass("active");
            $(focuselement).focus();
            return false;
        });

        $(closeelements).click
        (function() {
            popupelement.fadeOut(speed);
            $(openelements).removeClass("active");
            return false;
        });

        return popupelement;
    }

    $.fn.fadepopup = function(openelements, closeelements, focuselement, speed) {
        return CreateFadePopup(this, openelements, closeelements, focuselement, speed);
    }
})(jQuery);

(function($) {

    function Popup(popupelement, openelements, closeelements, focuselement, speed) {
        $(openelements).click
        (function() {
            if ($(openelements).hasClass("active")) {
                popupelement.hide(speed);
                $(openelements).removeClass("active");
                return false;
            }
            popupelement.show(speed);
            $(openelements).addClass("active");
            $(focuselement).focus();
            return false;
        });

        $(closeelements).click
        (function() {
            popupelement.hide(speed);
            $(openelements).removeClass("active");
            return false;
        });

        return popupelement;
    }

    $.fn.popup = function(openelements, closeelements, focuselement, speed) {
        return Popup(this, openelements, closeelements, focuselement, speed);
    }
})(jQuery);

(function($) {

    function CreateSlidingPopup(popupelement, openelements, closeelements, animationspeed) {
        $(openelements).click(function() {
            if ($(openelements).hasClass("active")) {
                popupelement.slideUp(animationspeed);
                if ($(openelements).hasClass("cartPanelLink")) {
                    $('.shopingBagContainer').removeClass("showShopingBagLayer");
                }
                $(openelements).removeClass("active");
                return false;
            }

            if ($(openelements).hasClass("cartPanelLink")) {
                $('.shopingBagContainer').addClass("showShopingBagLayer");
            }
            $(openelements).addClass("active");
            popupelement.slideDown(animationspeed);

            this.focus();
            return false;
        });

        $(closeelements).click(function() {
            popupelement.slideUp(animationspeed);
            if ($(openelements).hasClass("cartPanelLink")) {
                $('.shopingBagContainer').removeClass("showShopingBagLayer");
            }
            $(openelements).removeClass("active");
            return false;
        });

        return popupelement;
    }

    $.fn.slidingpopup = function(openelements, closeelements, animationspeed) {
        return CreateSlidingPopup(this, openelements, closeelements, animationspeed);
    }

})(jQuery);

function CreatePulPopup() {
    $('A[rel="pulpopup"]').click(function() {
        window.open('/misc/PulPopup.aspx', 'conditions', 'scrollbars=yes,toolbar=no,menubar=no,width=615,height=300,left=' + ((screen.width - 615) / 2) + ',top=' + ((screen.height - 300) / 2));
        return false;
    });
}


(function($) {
    $.extend($.fn, {
        defaultButton: function(selector) {
            this.keypress(function(e) {
                if (e.which == 13 && e.target.type != 'textarea') {
                    var tmpBtn = $(this).find(selector);
                    var ret = $(tmpBtn[0]).click();
                    if (ret && tmpBtn[0].type != 'submit')
                        eval(unescape(tmpBtn[0].href));
                    return false;
                }
            });
        }
    });
})(jQuery);

(function($) {
    $.fn.center = function(width, height) {
        this.css('position', 'absolute');
        this.css({ top: ($(window).height() - (height ? height : this.height())) / 2 + $(window).scrollTop() + 'px',
            left: ($(window).width() - (width ? width : this.width())) / 2 + $(window).scrollLeft() + 'px'
        });
        return this;
    }
})(jQuery);

jQuery.extend(jQuery.expr[":"], { reallyhidden: "(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length)" });
