/* 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;
jQuery.noConflict(); 

if ((typeof top.window.GB_windows) == "undefined"){
  top.window.GB_windows = [];
  top.window.GB_windows_width = [];
  top.window.GB_windows_height = [];
  var layer_index = 99;
  var overlay_index = 0;
}
function GB_open(url, winId, width, height, type) {
  top.window.document.body.style.overflow = "hidden";
  top.window.document.body.scroll = "no";
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  GB_ID = 'GB_window_'+winId;
  if (jQuery.browser.msie && typeof top.window.XMLHttpRequest == "undefined") {
    jQuery(document.body).append('<iframe id="ieFixFrame" name="ieFixFrame" src="BLOCKED SCRIPT\'&lt;html&gt;&lt;/html&gt;\';" width="100%" scrolling="no" frameborder="0"></iframe>');
  }
  jQuery(document.body).append("<div id='GB_overlay_" + overlay_index +"' class='GB_overlay' style='visibility: hidden; z-index:" + layer_index +";'></div>");
  layer_index = layer_index + 1;
//  if (jQuery.browser.msie) {
//    jQuery("#GB_overlay_" + overlay_index).show();
    jQuery("#GB_overlay_" + overlay_index).css('visibility','visible');
/*  } else {
    jQuery("#GB_overlay_" + overlay_index).fadeIn("slow");
    jQuery("#GB_overlay_" + overlay_index).css('visibility','visible');
  }*/

  jQuery('#GB_overlay_' + overlay_index).append('<div class="GB_preload_message"></div>');

  jQuery(document.body).append('<div id="'+GB_ID+'" class="GB_window" style="visibility:hidden; z-index:' + layer_index +';"><div id="ID_GB_caption" class="GB_caption"></div><a href="javascript:void(0);" title="Fenster schließen" class="GB_closeButton">&nbsp;</a></div>');
  layer_index = layer_index + 1;
  jQuery('#'+GB_ID+' a.GB_closeButton').click(function(){GB_close();});
  jQuery('#'+GB_ID).append('<iframe id="GB_frame_'+winId+'" name="GB_frame_'+winId+'" class="GB_frame" src="'+url+'" frameborder="0"></iframe>');

  jQuery('#GB_frame_'+winId).load(function() {
      top.window.GB_position(winId);
      jQuery('#'+GB_ID).fadeIn('slow'); 
      jQuery('#'+GB_ID).css('visibility','visible'); 
    }); 
  overlay_index = overlay_index + 1;
  jQuery('html, body').animate({scrollTop:0}, 'slow');
  top.window.GB_windows.push(winId);
  top.window.GB_windows_height.push(GB_HEIGHT);
  top.window.GB_windows_width.push(GB_WIDTH);
  return true;
}
/*
function treeViewClose() {
	currentWindowId = top.window.GB_windows[top.window.GB_windows.length-1];
	//jQuery('#GB_window_'+hideId).hide();
	console.log("treeViewClose() call");
	jQuery('#GB_window_' + currentWindowId + ' #treetable').empty();
}*/

function GB_close(){
	/*setTimeout("treeViewClose()", 0);*/
  return setTimeout("GB_closeEx()", 0);
}

function GB_closeEx() { /* this does NOT remove the tree ITSELF! it does just removed the WINDOW! */
  hideId = top.window.GB_windows[top.window.GB_windows.length-1];
  jQuery('#GB_window_'+hideId).hide();
  
  if (top.window.GB_windows.length > 1) {
/*  	jQuery('#GB_window_' + hideId).empty(); // doesnt change anything! */

    jQuery('#GB_window_' + hideId).remove();
    layer_index = layer_index - 1; overlay_index = overlay_index - 1;
    jQuery('#GB_overlay_' + overlay_index).remove();
    layer_index = layer_index - 1;
    jQuery('#ieFixFrame').remove();

    showId = top.window.GB_windows[top.window.GB_windows.length-2];
    top.window.GB_windows_height.pop(GB_HEIGHT);
    top.window.GB_windows_width.pop(GB_WIDTH);
    GB_HEIGHT = top.window.GB_windows_height[top.window.GB_windows_height.length - 1];
    GB_WIDTH = top.window.GB_windows_width[top.window.GB_windows_width.length - 1]; 
    top.window.GB_windows.pop(); 
  } else {
    overlay_index = overlay_index - 1;
/*    jQuery('#GB_overlay_'+ overlay_index).empty();*/

    jQuery('#GB_overlay_'+ overlay_index).remove();
    layer_index = layer_index - 1;
    jQuery('#ieFixFrame').remove();
    jQuery('#GB_window_'+hideId).remove();

    layer_index = layer_index - 1;
    top.window.GB_windows = [];
    top.window.GB_windows_height = [];
    top.window.GB_windows_width = [];
  }
  top.window.document.body.style.overflow = "scroll";
  top.window.document.body.scroll = "auto";
  return true;
}

function GB_close_specific(deleteWinId) {
  jQuery('#GB_window_'+deleteWinId).hide();
  var temp_GB_windows = [];
  var temp_GB_windows_width = [];
  var temp_GB_windows_height = [];
  for (var windowCount=0; windowCount < top.window.GB_windows.length; windowCount++) {
    if (top.window.GB_windows[windowCount] != deleteWinId){
      temp_GB_windows.push(top.window.GB_windows[windowCount]);
      temp_GB_windows_width.push(top.window.GB_windows_width[windowCount]);
      temp_GB_windows_width.push(top.window.GB_windows_width[windowCount]);
    }

  }
  
  jQuery('#GB_window_'+deleteWinId).remove();
  layer_index = layer_index - 1; overlay_index = overlay_index - 1;
  jQuery('#GB_overlay_' + overlay_index).remove();
  layer_index = layer_index - 1;
  jQuery('#ieFixFrame').remove();
  showId = top.window.GB_windows[top.window.GB_windows.length-1];

  top.window.document.body.style.overflow = "visible";
  document.body.scroll = "yes";
  GB_position_all();
  top.window.GB_windows_height = temp_GB_windows_height;
  top.window.GB_windows_width = temp_GB_windows_width;
  top.window.GB_windows = temp_GB_windows;
  GB_HEIGHT = top.window.GB_windows_height[top.window.GB_windows_height.length - 1];
  GB_WIDTH = top.window.GB_windows_width[top.window.GB_windows_width.length - 1]; 
  return true;
}

function GB_change_size(changeWidth, changeHeight) {
  if(changeWidth != 0) {
    top.window.GB_WIDTH = changeWidth;
    top.window.GB_windows_width.pop();
    top.window.GB_windows_width.push(changeWidth);
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).css('visibility','hidden');
    top.window.GB_position(top.window.GB_windows[top.window.GB_windows.length-1]);
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).fadeIn('slow'); 
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).css('visibility','visible'); 
  }
  if(changeHeight != 0){
    top.window.GB_HEIGHT = changeHeight;
    top.window.GB_windows_height.pop();
    top.window.GB_windows_height.push(changeHeight);
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).css('visibility','hidden');
    top.window.GB_position(top.window.GB_windows[top.window.GB_windows.length-1]);
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).fadeIn('slow'); 
    jQuery('#'+ 'GB_window_' + top.window.GB_windows[top.window.GB_windows.length-1]).css('visibility','visible'); 
  }
  return true;
}

function GB_position_only(winId, winWidth, winHeight) {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  var f = jQuery('#GB_frame_'+winId);
  if(f) {
    if (jQuery('#GB_frame_'+winId).length>0) {
      var innerDoc = document.getElementById(winId);
      if(innerDoc){
        var ch = innerDoc.scrollHeight;
        if (ch>(h-95)) ch = h-95;
      }
    else ch = winHeight;
    if (top.window.innerHeight < ch + 50) {
      ch = top.window.innerHeight - 50;
    }
      jQuery('#GB_window_'+winId).css({width:winWidth+'px',height:ch+'px',
        left: ((w - winWidth)/2)+'px', top: ((h - (ch+55))/2) });
      jQuery('#GB_window_'+winId+' iframe').css('height',ch+'px');
      if(ch > h) {
        top.window.document.getElementById("GB_frame_"+winId).style.overflow = "scroll";
      } else {
        top.window.document.getElementById("GB_frame_"+winId).style.overflowy = "hidden";
      }
      if(winHeight > document.body.clientHeight){
        top.window.GB_change_size(winWidth, document.body.clientHeight - 100);
      }
   }
  }
  return true;
}


function GB_position(winId) {
  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_HEIGHT = GB_HEIGHT || 600;
  var f = jQuery('#GB_frame_'+winId);
  if (jQuery('#GB_frame_'+winId).length>0) {
    var innerDoc = (f.get(0).contentWindow) ? f.get(0).contentWindow.document.body.parentNode : f.get(0).contentDocument.body;
    var ch = innerDoc.scrollHeight;
    if (ch>(h-95)) ch = h-95;
    else ch = GB_HEIGHT;
    if (top.window.innerHeight < ch + 50) {
      ch = top.window.innerHeight - 50;
    }
    jQuery('#GB_window_'+winId).css({width:GB_WIDTH+'px',height:ch+'px', left: ((w - GB_WIDTH)/2)+'px', top: ((h - (ch+55))/2) });
    jQuery('#GB_window_'+winId+' iframe').css('height',ch+'px');
  }
  return true;
}
function GB_position_all() {
  for (var windowCount=0; windowCount < top.window.GB_windows.length; windowCount++) {
    GB_position_only(top.window.GB_windows[windowCount], top.window.GB_windows_width[windowCount],top.window.GB_windows_height[windowCount]);
  }
  return true;
}
function GB_hide(winId, overlayid) {
  var compString = "#GB_window_" + winId + ",#GB_overlay_" + overlayid ;
  jQuery(compString).hide();
  return true;
}

function GB_getOpener() {
  var o = 'GB_frame_'+window.GB_windows[window.GB_windows.length-2];
  if (jQuery('#'+o).length>0) {
    return document.getElementById(o).contentWindow;
  } else {
    return window;
  }
  return true;
}

