/* 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_HEIGHT = 200;
var GB_WIDTH = 200;

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

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

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

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

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}
function GB_Close(t) {
  $("#GB_window,#GB_overlay").close();
}


function GB_position() {

    var de = document.documentElement;
    var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    
    var scrolledX, scrolledY;
    if(self.pageYOffset ) {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    } else if( document.documentElement && document.documentElement.scrollTop ) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    } else if( document.body ) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }
    // Determine the coordinates of the center of browser's window


    var centerX, centerY;
    if( self.innerHeight ) {
      centerX = self.innerWidth;
      centerY = self.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight ) {
      centerX = document.documentElement.clientWidth;
      centerY = document.documentElement.clientHeight;
    } else if( document.body ) {
      centerX = document.body.clientWidth;
      centerY = document.body.clientHeight; 
    }
    var leftOffset = scrolledX + (centerX - 250) / 2;
    var topOffset = scrolledY + (centerY - 200) / 2; 

    // I could have done the above calculation differently insted of the following - and + but for now it is okay 
    // the following overwrights the greybox.css cordinations
    // this is for the background and at the moment is invisable $("#GB_overlay").css({top: (topOffset -110) +"px", left: (leftOffset - 300) + "px", width:GB_WIDTH+50 +"px",height:GB_HEIGHT+70+"px" });
    $("#GB_window").css({top: (topOffset -80) +"px", left: (leftOffset - 280) + "px", width:GB_WIDTH+10 +"px",height:GB_HEIGHT+10+"px" });
    $("#GB_frame").css("height",GB_HEIGHT + 10 +"px"); 
    

   /*original without the scrolle bar changes $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px" });
    $("#GB_frame").css("height",GB_HEIGHT - 32 +"px");*/
}

