/*
	script voor custom alertboxjes in cme
*/
var WARNING_WINDOW = "warningWindow";
var CONFIRM_WINDOW = "confirmWindow";
var PRINT_WINDOW = "printWindow";
var ALERT_WINDOW = "alertWindow";
var WAIT_WINDOW = "waitWindow";
var INFO_WINDOW = "infoWindow";


var myWindow = null;

/* preload voor firefox */

var image_wi = new Image();
image_wi.src = "/shared/images/cme/wait_large.gif";
var image_wl = new Image();
image_wl.src = "/shared/images/cme/warning_large.gif";
var image_al = new Image();
image_al.src = "/shared/images/cme/alert_large.gif"
var image_pr = new Image();
image_pr.src = "/shared/images/cme/printer_large.gif";
/* 
	cancelled het myWindow
*/
function cancelWindow(){
	if(myWindow._shadow) document.body.removeChild(myWindow._shadow);
	document.body.removeChild(myWindow._window);
	myWindow = null;
	
	/*
		eerst alle selectboxen weer resetten
	*/
	var coll = document.getElementsByTagName("SELECT");
	for(var i=0;i<coll.length;i++){
		var oldDisplay = coll.item(i).getAttribute("oldDisplay");
		if(oldDisplay != null){
			coll.item(i).style.display = oldDisplay;
		} else {
			coll.item(i).style.display = "";
		}
	}	
	
}

/* 
	een warning window met custom message en 2 buttons
	warning windows in het midden van je browser
*/
function warningWindow(msg, url){
	if(myWindow) cancelWindow();

	myWindow = new windowObject(WARNING_WINDOW);

	myWindow.setHeader("/shared/images/cme/warning_large.gif", "Opgelet!");
	myWindow.setBody(msg.replace(/\n/g, "<br/>"));

	
	myWindow.x = window.document.body.clientWidth/2 - 160;
	myWindow.y = window.document.body.clientHeight/2 - 100;
	
	myWindow.addButton("Ja", "waitWindow('" + url + "');");
	myWindow.addButton("Nee", "cancelWindow()");
	
	return myWindow.showModal();
}




/* 
	een warning window met custom message en 2 buttons
	warning windows in het midden van je browser
*/
function alertWindow(msg, url){
	if(myWindow) cancelWindow();

	myWindow = new windowObject(ALERT_WINDOW);

	myWindow.setHeader("/shared/images/cme/alert_large.gif", "Opgelet!");
	myWindow.setBody(msg.replace(/\n/g, "<br/>"));

	
	myWindow.x = window.document.body.clientWidth/2 - 160;
	myWindow.y = window.document.body.clientHeight/2 - 100;
	
	myWindow.addButton("OK", "cancelWindow()");
	
	return myWindow.showModal();
}




/* een print window */
function printWindow(event, qUrl, sUrl){
	if(myWindow) cancelWindow(myWindow);
	if(!event) event = window.event;

	myWindow = new windowObject(PRINT_WINDOW);
	

	myWindow.setHeader("/shared/images/cme/printer_large.gif", "Printen");
	//myWindow.setBody("<br/>");
	
	if (sUrl) myWindow.addLink(sUrl, "/shared/images/cme/not_studytext_small.gif", "alleen studietekst");
	if (qUrl) myWindow.addLink(qUrl, "/shared/images/cme/question_small.gif", "teksten en vragen");

	myWindow.x = ((event.x)?event.x:event.pageX) - 150;
	myWindow.y = ((event.y)?event.y:event.pageY) - 50;
	
	myWindow.addButton("Annuleren", "cancelWindow()");
	
	return myWindow.showModal();
}

/* een wait window, werkt met warning window */
function waitWindow(url){

	if(myWindow) cancelWindow(myWindow);
	myWindow = new windowObject(WAIT_WINDOW);

	myWindow.setHeader("/shared/images/jqueryUiImages/lightbox-ico-loading-large.gif", "\n Even wachten A.U.B...");
	myWindow.x = window.document.body.clientWidth/2 - 80;
	myWindow.y = window.document.body.clientHeight/2 - 50;
	
	myWindow.showModal();
	
	if(url != null) document.location = url;
	return true;
}

/* 
	informatie windowtje
*/
function infoWindow(msg, title){
	if(myWindow) cancelWindow();

	myWindow = new windowObject(INFO_WINDOW);

	myWindow.setHeader("/shared/images/cme/course_large.gif", title);
	myWindow.setBody(msg.replace(/\n/g, "<br />"));

	var container = document.getElementById("contentElement");

	var coff = getObjectOffset(container);												/* offset van content element */

	myWindow.x = coff[0];
	myWindow.y = coff[1];
	
	myWindow.addButton("OK", "cancelWindow()");

	return myWindow.showModal();
	
}

/*
	haalt een bepaalde runtime style property op
*/
function getStyleInfo(obj, ieprop, ffprop){
	ffprop = ffprop == null ? ieprop : ffprop;
	if (obj.currentStyle) {
  	return eval("obj.currentStyle." + ieprop);
  } 
  else if (window.getComputedStyle) {
   	return document.defaultView.getComputedStyle(obj, "").getPropertyValue(ffprop);
  }
  return null;
}

/*
	vinden van de offset van een object
*/
function getObjectOffset(obj){

	var x = 0;
	var y = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			x += obj.offsetLeft
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x && obj.y){
		x += obj.x;
		y += obj.y;
	}
	
	return new Array(x,y);
}



/*
	splitst een size property in waarde en eenheid
*/
function splitSize(prop){
	var split = prop.match(/^(\d+(?:\.\d+)*)(\w+)$/);
	if(split){
		return new Array(split[1], split[2]);
	}
	return new Array(0, "px");
}


/*
	maakt/initialiseert een nieuw window object
	default de header, body, buttons en links
*/
windowObject = function(className){
	this.className = className;
	
	this._header = document.createElement("DIV");
	this._header.className = "header";
	
	this._body = document.createElement("DIV");
	this._body.className = "body";
	
	this._buttons = document.createElement("DIV");
	/* this._buttons.style.textAlign = "right"; */
	this._buttons.appendChild(document.createElement("BR"));
	
	this._links = document.createElement("DIV");
	/* this._links.style.textAlign = "left"; */
	
}

/*
	eventuele msgs
*/
windowObject.prototype.setBody = function(string){
	this._body.innerHTML = string;
	this._body.appendChild(document.createElement("BR"));
}


/*
	zet een header
*/
windowObject.prototype.setHeader = function(iconUrl, string){

	var icon = document.createElement("IMG");
	icon.className = "icon";
	icon.src = iconUrl;
	
	var font = document.createElement("FONT");
	font.className = "header";
	//font.appendChild(document.createTextNode("   " + string));
	font.innerHTML = string;
	
	this._header.appendChild(icon);
	this._header.appendChild(font);
	this._header.appendChild(document.createElement("BR"));



}


/*
	maakt een button voor het window, kan pas als this._buttons bestaat..
*/
windowObject.prototype.addButton = function(sValue, sOnClick){
	var btn = document.createElement("BUTTON");
	btn.className = this.className;
	btn.innerHTML = sValue;
	btn.onclick = new Function(sOnClick);
	this._buttons.appendChild(btn);
}

/*
	voegt een link toe
*/
windowObject.prototype.addLink = function(url, icon, str){
	
	
	
	var a = document.createElement("A");
	a.className = "small";
	a.target = "_blank";
	a.href = url;	
	a.onclick = new Function("cancelWindow(); return true;");
	
	var img = document.createElement("IMG");
	img.className = "printIcon";
	img.src = icon;
	img.vspace = "10px";
	
	a.appendChild(img);
	a.innerHTML += "&nbsp;&nbsp;" + str + "<br/>";
	
	this._links.appendChild(document.createElement("NOBR").appendChild(a));
}	


/*
	toont dit window
*/
windowObject.prototype.showModal = function(){
	/*
		eerst alle selectboxen hiden
	*/
	var coll = document.getElementsByTagName("SELECT");
	for(var i=0;i<coll.length;i++){
		if(coll.item(i).style){
			coll.item(i).setAttribute("oldDisplay", coll.item(i).style.display);
		}
		coll.item(i).style.display = "none";
	}
	if(this.rebuild()){
		if(this._shadow) document.body.appendChild(this._shadow);
		document.body.appendChild(this._window);
		this.moveTo(this.x, this.y);		
	}
	return false;
}

/*
	verplaatst het window naar de opgegeven coordinaten
	hou meteen rekening met eventuele scrolling offsets
*/
windowObject.prototype.moveTo = function(x,y){
	
	this.getScrollOffset();

	this.x = ((x)?x:this.x) + this._bodyScrollLeft;
	this.y = ((y)?y:this.y) + this._bodyScrollTop;
	
	/*	
	this._window.style.left=this.x + 'px';
	this._window.style.top=this.y + 'px';
	*/
	
	/* shadow , x en y herbruiken omdat mozilla andere maten heeft...*/
	try {
		x = 0;
		y = 0;
		if(navigator.appVersion.indexOf("MSIE")<0){
			x = 10;
			y = 10;
		}			
		/*
		this._shadow.style.height = (this._window.offsetHeight - y) + 'px';
		this._shadow.style.width = (this._window.offsetWidth - x) + 'px';
		this._shadow.style.left = (this.x + 3) + 'px';
		this._shadow.style.top = (this.y + 3) + 'px';
		*/
	}
	catch(e){
		alert(e.message);
	}
}

/*
	bouwt de html van het window, .header is opgegeven in de functies
	.body ook
*/
windowObject.prototype.rebuild = function(){
	try {
		var shadow = document.createElement("DIV");		
		shadow.className = this.className + " transparent";
		/*
		shadow.style.border = "none";
		shadow.style.backgroundColor = "#999999";
		*/
		
		this._shadow = shadow;

		var div = document.createElement("DIV");
		div.className = this.className;
		
		
		if (this._header) div.appendChild(this._header);
		if (this._body) div.appendChild(this._body);
		if (this._links.hasChildNodes()) div.appendChild(this._links);
		if (this._buttons.hasChildNodes()) div.appendChild(this._buttons);

		this._window = div;
		
		return true;
	}
	catch(e){
		alert(e.message);
		return false;
	}
}


/*
	nodig, omdat na een insert in de body de events gereset zijn!
*/
windowObject.prototype.setScrollOffset = function(x,y){
	if(!x) x = this._bodyScrollLeft;
	if(!y) y = this._bodyScrollTop;
	
	if(self.pageXOffset){
		self.pageXOffset = x;
		self.pageYOffset = y;
	} else if(document.documentElement && document.documentElement.scrollTop){
		document.documentElement.scrollLeft = x;
		document.documentElement.scrollTop = y;
	} else {
		document.body.scrollLeft = x;
		document.body.scrollTop = y;
	}		
}

windowObject.prototype.getScrollOffset = function (){
	
	if (self.pageYOffset){	//-- alles wat geen ie is
		x = self.pageXOffset;
		y = self.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop) { //-- ie strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body){	//-- alles wat ie is
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	this._bodyScrollLeft = x;
	this._bodyScrollTop = y;

	return new Array(x,y);
}

