$(document).ready(function(){
    /* fancybox init */
    $("#form1").fancybox({
        'titlePosition'		: 'inside',
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'scrolling'       : 'none',
        onComplete: function(){
            var $close_btn = $(".inline").find(".close_btn");
            if ($close_btn.length>0){
                $("#fancybox-close").css("display", "none");
            }
        }
    });

    /* replace close button */
    $(".close_btn").click(function(){
        $("#fancybox-close").click();
    });
	
	$("#form2").fancybox({
        'titlePosition'		: 'inside',
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'scrolling'       : 'none',
        onComplete: function(){
            var $close_btn = $(".inline").find(".close_btn");
            if ($close_btn.length>0){
                $("#fancybox-close").css("display", "none");
            }
        }
    });

    /* replace close button */
    $(".close_btn").click(function(){
        $("#fancybox-close").click();
    });

    /* lighting border on hover */
    $(".link-container").hover(
        function(){
            $(this).find("div.showflag").show();
        },
        function(){
            $(this).find("div.showflag").hide();
        }
    );

    /* lighting error border */
    $(".input").focus(function(){
        if (!$(this).hasClass("error")) $(this).css("border-color", "#66e");
    });

    $(".input").blur(function(){
        if (!$(this).hasClass("error")) $(this).css("border-color", "#999999");
    });

    // callback function to bring a hidden box back
    function callback() {
        $(this).css("border-color", "#990000");
        window.animations = false;
    };

    /* validation form */
    $("#commentForm").validate({
        errorClass: "error",
        validClass: "valid",
        highlight: function(element, errorClass, validClass) {
            $(element).css("border-color", "#990000");
            // most effect types need no options passed by default
            var options = {};
            // run the effect
            if (!window.animations) {
                $(element).effect( "highlight", options, 1000, callback );
                window.animations = true;
            }
        },
        unhighlight: function(element, errorClass, validClass) {
            if ($(element).hasClass(validClass))
            $(element).fadeOut(function() {
                $(element).css("border-color", "#999999");
                $(element).fadeIn();
            });
        },
        rules:{
            goods:{
                required:true
            },
            name:{
                required:true
            },
            email:{
                email: true,
                required:true
            },
            phone:{
                required:true
            }
        },
        messages:{
            goods:{
                required:"Введите наименование товара!"
            },
            name:{
                required:"Представьтесь, пожалуйста!",
				minlength:"Введите не менее 2 символов!"
            },
            email:{
                email: "Введите корректный EMail",
                required:"Введите EMail!"
            },
            phone:{
                required:"Введите телефон!"
            }
        }
    });
    /* validation form */
    $("#callForm").validate({
        errorClass: "error",
        validClass: "valid",
        highlight: function(element, errorClass, validClass) {
            $(element).css("border-color", "#990000");
            // most effect types need no options passed by default
            var options = {};
            // run the effect
            if (!window.animations) {
                $(element).effect( "highlight", options, 1000, callback );
                window.animations = true;
            }
        },
        unhighlight: function(element, errorClass, validClass) {
            if ($(element).hasClass(validClass))
            $(element).fadeOut(function() {
                $(element).css("border-color", "#999999");
                $(element).fadeIn();
            });
        },
        rules:{
            name:{
                required:true
            },
            phone:{
                required:true
            }
        },
        messages:{
            name:{
                required:"Представьтесь, пожалуйста!",
				minlength:"Введите не менее 2 символов!"
            },
            phone:{
                required:"Введите телефон!"
            }
        }
    });
});
