$(document).ready(function(){
	initLayout();
});

(function($)
{
	
	/* Layout: Buttons
	 * Function transforming buttons into a formated HTML/CSS button. 
	 * Bindings and actions are transfered onto the new object and the original Input is removed.
	 * 
	 * @Category		Module
	 * @Version			1.0
	 * @In core since	1.0
	 * @Returns 		HTML Object
	 * 
	 ************************************************************************************************/
	$.fn.layoutButton = function(options) {
		
		var defaults = {
			testString : "test"
		};
		var settings = $.extend({}, defaults, options);		
		
		$(this).each(function() {
			var inputButton = $(this);
			$(inputButton)
				.removeClass("transformToButton")
				.addClass("button");
			var buttonContent = {
				type : $(inputButton).attr("type"),
				value : $(inputButton).attr("value"),
				onclick : $(inputButton).attr("onclick"),
				target : $(inputButton).attr("target"),
				href : $(inputButton).attr("href"),
				id : $(inputButton).attr("id"),
				style : $(inputButton).attr("style"),
				classes : $(inputButton).attr("class")
			}
			
			var a = document.createElement('a');
			
			$(a)
				.attr({"id" : buttonContent.id, "class" : buttonContent.classes, "href" : buttonContent.href, "target" : buttonContent.target, "style" : buttonContent.style})
				.html('<span></span>'+buttonContent.value+'');
			
			if (buttonContent.onclick) {
				$(a).bind("click", function() {
					buttonContent.onclick();
				});
			};
			
			if (buttonContent.type == "submit") {
				var submitForm = $("form:has(input.transformToButton)");
				
				$(a).bind("click", function() {
					$("#"+submitForm.attr("id")).submit();
				});
			}
			
			$(inputButton)
				.after(a)
				.remove();
			
		});
		
	};
	
})(jQuery);


/* Layout: Border-Wrapper
 * Function for making sure boxes with a border gets the right width.
 * 
 * @Category		Module
 * @Version			1.0
 * @In core since	1.0
 * @Returns 		HTML Object
 * 
 ************************************************************************************************/
function fixBorderWrapper() {
	$(".box-border-wrapper").each(function() {
		var boxWrapper = $(this).closest(".box-wrapper");
		$(this).css({"width":$(boxWrapper).width()+"px"});
	});
}



/* LAYOUT
 * Initiate all layout functions transforming all supported elements.
 * 
 * @Category		Core
 * @Version			1.0
 * @Returns 		HTML Object
 * 
 ************************************************************************************************/
function initLayout() {
	if (!DISABLE_LAYOUT) {
		$("input.transformToButton").layoutButton();
		//fixBorderWrapper();
		//$(window).bind('resize', function() { fixBorderWrapper(); });
	}
}




/* Layout: Setup Ajax loader
 * Put in HTML and bind to events
 * 
 * @Category		Module
 * @Version			1.0
 * @In core since	1.0
 * @Returns 		HTML Object
 * 
 ************************************************************************************************/
(function($) {
	ajaxLoader = {
		init : function() {
			$(document.body).prepend('<div id="ajax-overlay"><div id="ajax-loader">&nbsp;</div></div>');
			$('#ajax-loader').css({opacity: "0.8"});
			$(window).resize(function() {ajaxLoader.resize();});
			this.hide();
		},
		show : function() {
			ajaxLoader.resize();
			$('#ajax-overlay').show();
			$('#ajax-loader').show();
		},
		hide : function() {
			$('#ajax-overlay').hide();
			$('#ajax-loader').hide();
		},
		resize : function() {
			var pageSize = _getPageSize();
			$('#ajax-overlay').css({width:pageSize[0],height:pageSize[1]});
		}
	}
	
	ajaxLoader.init();
})(jQuery);



function _getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

