if(typeof(jQ)=="undefined") var jQ=jQuery(document);
if(typeof(jQ)=="thisIsSystemPage") var thisIsSystemPage=false;

// Adicionando a função "trim" p/ os IE e os demais que não tem
if(typeof(String.prototype.trim)!=="function"){ String.prototype.trim=function(){ return this.replace(/^\s+|\s+$/g,""); }; }

var common=
{
    init:function()
    {
        common.formatHorizontalMenu();
        common.sideMenuHideItems();
        common.changeBuyQuantity();
        common.setClass();
        common.newsletter();
        common.quickView();
        common.jCycle();
    },
    ajaxStop:function()
    {
        common.quickView();
        common.pagination();
        common.boxResize();
    },
    windowOnload:function()
    {
        common.shareButtons();
    },
    setClass:function()
    {
        // Products list/table
        jQ.find(".searchResultsTime:last, .sub:last").addClass("last");
        // Banners
        jQ.find(".box-banner:first").addClass("first");
    },
    pagination:function()
    {
        jQuery(".pages .previous:not(.textRemoved), .pages .next:not(.textRemoved)").addClass("textRemoved").html("&nbsp;");
    },
	quickView:function()
	{
		jQ.find(".quickViewLink:not(.quickViewLinkActivated)").addClass("quickViewLinkActivated").bind("click",function(e){
			e.preventDefault();
			$('<iframe src="'+$(this).attr("href")+'" frameborder="0" allowtransparency="true"></iframe>').appendTo(jQ.find(".boxPopUp2-content").addClass("productQuickView"));
			common.popUp2.show({"loading":true,"openCallback":function(){ jQ.find(".boxPopUp2-wrap").css("width","860px"); }});
			common.popUp2.positioning();
			common.popUp2.close({"clean":true,"clickCallback":function(){ jQ.find(".boxPopUp2-wrap").removeAttr("style"); }});
		});
	},
    changeBuyQuantity:function()
    {
        var minus=jQ.find(".prateleira .qttArrowLeft");
        var more=jQ.find(".prateleira .qttArrowRight");
        var value=0;
        var val="";
        
        if(minus.length>0)
            minus.bind("click",function(){
                var input=jQuery(this).siblings(".amount-in-cart");
                val=input.val();
                value=parseInt(isNaN(val)?"0":val);
                input.val(value-1);
            });
        if(more.length>0)
            more.bind("click",function(){
                var input=jQuery(this).siblings(".amount-in-cart");
                val=input.val();
                value=parseInt(isNaN(val)?"0":val);
                input.val(value+1);
            });
    },
    jCycle:function()
    {
        var elem=jQ.find("div.bannersDisclaimerWrap");
        if(elem.length>0 && elem.children().length>0)
            elem.cycle({
                fx: "fade",
                speed: 1000,
                timeout: 5000,
                // next: ".bannerPrev",
                // prev: ".bannerNext",
                pager: ".pagination"
            });
    },
    newsletter:function()
    {
        var elem=jQ.find(".newsletter");
        if(elem.length>0)
        {
            function setHtml()
            {
                var error=jQ.find("input.newsletter-error");
                var success=jQ.find("input.newsletter-success");
                var success2=jQ.find("input.newsletter-success2");
                var pop1='<div class="boxPopUp2 popUpNewsletter popUpNewsletter-box"><div class="boxPopUp2-wrap"><span class="boxPopUp2-close popUpNewsletter-close">X</span><div class="boxPopUp2-content">';
                var pop2='</div></div></div><div class="boxPopUp2-overlay popUpNewsletter popUpNewsletter-overlay"></div>';
                
                if(typeof(error)!="undefined" && error.length>0 && error.val().search(/popUpNewsletter/i)<0)
                {
                    var errorText=pop1+"<div class='error'>"+error.val()+"</div>"+pop2;
                    var successText=pop1+"<div class='success'><div class='success1'>"+success.val()+"</div><div class='success2'>"+success2.val()+"</div></div>"+pop2;
                
                    error.val(errorText);
                    success.val(successText);
                    success2.val("");
                }
            }
            
            elem.delegate(".popUpNewsletter-overlay, .popUpNewsletter-close","click",function(){
                jQ.find('.newsletter .boxPopUp-overlay, .newsletter .boxPopUp').fadeOut();
                jQ.find(".newsletter-button-back").trigger("click");
                setTimeout(function(){
                    setHtml();
                    if (jQ.find("fieldset.success").length>0) document.location.reload();
                },100);
            });
            
            setHtml();
        }
    },
    formatHorizontalMenu:function()
    {
        var elem=jQ.find("ul.menu");
        var elemWidth=elem.width();
        var children=elem.children();
        var childrenLength=children.length;
        var childSize=parseInt(elemWidth/childrenLength);
        var reminder=parseInt(elemWidth%childrenLength);
        children.each(function(){
            var _this=jQuery(this);
            var thisWidth=0;
            if(reminder>0)
            {
                reminder--;
                thisWidth=1;
            }
			var borderLeft=parseInt(_this.css("border-left-width").replace("px",""));
			var borderRight=parseInt(_this.css("border-right-width").replace("px",""));
            thisWidth+=(childSize-((isNaN(borderLeft)?0:borderLeft)+(isNaN(borderRight)?0:borderRight)));
			_this.width(thisWidth);
        });
    },
    sideMenuHideItems:function()
    {
        jQ.find(".menu-departamento ul").each(function(){
            var $this=jQuery(this);
            if($this.find("li").length>3)
            {
                $this.find("li:gt(2)").hide().addClass("hide_items");
                $this.append("<li class='menu_show_all menuHidden'><a href='javascript:void(0)'>veja mais ▼</a></li><li class='menu_hidden_all hide menuHidden'><a href='javascript:void(0)'>compactar lista ▲</a></li>");
                $this.find(".menu_show_all").bind("click",function(){
                    $this.find(".hide_items").show(250);
                    jQuery(this).hide();
                    $this.find(".menu_hidden_all").show();
                });
                $this.find(".menu_hidden_all").bind("click",function(){
                    $this.find(".menu_show_all").show();
                    jQuery(this).hide();
                    $this.find(".hide_items").hide(250);
                });
            }
        });
    },
    boxResize:function()
    {
        var elemA=$(".historico-nav");
        if(elemA.length>0 && elemA.is(":not(.resized)") && elemA.text().trim().length>0)
        {
            var elemB=$(".connectWrap");
            if(elemB.length>0)
            {
                elemA.addClass("resized");
                var elemAHeight=elemA.outerHeight(true);
                var elemBHeight=elemB.outerHeight(true);
                if(elemAHeight>elemBHeight)
                    elemB.css({"height":(elemB.height()+(elemAHeight-elemBHeight))});
                else if(elemAHeight<elemBHeight)
                    elemA.css({"height":(elemA.height()+(elemBHeight-elemAHeight))});
            }
        }
    },
    shareButtons:function()
    {
		if(!thisIsSystemPage)
		{
			jQ.find("#productShareWrap").html('<div class="addthis_toolbox addthis_default_style"> <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a> <a class="addthis_button_tweet"></a> <a class="addthis_counter addthis_pill_style"></a></div>');
			jQ.find("#shareWrap").html('<div class="addthis_toolbox addthis_default_style"> <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a> <a class="addthis_button_tweet"></a> <a class="addthis_button_google_plusone" g:plusone:size="medium"></a> </div>');
			jQuery.ajax({url:"https://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e8b73fd3c73c8f9", dataType:"script", success:function(){addthis.init();}});
		}
	},
    popUp2:
    {
        positioning:function()
        {
            var _document=jQuery(document).scrollTop();
            var _window=jQuery(window).height();
            var popUp=jQ.find(".boxPopUp2");
            var popUpSize=popUp.outerHeight(true);
            var remainder=(popUpSize>=_window)?20:(_window-popUpSize)/2;
            
            popUp.css("top",(_document+remainder)+"px");
        },
        show:function(options)
        {
            options=options||{};
            jQ.find(".boxPopUp2-overlay").fadeTo("fast",0.5,function(){
                var popUp=jQ.find(".boxPopUp2").show();
                if(typeof(options.clean)=="boolean" && options.clean==true)
                    popUp.find(".boxPopUp2-content").empty();
                if(typeof(options.loading)=="boolean")
                {
                    if(options.loading!=true)
                        common.popUp2.hideLoading();
                    else
                        common.popUp2.showLoading();
                }
                else
                    common.popUp2.hideLoading();
				if(typeof(options.openCallback)=="function")
					options.openCallback();
            });
        },
        hideLoading:function()
        {
            jQ.find(".boxPopUp2-content:visible").css("background-image","none");
        },
        showLoading:function()
        {
            jQ.find(".boxPopUp2-content:visible").css("background-image",'url("/arquivos/ajax-loader.gif")');
        },
        close:function(options)
        {
            options=options||{};
            var close=function()
            {
                jQ.find(".boxPopUp2,.boxPopUp2-overlay").fadeOut("fast");
                jQ.find(".boxPopUp2 .boxPopUp2-content").empty();
                jQ.find(".boxPopUp2-content").attr("class","boxPopUp2-content");
            };

            if(typeof(options.callback)=="function") 
                options.callback(); 
            if(typeof(options.closeNow)=="boolean" && options.closeNow==true)
                close(); 

            if(jQ.find(".boxPopUp2-close.boxPopUp2-clickActive, .boxPopUp2-overlay.boxPopUp2-clickActive").length<1)
                jQ.find(".boxPopUp2-close, .boxPopUp2-overlay").addClass("boxPopUp2-clickActive").bind("click",function(){
                    if(typeof(options.clickCallback)=="function") 
                        options.clickCallback();
                    close(); 
                });
        }
    }
};

jQuery(function(){ common.init(); jQ.ajaxStop(function(){ common.ajaxStop(); }); });
jQuery(window).load(function(){common.windowOnload();});
