if (typeof popkong              == 'undefined') popkong              = function() {};
if (typeof popkong.macro        == 'undefined') popkong.macro        = function() {};
if (typeof popkong.macro.layout == 'undefined') {
	popkong.macro.layout = function() {
		var $ = AJS.$;
		
		this.extendElementsObj = function(elements) {
			elements.areEmpty = !(elements.length > 0);
			elements.wrap     = !elements.areEmpty && elements.slice(0, 1).hasClass("pop-wrap");
			elements.nowrap   = !elements.areEmpty && !elements.wrap;
			// elements.width = ...
			if (!elements.areEmpty) {
				var configuredWidth = popkong.utils.getCssParam(elements.slice(0, 1), "width");
				if (configuredWidth)
					// remove the % at the end for easier processing
					elements.width = configuredWidth.substring(0, configuredWidth.length - 1);
				else
					elements.width = 25;
			} else {
				elements.width = 0;
			}
			
			return elements;
		};
		
		var wikiContainer = $("#content .wiki-content:first");
		
		var eastElements = this.extendElementsObj($(".pop-east"));
		var westElements = this.extendElementsObj($(".pop-west"));
		
		var eastContainer = $("<div id='pop-layout-east-container'></div>");
		var westContainer = $("<div id='pop-layout-west-container'></div>");
		var mainContainer = $("<div id='pop-layout-main-container'></div>");
		
		this.determineCentralWidth = function() {
			var centralWidth = "100";
			
			if (eastElements.nowrap)
				centralWidth = centralWidth - eastElements.width - 1;
			
			if (westElements.nowrap)
				centralWidth = centralWidth - westElements.width - 1;
			
			return centralWidth + "%";
		};
		
		this.styleContainers = function() {
			mainContainer.css("width", this.determineCentralWidth());
			mainContainer.css("margin-left",  (westElements.nowrap) ? "0.5%" : "0"); // %s add up to 99 or 99.5%, due to troubles in IE
			mainContainer.css("margin-right", (eastElements.nowrap) ? "0.5%" : "0");
			if (eastElements.nowrap || westElements.nowrap) {
				// As soon as we float, we detach from parent container so height cannot be computed,
				// so first we need set the height of main.
				// Only do in edit mode (otherwise causes false calculations in other views)
				
				if (window.location && window.location.href) {
					var loc = window.location.href;
					if(loc.indexOf("editpage.action") > -1 ) {
						$('#main').css('height',mainContainer.height() + "px");
					}
				}
				
				mainContainer.css("float", "right");
			}
			
			var centralWidthDcml = mainContainer.css("width").substring(0, mainContainer.css("width").length - 1) / 100;
			
			// set east width
			if (eastElements.wrap) {
				eastContainer.css("margin-left", "15px");
				eastContainer.css("width", 1 / centralWidthDcml * eastElements.width + "%");
			} else {
				eastContainer.css("width", eastElements.width + "%");
			}
			
			// set west width
			if (westElements.wrap) {
				westContainer.css("margin-right", "15px");
				westContainer.css("width", 1 / centralWidthDcml * westElements.width + "%");
			} else {
				westContainer.css("width", westElements.width + "%");
			}
		};
		
		this.insertContainer = function(container, wrapping) {
			if (wrapping) mainContainer.prepend(container)
			else          wikiContainer.prepend(container);
		};
		
		this.insertLayoutContainers = function() {
			if (!eastElements.areEmpty) {
				eastContainer.append(eastElements);
				this.insertContainer(eastContainer, eastElements.wrap);
			}
			
			if (!westElements.areEmpty) {
				westContainer.append(westElements);
				this.insertContainer(westContainer, westElements.wrap);
			}
		};
		
		this.checkForErrors = function() {
			var tooMuchWrapConfig = "<div class=\"error\">To decrease confusion, wrapping and width may only be set for the first East and West {pop-layout}</div>";
			if (eastElements.slice(1).hasClass("pop-configured")) { 
				eastContainer.prepend(tooMuchWrapConfig);
			}
		
			if (westElements.slice(1).hasClass("pop-configured")) {
				westContainer.prepend(tooMuchWrapConfig);
			}
		};
		
		this.insertMainContainer = function() {
			mainContainer.append(wikiContainer.contents());
			wikiContainer.append(mainContainer);
		};
		
		this.doLayout = function() {
			this.insertMainContainer();
			
			this.styleContainers();
			
			this.checkForErrors();
			
			this.insertLayoutContainers();
		};
		
		this.doLayout();		
		
	}

	AJS.toInit(popkong.macro.layout);
}


