
/* Oxebox 0.96
 **************
 * Div-Popup mit iFrame/Div und buntischen Hintergrund
 *
 * Usage:
 * oxebox_open(string TITEL, string URL, int BREITE, int HÖHE [, string CALLBACK_FUNKTION]);
 * 
 * - Wenn URL leer ist wird Div verwendet anstatt iFrame
 * - Wenn Parameter CALLBACK_FUNKTION existiert wird diese nach
 *	 oxebox_create() aufgerufen (nützlich um Div zu füllen)
 *
 * Autor: Peter Navrodt
 * Letzte Änderung: 29.04.09, PN
 *
 * http://www.ladies.de                 (c) 2008-2009 RTO GmbH
 **************************************************************/

var oxe_x, oxe_y, oxe_x_b, oxe_y_b, oxe_width, oxe_height, oxe_title, oxe_trans_time;
var oxe_trans_max = 7;
var oxe_trans_interval = 50;
var oxe_add_height = 27;
var oxe_add_width = 6;

if(typeof(OXE_ROOT_DIR) == "undefined") var OXE_ROOT_DIR = "oxebox";
if(typeof(OXE_IMAGE_CLOSE) == "undefined") var OXE_IMAGE_CLOSE = "oxeclose.gif";
if(typeof(OXE_TEXT_CLOSE) == "undefined") var OXE_TEXT_CLOSE = "";
if(typeof(OXE_CSS_HEAD) == "undefined") var OXE_CSS_HEAD = "head";
if(typeof(OXE_CSS_TITLE_START) == "undefined") var OXE_CSS_TITLE_START = '<span class="title">';
if(typeof(OXE_CSS_TITLE_END) == "undefined") var OXE_CSS_TITLE_END = '</span>';
if(typeof(OXE_CSS_BG_CLASS) == "undefined") var OXE_CSS_BG_CLASS = 'oxebox_bg';

function oxebox_open(ptitle, url, pwidth, pheight, cb_function, oxevar) {
	oxe_width = pwidth ? pwidth : 600;
	oxe_height = pheight ? pheight : 400;
	oxe_title = ptitle;
	
	divid = "oxebox_container";
	div_oxebox = document.createElement("div");
	div_oxebox.setAttribute("class", "oxebox");
	div_oxebox.setAttribute("className", "oxebox");
	div_oxebox.setAttribute("id", divid);
	div_body = document.getElementsByTagName("body")[0];
	div_body.appendChild(div_oxebox);
	document.onkeydown = function(e) {e = e ? e : window.event; oxe_checkkey(e, divid); }
	bg_open();
	if(typeof(cb_function) != 'undefined') {
		setTimeout('oxebox_create("'+url+'","'+divid+'","'+cb_function+'");', oxe_trans_time);
	} else {
		setTimeout('oxebox_create("'+url+'","'+divid+'");', oxe_trans_time);
	}
}

function oxebox_create(url, divid, cb_function) {
	var out = '';
	var close_link = '';
	div_pink = document.getElementById(divid);
	get_xy();
	div_pink.style.left = oxe_x+'px';
	div_pink.style.top = oxe_y+'px';
	div_pink.style.display = 'block';
	iframe_id = "oxe_frame";
	if(OXE_TEXT_CLOSE != "") {
		close_link = '<a href="javascript:oxebox_close(\''+divid+'\');">'+OXE_TEXT_CLOSE+'</a> ';
	}
	if(oxe_title != '') {
		out += '	<div class="'+OXE_CSS_HEAD+'" id="head">'+OXE_CSS_TITLE_START+oxe_title+OXE_CSS_TITLE_END+' <div class="close">'+close_link+'<a href="javascript:oxebox_close(\''+divid+'\');"><img src="'+OXE_ROOT_DIR+'/'+OXE_IMAGE_CLOSE+'" border="0" /></a></div></div>';
	}
	if(url == '') {
		out += '	<div id="'+iframe_id+'" class="divcontent" name="'+iframe_id+'" style="width:'+oxe_width+'px;height:'+oxe_height+'px;"></div>';
	} else {
		out += '	<iframe id="'+iframe_id+'" name="'+iframe_id+'" class="pink" width="'+oxe_width+'" height="'+oxe_height+'" src="'+url+'" frameborder="0"></iframe>';
	}
	div_pink.innerHTML = out;
	div_head = document.getElementById("head");
	if(!empty(div_head)) {
		div_head.style.width = oxe_width+'px';
	}
	window.onresize = function() { oxebox_resize(); }
	if(url != '') {
		oxe_win = document.getElementById(iframe_id).contentWindow;
		try {
			// Bei externen Seiten im iFrame funktioniert das nicht
			oxe_win.onkeydown = function(e) { e = e ? e : window.event; oxe_checkkey(e, divid); }
		} catch(e) {}
	}

	if(typeof(cb_function) != 'undefined' && typeof(cb_function) == 'string') {
		div_oxe_frame = document.getElementById(iframe_id);
		eval(''+cb_function+'(div_oxe_frame)');
	}
}
function oxebox_close(divid) {
	if(typeof(divid) == "undefined") {
		divid = "oxebox_container";
	}
	document.onkeydown = function() {}
	window.onresize = function() {}
	div_oxebox = document.getElementById(divid);
	div_oxebox.innerHTML = '';
	div_oxebox.style.left = '0px';
	div_oxebox.style.top = '0px';
	div_oxebox.style.display = 'none';
	div_body = document.getElementsByTagName("body")[0];
	div_body.removeChild(div_oxebox);
	bg_close();
}
function bg_open() {
	div_oxebox_bg = document.createElement("div");
	div_oxebox_bg.setAttribute("class", "oxebox_bg");
	div_oxebox_bg.setAttribute("className", "oxebox_bg");
	div_oxebox_bg.setAttribute("id", "oxebox_bg");
	div_body = document.getElementsByTagName("body")[0];
	div_body.appendChild(div_oxebox_bg);
	div_bg = document.getElementById("oxebox_bg");
	
	get_bodyxy();
	div_bg.style.width = oxe_x_b+"px";
	div_bg.style.height = oxe_y_b+"px";
	div_bg.style.filter = "alpha(opacity=0)";
	div_bg.style.opacity = "0.0";
	div_bg.style.zIndex = "1110";
	div_bg.style.display = "block";
	initFade(div_bg);
}
function bg_close() {
	div_bg = document.getElementById("oxebox_bg");
	div_bg.style.width = "0px";
	div_bg.style.height = "0px";
	div_bg.style.display = "none";
	div_body = document.getElementsByTagName("body")[0];
	div_body.removeChild(div_bg);
}
function oxebox_resize() {
	div_bg = document.getElementById("oxebox_bg");
	get_bodyxy();
	div_bg.style.width = oxe_x_b+"px";
	div_bg.style.height = oxe_y_b+"px";
	get_xy();
	div_pink.style.left = oxe_x+'px';
	div_pink.style.top = oxe_y+'px';
}
function initFade(div_bg) {
	oxe_trans_time = 0;
	for (var i=0;i<oxe_trans_max;i++) {
		setTimeout('setOpacity('+i+', div_bg)',oxe_trans_interval*i);
		oxe_trans_time = oxe_trans_time + ((oxe_trans_interval-25)*i);
	}
	return false;
}
function setOpacity(value, div_bg)
{
	div_bg.style.opacity = value/10;
	div_bg.style.filter = 'alpha(opacity=' + value*10 + ')';
}
function Numsort (a, b) {
	// Ewige Blumenkraft aktiviert!
	return a - b;
}
function get_xy() {
	arr_x = new Array;
	arr_y = new Array;
	ipos = 0;

	/* Innere Maße (des sichtbaren Bereichs)
	 *****************************************/
	// Width
	if (typeof(document.body.clientWidth) != "undefined") {
		arr_x.push(document.body.clientWidth);
	}
	if (typeof(self.innerWidth) != "undefined") {
		arr_x.push(self.innerWidth);
	}
	if (document.documentElement && document.documentElement.clientWidth) {
		arr_x.push(document.documentElement.clientWidth);
	}
	if(arr_x.length > 0) {
		// Kleinsten Wert verwenden, wenn nicht 0 (ohne Scrollbalken)
		arr_x.sort(Numsort);
		for(i = 0; i < arr_x.length; i++) {
			if(arr_x[i] != 0) {
				ipos = i;
				break;
			}
		}
		x_w = arr_x[ipos];
	} else {
		x_w = 950;
	}
	// Height
	ipos = 0;
	if (typeof(document.body.clientHeight) != "undefined") {
		arr_y.push(document.body.clientHeight);
	}
	if (typeof(self.innerHeight) != "undefined") {
		arr_y.push(self.innerHeight);
	}
	if (document.documentElement && document.documentElement.clientHeight) {
		arr_y.push(document.documentElement.clientHeight);
	}
	if(arr_y.length > 0) {
		// Kleinsten Wert verwenden, wenn nicht 0 (ohne Scrollbalken)
		arr_y.sort(Numsort);
		for(i = 0; i < arr_y.length; i++) {
			if(arr_y[i] != 0) {
				ipos = i;
				break;
			}
		}
		y_w = arr_y[ipos];
	} else {
		y_w = 780;
	}

	/* Scroll Offset
	 *****************************************/
	if (self.pageYOffset) { // all except Explorer
		x_o = self.pageXOffset;
		y_o = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		x_o = document.documentElement.scrollLeft;
		y_o = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
		x_o = document.body.scrollLeft;
		y_o = document.body.scrollTop;
	}
	
	// Mitte berechnen
	oxe_x = Math.round(x_w/2 - (oxe_width+oxe_add_width)/2 + x_o);
	oxe_y = Math.round(y_w/2 - (oxe_height+oxe_add_height)/2 + y_o);
	if(oxe_y < 0) {
		oxe_y = 0;
	}
	if(oxe_x < 0) {
		oxe_x = 0;
	}
}

function get_bodyxy() {
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;

	if (test1 > test2) { // all but Explorer Mac
		oxe_x_b = document.body.scrollWidth;
		oxe_y_b = document.body.scrollHeight;
	} else { // Explorer Mac;
		//would also work in Explorer 6 Strict, Mozilla and Safari
		oxe_x_b = document.body.offsetWidth;
		oxe_y_b = document.body.offsetHeight;
	}
	
	var strMarginLeft = getCurrentStyle(document.body, 'margin-left');
	var strMarginRight = getCurrentStyle(document.body, 'margin-right');
	
	if(strMarginLeft != '' && strMarginLeft.match(/px/)) {
		oxe_x_b = oxe_x_b + parseInt(strMarginLeft.replace(/px/, ''));
	}
	if(strMarginRight != '' && strMarginRight.match(/px/)) {
		oxe_x_b = oxe_x_b + parseInt(strMarginRight.replace(/px/, ''));
	}
}

function oxe_checkkey(e, divid) {
	if(!e) var e = window.event;
	var code = (e.which || e.keyCode);
	if(code == 27) {
		oxebox_close(divid);
	}
}

// Style eines Elements herausfinden (egal ob in css Datei, <style> tag oder direkt definiert)
function getCurrentStyle(elem, prop) {
   if( elem.currentStyle ) {
      var ar = prop.match(/\w[^-]*/g);
      var s = ar[0];
      
      for(var i = 1; i < ar.length; ++i) {
         s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
      }
           
      return elem.currentStyle[s];
   }
   else if( document.defaultView.getComputedStyle ) {
      return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
   }
}

if(typeof empty != 'function') {
	function empty(str) {
		if(str === undefined || str === null || str == "") {
			return true;
		}
		return false;
	}
}
