/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Reworked by: Benjamin Yu (http://foofiles.com/, http://badpopcorn.com/)
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

(function() {
  var settings = {}

  GB_overlay_size = function() {
    try {
      var de = document.documentElement;
      w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
      h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    } catch(err) {
      w = jQuery(document.body).width();
      h = jQuery(window).height();
    }
    jQuery("#GB_overlay").css({height:"100%",width:"100%"});
  }

  jQuery.GB_position = function() {
    var de = document.documentElement;
    var w = jQuery(document.body).width();
    jQuery("#GB_window").css({
      width: settings.width+"px",
      height: settings.height+"px",
      left: ((w - settings.width)/2)+"px" });
    jQuery("#GB_frame").css("height",settings.height - 32 +"px");
  }

  jQuery.GB_hide = function() {
    jQuery("#GB_window,#GB_overlay").remove();
    if(settings.callback && typeof(settings.callback) == 'function') {
      settings.callback.apply();
    }
  }

  jQuery.GB_show = function(url, pagePosition, options) {
    settings = jQuery.extend({
      close_img: "http://moss2007/PublishingImages/gb-close.png",
      height: 400,
      width: 400,
      animation: false,
      overlay_clickable: true,
      callback: null,
      caption: ""
      }, options || {});

    jQuery(document.body)
      .append(
        "<div id='GB_overlay'></div>" +
        "<div id='GB_window'><div id='GB_caption'></div>" +
        "<img src='" + settings.close_img + "' alt='Zatvori prozor'/></div>");
    jQuery("#GB_window img").click(jQuery.GB_hide);
    if(settings.overlay_clickable) {
      jQuery("#GB_overlay").click(jQuery.GB_hide);
    }
    jQuery(window).resize(jQuery.GB_position);
    jQuery(window).resize(GB_overlay_size);
    GB_overlay_size();

    jQuery("#GB_window").append("<div id='GB_frame'></div>");
    jQuery("#GB_window #GB_frame").load(url+" "+pagePosition);

    jQuery("#GB_caption").html(settings.caption);
    jQuery("#GB_overlay").show();
    jQuery.GB_position();

    if(settings.animation) {
      jQuery("#GB_window").slideDown("slow");
    } else {
      jQuery("#GB_window").show();
    }
  }

})();

