var $J = jQuery.noConflict();
/*
* EQUAL HEIGHT
*/
$J.fn.equalHeight=function(){var height=0;var maxHeight=362;this.each(function(){height=$J(this).outerHeight();maxHeight=(height>maxHeight)?height:maxHeight});return this.each(function(){var t=$J(this);var minHeight=maxHeight-(t.outerHeight()-t.height());var property=$J.browser.msie&&$J.browser.version<7?'height':'min-height';t.css(property,minHeight+'px')})};

var LAS = {
	carousel : {
	    init: function() {
	        if($J(".carousel")) {
	            LAS.carousel.getSize();
	            $J(".carousel .items").each(function(i) {
	               if ($J(this).children().length <= LAS.carousel.settings.size) {
	                   $J(this).parent().parent().find(".prev, .next, .prevPage, .nextPage").hide();
	               }
	               i++;
	            });
	            $J(".carousel").scrollable(this.settings).circular();
	        }
	    },
	    settings : {
	        activeClass: "active",
	        api: null,
	        clickable: false,
	        disabledClass: "_no-display_",
	        easing: "linear",
	        hoverClass: null,
	        item: null,
	        items: ".items",
	        keyboard: true,
	        loop: false,
	        nextPage: ".nextPage",
	        next: ".next",
	        prevPage: ".prevPage",
	        prev: ".prev",
	        speed: 400,
	        vertical: false
	    },
	    getSize : function(){
	        if ($J(".cart .carousel").length > 0){
	            LAS.carousel.settings.size = 2;
	            LAS.carousel.settings.keyboardSteps = 2;
	        }else if($J(".bestsellers .carousel").length > 0) {
	        	LAS.carousel.settings.size = 3;
	        	LAS.carousel.settings.keyboardSteps = 3;
	        }else if($J(".pro-carousel .carousel").length > 0) {
	        	LAS.carousel.settings.size = 4;
	        	LAS.carousel.settings.keyboardSteps = 4;
	        }else if($J(".new-arrival-carousel .carousel").length > 0) {
	        	LAS.carousel.settings.size = 1;
	        	LAS.carousel.settings.keyboardSteps = 1;
	        }else if($J(".brands-carousel .carousel").length > 0) {
	        	LAS.carousel.settings.size = 3;
	        	LAS.carousel.settings.keyboardSteps = 3;
            }else if($J(".featured_style .carousel").length >0){
                LAS.carousel.settings.size = 3;
	        	LAS.carousel.settings.keyboardSteps = 3;
	        }else{
	        	LAS.carousel.settings.size = 6;
	        	LAS.carousel.settings.keyboardSteps = 6;
	        }
	    }
	},
	tabGen : {
        init : function(tabs, panes) {
            tabs = tabs ? tabs : "ul.tabs";
            panes = panes ? panes : "div.panes";
            if($J(tabs).length) {
                this.api = $J("ul.tabs").tabs(panes+" > div", this.config);
            }
        },
        config : {api:true},
        slideShow : function() {
            $J(".slidetabs").tabs(".slide_images > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});
            $J(".brandslidetabs").tabs(".brand_slide_images > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});            
            $J(".brandslidetabs2").tabs(".brand_slide_images2 > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});            
            $J(".brandslidetabs3").tabs(".brand_slide_images3 > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});            
            $J(".brandslidetabs4").tabs(".brand_slide_images4 > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});            
            $J(".brandslidetabs5").tabs(".brand_slide_images5 > div", {
                // enable "cross-fading" effect
                effect: 'fade',
                fadeOutSpeed: "slow",
                rotate: true
            }).slideshow({autoplay: true, interval: 3000});                        
        },
        showReviews : function() {
            this.api.click('#reviews');
        }
    },
    addToCartButtons : function () {
        if ($J(".btn-cart").length > 0 || $J(".btn-cart-lg").length > 0) {
            var cart = $J('#perpetual_cart');
            $J(".btn-cart, .btn-cart-lg").click(function(e) {
             var productAddToCartForm = new VarienForm('product_addtocart_form');
            // Added for form validation on product page
             if(!productAddToCartForm.validator.validate()){
                 return;
             };
                if (!$J("#product-options-wrapper div").hasClass("validation-error")){
                e.preventDefault();
                //Validator.prototype();
                if ($J(this).hasClass('btn-cart-lg')) {
                    var params = '';
                    $J('#product_addtocart_form input:radio:checked, #product_addtocart_form input:not(":radio"), #product_addtocart_form select').each(function(i) {
                        if (this.value != '') {
                            if (i != 0) { params += '&' }
                            params += this.name + '=' + this.value;
                        }
                    });
                }
                else {
                    params = 'product=' + this.id + '&qty=1';
                }

                $J.ajax({
                    type: "GET",
                    //url: "/ajaxCart.php",
                    url: "/ajaxcart/perpcart/add",
                    beforeSend: function() {
                        $J('#perpetual_cart .cart-link').addClass('loading');                    
                        $J('#perpetual_cart #my-cart').addClass('loading');
                        $J('html,body').animate({'scrollTop':'0px'}, 750);
                    },
                    data: params,
                    success: function(data, textStatus){
                        var data = $J.parseJSON(data);
                        if (textStatus == "error"){
                            $J('#perpetual_cart .cart-link').removeClass('loading');                                                
                            $J('#perpetual_cart #my-cart').removeClass('loading');
                            alert("There was an error adding this item to your cart.  Please call customer service for assistance.", "Error");
                            return;
                        }
                        if (data.result == "error"){
                            $J('#perpetual_cart .cart-link').removeClass('loading');                        
                            $J('#perpetual_cart #my-cart').removeClass('loading');
                            alert("Sorry, an error occurred while adding the item to your cart.  The error was: '" + data.message + "'");
                            return;
                        }
                        $J.ajax({
                            type: "GET",
                            //url: "/updateCart.php",
                            url: "/ajaxcart/perpcart/update",
                            dataType: 'html',
                            success: function(msg){
                                cart.html(msg);
                                Enterprise.TopCart.showCart(7);
                            }
                        });
                    }
                });
            };
            });
        }
        if ($J('#perpetual_cart .btn-remove').length > 0) {
            $J("#perpetual_cart .btn-remove").each(function() {
                $J(this).click(function(){
                    e.preventDefault();
                    $J('#perpetual_cart #pc_container').addClass('loading');
                    var removeLink = $J(this).attr("href");
                    $J.ajax({
                        type: "GET",
                        url: removeLink,
                        success: function() {
                            $J.ajax({
                                type: "GET",
                                //url: "/updateCart.php",
                                url: "/ajaxcart/perpcart/update",
                                dataType: 'html',
                                success: function(msg){
                                    cart.html(msg);
                                }
                            });
                        }
                    });
                });
            });
        }
    },
    megaMenu : {
        init: function(menu) {
            //var userAgent = navigator.userAgent.toLowerCase();
            //$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
            menu.megamenu();
//            if($J.browser.msie){
//                $J("ul#ddli li.mega div").css('top', '60px');
//            }
//            else if($J.browser.webkit) {
//                $J("ul#ddli li.mega div").css('top', '-30px');
//            }
//            else if($J.browser.mozilla) {
//                $J("ul#ddli li.mega div").css('top', '-20px');
//            }
            var megaConfig = {
                interval: 300,
                sensitivity: 6,
                over: LAS.megaMenu.addMega,
                timeout: 600,
                out: LAS.megaMenu.removeMega
            };
            var megaCommunityConfig = {
                interval: 300,
                sensitivity: 6,
                over: LAS.megaMenu.addMegaCommunity,
                timeout: 600,
                out: LAS.megaMenu.removeMegaCommunity
            };
            $J("li.mega").hoverIntent(megaConfig);
            $J("li.mega_community").hoverIntent(megaCommunityConfig);
        },
        initMenu : function(menu) {
            return menu.each(function() {
                $J('.menuitem', this).hide();
                $J('li.expand > .menuitem', this).show();
                $J('li a', this).click(function(e) {
                    var $this = $J(this);
                    var $previous = $this.parent();
                    $this.addClass('current');

                    if ($previous.hasClass('level1') && $this.hasClass('current')) {
                        $J('li.level1 a.current').addClass('active');
                        $J('li.level2 a.current').removeClass('active_l2');
                    }
                    else if ($previous.hasClass('level2') && $this.hasClass('current')) {
                        $J('li.level2 a.current').addClass('active_l2');
                    }

                    e.stopImmediatePropagation();
                    var theElement = $this.next();
                    var parent = $this.parent().parent();

                    if (parent.hasClass('noaccordion')) {
                        if (theElement[0] === undefined) {
                            window.location.href = this.href;
                        }
                        theElement.slideToggle('normal', function() {
                            var slideThis = $J(this);
                            if (slideThis.is(':visible')) {
                               slideThis.prev().addClass('active');
                            }
                            else {
                               //slideThis.prev().removeClass('active');
                            }
                        });
                        return false;
                    }
                    else if ( theElement.hasClass('menuitem') ) {
                        if (theElement.is(':visible')) {
                            if (parent.hasClass('collapsible')) {
                                $J('.menuitem:visible', parent).slideUp('normal', function() {
                                    if ($previous.hasClass('level1')) {
                                        $J('li.level1 a.current').removeClass('active');
                                        $J('li.level1 a.current').removeClass('current');
                                    }
                                    else if ($previous.hasClass('level2')) {
                                        $J('li.level2 a.current').removeClass('active_l2');
                                        $J('li.level2 a.current').removeClass('current');
                                    }
                                });
                                return false;
                            }
                            return false;
                        }
                        else {
                            $J('.menuitem:visible', parent).slideUp('normal', function() {
                                if ($previous.hasClass('level1')) {
                                    $J('li.level1 a.current').removeClass('active');
                                    $J('li.level1 a.current').removeClass('current');
                                }
                                else if ($previous.hasClass('level2')) {
                                    $J('li.level2 a.current').removeClass('active_l2');
                                    $J('li.level2 a.current').removeClass('current');
                                }
                            });
                            $J('ul.menu ul.menuitem li:has(ul)').addClass('parentMenu');
                            theElement.slideDown('normal', function() {
                                if ($previous.hasClass('level1')) {
                                    $this.addClass('current');
                                    $J('li.level1 a.current').addClass('active');
                                }
                                else if ($previous.hasClass('level2')) {
                                    $this.addClass('current');
                                    $J('li.level2 a.current').addClass('active_l2');
                                }
                            });
                            return false;
                        }
                    }
                });
            });
        },
        addMega : function() {
            $J(this).addClass("hovering");
            $J("ul#ddli li.mega a#shopby").removeClass("shopby");
            $J("ul#ddli li.mega a#shopby").addClass("shopbyactive");
        },
        removeMega : function() {
            $J(this).removeClass("hovering");
            $J("ul#ddli li.mega a#shopby").addClass("shopby");
            $J("ul#ddli li.mega a#shopby").removeClass("shopbyactive");
        },
        addMegaCommunity : function() {
            $J(this).addClass("community_hover");
        },
        removeMegaCommunity : function() {
            $J(this).removeClass("community_hover");
        }
    },
    perpetualCartActions : function() {
        $J("#checkout-link-top").click(function(){
            $J("#my-cart").addClass('cart-header-active');
        });
        $J("#close_cart").click(function(){
            $J("#my-cart").removeClass('cart-header-active');
        });
        $J('body').click(function(e) {
            if (!$J('body#click-mask').length) { return; }
            var target = e.target ? e.target : e.srcElement;
            if (!$J(target).closest('#perpetual_cart').length) { Enterprise.TopCart.hideCart(); }
        });
        
        $J('#perpetual_cart, #pc_container').hover(
                function() {
                    if (Enterprise.TopCart.interval !== null) {                   
                         clearTimeout(Enterprise.TopCart.interval);
                         Enterprise.TopCart.interval = null;
                    }
                },
                function() {
                    if($J(Enterprise.TopCart.elementHeader).hasClass('expanded')) {
                      /* Enterprise.TopCart.interval = setTimeout('Enterprise.TopCart.hideCart()', 2000);*/
                    }
                }
        );
    }
};

$J(document).ready(function() {
    $J("#nav li").each( function(){
        if($J(this).hasClass('active')){
         $J(this).children().addClass('active');
        }
    });

    if ($J(".slidetabs").length) {
        LAS.tabGen.slideShow();
    }
    if ($J(".carousel").length) {
        LAS.carousel.init();
    }
    if ($J("ul.tabs").length) {
        LAS.tabGen.init();
    }
    if ($J('.menu').length) {
        LAS.megaMenu.initMenu($J('.menu'));
    }

    $J(".butPause").click(function(event) {
        event.preventDefault();
        $J(".slidetabs").data("slideshow").stop();
        $J(this).css('display', 'none');
        $J(".butPlay").css('display', 'block');

    });

    $J(".butPlay").click(function(event) {
        event.preventDefault();
        $J(".slidetabs").data("slideshow").play();
        $J(this).css('display', 'none');
        $J(".butPause").css('display', 'block');
    });

	// Prevent slideshow from proceeding to next slide when clicking one of the slides
	$J(".slide_images div").unbind('click');
	$J(".slide_images div").click(function(event) {
		$J('.butPause').click();
	});

    //$J('.products-grid li').equalHeight();
    $J('li div.product-box').equalHeight();
	$J('ul.checkout-types li:eq(1)').addClass("checkout-types-li");
    LAS.megaMenu.init($J('#nav'));
    LAS.addToCartButtons();
    LAS.perpetualCartActions();

    $J('img').bind('contextmenu', function(e){
        return false;
    });

    $J('.opc-progress-wrapper .callout').click(function(event){
        event.preventDefault();
        OpenLHNChat();
    });
});


