var Tools = new Class({
	initialize: function() {
		
	},
	log: function(test) {
		return console ? console.log(test) : alert(test); 
	}
});

//<![CDATA[
var System = Tools.extend({
	initialize: function() {
		//this.canvas = $('canvas');
		//this.btnLayout = $('css-switch-button');
		this.parent();
		this.setup();
	},
	setup: function() {
		this.initRoundBox();
		this.createSidebar();
		this.setFocus("wiki-s", "s-txtbox");
		this.moveCategory();
		//this.setLayoutWidth(false);
		//this.btnLayout.addEvent('click', function(e){this.setLayoutWidth(true, e)}.bind(this));
	},
	initRoundBox: function() {
		var boxes = $$(".box");
		boxes.each(function(box) {
			var els = $$(".contents");
			els.each(function(el) {
				var aryDivs = new Array("tm", "bm", "lm", "rm", "tl", "tr", "bl", "br");				
				for (var i=0; i<aryDivs.length; i++) {
					var divName = aryDivs[i];
					aryDivs[i] = new Element('div'); 
					aryDivs[i].addClass(divName);
					aryDivs[i].injectBefore(el).adopt(el);
				}			
			});
		});
	},
	setLayoutWidth: function(blnToggle, event) {
		var strNewWidth = '';
		var cookie = Cookie.get("mrclean-width");
		if (!blnToggle) {
			strNewWidth = cookie ? cookie : 'fixed';
		} else if (cookie && cookie == 'fixed') {
			strNewWidth = 'full';
		} else {
			strNewWidth = 'fixed';
		}
	   
		this.refreshLayoutWidth(strNewWidth);
		Cookie.set("mrclean-width", strNewWidth, { path: wgCookiePath, domain: wgCookieDomain, duration: 2000 });
	
		if (typeof event != 'undefined') {
			if (event.preventDefault) {
				// Non-IE
				event.preventDefault();
			} else {
				// IE
				event.returnValue = false;
			}
		}
	},
	refreshLayoutWidth: function(width) {
		var oldWidth;
		var buttonTxt;
		if (width === 'fixed') {
			buttonTxt = "Change to Liquid Layout";
			oldWidth  = "full";
		} else {
			buttonTxt = "Change to Fixed Layout";
			oldWidth  = "fixed";
		}		
		this.canvas.addClass(width);
		this.canvas.removeClass(oldWidth);
		this.btnLayout.setText(buttonTxt);
	},
	createSidebar: function() {
		if ($chk($('sidebar'))) $('sidebar').injectBefore('main');
	},
	setFocus: function(btn, input) {
		$(btn).addEvent('mouseover', function(){
			$(input).focus();
		});	
	},
	moveCategory: function() {
		if ($chk($$('p.catlinks')[0])) {
			var cat         = $$('p.catlinks')[0];
			var legal       = $('legal');
			var replaceHTML = legal.innerHTML + cat.innerHTML;
			//this.log(replaceHTML);
			cat.remove();
			legal.setHTML(replaceHTML);
		}
	}
});

// Onload
Window.onDomReady(function() {
	new System;
});
//]]>
