 

var popupStatus = 0; 

function loadPopup(){  
  if(popupStatus==0){
    $("#backgroundPopup").css({"opacity": "0.7"});  
    $("#backgroundPopup").fadeIn("slow");  
    $("#popup").fadeIn("slow");  
    popupStatus = 1;  
  }  
}  

function disablePopup(){  
  if(popupStatus==1){  
    $("#backgroundPopup").fadeOut("slow");  
    $("#popup").fadeOut("slow");  
    popupStatus = 0;  
  }  
}  

function centerPopup(){  
  var windowWidth = document.documentElement.clientWidth;  
  var windowHeight = document.documentElement.clientHeight;  
  var popupHeight = $("#popup").height();  
  var popupWidth = $("#popup").width();
  //console.log(popupHeight + ' = ' + popupWidth);
  //console.log(windowHeight + ' = ' + windowWidth);
  //console.log((windowHeight * 0.8) - 50);
  $("#popup").css({
    //"height": windowHeight * 0.8, 

    "position": "absolute",  
    "top":  windowHeight/2-popupHeight/2,  
    "left": windowWidth/2-popupWidth/2  
  });
  //$("#popupContent").css({"height": (windowHeight * 0.8) - 10}); 
  $("#backgroundPopup").css({"height": windowHeight}); 
}






$(document).ready(function() {


  $('#popupContent form').livequery('submit',function(event) {
    event.preventDefault();

    var error = '';
    if ($('#label_name').attr('value') == "") {error += 'Please provide your "Full Name"\n'}
    if ($('#label_email').attr('value') == "") {error += 'Please provide your "Email Address"\n'}
    if ($('#label_agree').val() != undefined && ! $('#label_agree').is(':checked')) {error += 'You must agree with our Date Privacy Statement\n'}
    if (error != "") {$('*').animate({scrollTop:0}, 0);alert(error); return;}
    $('#popupContent').html('<img style="margin:120px 250px;" src="/images/loading.gif" alt="">');

    var data_text = $(this).serialize();
    $.ajax({
      type: "POST",
      url: "/",
      data: data_text,
      success: function(data){
        $('#popupContent').html(data);
        $('#popupContent').animate({scrollTop:0}, 0);
        if($.browser.msie){$(".fm-req").append(' *');$("label.fm-req").removeClass('fm-req');}
      }
    });
  });

  $("a.popup").click( function(event){
    event.preventDefault();
    $('html, body').animate({scrollTop:0}, 0);
    $('#popupContent').html('<img style="margin:120px 250px;" src="/images/loading.gif" alt="">');
    pageTracker._trackPageview($(this).attr('href'));
    $.get($(this).attr('href') + "&ajax=1", function(data){
      $('#popupContent').html(data);
      if($.browser.msie){$(".fm-req").append(' *');$("label.fm-req").removeClass('fm-req');}
    });
    centerPopup();
    loadPopup();

  });  

  $(".popupClose").click(function(){disablePopup();});  
  $("#backgroundPopup").click(function(){disablePopup();});  
   

});
