//GENERAL JS
$(function() {
    // CORNERS
    $('.rounded').corner('round bl br 15px');
    $("#mainMenuBackground").corner("round tl tr 15px");

    // CONTACT FORM
    $('.dialog').dialog({
        autoOpen: false,
        hide: 'drop',
        modal: true,
        show: 'drop',
        width: 490,
        height:515
    });
    loadContactFormJqueryValidator();
    $("#headerContact").hover(function() {
        $("#headerContactTitle").addClass("underline");
    }, function() {
        $("#headerContactTitle").removeClass("underline");
    });

    // MAIN MENU
    $(".mainLink").mouseover(function() {
        $(this).addClass("mainMenuLinkHover");
    });
    // make mousout into a hoverIntent so that it fades with the submenus
    $(".mainLink").mouseout(function() {
        $(this).removeClass("mainMenuLinkHover");
    });
    $(".mainLink").hoverIntent({
        over: function() {
            $(this).find(".subMenu").fadeIn("250");
        },
        timeout: 350,
        out: function() {
            $(this).find(".subMenu").hide();
        }
    });
//hide all then display next if move from one menu to the next for faster switching?
});
//END GENERAL JS

//CONTACT FORM
function contactRequired() {
    var contactVal = $('#email').attr('value') + $('#phone1').attr('value') + $('#phone2').attr('value') + $('#phone3').attr('value');
    if (contactVal == '') {
        return true;
    } else {
        return false;
    }
}

function loadContactFormJqueryValidator() {
    $('#contactUsForm').validate({
        groups: {
            phone: 'phone1 phone2 phone3'
        },
        rules: {
            name: {
                required: true,
                minlength: 3
            },
            email: {
                email: true,
                required: {
                    depends: contactRequired
                }
            },
            phone1: {
                digits: true,
                rangelength: [3,3],
                required: {
                    depends: contactRequired
                }
            },
            phone2: {
                digits: true,
                rangelength: [3,3],
                required: {
                    depends: contactRequired
                }
            },
            phone3: {
                digits: true,
                rangelength: [4,4],
                required: {
                    depends: contactRequired
                }
            }
        },
        messages: {
            name: {
                required: 'Please specify your name.'
            },
            email: {
                email: 'Please enter a valid email address (name@domain.com).',
                required: 'Please fill out either the <span style="text-decoration:underline;">Email</span> field or the <span style="text-decoration:underline;">Phone Number</span> field.'
            },
            phone1: {
                digits: 'Please enter only numbers (0-9).',
                rangelength:'Please enter a valid phone number (123-123-1234).',
                required: function() {
                    var phoneNum = $('#phone1').attr('value') + $('#phone2').attr('value') + $('#phone3').attr('value');
                    if (phoneNum == '') {
                        return 'Please fill out either the <span style="text-decoration:underline;">Email</span> field or the <span style="text-decoration:underline;">Phone Number</span> field.';
                    } else {
                        return 'Please enter a valid phone number (123-123-1234).';
                    }
                }
            },
            phone2: {
                digits: 'Please enter only numbers (0-9).',
                rangelength:'Please enter a valid phone number (123-123-1234).',
                required: function() {
                    var phoneNum = $('#phone1').attr('value') + $('#phone2').attr('value') + $('#phone3').attr('value');
                    if (phoneNum == '') {
                        return 'Please fill out either the <span style="text-decoration:underline;">Email</span> field or the <span style="text-decoration:underline;">Phone Number</span> field.';
                    } else {
                        return 'Please enter a valid phone number (123-123-1234).';
                    }
                }
            },
            phone3: {
                digits: 'Please enter only numbers (0-9).',
                rangelength:'Please enter a valid phone number (123-123-1234).',
                required: function() {
                    var phoneNum = $('#phone1').attr('value') + $('#phone2').attr('value') + $('#phone3').attr('value');
                    if (phoneNum == '') {
                        return 'Please fill out either the <span style="text-decoration:underline;">Email</span> field or the <span style="text-decoration:underline;">Phone Number</span> field.';
                    } else {
                        return 'Please enter a valid phone number (123-123-1234).';
                    }
                }
            }
        },
        wrapper: 'p',
        errorPlacement: function(error, element) {
            var id = element.attr('id');
            if (id.substring(0,5) == 'phone') {
                error.insertAfter('#phone3');
            } else {
                error.insertAfter(element);
            }
        }
    /*errorPlacement: function(error, element) {
                error.appendTo(element.parent('td').parent('tr'));
            }*/
    });

    // Modal for Contact form.
    $('#contactFormDialog').dialog('option', 'buttons', {
        "Send": function() {
            $(this).attr("disabled", "disabled");
            $("#contactUsForm").validate();
            $.post("../includes/contactForm.php", $("#contactUsForm").serialize(), function(data){
                $('#contactFormDialog').html(data);
                if ($("#contactFormStatus").val() == "success") {
                    $(".dialog").dialog('option', 'buttons', {
                        "Close": function() {
                            $(this).dialog("close");
                        }
                    });
                }
            });
        //                $.ajax({
        //
        //                });
        },
        "Cancel": function() {
            $(this).dialog('close');
        }
    });

    $('#contactFormDialog').dialog('option', 'title', 'Let Us Contact You!');

    $('#headerContact').click(function() {
        $('#contactFormDialog').dialog('open');
    });
    $('#headerContact').click(function(e) {
        $('#contactFormDialog').dialog('open');
        e.preventDefault();
    });
}
//END CONTACT FORM