// javascripts voor wp-studie

function CreateDebugWindow() {
	oDebugWindow = new DragDiv('debugwindow', 750, 400, 200, 300, new DragDivDebug(oDragDivCloseImage));
	oDebugWindow.isVisible = false;
	oDebugWindow.layerLevel = 40;
	oDebugWindow.Create();
}

function CloseTypeAhead() {
	oTypeAheadDiv.style.display = 'none';
}


function PositionHash() {
	var oArticle = document.getElementById('article_content');
	if(!oArticle) {return;}
	if(document.location.hash) {
		var oFocus = document.getElementById(document.location.hash.substr(1));
		if(oFocus) {
			oFocus.scrollIntoView(true);
			oArticle.style.top = oArticle.style.top;
			oArticle.style.height = (iScreenY-180) + 'px';
			oArticle.style.left = '0px';
		}
	}
}

function FitTitel() {
	//titel passend maken
	var aTitels= getElementsByClassName('titel');
	if(aTitels.length > 0) {
		var oTitel = aTitels[0];
		var iTitelWidth = oTitel.offsetWidth;
		var iFontSize = 26;
		if(iTitelWidth > 410) {
			while(iTitelWidth > 410) {
				iFontSize--;
				oTitel.style.fontSize = iFontSize + 'px';
				iTitelWidth = oTitel.offsetWidth;
			}
		}
	}
}

function ScrollToTop() {
	var oArticle = document.getElementById('article_content');
	if(!oArticle) {return;}
	oArticle.scrollTop = 0;
}


// ===========================
// Zoek resultaten / typeahead
// ===========================


function PositionTypeAhead() {
	if(oZoekBox && oTypeAheadDiv) {
		oPosition.setByElement(oZoekBox);
		oTypeAheadDiv.style.position = 'absolute';
		oTypeAheadDiv.style.width = oZoekBox.offsetWidth + 72 + 'px';
		oTypeAheadDiv.style.top = oPosition.y + 50 + 'px';
		oTypeAheadDiv.style.left = oPosition.x + 'px';
		oTypeAheadDiv.style.zIndex = 50;
		oTypeAheadDiv.style.display = 'block';
		oTypeAheadContent.style.zIndex = 50;
		oTypeAheadContent.style.display = 'block';
	}
}

function CloseTypeAhead() {
	oTypeAheadDiv.style.display = 'none';
}

function TimedTypeAhead() {
	var sSearchTerm = oZoekBox.value;
	if(sSearchTerm.length == 0 ) {
		oTypeAheadContent.innerHTML = '';
		oTypeAheadDiv.style.display = 'none';
	} else {
		if(sLastSearchTerm != sSearchTerm) {
			sLastSearchTerm = '' + sSearchTerm;
			TypeAhead(sSearchTerm);
		}
	}
}

function TypeAhead(s) {
	if(!s) {return;}
	if(s.length==0) {return;}

	if(oXMLRequest.readyState > 0 && oXMLRequest.readyState < 4) {oXMLRequest.abort();}
	var url = document.location.href.split('?')[0] + '?id=-238673&contentonly=true&listsearch=' + escape(s);
	//var url = 'http://www.winklerprins.com/?id=-238673&contentonly=true&listsearch=' + escape(s);
	//prompt('zoekurl', url);
	oXMLRequest.open("GET", url, true);
	oXMLRequest.onreadystatechange = function() {
		if(oXMLRequest.readyState == 4) {
			oResultXML = LoadXMLString(oXMLRequest.responseText);
			DisplaySearchResult(oResultXML);
		}
	}
	oXMLRequest.send(null);
}


function DisplaySearchResult(oResultXML) {
	var oHTML = ProcessXMLAndXSL(oResultXML, oXSL);
	oTypeAheadContent.innerHTML = '';
	if(typeof(oHTML)=='string') {
	 	oTypeAheadContent.innerHTML = oHTML;
	} else {
	 	oTypeAheadContent.appendChild(oHTML);
	}
	PositionTypeAhead();
	oTypeAheadDiv.style.display = '';
}

function FillZoekBox(s) {
	sLastSearchTerm = s;
	oZoekBox.value = s;
	oTypeAheadDiv.style.display = 'none';
}

function CloseAtlas() {
	if(oAtlas) {oAtlas.style.display = 'none';} else {alert('no atlas');}
}

function DisplayAtlas() {
	if(oAtlas) {oAtlas.style.backgroundColor = '#FFFFFF';oAtlas.style.display = 'block';} else {alert('no atlas');}
}

function PrintArticle(){
	var oArticle = document.getElementById('article_content');
	if(oArticle) {
		var sArticle = oArticle.innerHTML;
		sArticle = sArticle .replace(/default.aspx\?keepAlive=true/g, '');
		sArticle = sArticle .replace(/ class="librioshighlight"/g, '');
		var dtNow = new Date();
		var iCurrentYear;
		if(dtNow.getFullYear()) {
			iCurrentYear = dtNow.getFullYear();
		} else {
			iCurrentYear = LongYear(dtNow.getYear());
		}
		var sPrint = '<html><head><link rel="stylesheet" href="/online/styles/zinder.css"/><style>.printnote{display:none;}.classtitlefromcontent{display:none;}div.beeld span {display:none;}div.bijschrifttekstjr{display:none;}</style></head><body onload="window.print()">' + sArticle + '<p><i>Junior Winkler Prins online. ' + '&#169; '  + iCurrentYear + ' Uitgeverij Unieboek | Het Spectrum bv</i></p></body></html>';
		var oWin = window.open('','print_content','scrollbars=1,width=800,height=600');
		oWin.document.open();
		oWin.document.write(sPrint);
		oWin.document.close();
		oWin.focus();
	}
}

function CloseWoordenboek() {
	var oWoordenboek = document.getElementById('woordenboek');
	if(oWoordenboek) {oWoordenboek.style.display = 'none';} else {alert('geen woordenboek-div');}
}

function DisplayWoordenboek() {
	var oWoordenboek = document.getElementById('woordenboek');
	if(oWoordenboek) {oWoordenboek.style.display = 'block';} else {alert('geen woordenboek-div');}
}

function getClickElement(ev) {
	if (window.event) {return window.event.srcElement;}
	if (ev.srcElement) {return ev.srcElement;}
	if (ev.target) {return ev.target;}
	return null;
};


function OpenMedia(ev) {
	var oElement = getClickElement(ev);
	if(!oElement) {return;}
	if(!oElement.tagName) {return;}
	if(oElement.tagName=='IMG' || oElement.tagName=='img') {
		if(oElement.parentNode.getAttribute('rel')=='lightbox[gallery]') {
			//do nothing or open in Flash

			//oFlash.openMedia(oElement.getAttribute('src'));
			try {
				ev.cancelBubble = true;
				ev.returnValue = false;
				try {
					ev.preventDefault();
					ev.stopPropagation();
				} catch(e) {
					//				
				}
			} catch(e) {
				ev.preventDefault();
				ev.stopPropagation();
			}
		}
	}
}

// ================ //
// lib functions    //
// ================ //

function LongYear(year) {
	if (year < 100) {return 1900 + year;}
	return year;
}


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function CreateXMLRequest() {
	//source: http://www.guru4.net/articoli/javascript-soap-client/en/
	try {
		if(typeof(XMLHttpRequest) != 'undefined') {
			var req = new XMLHttpRequest();
			if(req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load",function() {req.readyState = 4;if(typeof req.onreadystatechange == "function") req.onreadystatechange(); }, false);
	        }
	        return req;
		}
		if(window.ActiveXObject) {
			var progids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			var o;
			for(var i = 0; i < progids.length; i++) {
				try {
					o = new ActiveXObject(progids[i]);
					return o;
				} catch (e) {};
			}
			throw new Error("Could not find an installed XML parser");
		}
	} catch (e) {
		alert (e.message);
	}
	throw new Error("Your browser does not support XmlHttp objects");	
};

function CreateXMLDoc() {
	var oXML;
	try {oXML = new ActiveXObject('Msxml2.DOMDocument');}
	catch(e) {oXML = false;}
	if(!oXML) {
		try {oXML = document.implementation.createDocument("", "", null);}
		catch(e) {oXML = false;}
	}
	if(!oXML) {
		alert('could not create XML document');
	} else {
		oXML.async = false;	
	}
	return oXML;
}

function LoadXMLFile(sURL) {
	var oXML, oRequest;
	try {oXML = new ActiveXObject('Msxml2.DOMDocument');}
	catch(e) {oXML = false;}
	if (oXML) {
		//IE load XML document
		oXML.async = false;
		oXML.load(sURL);
		if(oXML.parseError.errorCode) {
			alert('error: ' + oXML.parseError.description);
			return false;
		}
	}
	if (!oXML) {
		//Mozilla load XML document
		try {oXML = document.implementation.createDocument("", "", null);}
		catch(e) {oXML = false;}
		if(oXML) {
			oXML.async = false;
			try {oXML.load(sURL);}
			catch(e) {oXML = false;}
		}
	}
	if(!oXML) {
		try {
			oRequest = new XMLHttpRequest();
			oRequest.open("GET", sURL, false);
			oRequest.send(null);
			oXML = oRequest.responseXML;
		}
		catch(e) {oXML = false;}
	}
	return oXML;
}

function LoadXMLString(s) {
	var oXML;
   	if (window.ActiveXObject) {
		// MSIE
		oXML = new ActiveXObject('Msxml2.DOMDocument');
		oXML.async = 'false';
		oXML.loadXML(s);
		return oXML;
	} else {
		if (document.implementation.createDocument) {
			var oParser = new DOMParser();
			oXML = oParser.parseFromString(s, 'text/xml');
			return oXML;
		}
	}
}

function ProcessXMLAndXSL(oXML, oXSL) {
	var sHTML = '';
	if(window.ActiveXObject) { //IE
		try {
			sHTML = oXML.transformNode(oXSL);
			return sHTML;
		} catch (e) {alert('could not transform xml and xsl');}
	}

	//Mozilla
	if (document.implementation && document.implementation.createDocument) {
		var oProcessor = new XSLTProcessor();
		try {
			oProcessor.importStylesheet(oXSL);
			sHTML = oProcessor.transformToFragment(oXML, document);
		} catch (e) {alert('could not transform xml and xsl, e = ' + e.message);}
		return sHTML;
	}

	//other browsers not handled
	return '';
}


function getFlashMovieObjectByName(movieName) {
	if (window.document[movieName]) {return window.document[movieName];}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName]) {
			return document.embeds[movieName];
		}
	} else {
		return document.getElementById(movieName);
	}
}

function addEvent(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else {
		if (el.addEventListener) { // Gecko / W3C
			el.addEventListener(evname, func, false);
		} else {
			el["on" + evname] = func;
		}
	}
}

// ================ //
// BoxPosition      //
// ================ //

var BoxPosition;

BoxPosition = function() {
	this.id = '';
	this.xCompensation = 0;
	this.yCompensation = 0;
	this.x = 0;
	this.y = 0;
	this.r = 0;
	this.b = 0;
	this.setByNumbers = function(x, y, r, b) {
		this.x = x;
		this.y = y;
		this.r = r;
		this.b = b;
	}
	this.setByElement = function(oObject) {
		var xyPosition = BoxPosition.getAbsolutePos(oObject);
		this.x = xyPosition.x;
		this.y = xyPosition.y;
		this.r = this.x + oObject.offsetWidth;
		this.b = this.y + oObject.offsetHeight;
	}
	this.isInBox = function() {
		var bReturn = false;
		var xpos = BoxPosition.MouseX;
		var ypos = BoxPosition.MouseY;
		if(xpos >=  this.x && xpos <= this.r && ypos >= this.y && ypos <= this.b) {bReturn = true;}
		return bReturn;
	}
};

BoxPosition.getAbsolutePos = function(oObject, bNoScrollCompensation) {
	var r = {x: 0, y:0}
	if(bNoScrollCompensation) {
		r.y = oObject.offsetTop;
		r.x = oObject.offsetLeft;
	} else {
		r.y = oObject.offsetTop - oObject.scrollTop;
		r.x = oObject.offsetLeft - oObject.scrollLeft;
	}
	while (oObject.offsetParent) {
		oObject = oObject.offsetParent
		if(bNoScrollCompensation) {
			r.y += oObject.offsetTop;
			r.x += oObject.offsetLeft;
		} else {
			r.y += oObject.offsetTop - oObject.scrollTop;
			r.x += oObject.offsetLeft - oObject.scrollLeft;
		}
	}
	return r;
};

BoxPosition.MouseX = 0;
BoxPosition.MouseY = 0;

BoxPosition.GetMouseXY = function(ev) {
	if(ev) {
		if(ev.clientX) {
			BoxPosition.MouseX = ev.clientX + document.body.scrollLeft;
			BoxPosition.MouseY = ev.clientY + document.body.scrollTop;
		} else {
			BoxPosition.MouseX = ev.pageX;
			BoxPosition.MouseY = ev.pageY;
		}
	} else {
		//WriteAction('BoxPosition.GetMouseXY; no event');
	}
};


// ================ //
// file loaded:     //
// ================ //

bExternalLoaded=1;
if(document.bPageLoaded) {libriosbodyload();}


