/**
 * Global Vars
 */

var popupStatus = 0; //contact window status

/**
 * pre-submit AJAX callback
 * @param {Object} formData
 * @param {Object} jqForm
 * @param {Object} options
 */
function showRequest(formData, jqForm, options) {
    return true;
}

/**
 * post-submit AJAX callback
 *
 * @param {Object} responseText
 * @param {Object} statusText
 * @param {Object} xhr
 * @param {Object} $form
 */
function showResponse(responseText, statusText, xhr, $form)  {
    $("#contact_form").fadeOut("fast");
    $("#thank_you").fadeOut("fast");
    $("#input_area img").css({"display": "block"});
    setTimeout('disablePopup()', 2500);
}

/**
 * Loads the contact window
 */
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#contact_us_background").css({"opacity": "0.3"});
        $("#contact_us_background").fadeIn("slow");
        $("#contact_us").fadeIn("slow");
        popupStatus = 1;
    }
}

/**
 * Disables the contact window
 */
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#contact_us_background").fadeOut("slow");
        $("#contact_us").fadeOut("slow");
        popupStatus = 0;
    }
}

/**
 * Centers the contact window
 */
function centerPopup(){
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var popupHeight = $("#contact_us").height();
    var popupWidth = $("#contact_us").width();
    //centering
    $("#contact_us").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6

    $("#contact_us_background").css({
        "height": windowHeight
    });
}

/**
 * Main JQUERY loop
 */
$(document).ready(function(){

    // ajax set up
    var options = {
        url:           'contact',
        dataType:      'json',
        target:        '#output2',   // target element(s) to be updated with server response
        beforeSubmit:  showRequest,  // pre-submit callback
        success:       showResponse  // post-submit callback
    };

    $(".a").hover(
            function() {
                $(this).stop().animate({"opacity": "0"}, "fast");
            },
            function() {
                $(this).stop().animate({"opacity": "1"}, "slow");
            });

    $("#contact_form").validate({
           submitHandler: function(form) {
               $(form).ajaxSubmit(options);}
       });

    // contact form validation and effects
    $('#contact_form').slidinglabels({
        topPosition  : '5px',  // how far down you want each label to start
        leftPosition : '5px',  // how far left you want each label to start
        axis         : 'x',    // can take 'x' or 'y' for slide direction
        speed        : 'fast'  // can take 'fast', 'slow', or a numeric value
    });

    // links
    $("#email_us").click(function(){
        centerPopup();
        loadPopup();
    });

    $("#contact_us_close").click(function(){
      disablePopup();
    });
    $("#contact_us_background").click(function(){
      disablePopup();
    });

    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
          disablePopup();
        }
    });

    // bind to the form's submit event
    $('#submit').click(function() {
        $("#contact_form").submit();
    });
});
