function getElement(id) {
  if(typeof(id) == "string") 
    return document.getElementById(id);
  else
    return id;
}
var $ = getElement;

function getBody() { return getElementsByTagAndClassName('body')[0] };

function getElementsByTagAndClassName(tag_name, class_name, /* optional */parent) {
  var class_elements = new Array();
  if(parent == null || parent == "undefined")
    parent = document;
  if(tag_name == null || tag_name == "undefined")
    tag_name = '*';

  var els = parent.getElementsByTagName(tag_name);
  var els_len = els.length;
  var pattern = new RegExp("(^|\\s)" + class_name + "(\\s|$)");

  for (i = 0, j = 0; i < els_len; i++) {
    if ( pattern.test(els[i].className) || class_name == null ) {
      class_elements[j] = els[i];
      j++;
    }
  }
  return class_elements;
}


function appendChildNodes(node/*, nodes...*/) {
  if(arguments.length >= 2) {
    for(var i = 1; i < arguments.length; i++) {
      var n = arguments[i];
      if(typeof(n) == "string")
        n = document.createTextNode(n);
      else if(isDefined(n))
        node.appendChild(n);
    }
  }
  return node;
}
var ACN = appendChildNodes;

function showElement(elm) { elm.style.display = ''; }
function hideElement(elm) { elm.style.display = 'none'; }

function swapDOM(dest, src) {
  dest = getElement(dest);
  var parent = dest.parentNode;
  if (src) {
    src = getElement(src);
    parent.replaceChild(src, dest);
  } else {
    parent.removeChild(dest);
  }
  return src;
}

function removeElement(elm) {
  swapDOM(elm, null);
}

function createDOM(name, attrs) {
  var i = 1;
  elm = document.createElement(name);

  if(isDefined(attrs[0]) && typeof(attrs[0]) != "string") {
    for(k in attrs[0]) {
      if(k == "style")
        elm.style.cssText = attrs[0][k];
      else if(k == "class")
        elm.className = attrs[0][k];
      else
        elm.setAttribute(k, attrs[0][k]);
    }
    for(i; i < attrs.length; i++) {
      var n = attrs[i];
      if(isDefined(n)) {
        if(typeof(n) == "string")
          n = document.createTextNode(n);
        elm.appendChild(n);
      }
    }
  }
  else {
    //We have just a string...
    var n = attrs[0];
    if(isDefined(n)) {
      n = document.createTextNode(n);
      elm.appendChild(n);
    }
  }
  return elm;
}

var DIV = function() { return createDOM.apply(this, ["div", arguments]); };
var IMG = function() { return createDOM.apply(this, ["img", arguments]); };
var IFRAME = function() { return createDOM.apply(this, ["iframe", arguments]); };

function isDefined(o) {
  return (o != "undefined" && o != null)
}

var GB_HEADER = null;
var GB_WINDOW = null;
var GB_IFRAME = null;
var GB_OVERLAY = null;
var GB_TIMEOUT = null;

var GB_HEIGHT = 400;
var GB_WIDTH = 400;

var GB_caption = null;

var GB_last_win_url = null;

function GB_show(caption, url /* optional */, height, width) {
  try {
    if(height != 'undefined')
      GB_HEIGHT = height;
    if(width != 'undefined')
      GB_WIDTH = width;

    initIfNeeded();
    GB_IFRAME.src = url;
    GB_IFRAME.opener = this;

    GB_caption.innerHTML = caption;

    GB_setPosition();
    if(GB_ANIMATION) {
      positionRightVertically(GB_HEADER, -(GB_HEIGHT));
      positionRightVertically(GB_WINDOW, -(GB_HEIGHT+22));
    }

    showElement(GB_OVERLAY);
    showElement(GB_HEADER);
    showElement(GB_WINDOW);

    GB_setWidth();

    if(GB_ANIMATION) {
      GB_animateOut(-GB_HEIGHT);
    }
    return false;
  }
  catch (e) {
    return false;
  }
}

function GB_hide() {
  GB_IFRAME.src = "";
  hideElement(GB_WINDOW);
  hideElement(GB_HEADER);
  hideElement(GB_OVERLAY);
}

function GB_setPosition() {
  positionRightVertically(GB_HEADER, 0);
  positionRightVertically(GB_WINDOW, 22);
}

function GB_animateOut(top) {
  if(top+getScrollTop() < 0) {
    positionRightVertically(GB_WINDOW, top+22);
    positionRightVertically(GB_HEADER, top);
    GB_TIMEOUT = window.setTimeout(function() { GB_animateOut(top+50); }, 1);
  }
  else {
    GB_WINDOW.style.top = getScrollTop()+22+"px";
    GB_HEADER.style.top = getScrollTop()+"px";
    clearTimeout(GB_TIMEOUT);
  }
}

function GB_setWidth() {
  var array_page_size = GB_getWindowSize();

  //Set size
  GB_WINDOW.style.width = GB_WIDTH + "px";
  GB_IFRAME.style.width = GB_WIDTH + "px";
  GB_HEADER.style.width = GB_WIDTH + "px";

  GB_WINDOW.style.height = GB_HEIGHT + "px";
  GB_IFRAME.style.height = GB_HEIGHT - 5 + "px";

  GB_OVERLAY.style.width = array_page_size[0] + "px";

  var max_height = Math.max(getScrollTop()+array_page_size[1], getScrollTop()+GB_HEIGHT+30);
  GB_OVERLAY.style.height = max_height + "px";

  GB_WINDOW.style.left = ((array_page_size[0] - GB_WINDOW.offsetWidth) /2) + "px";
  GB_HEADER.style.left = ((array_page_size[0] - GB_HEADER.offsetWidth) /2) + "px";
  
}

function GB_init() {
  //Create the overlay
  GB_OVERLAY = DIV({'id': 'GB_overlay'});

  if(GB_overlay_click_close)
    GB_OVERLAY.onclick = GB_hide;

  getBody().insertBefore(GB_OVERLAY, getBody().firstChild);

  //Create the window
  GB_WINDOW = DIV({'id': 'GB_window'});

  GB_HEADER = DIV({'id': 'GB_header'});
  GB_caption = DIV({'id': 'GB_caption'}, "");

  var close = DIV({'id': 'GB_close'}, IMG({'src': GB_IMG_DIR + 'pop_close.gif', 'alt': 'Close window'}));
  close.onclick = GB_hide;
  ACN(GB_HEADER, close, GB_caption);

  getBody().insertBefore(GB_WINDOW, GB_OVERLAY.nextSibling);
  getBody().insertBefore(GB_HEADER, GB_OVERLAY.nextSibling);

}

function initIfNeeded() {
  if(GB_OVERLAY == null) {
    GB_init();
    GB_addOnWinResize(GB_setWidth);
    window.onscroll = function() { GB_setPosition(); GB_setWidth(); };
  } 
  //Remove the old iFrame
  var new_frame = IFRAME({'id': 'GB_frame', 'name': 'GB_frame'});
  if (GB_IFRAME != null)
    removeElement(GB_IFRAME);
  ACN(GB_WINDOW, new_frame);
  GB_IFRAME = new_frame;
}

function GB_getWindowSize(){
	var window_width, window_height;
	if (self.innerHeight) {	// all except Explorer
		window_width = self.innerWidth;
		window_height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		window_width = document.body.clientWidth;
		window_height = document.body.clientHeight;
	}	
	return [window_width, window_height];
}

function GB_addOnWinResize(func) {
  var oldonrezise = window.onresize;
  if (typeof window.onresize != 'function')
    window.onresize = func;
  else {
    window.onresize = function() {
      oldonrezise();
      func();
    }
  }
}

function positionRightVertically(elm, value) {
  elm.style.top = getScrollTop()+value+"px";
}

function getScrollTop() {
  //From: http://www.quirksmode.org/js/doctypes.html
  var theTop;
  if (document.documentElement && document.documentElement.scrollTop)
      theTop = document.documentElement.scrollTop;
  else if (document.body)
      theTop = document.body.scrollTop;
  return theTop;
}
