var BoxMaker = function () {
			
	this.classNames = ["beigebox1", "beigebox2", "beigebox3"];
	this.styles = [["topleft", "middleleft", "bottomleft"], ["topmiddle", "middlemiddle", "bottommiddle"], ["topright", "middleright", "bottomright"]]

	this.init = function() {
		this.imageRotator();
		this.applyBorders(document.getElementsByTagName("div"));
		this.applyBorders(document.getElementsByTagName("table"));
		this.applyBorders(document.getElementsByTagName("a"));
		this.quickLinks();
	}
	
	this.imageRotator = function() {
		var items = document.getElementsByTagName("ul");
		for ( var i = 0; i < items.length; i++ ) {
			if ( items[i].className == "imgrot" ) {
				
				var img, rnd, listitems, div;
				
				listitems = items[i].getElementsByTagName("li");
				
				rnd = Math.floor(Math.random()*listitems.length);
				
				img = listitems[rnd].getElementsByTagName("img")[0];
				if(img) {
					div = document.createElement("div");
					div.style.textAlign = "center";
					div.appendChild(img);
					items[i].parentNode.insertBefore( div, items[i].nextSibling );
				}
				div = document.createElement("div");
				div.innerHTML = listitems[rnd].innerHTML;
				div.className = "beigebox1";
				img.parentNode.insertBefore( div, img.nextSibling );
				
			}
		}
	}
	
	this.quickLinks = function() {
		
		var items = document.getElementsByTagName("ul");
		
		for ( var i = 0; i < items.length; i++ ) {
			if ( items[i].className == "greybox4" ) {
			
				var titleli = items[i].getElementsByTagName("li")[0];
			
				if ( titleli ) {
					
					var a = document.createElement("a");
					a.id = "alist"+i
					a.innerHTML = titleli.innerHTML;
					a.href = "javascript:void(0);";
					a.parent = this;
					a.list = items[i];
					a.onmouseover = this.showList;
					a.onmouseout = this.hideList;
					
					var header = document.createElement("h2")
					header.appendChild(a);
					
					items[i].id = "dlist"+i
					items[i].onmouseover = this.showList;
					items[i].onmouseout = this.hideList;
					items[i].parent = this;
					items[i].list = items[i];
					items[i].removeChild( titleli );
					items[i].parentNode.insertBefore( header, items[i].nextSibling );
					items[i].className = "boxstyle makepopup";		
					
					var tbl = this.createBorder( header, "greybox4" );
					
					items[i].tbl = tbl

				}
			}
		}
	}
	
	this.hideList = function() {
		this.parent.timer = setTimeout("document.getElementById('" + this.list.id + "').style.display = 'none';", 333);
	}
	
	this.showList = function() {
		if (this.parent.timer) clearInterval(this.parent.timer);
		var pos = this.parent.getPos(this.list.tbl);
		this.list.style.display = "block";
		this.list.style.width = ( this.list.tbl.offsetWidth - 6 ) + "px";
		this.list.style.top = ( pos.y + this.list.tbl.getElementsByTagName("tbody")[0].offsetHeight ) + "px";
		this.list.style.left = ( pos.x ) + "px";
	}
	
	this.getPos = function( obj ){
		var x = 0;
		var y = 0;
		while( obj != null ) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
		return { x:x, y:y };
	}


	this.applyBorders = function(items) {
		for ( var i = 0; i < items.length; i++ ) {
			for ( var j = 0; j < this.classNames.length; j++ ) {
				if ( items[i].className == this.classNames[j] ) {
					this.createBorder( items[i], this.classNames[j] );
				}
			}
		}
	}

	this.createBorder = function(div, oClass) {
		var title = div.getElementsByTagName("h2")[0]

		var tbl, tbody, row, cell;
		tbl = document.createElement("table");
		tbl.className = "opt" + oClass;
		tbody = document.createElement("tbody");
		tbl.appendChild( tbody );
		
		if ( title && title.className != null) {
			if ( title.className == oClass ) tbl.className = tbl.className + " head" + oClass; 
			if ( title.className == "beigebox3" ) tbl.className = tbl.className + " headbeigebox3"; 
			title.className = "";
		}
		
		div.parentNode.insertBefore( tbl, div.nextSibling ); // Put table 
		
		for ( y = 0; y < 3; y++ ) {
			row = document.createElement("tr");
			for ( x = 0; x < 3; x++ ) {
				cell = document.createElement("td")
				cell.className = this.styles[x][y];
				if ( x == 1 && y == 0 && title ) { cell.appendChild( title ) }
				if ( x == 1 && y == 1 ) { cell.appendChild( div ) }
				row.appendChild( cell );
			}
			tbody.appendChild( row );
		}
		div.className = "";
		return tbl;
	}
	this.init();
}

Event.observe( window, "load", function() { new BoxMaker() } );
