/*

	Lost Boys 2005.
	
	De inhoud van dit bestand is in opdracht vervaardigd en eigendom van onze opdrachtgever.
	Niet hergebruiken zonder toestemming.
	Neem voor vragen contact op met Lost Boys, www.lostboys.nl.

	The contents of this file have been produced for and are the property of our client.
	Do not reuse without permission.
	Any questions? Please contact Lost Boys, www.lostboys.nl.

*/
Page=function(){
	if(typeof DropdownNav=="function") this.dropdownNav=new DropdownNav("primary");
	if(typeof Submits=="function") this.customSubmits=new Submits();
	if(typeof SpecialLinks=="function") this.specialLinks=new SpecialLinks();
	if(typeof PrefilledInputs=="function") this.prefilledInputs=new PrefilledInputs();
	if(typeof SelectItems=="function") this.selectItems=new SelectItems();
	if(typeof TabbedItems=="function") this.tabbedItems=new TabbedItems();
	
	if(typeof Calendars=="function") this.calendars=new Calendars();
	if(typeof Popupcart=="function")this.popupcart=new Popupcart();	
	
	if(typeof SkipLinks=="function") this.skipLinks=new SkipLinks("skip");
}

Page.prototype.destroy=function(){
	if(this.dropdownNav) this.dropdownNav.destroy();
	if(this.customSubmits) this.customSubmits.destroy();
	if(this.specialLinks) this.specialLinks.destroy();	
	if(this.selectItems) this.selectItems.destroy();
	if(this.tabbedItems) this.tabbedItems.destroy();
	
	if(this.calendars) this.calendars.destroy();
	if(this.popupcart) this.popupcart.destroy();
	
	if(this.skipLinks) this.skipLinks.destroy();
}

// custom submit buttons

Submits=function() {
	this.objects=new Array;
	var elements=document.getElementsByTagName("input");
	if(elements.length==0) return;
	for(var i=elements.length-1;i>=0;i--) {
		if(elements[i].type=="submit" && elements[i].className.match("custom")) {
			var self=this;
			this.objects[this.objects.length]=elements[i];
			if(!elements[i].disabled) {
				var button=document.createElement("a");
				button.relatedInput = elements[i];
				button.onclick=function(){
					//return self.submit(this);
					this.relatedInput.click();
					return false;
				}
				button.href="#";
			} else {
				var button=document.createElement("span"); 
			}
			button.innerHTML="<span>"+elements[i].value+"</span>";
			button.className=elements[i].className+" button";
			//elements[i].parentNode.replaceChild(button,elements[i]);
			elements[i].parentNode.insertBefore(button,elements[i].nextSibling);
		}
	}
	addClass(document.getElementsByTagName("body")[0],"customsubmits-ok");
}

Submits.prototype.submit=function(obj){alert(3);
	var element=obj;
	for(var i=0;i<99;i++){
		if(element.nodeName=="FORM" || element.nodeName=="BODY") break;
		element=element.parentNode;
	}
	if(element.nodeName=="FORM") {
		if(element.submitCatcher) 
			element.submitCatcher() 
		else 
			alert(element.onsubmit);
	}
	return false;
}

Submits.prototype.destroy=function(){
	if(this.objects.length==0) return;
	for(var i=0;i<this.objects.length;i++) this.objects[i].onclick=null;
}

// special links

SpecialLinks=function() {
	var elements=document.getElementsByTagName("a");
	if(elements.length==0) return;
	this.links=new Array;
	for(var i=0;i<elements.length;i++) {
		if(elements[i].rel.match("external")) {
			this.links[this.links.length]=elements[i];
			elements[i].onclick=function(){return !(window.open(this.href));}
			elements[i].title+=" (Nieuw venster)";
		}
		if(elements[i].rel.match("help")) {
			this.links[this.links.length]=elements[i];
			elements[i].onclick=function(){
				return !(window.open(this.href,"","width=600,height=450,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,status=yes,toolbar=yes"));
			}
			elements[i].title+=" (Nieuw venster)";
		}
	}
}

SpecialLinks.prototype.destroy=function(){
	for(var i=0;i<this.links.length;i++){
		this.links[i].onclick=null;
	}
}

// prefilled form fields

PrefilledInputs=function(){
	var elements=document.getElementsByTagName("input");
	for(var i=0;i<elements.length;i++){
		if(elements[i].type=="text" && elements[i].className.match("prefilled")) this.prefill(elements[i]);
	}
}

PrefilledInputs.prototype.prefill=function(obj){
	var date=new Date;
	var id=obj.id;
	if(id.match("day")) obj.value=digits(date.getDate(),2);
	if(id.match("month")) obj.value=digits(date.getMonth()+1,2);
	if(id.match("year")) obj.value=date.getFullYear();	
	if(id.match("hours")) obj.value=digits(date.getHours(),2);
	if(id.match("minutes")) obj.value=digits(date.getMinutes(),2);
	if(id.match("date")) obj.value=digits(date.getDate(),2)+"-"+digits(date.getMonth()+1,2)+"-"+date.getFullYear();
}

// dropdown navigation

DropdownNav=function(id){
	this.DOMObject=document.getElementById(id);
	if(document.all){
		this.iframe=document.createElement("iframe");
		this.iframe.id = "dropdown";
		this.iframe.frameborder="0";
		this.iframe.width=this.iframe.height="100";
		this.DOMObject.appendChild(this.iframe);
	}
	if(!this.DOMObject || this.DOMObject.getElementsByTagName("ul").length==0) return;	
	var children=this.DOMObject.childNodes;
	for(var i=0;i<children.length;i++){
		if(children[i].nodeName!="LI" || children[i].className.match("single")) continue;
		children[i].ref=this;
		// open
		children[i].onmouseover=
		children[i].onfocus=
		children[i].onfocusin=function(e){
			this.ref.show(this);
		}
		// close
		children[i].onmouseout=
		children[i].onblur=
		children[i].onfocusout=function(e){
			this.ref.hide(this);
		}
	}
}

DropdownNav.prototype.destroy=function(){
	if(!this.DOMObject || this.DOMObject.getElementsByTagName("ul").length==0) return;
	var children=this.DOMObject.childNodes;
	for(var i=0;i<children.length;i++){
		if(children[i].nodeName!="LI") continue;
		children[i].onmouseover=children[i].onfocus=children[i].onfocusin=null;
		children[i].onmouseout=children[i].onblur=children[i].onfocusout=null;
		children[i].ref=null;
	}
}

DropdownNav.prototype.show=function(obj){
	if(obj.className==""){
		obj.className="show";
		if(document.all) {
			var submenu = obj.getElementsByTagName("ul")[0];
			this.iframe.className = "show";
			this.iframe.style.height = submenu.offsetHeight+"px";
			this.iframe.style.left = calculateLeft(submenu) + "px";
		}
	}
}

DropdownNav.prototype.hide=function(obj){
	if(obj.className=="show"){
		obj.className="";
		if(document.all) this.iframe.className = "";
	}
}

// XML reader

var XML_LOADING       = 1;
var XML_LOADED        = 2;
var XML_INTERACTIVE   = 3;
var XML_COMPLETE      = 4;
function XMLReader() {
	this.xmlhttp = null;

	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	}

	if (this.xmlhttp == null) {
		return null;
	}
	this.handlers = new Array(5);
}
XMLReader.prototype.load = function (URL, async, asText) {
	this.asText = (asText) ? true : false;
	this.xmlhttp.open("GET", URL, async);
	var self = this;
	if (async) {
		this.xmlhttp.onreadystatechange = function() {
			self.callHandler(self);
		};
		this.xmlhttp.send(null);
	} else {
		this.xmlhttp.send(null);
		if (asText) {
			return this.xmlhttp.responseText;
		} else {
			return this.xmlhttp.responseXML;
		}
	}
}
XMLReader.prototype.callHandler = function (self) {
	if (self.handlers[self.xmlhttp.readyState]) {
		if (self.xmlhttp.readyState == XML_COMPLETE) {
			if (self.asText)
				self.handlers[self.xmlhttp.readyState](self.xmlhttp.responseText);
			else
				self.handlers[self.xmlhttp.readyState](self.xmlhttp.responseXML);
		} else {
			self.handlers[self.xmlhttp.readyState]();
		}
	}
}
XMLReader.prototype.setHandler = function (state, pointer) {
	this.handlers[state] = pointer;
}
XMLReader.prototype.clearHandlers = function () {
	for (var i=0; i < this.handlers.length; i++) this.handlers[i] = null;
}
// tabbed items

// all tabbed sections
TabbedItems=function(){
	this.sections=new Array;
	var sections=getElementsByClassName("tabbed-section",document);
	if(sections.length==0) return;
	for(var i=0;i<sections.length;i++){
		this.sections[this.sections.length]=new TabbedSection(sections[i],"ts"+i);
	}
	addClass(document.getElementsByTagName("body")[0],"tabbeditems-ok");
}

TabbedItems.prototype.destroy=function(){
	for(var i=0;i<this.sections.length;i++){
		for(var j=0;j<this.sections[i].tabs.childNodes;j++){
			var u=this.sections[i].tabs.childNodes[j];
			u.number=u.onclick=null;
		}
	}
}

TabbedSection=function(obj,id){
	// gather children and construct the menu
	this.items=getElementsByClassName("tabbed-item",obj);
	this.headers=getElementsByClassName("tabbed-header",obj);
	// create tabbed menu
	this.tabs=document.createElement("ul");
	this.tabs.className="tabs";
	for(var i=0;i<this.headers.length;i++){
		var tab=document.createElement("li");
		var self=this;
		tab.number=i;
		if(this.headers[i].title) var label=this.headers[i].title
		else var label=this.headers[i].innerHTML;
		tab.innerHTML="<a onclick='this.blur()' href=\"#\">"+label+"</a>";
		tab.id=id+"-t"+i;
		tab.onclick=function(){ 
			self.resetAll(this.number);
			return false;
		}
		this.tabs.appendChild(tab);
		if (this.items[i].className.match("current")) {
			tab.className = "current";
			this.currentIsSet = true;
		}
	}
	this.tabs.childNodes[0].className="first";
	obj.insertBefore(this.tabs,this.items[0]);
	// set defaults
	if(!this.currentIsSet) this.resetAll(0);
}

TabbedSection.prototype.resetAll=function(exception){
	for(var i=0;i<this.items.length;i++){
		if(i!=exception){
			removeClass(this.items[i],"current");
			removeClass(this.tabs.childNodes[i],"current");
		} else {
			addClass(this.items[i],"current");
			addClass(this.tabs.childNodes[i],"current");
		}
	}
	tickle();
}

// select items

SelectItems=function(){
	this.sections=new Array;
	var sections=getElementsByClassName("select-section",document);
	if(sections.length==0) return;
	for(var i=0;i<sections.length;i++){
		this.sections[this.sections.length]=new SelectSection(sections[i],"ss"+i);
	}
	addClass(document.getElementsByTagName("body")[0],"selectitems-ok");
}

SelectItems.prototype.destroy=function(){
	for(var i=0;i<this.sections.length;i++){
		this.sections[i].select.onchange=null;
	}
}

SelectSection=function(obj,id){
	// gather children and construct the menu
	var list=obj.getElementsByTagName("dl")[0];
	this.select=document.createElement("select");
	this.select.id=id;
	var self=this;
	this.anchors=new Array;
	this.items=obj.getElementsByTagName("dd");
	this.headers=obj.getElementsByTagName("dt");
	for(var i=0;i<this.headers.length;i++){
		var option=document.createElement("option");
		var anchor=this.headers[i].getElementsByTagName("a")[0];
		this.anchors[this.anchors.length]=anchor;
		option.innerHTML=anchor.innerHTML;
		// make it an option
		this.select.appendChild(option)
	}
	// remove all headers
	for(var i=this.headers.length-1;i>=0;i--){
		list.removeChild(this.headers[i]);
	}
	// insert select
	obj.insertBefore(this.select,list);
	this.select.onchange=function(){
		self.resetAll(this.selectedIndex);
	}
	// create and insert associated label
	var label=document.createElement("label");
	label.innerHTML=obj.getElementsByTagName("h3")[0].innerHTML;
	label.htmlFor=this.select.id;
	obj.replaceChild(label,obj.getElementsByTagName("h3")[0]);
	// create action button and insert before tail
	this.button=document.createElement("a");
	this.button.href=this.anchors[0].href;
	this.button.title="Lees meer over "+this.anchors[0].innerHTML;
	this.button.className="button";
	this.button.innerHTML="<span>Lees meer</span>";
	obj.insertBefore(this.button,getElementsByClassName("tail",obj)[0]);
	// default settings
	this.select.childNodes[0].selected="selected";
	this.resetAll(0);
}

SelectSection.prototype.resetAll=function(exception){
	// set href, title of button
	this.button.href=this.anchors[exception].href;
	this.button.title="Lees meer over "+this.anchors[exception].innerHTML;
	// if any items exist, deal with the current class
	if(this.items.length==0) return;
	for(var i=0;i<this.items.length;i++){
		if(i!=exception) removeClass(this.items[i],"current");
		else addClass(this.items[i],"current");
	}
}

// support functions
hasSupport=function(){
	// block MSIE5.0x/Win, MSIE5.x/Mac
	ua=navigator.userAgent;
	if((ua.indexOf("MSIE 5.0")!=-1 && ua.indexOf("Windows")!=-1) || (ua.indexOf("MSIE 5.2")!=-1 && ua.indexOf("Mac")!=-1) || !document.getElementsByTagName || !document.getElementById) 
		return false; 
	else 
		return true;
}

hasCssSupport=function(){
	if (!document.styleSheets[0].disabled) 
		return true;
	else 
		return false;
}

addClass=function(obj,cName) { 
	removeClass(obj,cName); 
	return obj.className+=(obj.className.length>0?' ':'')+cName; 
}

removeClass=function(obj,cName) {
	return obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); 
}

cancelBubble=function(e){
	if(window.event){
		window.event.cancelBubble=true;
		window.event.returnValue=true;
	}
	if(e && e.stopPropagation && e.preventDefault){
		e.stopPropagation();
		e.preventDefault();
	}
}

getSourceElement=function(e){
	if(e && e.target) return e.target;
	if(window.event && window.event.srcElement) return window.event.srcElement;
}

getElementsByClassName=function(cName,baseElement){
	var results=new Array;
	var objs=document.getElementsByTagName("*").length>0 ? baseElement.getElementsByTagName("*") : baseElement.all;
	if(!objs) objs=baseElement.all;
	for(var i=0;i<objs.length;i++){
		if(objs[i].className.match(cName)) results[results.length]=objs[i]
	}
	return results;
}

removeChildren=function(obj){
	for(var i=obj.childNodes.length-1;i>=0;i--) obj.removeChild(obj.childNodes[i]);
}

var w;
log=function(m){
	if(!w) w=window.open();
	w.document.write(m+'<br />');
}

props=function(m){
	var r=m.nodeName+":\n";
	for(i in m) r=r+i+" / ";
	return r;
}

digits=function(val,amount){
	var pre;
	for(var i=0;i<amount;i++) pre+="0";
	val=pre+val;
	return val.substring(val.length-amount,val.length);
}

calculateLeft=function(object) {
	if (object) return object.offsetLeft + calculateLeft(object.offsetParent); 
	else return 0;
}

calculateTop=function(object) {
	if (object) return object.offsetTop + calculateTop(object.offsetParent); 
	else return 0;
}
Object.prototype.method=function(method) {
	var context=this;
	return function(){
		method.apply(context,arguments);
	}
}
dateToString=function(date){
	var day="0"+date.getDate();
	var month="0"+(date.getMonth()+1);
	var year=date.getFullYear();
	var result=digits(day,2)+"-"+digits(month,2)+"-"+year;
	return result;
}

stringToDate=function(string){
	var u=string.split("-");
	var result=new Date(u[2],u[1]-1,u[0]);
	return result;
}

validDate=function(string){
	var date=stringToDate(string);
	return (
		date=="Invalid Date" || 
		date=="NaN"
	) ? false : date;
}

tickle=function(){
	var u=document.getElementsByTagName("body")[0];
	addClass(u,"tickle");
	//removeClass(u,"tickle");
}

// skip links

SkipLinks=function(obj){
	this.DOMObject=document.getElementById(obj);
	this.anchors=this.DOMObject.getElementsByTagName("a");
	if(this.anchors.length==0) return;
	var self=this;
	for(var i=0;i<this.anchors.length;i++){
		this.anchors[i].onfocus=function(){ addClass(this,"show"); }
		this.anchors[i].onblur=function(){ removeClass(this,"show"); }
		this.anchors[i].onclick=function(){
			return self.doFocus(this.href.split("#")[1]);
		}
	}
}

SkipLinks.prototype.doFocus=function(obj){
	var element=document.getElementById(obj);
	var target=document.createElement("a");
	target.className="anchorStop";
	target.href="#"; target.name=obj;
	element.insertBefore(target,element.childNodes[0]);
	target.focus();
	target.onblur=function(){var self=this;setTimeout(function(){self.parentNode.removeChild(self)},50);}
	return false;
}

SkipLinks.prototype.destroy=function(){
	if(this.anchors.length==0) return;
	for(var i=0;i<this.anchors.length;i++){
		this.anchors[i].onfocus=this.anchors[i].onblur=this.anchors[i].onclick=null;
	}
}




