$(function() {
    $(".logoLine").bind('mouseover',
    function() {
        $("#logoGraphic .logoLine").removeClass('half');
        $(this).addClass('half');
    }).bind('click',
    function() {
        setupEmailForm();
    });
	$("#contactLink").bind('click',
    function() {
        setupEmailForm();
		return false
    });
	
    var sendIt = "";
    setupEmailForm = function() {
        sendIt = false;
        $("<div id='emailForm'></div>").append("<div id='actionMessage'></div>").append("<form action='' method='POST' id='emailFormData'></form>").dialog({
            "modal": true,
            "resizable": false,
			"overlay": {
                "opacity": 0.4,
                "background": "black"
            },
            "title": "Contact Me",
            "width": 450,
            "height": 375,
            "buttons": {
                "send it": function() {
                    sendIt = true;
                    postEmail();
                    if ($("#optIn").is(":checked")) {
                        postSubscriber();
                    }
                },
                "cancel": function() {
                    $("#emailForm").remove();
                }
            },
            "open": function() {
                $("#emailFormData").append("<label for='firstName'>first name</label>").append("<input type='text' name='firstName' id='firstName' class='text' />").append("<label for='lastName'>last name</label>").append("<input type='text' name='lastName' id='lastName' class='text' />").append("<label for='email'>email</label>").append("<input type='text' name='email' id='email' class='text' />").append("<div class='emailResponse' style='display:none'></div>").append("<label for='emailMessage'>message</label>").append("<textarea name='emailMessage' id='emailMessage' rows='1' cols='1'></textarea>").append("<div class='clear'></div>").append("<input type='checkbox' name='optIn' id='optIn' value='true'/>").append("<label for='optIn'>subscribe to 4.5L announcements as well?</label>");
                $("#firstName, #lastName, #email, #subject, #emailMessage").toggleVal({
                    "populateFrom": "label",
                    "removeLabels": true
                });
                $("#email").blur(function() {
                    validateEmail();
                });
            },
            "close": function() {
                $("#emailForm").remove();
            }
        });
    };
    validateEmail = function() {
        if ($(".emailResponse").is(":visible")) {
            $("#emailForm").animate({
                "height": 265
            },
            300);
            $(".emailResponse").slideUp();
        }
        $("#email").each(function() {
            if ($(this).val() == $(this).data("defText")) {} else {
                $("#email").removeClass("invalid").removeClass("valid").addClass("checkingInput");
                var email = $("#email").val();
                $.ajax({
                    "url": "/phpFunc/validateEmail.php",
                    "data": "action=checkEmail&email=" + email,
                    "dataType": "json",
                    "type": "post",
                    "success": function(j) {
                        if (j.ok == true) {
                            $("#email").removeClass("invalid").removeClass("checkingInput").addClass("valid");
                            if ($(".emailResponse").is(":visible")) {
                                $("#emailForm").animate({
                                    "height": 265
                                },
                                300);
                                $(".emailResponse").slideUp();
                            }
                            if (sendIt == true) {
                                postEmail();
                            }
                        } else {
                            $("#email").removeClass("valid").removeClass("checkingInput").addClass("invalid");
                            $("#emailForm").animate({
                                "height": 280
                            },
                            300);
                            $(".emailResponse").html(j.msg).slideDown();
                        }
                    }
                });
            }
        });
    };
    postEmail = function() {
        if ($(".valid")[0]) {
            $(".toggleval").each(function() {
                if ($(this).val() == $(this).data("defText")) {
                    $(this).val("");
                }
            });
            var serializedForm = $("#emailForm form").serialize();
            $.ajax({
                "type": "POST",
                "url": "/phpFunc/sendMail.php",
                "data": serializedForm,
                "beforeSend": function() {
                    $("#emailForm").animate({
                        "opacity": 0.4
                    },
                    200);
                    $("#ui-dialog-title-emailForm").html("sending...");
                },
                "success": function(msg) {
                    $("#emailForm").fadeOut(300,
                    function() {
                        $("<div class='feedback'></div>").insertBefore("#emailForm");
                        if (msg == "nice") {
                            $("#ui-dialog-title-emailForm").html("message sent...").fadeIn();
                            $(".feedback").html("Thanks. <p id='userResponse'>Your email has been sent.</p><p> Sorry about the form, but it keeps the spam away.</p>");
                            $("button:contains('send it')").hide();
                            $("button:contains('cancel')").html("close");
                        } else {
                            $(".feedback").html("doh... sorry, something went wrong there.").fadeIn();
                            $(".feedback").append("<p>" + msg + "</p>");
                        }
                    });
                }
            });
        }
    };
    postSubscriber = function() {
        var serializedForm = $("#emailForm form").serialize();
        $.ajax({
            "type": "POST",
            "url": "/phpFunc/insertSubscriber.php",
            "dataType": "json",
            "data": "action=insertSubscriber&" + serializedForm,
            "beforeSend": function() {},
            "success": function(j) {
                if (j.ok == true) {
                    $("#userResponse").append(j.msg);
                } else {}
            }
        });
    };
  
});
