var MenuObject=Array();

function getMenuObjFromId(menuId){
	return MenuObject[menuId];
}

//var menudata=[["Home",[["sub1"],["sub2"],["sub3"]]],["Wie zijn we",[["sub1"],["sub2",[["sub1"],["sub2"],["sub3"]]],["sub3"]]],["Wat doen wij",[["sub1"],["sub2"],["sub3"]]],["Wie bent u",[["sub1"],["sub2"],["sub3"]]]];
//var menudata=[[["home","http://www.diecyde.com"]],[["Wie zijn we","http://www.diecyde.com"],[[["home","http://www.diecyde.com"]],[["Wie zijn we","http://www.gekkehenkie.com"]],[["Wat doen we","http://www.diecyde.com"]]]],[["Wat doen we","http://www.diecyde.com"]],[["Wie bent u","http://www.diecyde.com"]]];

function menuSkin(begin,end,menuItem,itemClassSelect){
	this.begin=begin;
	this.End=end;
	this.menuItem=menuItem;
	this.itemClassSelect=itemClassSelect;
}

function menuItem(part1,part2){
	this.itemp1=part1;
	this.itemp2=part2;
	this.placecontent=0;
	if(part2)this.placecontent=1;	
}


function systeminit(){
	var menu;
	BrowserDetect.init();
	menu= new DropMenu("menucnt");
	menu.loadData(menudata);
	menu.newMenuSkinnlayer("<table id='menucnt' class='subhead'><tbody><tr>","</tr></tbody><table>",Array(new menuItem("<TR><TD nowrap;>","</TD></TR>")));
	menu.newMenuSkinnlayer("<table class='dhtmlsub'><tbody>","</tbody></table>",Array(new menuItem("<TR><TD class='item'>","</TD></TR>")),"item");
	menu.buildmenu();
}


//,new menuItem("<td class='spacer'></td>")
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function DropMenu(linkName){
	this.parentName=linkName;
	this.parentObject=document.getElementById(linkName);
	this.stylesheetTable;
	this.MenuSkinn=Array();
	this.LayerHolder=Array();
	this.menuMstrId=MenuObject.length;
	this.menuData=Array();
	this.menuSubData=Array();
	this.timeoutTimer;
	MenuObject.push(this);
	
	this.newMenuSkinnlayer=newMenuSkinnlayer;
	this.createNewTdItem=createNewTdItem;
	this.buildmenu=buildmenu;
	this.installevents=installevents;
	this.loadData=loadData;
	this.mnumsedwn=mnumsedwn;
	this.mnumseovr=mnumseovr;
	this.mnumseout=mnumseout;
	this.mnumseclk=mnumseclk;
	this.buildSubMenu=buildSubMenu;
	this.destroyWindow=destroyWindow;
	this.getSkinObj=getSkinObj;
	this.checkDestroyTree=checkDestroyTree;
		
}

function destroyWindow(level){
	for(var x=this.LayerHolder.length-1;x>=level;x--){
		document.body.removeChild(this.LayerHolder.pop());		
	}
}

function loadData(arrayval){
	this.menuData=arrayval;
}

function mnumsedwn(E,depth,itemid){
 if (!E) var E = window.event;  
   
}

function mnumseovr(E,depth,itemid){
 if (!E) var E = window.event;
 this.buildSubMenu(depth,itemid);
 this.timeoutTimer=0; 
   
}

function mnumseout(E,depth,itemid){
 if (!E) var E = window.event;

  this.timeoutTimer++;
  this.checkDestroyTree();
   
}

function checkDestroyTree(){
	if(this.timeoutTimer>0){
		if(this.timeoutTimer>5){
		 this.destroyWindow(0);
		}
		this.timeoutTimer++;
		setTimeout("getMenuObjFromId("+this.menuMstrId+").checkDestroyTree()",500);
	}  
}

function mnumseclk(E,depth,itemid){
 if (!E) var E = window.event;  
 	if(this.menuSubData[depth][itemid][0][1])
 	document.location=this.menuSubData[depth][itemid][0][1];
 	this.destroyWindow(0);
   //alert(this.menuSubData[depth][itemid][0][1]);
}

function newMenuSkinnlayer(begin,end,menuItem,selectclass){
	this.MenuSkinn.push(new menuSkin(begin,end,menuItem,selectclass));
}

function createNewTdItem(part1,part2){
	return new menuItem(part1,part2);
}

function getSkinObj(depth){
	var skinid=depth;
	if(this.MenuSkinn.length<=depth)skinid=this.MenuSkinn.length-1;
	return 	this.MenuSkinn[skinid];
}

function buildSubMenu(depth,itemid){
	this.destroyWindow(depth);
	//alert(this.menuSubData[depth][itemid-1]);
	if(this.menuSubData[depth][itemid][1]){
		var subsub=this.menuSubData[depth][itemid][1];
		var subdepth=depth+1;
		var tempwndw = document.createElement('div');
		tempwndw.className="window_layer";
		
		var myskinn=this.getSkinObj(depth+1);		
		
		//var buildwdata=""		
		var buildwdata=myskinn.begin;
		for(var x=0;x<subsub.length;x++){
			for(var y=0;y<myskinn.menuItem.length;y++){
				//MenuSkinn
				buildwdata+=myskinn.menuItem[y].itemp1;
				if(myskinn.menuItem[y].placecontent){
					buildwdata+=subsub[x][0][0]+myskinn.menuItem[y].itemp2;
				}
			}
		}
		buildwdata+=myskinn.End;
		//alert(buildwdata);
		tempwndw.innerHTML=buildwdata;
		this.menuSubData[subdepth]=subsub;
		
		//positie zetten

				if(depth==0){
					var searchlist=this.parentObject.getElementsByTagName("td");
				  for(var x=0;x<searchlist.length;x++){
				  	if(getAttributeValue(searchlist[x],"item")==itemid){
					      tempwndw.style.left=findPosX(searchlist[x])+searchlist[x].offsetWidth+3;
					      tempwndw.style.top=findPosY(searchlist[x]);//+searchlist[x].offsetHeight+10;
					      break;
					  }
					}
				}else{
					var searchlist=this.LayerHolder[depth-1].getElementsByTagName("td");
				  for(var x=0;x<searchlist.length;x++){
				  	if(getAttributeValue(searchlist[x],"item")==itemid){
					      tempwndw.style.left=findPosX(searchlist[x])+searchlist[x].offsetWidth;
					      tempwndw.style.top=findPosY(searchlist[x]);
					      break;
		      	}
					}
				}
				

		this.installevents(tempwndw,depth+1,myskinn.itemClassSelect);	
		//alert(tempwndw.innerHTML);
		//tempwndw.innerHTML=myskinn.begin+tempwndw.innerHTML+myskinn.End;	
		document.body.appendChild(tempwndw);
		this.LayerHolder[depth]=tempwndw;
	}	
}

function getAttributeValue(Objectname,attributeName){
	for(y=0;y<Objectname.attributes.length;y++){
	   if(Objectname.attributes[y].nodeName==attributeName){
	      return Objectname.attributes[y].value;
	   }
	}
	return -1;
}

function buildmenu(){
	
	var dumpstr=this.MenuSkinn[0].begin;
	for(var x=0;x<this.menuData.length;x++){
		for(var y=0;y<this.MenuSkinn[0].menuItem.length;y++){
			//MenuSkinn
			dumpstr+=this.MenuSkinn[0].menuItem[y].itemp1;
			if(this.MenuSkinn[0].menuItem[y].placecontent){
				dumpstr+=this.menuData[x][0][0]+this.MenuSkinn[0].menuItem[y].itemp2;
			}
		}
	}
	var dumpwindow=this.parentObject;
	dumpstr+=this.MenuSkinn[0].End;

	var newdiv = document.createElement("div");
	newdiv.innerHTML = dumpstr;

	dumpwindow.parentNode.replaceChild(newdiv.firstChild,dumpwindow);
	dumpwindow=document.getElementById(this.parentName);
    this.parentObject=dumpwindow;

	this.menuSubData[0]=this.menuData;
	this.installevents(dumpwindow,0);
}

function installevents(installobj,depth){
	var menuCells = null;
	var idvar=0;
	if (typeof(installobj.style) == 'undefined') { return false;}
	if (typeof(document.getElementsByTagName) != 'undefined') { menuCells = installobj.getElementsByTagName('td'); }
	else if (typeof(this.parentObject.cells) != 'undefined') { menuCells = installobj.cells; }
	else { return false; }
		/*
	var totcell=0;
	for(var c=0;c<menuCells.length;c++){
		if(menuCells[c].header == 'undefined')totcell++;
	}
	alert(totcell);
			*/
	for(var c=0;c<menuCells.length;c++){
      idvar=Math.floor(c/this.getSkinObj(depth).menuItem.length);
      		menuCells[c].setAttribute('menu',this.menuMstrId);
		      menuCells[c].setAttribute('depth',depth);
		      menuCells[c].setAttribute('item',idvar);
		   if(BrowserDetect.browser=="Explorer"){idvar

	      	menuCells[c].onmousedown=function(){getMenuObjFromId(this.menu).mnumsedwn(event,this.depth,this.item);};
	      	menuCells[c].onmouseover=function(){getMenuObjFromId(this.menu).mnumseovr(event,this.depth,this.item);};
	      	menuCells[c].onmouseout=function(){getMenuObjFromId(this.menu).mnumseout(event,this.depth,this.item);};
	      	menuCells[c].onclick=function(){getMenuObjFromId(this.menu).mnumseclk(event,this.depth,this.item);};
	     }else{
		      menuCells[c].setAttribute('onmousedown',"getMenuObjFromId("+this.menuMstrId+").mnumsedwn(event,"+depth+","+idvar+");");
		      menuCells[c].setAttribute('onmouseover',"getMenuObjFromId("+this.menuMstrId+").mnumseovr(event,"+depth+","+idvar+");");
		      menuCells[c].setAttribute('onmouseout',"getMenuObjFromId("+this.menuMstrId+").mnumseout(event,"+depth+","+idvar+");");
		      menuCells[c].setAttribute('onclick',"getMenuObjFromId("+this.menuMstrId+").mnumseclk(event,"+depth+","+idvar+");");
		   }

	}
}




/*
    json.js
    2006-04-28

    This file adds these methods to JavaScript:

        object.toJSONString()

            This method produces a JSON text from an object. The
            object must not contain any cyclical references.

        array.toJSONString()

            This method produces a JSON text from an array. The
            array must not contain any cyclical references.

        string.parseJSON()

            This method parses a JSON text to produce an object or
            array. It will return false if there is an error.
*/
(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};