/**	
* Custom Modal dialog
* @author Julian Guy
*/
jQuery(document).ready(function($){
jQuery.modal = function (modalContent, options) {

  // Cufon Support
  function doCufon() {
    if (typeof Cufon != "undefined") {
      Cufon.refresh();
    }
  }

  // IE6?
  var ie6 = ($.browser.msie && $.browser.version <= 6) ? true : false;

  function closeModal() {
    $("#jQueryModalWrapper").fadeOut(200, function () {
      $("#jQueryModalOverlay,#jQueryModalIFrame,#modalWindow,#jQueryModalWrapper").remove();
    });
  }

  // Default settings
  var settings = jQuery.extend({
    verticalCentering: false,
    fixed: false,
    complete: function () { }
  }, options);

  // Check for modal HTML construct
  var modalWrapper = $("#jQueryModalWrapper");
  if (modalWrapper.size() == 0) {
    var modalHTML = ie6 ? '<div id="jQueryModalWrapper"><iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="javascript:\'\';" style="height:100%; width: 100%;" id="jQueryModalIFrame"></iframe>' : '<div id="jQueryModalWrapper" class="normal">';
    modalHTML = $(modalHTML + '<div id="jQueryModalOverlay"></div><div id="jQueryModalWindow"></div></div>');
    $("body").append(modalHTML);
    modalWrapper = $("#jQueryModalWrapper");
  }

  // Hide Modal
  $("#jQueryModalOverlay,#jQueryModalWindow", modalHTML).hide();

  // Append HTML to modal Window
  var modalWindow = $("#jQueryModalWindow");
  modalContent = $(modalContent);
  
  modalContent = $("#newsletter-form-block", modalContent);
  
  $("#close", modalContent).css({ cursor: "pointer" }).click(function () {
    closeModal();
    return false;
  })
  
  modalWindow.empty().html(modalContent);
  doCufon();

  // Screen Dimensions
  var screenHeight = $(document).height();
  $("#jQueryModalOverlay").css({ height: screenHeight + "px" })


  if (ie6) {
    var screenWidth = $(document).width()
    $("#jQueryModalOverlay").css({ width: screenWidth + "px" })
  }

  // Position window (Fixed, vertically centering style)
  if (settings.verticalCentering) {
    modalWindow.css({ "top": "50%", "margin-top": "-" + (windowHeight / 2) + "px" });
  }


  // Position window (Standard absolute style)
  var windowWidth = modalWindow.width();
  var windowHeight = modalWindow.height();
  //alert(windowWidth);
  //modalWindow.css({ "margin-left": (windowWidth / 2) + "px" });
  modalWindow.css({ "margin": "0 auto" });
  modalWindow.css({ "width": "895px" });  

  if (settings.fixed) {
    modalContent.css({ "position": "fixed", "top": ($(window).height() / 2) - (windowHeight / 2) - 20 });
  }

  // Show Modal
  $("#jQueryModalOverlay, #jQueryModalIFrame").fadeIn(100, function () {
    modalWindow.fadeIn(200, settings.complete);
    $("html").addClass("modalActive");
    if (!settings.fixed) {
      var offset = modalWindow.offset();
      if ($(document).scrollTop() > offset.top) {
        $('html,body').animate({ scrollTop: offset.top }, 1000)
      }
    }
  });


};
});
