/* Greybox abgewandelt zu speziellem Fenster
 * Required: http://jquery.com/
 * Based on code by:  John Resig and 4mir Salihefendic (http://amix.dk) 
 * Turned into plugin by Dave McFarland
 * License: LGPL (read more in LGPL.txt)
 */


(function($) {

var settings; //global to this function
var GD_DONE = false;

$.fn.greydiv = function(callerSettings) {
    var GD_done = false;

    settings = $.extend({
      gbHeight : 400,
      gbWidth : 400,
      ffMacFlash: false
     },callerSettings || {});
  
     return this.click(function(evt){
     evt.preventDefault();
     var t = this.title || $(this).text() || this.href;
     GD_show(t,this.href);
   });
 } // end of greybox function

var GD_hide = function() {
  $("#GD_window,#GD_overlay").hide();
} 

var GD_show = function(caption, url) {
  if(!settings.GD_DONE) {
    $(document.body)
      .append('<div id="GD_overlay"></div><div id="GD_window"><div id="GD_loading"></div></div>');
    //make overlay fill entire height of doc
    var docH = $(document).height();
    $('#GD_overlay').css('height', docH);


    if(isMacFF()&&settings.ffMacFlash){
      $("#GD_overlay").addClass("GD_overlayMacFFBGHack");//use png overlay so hide flash
    }else{
      $("#GD_overlay").addClass("GD_overlayBG");//use background and opacity
    }

    $("#GD_overlay").click(GD_hide);
    $(window).resize(GD_position);
    settings.GD_DONE = true;
  }

  $("#GD_frame").remove(); // remove previous iFrame
  $("#GD_caption").html(caption); // add caption
  $("#GD_overlay").show(); // show grey overlay
  $('#GD_loading').show();
  GD_position(); // resize and place in middle of window
  $("#GD_window").append("<iframe id='GD_frame' src='"+url+"'></iframe>");
  
  $("#GD_frame").css("height",settings.gbHeight +"px");
  $("#GD_frame").bind('load',function() {$('#GD_loading').hide()});
  $('#GD_window').show();


}

var GD_position = function() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  $("#GD_window").css({
      width:settings.gbWidth+"px",
      height:settings.gbHeight+"px",
      left: ((w - settings.gbWidth)/2)+"px" ,
      top: (((h - settings.gbHeight)/2)-15)+"px" 
   });

}

var isMacFF = function() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && $.browser.mozilla) {
    return true;
  } else {
    return false;
  }
}

})(jQuery);
