/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_ANIMATION = false;
var GB_HEIGHT, GB_WIDTH;

/* Temp solution for IE8 scrollbar problem */
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
var ver = getInternetExplorerVersion();

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<a href='#' id='GB_close'><img src='close.gif' alt='Close window' /></a></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_overlay").fadeIn("slow");
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"' frameborder='0'><a href='"+url+"'>"+url+"</a></iframe>");
  $("#GB_caption").html(caption);
  GB_position();

  if(GB_ANIMATION) {
    $("#GB_window").slideDown("slow");
  } else {
    $("#GB_window").show();
  }
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
  $("html,body").css({"overflow":"auto","height":"auto"});
  if(ver < "8") {
    $("html").css({"overflow-y":"scroll"});
  } else {
    $("body").css({"overflow-y":"scroll"});
  }
}

function GB_position() {
  $("html,body").css({"height":"100%", "overflow":"hidden"});
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  $("#GB_window").css({"width":GB_WIDTH, "height":GB_HEIGHT, "top":"0px", "left":"0px" });
  // If values is in pixels
  if(GB_WIDTH.lastIndexOf("px")) {
	var left = (w/2) - (GB_WIDTH.slice(0,-2) / 2);
	$("#GB_window").css("left", left);
  }
  if(GB_HEIGHT.lastIndexOf("px")) {
	var top = (h/2 - 100) - (GB_HEIGHT.slice(0,-2) / 2);
	$("#GB_window").css("top", "8%");
  }
  $("#GB_frame").css("height",GB_HEIGHT);
}
