var site_folder = "/";


function catalog_list_style(){
    //catalog list effects
    $("#products_list li").bind("mouseover",function() {
        $(this).children().children(".product_name").addClass("product_name_over");
    }).bind("mouseout", function() {
        $(this).children().children(".product_name").removeClass("product_name_over");
    });
}

//remove jquery validate messages
$.extend($.validator.messages, {
    required: "",
    email: "",
    digits: "",
    maxlenght: "",
    minlenght: ""
});

$(window).load(function() {
    $(document).bgStretcher({
        images: ['/templates/images/bg.jpg'], imageWidth: 1024, imageHeight: 768
    });


    //
    //basic styling

    $("#categories_top_menu > li").each(function() {
        if($(this).hasClass("selected")) {
            $(this).children("div").children("a").css({"color": $(this).children("div").attr("color")});
        }else{
            $(this).children("div").children("a").css({"color": "#ccc"});
        }
    });

    $("a[rel=portfolio]").fancybox();
    $("a[rel=preview]").fancybox();
    $("a[rel=product_preview]").fancybox();

    $("a[rel=login_dialog]").fancybox({
        onStart : function() {
             $("#dialog_login").show();
             $.fancybox.resize(); },
        onCleanup : function() {
            $("#dialog_login").hide();
        }
    });
    $("a[rel=dialog_recpassword]").fancybox({
        onStart : function() {
             $("#dialog_recpassword").show();
             $.fancybox.resize(); },
        onCleanup : function() {
            $("#dialog_recpassword").hide();
        }
    });
   
    $(".login_form").validate({
           invalidHandler: function(form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    $("#login_error_message").html($("#login_error_message").attr("default")).show();
                }else{
                    $("#login_error_message").hide();
                }
            }
        });
    $("#login_submit").click(function(){
        var opt = {
            url: site_folder+"mods/global_functions.php",
            type:"post",
            dataType : "json",
            success:function(data) {
                if(data.erro == 1) {
                    $("#login_error_message").html(data.message).css({color:"#ff0000"}).show();
                }else{
                    $(".userinfo").html(data.userdiv).show();
                    $(".cartinfo").html(data.budgetdiv).show();
                    $.fancybox.close();
                }
            }
        };
        if($(".login_form").valid()) {
            $(".login_form").ajaxSubmit(opt);
        }
        return false;
    });

    $(".recoverpassword_form").validate({
           invalidHandler: function(form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    var default_val = $("#recoverpassword_result").attr("default");
                    $("#recoverpassword_result").html(default_val).show();
                    $.fancybox.resize();
                }else{
                    $("#recoverpassword_result").hide();
                    $.fancybox.resize();
                }
            }
        });
    $("#recoverpassword_submit").click(function(){
        var opt = {
            url: site_folder+"mods/global_functions.php",
            type:"post",
            dataType : "json",
            success:function(data) {
                if(data.erro == 1) {
                    //alert(data.message);
                    $("#recoverpassword_result").html(data.message).css({color:"#ff0000"}).show();
                }else{
                    $("#recoverpassword_result").html(data.message).css({color:"#8CC63F"}).show();
                    setTimeout(function() {
                        $("#recoverpassword_result").fadeOut("slow");
                        $.fancybox.close();
                    },5000);
                }
            }
        };
        if($(".recoverpassword_form").valid()) {
            $(".recoverpassword_form").ajaxSubmit(opt);
        }
        return false;
    });

    $(".home_subvat_hover li a").hover(function() {
        var imgpath = $(this).parent().attr("path");
        var finalpath = "?thumb="+imgpath+"|240x120|R";
        $(this).parent().parent().parent().children("div.cat_image").children("img").attr({"src" : finalpath });
    },function() {
        var homeimg = $(this).parent().parent().parent().children("div.cat_image").children("img").attr("default");
        $(this).parent().parent().parent().children("div.cat_image").children("img").attr({"src" : homeimg });
    });
    //search product field
    $("#search_field").focus(function() {
        if($(this).val() == $(this).attr("default")){
            $(this).val("");
        }
    }).blur(function() {
        if($(this).val() == ""){
            $(this).val($(this).attr("default"));
        }
    });
    $("#search_button").click(function() {
        $(this).submit();
    });

    /* NEWSLETTER WINDOW */

    $("a[rel=open_newsletter]").fancybox({
        onStart : function() {
            $("#dialog_newsletter").show();
            $(".newsletter_form").show();
            $("#newsletter_error").hide();
            $("#newsletter_result").hide();
            $("#dialog_newsletter input:not(.submit)").removeClass("error").val("");
        },
        onCleanup : function() {
            $("#dialog_newsletter").hide();
        },
        autoDimensions : false,
        width:'auto',
        height:'auto'
    });

    $("#newsletter_input").focus(function() {
        el = $(this);
        if(el.val() == el.attr("default")){el.val("");}
    }).blur(function() {
        el = $(this);if(el.val() == ""){el.val(el.attr("default"));}
    });
    $("#newsletter_submit").click(function(){
        $(".newsletter_form").validate({
            errorContainer: "",
            errorLabelContainer:"" ,
            showErrors: function(errorMap, errorList) {
                if(this.numberOfInvalids() > 0) {
                    $("#newsletter_error").show();
                    //$.fancybox.resize();
                }else{
                    $("#newsletter_error").hide();
                    //$.fancybox.resize();
                }
                this.defaultShowErrors();
            }
        });
        var opts = {
            url: site_folder+"mods/global_functions.php",
            type:"post",
            dataType : "json",
            resetForm: true,
            success:function(data) {
                $.fancybox.resize();
                if(data.erro == 1) {
                    $("#newsletter_result").html(data.message).css({color:"#ff9999"}).show();
                    setTimeout(function() {
                        $("#newsletter_result").hide();
                        $.fancybox.resize();
                    },3000);
                }else{
                    $("#newsletter_result").html(data.message).css({color:"#8CC63F"}).show();
                    setTimeout(function() {
                        $("#newsletter_result").fadeOut("slow");
                        $.fancybox.resize();
                    },5000);
                }
            }
        };
        if($(".newsletter_form").valid()) {
            $(".newsletter_form").ajaxSubmit(opts);

        }
        return false;
    });
    
    
    /* informações acerca do produto WINDOW */
    $("a[rel=open_productinfo]").fancybox({
        onStart : function() {
            $("#dialog_productinfo").show();
            $(".product_form").show();
            $("#productform_error").hide();
            $("#productform_result").hide();
            $("#dialog_productinfo input:not(.noclear)").removeClass("error").val("");
            $("#dialog_productinfo textarea:not(.noclear)").removeClass("error").val("");
            $.fancybox.resize();
        },
        onCleanup : function() {
            $("#dialog_productinfo").hide();
        },
        autoDimensions : false,
        width:'auto',
        height:'auto'
    });

    $("#productform_submit").click(function(){
        $(".product_form").validate({
            errorContainer: "",
            errorLabelContainer:"" ,
            showErrors: function(errorMap, errorList) {
                if(this.numberOfInvalids() > 0) {
                    $("#productform_error").show();
                    //$.fancybox.resize();
                }else{
                    $("#productform_error").hide();
                    //$.fancybox.resize();
                }
                this.defaultShowErrors();
            }
        });
        var opts = {
            url: site_folder+"mods/global_functions.php",
            type:"post",
            dataType : "json",
            success:function(data) {
                $.fancybox.resize();
                if(data.erro == 1) {
                    $(".product_form").hide();
                    $("#productform_result").show();
                    $.fancybox.resize();
                    setTimeout(function() {
                        $("#productform_result").hide();
                        $.fancybox.close();
                    },3000);
                }else{
                    $("#productform_result").html(data.message).css({color:"#8CC63F"}).show();
                    setTimeout(function() {
                        $("#productform_result").fadeOut("slow");
                        $.fancybox.resize();
                    },5000);
                }
            }
        };
        if($(".product_form").valid()) {
            $(".product_form").ajaxSubmit(opts);

        }
        return false;
    });
    
    /* /informações acerca do produto WINDOW */
    /* store top menu */
    var inprog = 0;
    $("#categories_top_menu > li").hover(function() {
           // if (inprog == 0) {
               //  inprog = 1;
                 $(this).children("div").children("a").css({"color": $(this).children("div").attr("color")});
                 $(this).children("ul").slideDown(200);
           //  }
        },function(){
            $(this).children("ul").slideUp(400);
            if(!$(this).hasClass("selected")) {
                $(this).children("div").children("a").css({"color": "#ccc"});
            }
    });
    $("#categories_top_menu li ul li").click(function(event) {
        event.stopImmediatePropagation();
        $(this).children("ul").slideToggle();
    });
    /*
    $("#newsletter_submit").click(function(){
        $(".newsletter_form").validate({
            errorContainer: "",
            errorLabelContainer: "",
            messages: {
                newsletter_email : ""
            },
            showErrors: function(errorMap, errorList) {
                if(this.numberOfInvalids() > 0) {
                    $("#newsletter_error").show();
                    $.fancybox.resize();
                }else{
                    $("#newsletter_error").hide();
                    $.fancybox.resize();
                }
                this.defaultShowErrors();
            }
        });

        var options = {
            url: site_folder+"mods/global_functions.php",
            type:"post",
            dataType : "json",
            success:function(data) {
                $(".newsletter_form").hide();
                
                if(data.erro == 1) {
                    $("#newsletter_result").html(data.message).css({color:"#ff0000"}).show();
                    $.fancybox.resize();
                    setTimeout(function() {
                        $(".newsletter_form").show();
                        $("#newsletter_result").hide();
                        $.fancybox.resize();
                    },3000);
                }else{
                    $("#newsletter_result").html(data.message).css({color:"#68B5CA"}).show();
                    $.fancybox.resize();
                    setTimeout(function() {
                        $("#newsletter_result").fadeOut("slow",
                            function () {
                                $.fancybox.close();
                            });
                    },5000);
                }
            }
        };
        if($(".newsletter_form").valid()) {
            $(".newsletter_form").ajaxSubmit(options);
        }
        return false;
    });*/
    $("#front_form").validate({
            errorContainer: "",
            errorLabelContainer: "",
            showErrors: function(errorMap, errorList) {
                if(this.numberOfInvalids() > 0) {
                    $("#error_container").show();
                }else{
                    $("#error_container").hide();
                }
                this.defaultShowErrors();
            }
        });
      $(".validate").validate({
            errorContainer: "",
            errorLabelContainer: $("#validate_msg_container"),
            showErrors: function(errorMap, errorList) {
                if(this.numberOfInvalids() > 0) {
                    $("#error_validate").show();
                }else{
                    $("#error_validate").hide();
                }
                this.defaultShowErrors();
            }
        });

        
/*
	//styles
	$("#site_lang_menu li:last").css({"border":"none"});
	
	catalog_list_style();
	
	
	//hover hacks
	
	$("#top_plogin").mouseover(function () { $(this).addClass("partner_login_hover"); })
	.mouseout(function () { $(this).removeClass("partner_login_hover"); });
	
	$("#top_plocator").mouseover(function () { $(this).addClass("partner_locator_hover"); })
	.mouseout(function () { $(this).removeClass("partner_locator_hover"); });

	$(".product_options ul li").mouseover(function () { $(this).addClass("product_options_hover"); })
	.mouseout(function () { $(this).removeClass("product_options_hover"); });
	
	$("a[rel=preview]").fancybox();
	$("a[rel=wform]").fancybox({
		onStart : function() { $("#recomendForm").show(); },
		onCleanup : function() { $("#recomendForm").hide(); },
		autoDimensions : false,
		width:455,
		height:'auto'
	});
		
	});

	$('#main_menu ul').superfish({ 
		delay:         100,               
	    animation:     {opacity:'show'},   
	    speed:         'normal'         
	});
	
	//home
	$('#home_banner').coinslider({ 
		width: 900, 
		height:350,
		navigation: true, 
		delay: 8000,
		spw: 10, // squares per width
		sph: 4, // squares per height
		effect: 'rain' // random, swirl, rain, straight
	});
	
	
	//product icons tooltip
	
	$('.product_icons').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    extraClass: "pretty", 
	    fixPNG: true, 
	    opacity: 0.95, 
	    left: -120 
	});
	
	Cufon.replace('h3', { fontFamily: 'MorganBg1' });
	Cufon.replace('h2', { fontFamily: 'MorganBg1' });
	Cufon.replace('h1', { fontFamily: 'MorganBg1' });
	
	$("select.sSelect").sSelect();
	
	$("#catalog_select select").change(function(){
		var options = { 
				target:$("#catalog_list"),
				url: site_folder+ "mods/store_funcs.php",
		        type:"post",
		        success:function(data) {
					catalog_list_style();
					Cufon.replace('h2', { fontFamily: 'MorganBg1' });
		        }
		    }; 
		$("#catalog_select").ajaxSubmit(options);
	});
	
	
	$(".product_image").easySlider({
		nextText : "",
		prevText : "",
		controlsFade: false	
	});

	
	//search product field
	$("#search_input").focus(function() {
		if($(this).val() == $(this).attr("default")){
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == ""){
			$(this).val($(this).attr("default"));
		}
	});
	
	$("#search_submit").click(function() {
		$(this).parent().submit();
	});
	
	$("#btn_moreinfo").click(function(e){
	    var relativeX = e.pageX;
	    var relativeY = e.pageY;
	    $("#more_info_container").show().css({left:relativeX, top:relativeY});
 	});
	
	
	$("#moreinfo_form input, #moreinfo_form textarea").focus(function() {
		el = $(this);
		if(el.val() == el.attr("default")){ el.val(""); }
	}).blur(function() {
		el = $(this); if(el.val() == ""){ el.val(el.attr("default")); }
	});
	$(".btn_close").click(function() {
		$("#more_info_container").fadeOut("slow");
	});
	/*
	$("#moreinfo_submit").click(function(){
		$("#moreinfo_form").validate({
			 errorContainer: "",
			 errorLabelContainer: "",
			 messages: {
			     moreinfo_name: "",
			     moreinfo_email: "",
			     moreinfo_message: ""
			 },
			 showErrors: function(errorMap, errorList) {
				if(this.numberOfInvalids() > 0) {
					$("#moreinfo_form_errors").show();
				}else{
					$("#moreinfo_form_errors").hide();
				}
				this.defaultShowErrors();
			}
		});
		    	  
		var options = { 
				target:$("#moreinfo_result"),
				url: site_folder+"mods/global_functions.php",
		        type:"post",
		        clearForm: true,
		        dataType : "json",
		        success:function(data) {
					$("#moreinfo_form").hide();
					$("#moreinfo_result").html(data.message).show();
					setTimeout(function() {
						$("#more_info_container").fadeOut("slow",function() {
							$("#moreinfo_form").show();
							$("#moreinfo_result").hide();	
						}); 
						
					},5000);
					
		        }
		    }; 
		if($("#moreinfo_form").valid()) {
			$("#moreinfo_form").ajaxSubmit(options);
		}
		return false;		
    });	
	*/
/*
	$("#newsletter_add").focus(function() {
		el = $(this);
		if(el.val() == el.attr("default")){ el.val(""); }
	}).blur(function() {
		el = $(this); if(el.val() == ""){ el.val(el.attr("default")); }
	});
	
	
	$(".styledButton").css({'padding' : '3px 20px', 'font-size' : '12px'}).styledButton();
	
	$("#recomendSubmit").click(function() {
		var options = { 
			url: site_folder+"mods/global_functions.php",
	        type: "post",
	        success: function(data) {
					$("#recomend").hide();
					$("#recomend_result").show();
					setTimeout(function() {	$.fancybox.close();	},5000);
					
	        }
	    }; 
		if($("#recomend").valid()) {
			$("#recomend").ajaxSubmit(options);
		}
	});
	
	$("#recomend").validate({
		 errorContainer: "",
		 errorLabelContainer: "",
		 showErrors: function(errorMap, errorList) {
			if(this.numberOfInvalids() > 0) {
				$("#recomend_error_container").show();
			}else{
				$("#recomend_error_container").hide();
			}
			this.defaultShowErrors();
		}
	});
	
	$("#plogin_submit").click(function() {
		var options = { 
			url: site_folder+"mods/global_functions.php",
	        type: "post",
	        success: function(data) {
					$("#plogin_form").hide();
					$("#plogin_form_result").show();
					setTimeout(function() {	$.fancybox.close();	},5000);
	        }
	    }; 
		if($("#plogin_form").valid()) {
			$("#plogin_form").ajaxSubmit(options);
		}
	});
	
	$("#plogin_form").validate({
		 errorContainer: "",
		 errorLabelContainer: "",
		 showErrors: function(errorMap, errorList) {
			if(this.numberOfInvalids() > 0) {
				$("#plogin_error_container").css({color:"#ff0000"});
			}else{
				$("#plogin_error_container").css({color:"#fff"});
			}
			this.defaultShowErrors();
		}
	});
	
	//plugins forms
	
	$("#front_form").validate({
		 errorContainer: "",
		 errorLabelContainer: "",
		 showErrors: function(errorMap, errorList) {
			if(this.numberOfInvalids() > 0) {
				$("#error_container").show();
			}else{
				$("#error_container").hide();
			}
			this.defaultShowErrors();
		}
	});
	*/
});
