$(document).ready(function(){

  //0 means disabled; 1 means enabled;
  var popupStatus = 0;
  var popup_links = $('a.ajax_popup');
  if($(popup_links).length > 0){
    $('body').append('<div id="popupHolder"><a id="popupHolderClose" title="Close" href="#">x</a><div id="popupHolderContent"></div><div id="popupNote">Press ESC to cancel</div></div>');
    $('body').append('<div id="popupBackground"></div>');

    $(popup_links).click(function(){
      //centering with css
      centerPopup();
      //load popup
      loadPopup($(this).attr('href'));
      //autosizePopup();
      return false;
    });
  }
  //loading popup with jQuery magic!
  function loadPopup(href){
    //loads popup only if it is disabled
    if(popupStatus==0){
      $("#popupBackground").css({
        "opacity": "0.7"
      });
      $("#popupBackground").fadeIn("slow");
      $("#popupHolder").fadeIn("slow");

      var result = '';
      params = 'ajax=1';
      hash_mark_pos = href.indexOf('#');
      if(hash_mark_pos >= 0)
        href = href.substr(0, hash_mark_pos);
      $.ajax({
        type: "GET",
        url: href,
        data: params,
        async: false,
        dataType: "text",
        success: function(response){
          result = response;
        },
        error: function(xhr, desc, exception){ result = 'An error occurred.'; }
      });
      $("#popupHolder").css({
        'width': 'auto',
        'height': 'auto',
        'background-image': 'none'
      });
      $("#popupNote").css('display','none');
      $("#popupHolderContent").html(result);

      //centering with css
      centerPopup();

      popupStatus = 1;
    }
  }
  //disabling popup
  function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
      $("#popupBackground").fadeOut("slow");
      $("#popupHolder").fadeOut("slow",function(){
        $(this).removeAttr('style');
        $("#popupHolderContent").html('');
        $("#popupNote").css('display','block');
        popupStatus = 0;
      });
    }
  }
  //centering popup
  function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var scrollTop = document.documentElement.scrollTop;
    var popupHeight = $("#popupHolder").height();
    var popupWidth = $("#popupHolder").width();
    //centering
    $("#popupHolder").css({
      "position": "absolute",
      "top": scrollTop+(windowHeight/2-popupHeight/2),
      "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6
    $("#popupBackground").css({
      "height": windowHeight
    });
  }
  
  function autosizePopup(){
    $("#popupHolderContent").css('height','auto').css('width','auto');
  }
  //CLOSING POPUP
  //Click the x event!
  $("a#popupHolderClose").click(function(){
    disablePopup();
    return false;
  });
  //Click out event!
  $("#popupBackground").click(function(){
    disablePopup();
  });
  //Press Escape event!
  $(document).keypress(function(e){
    if(e.keyCode==27 && popupStatus==1){
      disablePopup();
    }
  }).scroll(function(){
    if(popupStatus == 1)
      centerPopup();        
  });
  
});
