﻿$(document).ready(function() {
    $("#tabs").tabs();
    var $emailMsgPopUp = $('<div class="popUp"><div class="contentWrapper"></div></div>').appendTo($('body'));
    var $form = $('#contactForm').submit(function() {
        var $name = $('#name');
        var name = $.trim($name.val());
        var $email = $('#email');
        var email = $.trim($email.val());
        var $description = $('#description');
        var description = $.trim($description.val());
        var valid = true;
        if (name == '') {
            valid = false;
            $name.effect("highlight", { color: '#800517' }, 1000);
        }
        $email.prev().text('Email');
        if (email == '') {
            valid = false;
            $email.effect("highlight", { color: '#800517' }, 1000);
        }
        else if (email.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/) == null) {
            valid = false;
            $email.prev().text('Email * Invalid Email');
            $email.effect("highlight", { color: '#800517' }, 1000);
        }
        if (description == '') {
            valid = false;
            $description.effect("highlight", { color: '#800517' }, 1000);
        }
        if (valid) {
            showPopUp('Sending Message...<br/><br/><img src="/Content/images/loading.gif" />');
            $.post($form.attr("action"), $form.serialize(), function(json) {
                startPopupCloseTimeout(json.msg, 4000);
                if (json.success) {
                    $form[0].reset();
                }
            }, "json");
        }
        return false;
    });

    var showPopUp = function(msg) {
        var $container = $('.table2');
        var offset = $container.offset();
        var containerHeight = $container.outerHeight(true);
        $emailMsgPopUp.css({
            top: offset.top,
            left: offset.left,
            height: containerHeight,
            width: $container.outerWidth(true)
        }).fadeIn(500).find('.contentWrapper').css("margin-top", 75).html(msg);
    };

    var startPopupCloseTimeout = function(newMsg, time) {
        $emailMsgPopUp.find(".contentWrapper").fadeOut(500, function() {
            var $this = $(this);
            $this.html(newMsg).fadeIn(500);
            setTimeout(function() {
                $emailMsgPopUp.fadeOut(2000);
            }, time);
        });
    };
});