/****
 GreyBox
   Copyright Amir Salihefendic 2006
 AUTHOR
   4mir Salihefendic (http://amix.dk) - amix@amix.dk
 VERSION
	 1.63
 LICENSE
  LGPL (read more in LGPL.txt)
 SITE
   http://amix.dk/greybox
****/
var GB_HEADER = null;
var GB_WINDOW = null;
var GB_IFRAME = null;
var GB_OVERLAY = null;

var GB_HEIGHT = 400;
var GB_WIDTH = 400;

var GB_caption = null;

var GB_CALLBACK = null;

var GB_overlay_click_close = false;

var GB_reload_on_close = false;

function GB_show(caption, url, height, width, overlay_click_close, reload_on_close, callback) {
	try {
		GB_HEIGHT = height || GB_HEIGHT;
		GB_WIDTH = width || GB_WIDTH;
		GB_CALLBACK = callback;
		GB_overlay_click_close = overlay_click_close || GB_overlay_click_close;
		GB_reload_on_close = reload_on_close || GB_reload_on_close;
				
		GB_Setup();
		
		GB_caption.innerHTML = caption;
		
		GB_setWidth();
		
		GB_showElement(GB_OVERLAY);
		GB_showElement(GB_HEADER);
		GB_showElement(GB_WINDOW);
		
		GB_IFRAME.src = url;
		GB_IFRAME.opener = this;
		return false;
	}
	catch (e) {
		alert(e);
		return false;
	}
}

function GB_hide() {
	GB_IFRAME.src = "";
	removeElement(GB_WINDOW);
	removeElement(GB_HEADER);
	removeElement(GB_OVERLAY);
	removeElement(GB_IFRAME);
	removeElement(GB_caption);
	GB_WINDOW = null;
	GB_HEADER = null;
	GB_OVERLAY = null;
	GB_IFRAME = null;
	GB_caption = null;
	if (GB_CALLBACK) GB_CALLBACK();
	if(GB_reload_on_close)
		window.location.reload();
}

function IsIE6(){
	ua = navigator.userAgent;
	v = ua.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
	if (v) return parseFloat( v[1] ) < 7;
	return false;
}

function GB_setWidth() {
    if (!GB_WINDOW) return;

	var 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";

	if (IsIE6()){
		GB_OVERLAY.style.width = size[0] + "px";
		GB_OVERLAY.style.height = size[1] + "px";
	}
	GB_WINDOW.style.height = (GB_HEIGHT - 22) + "px";
	GB_IFRAME.style.height = (GB_HEIGHT - 22) + "px";
  
	GB_WINDOW.style.left = getScrollLeft() + ((size[0] - GB_WINDOW.offsetWidth) /2) + "px";
	GB_HEADER.style.left = getScrollLeft() + ((size[0] - GB_HEADER.offsetWidth) /2) + "px";
  
	var top = (size[1] - (GB_HEIGHT+27)) /2;
	if (top < 0) top = 0;
	GB_HEADER.style.top = getScrollTop()+top+"px";
	GB_WINDOW.style.top = getScrollTop()+(top+27)+"px";
	
	GB_OVERLAY.style.top = getScrollTop() + "px";
	GB_OVERLAY.style.left = getScrollLeft() + "px";
}

function GB_Create() {
	//Create the overlay
	GB_OVERLAY = DIV({'id': 'GB_overlay'});
	
	if(GB_overlay_click_close) GB_OVERLAY.onclick = GB_hide;
	
	var body = document.body;
    body.insertBefore(GB_OVERLAY, body.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 + 'close.gif', 'alt': 'Close window'}));
	close.onclick = GB_hide;
	ACN(GB_HEADER, close, GB_caption);
	
	body.insertBefore(GB_WINDOW, GB_OVERLAY.nextSibling);
	body.insertBefore(GB_HEADER, GB_OVERLAY.nextSibling);
	
	GB_IFRAME = IFRAME({'id': 'GB_frame', 'name': 'GB_frame', 'frameBorder': 0});
	ACN(GB_WINDOW, GB_IFRAME);
}

function GB_Setup() {
	GB_Create();
    GB_addOnWinResize(GB_setWidth);
    window.onscroll = function() { GB_setWidth(); };
}

function GB_getWindowSize(){
	var doc = document;
	var de = doc.documentElement;
	var db = doc.body;
	var width, height;
	if (self.innerHeight) {	// all except Explorer
		width = self.innerWidth;
		height = self.innerHeight;
	} else if (de && de.clientHeight) { // Explorer 6 Strict Mode
		width = de.clientWidth;
		height = de.clientHeight;
	} else if (db) { // other Explorers
		width = db.clientWidth;
		height = db.clientHeight;
	}	
	return [width, height];
}

function GB_addOnWinResize(func) {
	var oldonrezise = window.onresize;
	if (typeof window.onresize != 'function')
		window.onresize = func;
	else {
		window.onresize = function() {
			oldonrezise();
			func();
		}
	}
}

function getScrollLeft() {
	var t;
	if (document.documentElement && document.documentElement.scrollLeft)
		t = document.documentElement.scrollLeft;
	else if (document.body)
		t = document.body.scrollLeft;
	return t;
}

function getScrollTop() {
	var t;
	if (document.documentElement && document.documentElement.scrollTop)
		t = document.documentElement.scrollTop;
	else if (document.body)
		t = document.body.scrollTop;
	return t;
}

function getElement(id) {
	if(typeof(id) == "string") 
		return document.getElementById(id);
	else
		return id;
}

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;
}

/**** 
  DOM manipulation 
 ****/
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 replaceChildNodes(node/*, nodes...*/) {
  var child;
  while ((child = node.firstChild)) {
    node.removeChild(child);
  }
  if (arguments.length < 2) {
    return node;
  } else {
    return appendChildNodes.apply(this, arguments);
  }
}
var RCN = replaceChildNodes;

function insertAfter(node, referenceNode) {
  referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}

function insertBefore(node, referenceNode) {
  referenceNode.parentNode.insertBefore(node, referenceNode);
}

function GB_showElement(elm) { elm.style.display = ''; }

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" || typeof(n) == "number")
          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 A = function() { return createDOM.apply(this, ["a", arguments]); };
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]); };

/**** 
  Misc 
 ****/
function keys(obj) {
  var rval = [];
  for (var prop in obj) {
    rval.push(prop);
  }
  return rval;
}

function isDefined(o) {
  return (o != "undefined" && o != null)
}

function isArray(obj) { 
  try { return (typeof(obj.length) == "undefined") ? false : true; }
  catch(e)
  { return false; }
}
