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

var JFrame_ROOT = "javascript/jquery/plugins/jframe/";
var JFrame_DONE = false;
var JFrame_HEIGHT = 400;
var JFrame_WIDTH = 400;

function JWindow_show(caption, url, height, width) {
  JFrame_HEIGHT = height || 400;
  JFrame_WIDTH = width || 400;
  if(!JFrame_DONE) {
    $(document.body)
      .append("<div id='JFrame_overlay'></div><div id='JFrame_window'><div id='JFrame_caption'></div>"
        + "<img src='"+JFrame_ROOT+"close.gif' alt='Close window'/></div>");
    $("#JFrame_window img").click(JFrame_hide);
    $("#JFrame_overlay").click(JFrame_hide);
    $(window).resize(JFrame_position);
    $(window).scroll(JFrame_position);
    JFrame_DONE = true;
  }

  $("#JFrame_frame").remove();
  $("#JFrame_window").append("<iframe id='JFrame_frame' src='"+url+"'></iframe>");

  $("#JFrame_caption").html(caption);
  JFrame_position();
	
  /** Finally show the frame */
  $("#JFrame_window,#JFrame_overlay").hide();
  $("#JFrame_overlay").show();
  $("#JFrame_window").fadeIn('slow');
 
}

function JFrame_hide() {
  $("#JFrame_window,#JFrame_overlay").hide();
}

function JFrame_position() {
	var de = document.documentElement;
    var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
    var dsoctop=document.all? iebody.scrollTop : pageYOffset;
    
    var height = h < JFrame_HEIGHT ? h - 32 : JFrame_HEIGHT;
    var top = (h - height)/2 + dsoctop;
    
    $("#JFrame_window").css({width:JFrame_WIDTH+"px",height:height+"px",
      left: ((w - JFrame_WIDTH)/2)+"px", top: top+"px" });
    $("#JFrame_frame").css("height",height - 32 +"px");
    $("#JFrame_overlay").css({height:h, top:dsoctop + "px", width:w});
}
