/* 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 = 400;
var GB_WIDTH = 400;

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    var pageData = getPageSize();

    $(document.body)
      .append("<div id='GB_overlay' style='height:"+pageData[1]+"px; width:"+pageData[0]+"px;'></div><div id='GB_window' style='overflow: hidden;'><div id='GB_caption'></div>"
      //.append("<div id='GB_window' style='overflow: hidden;'><div id='GB_caption'></div>"
        + "<img src='http://www.stoebich.de/www/inc/greybox/close.gif' alt='close' title='close'/></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+"' style='overflow: hidden;' scrolling='no' ></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  //if(GB_ANIMATION) {
  //  alert("slow");
  //  $("#GB_window").slideDown("slow");
  //} else {
  //  alert("show");
    $("#GB_window").show();
 // }
}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function GB_position() {

    // anpassungen an scrollposition
    var currScrollY=0;
    if (document.all) { // IE
        currScrollY = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
    } else { // Good Browsers
        currScrollY = window.scrollY;
    }

    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+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px",top: (((h - GB_HEIGHT)/2)+currScrollY)+"px" });
    $("#GB_frame").css("height",GB_HEIGHT - 1 +"px");
    //$("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}



function getPageSize(){

var xScroll, yScroll;

if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
xScroll = document.documentElement.scrollWidth;
yScroll = document.documentElement.scrollHeight;
} else { // Explorer Mac...would also work in Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}

var windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}

// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}

// for small pages with total width less then width of the viewport
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}


arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
return arrayPageSize;
}
