/*
postForm(<form element>[,<callback function>,<clear>])

Form element: 
The originating form. Needed for disabling and
resetting the form. If the form has a button
with <form id>_submit as id, a loading effect 
will be added.

Callback function (optional): 
Function to call after completion. 
postForm() has a built in error-reporter.
Example: callback(form,success,response);

Clear (optional):
If true (standard) the form element will
be restored to its original state using
form.reset();.
*/
function postForm(form,callback,clear) {
	if(clear!=false) clear=true;
	if(!form) {
		alert("Please specify the source form element!");
		return false;
	}
	if(!callback) {
		var callback = function(form,success,response) {
			if(success) {
				alert("Failed to post your request from "+form.id+" ("+response.status+")! Sorry!");
				return false;
			}
		}
	}
	if($(form.id+"_submit")) {
		var submit = $(form.id+"_submit");
		var tmp_className = submit.className;
		var tmp_innerHTML = submit.value;
		submit.value = "Vänta...";
		submit.className = "loading";
	}
	var url;
	if($$(".mceEditor").length>0) tinyMCE.triggerSave();
	var params = form.serialize();
	form.disable(form);
	if(form.action.indexOf("?")!=-1) url = form.action+"&ajax=1";
	else url = form.action+"?ajax=1";
	new Ajax.Request(url,{
		method:"post",
		asynchronous:true,
		postBody:params,
		onSuccess:function(response) {
			callback(form,true,response);
		},
		onFailure:function(response) {
			callback(form,false,response);
		}
	});
	if(submit) {
		submit.className = tmp_className;
		submit.value = tmp_innerHTML;
	}
	if(clear) form.reset(form);
	form.enable(form);
	form.focusFirstElement(form);
	return false;
}
/*
trim(str)

Trims a string.

str: String to be trimed.
*/
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
}

/*
function(url[,stule])

Opens a modal floated div.

url: Content to be putted in the box
style: the style-parameter of the div
*/
function openFloater(url,callback) {
	if(!url) {
		alert("Please specify an URL!");
		return false;
	}
	Event.observe(document,"keyup",floater_esc = function(e) {
		if(e.keyCode==27) destroyFloater();
	});
	var params;
	if(url.indexOf("?")!=-1 && url.split("?").length==2) {
		params = url.split("?")[1];
		url = url.split("?")[0];
	}
	var modal_window,modal_wrapper;
	if($("modal_window").visible()) {
		modal_window = "modal_window2";
		modal_wrapper = "modal_wrapper2";
	} else {
		modal_window = "modal_window";
		modal_wrapper = "modal_wrapper";
	}
	new Ajax.Updater(modal_window,url,{
		method:'get',
		parameters:params,
		evalScripts:true,
		onComplete: function(t) {
			scrollarr = getScrollOffset();
			sizearr = getPageSize();
			$(modal_window).style.top = (scrollarr[1]+50)+"px";
			$(modal_window).style.left = (scrollarr[0]+((document.documentElement.clientWidth/2))-($(modal_window).getWidth()/2))+'px';
			$(modal_wrapper).setStyle({height:sizearr[1]+"px",width:sizearr[0]+"px"});
			new Effect.Appear($(modal_wrapper),{duration: 0.2,from: 0, to: 0.2});
			new Effect.Appear($(modal_window),{duration: 0.2});
			if(callback) callback();
		},
		onFailure: function(t) { alert("Failed to load floater! Ajax status: "+t.status); }
	});
}


function openFloater2(url,style) {
	if(!url) {
		alert("Please specify an URL!");
		return false;
	}
	Event.observe(document,"keyup",floater_esc = function(e) {
		if(e.keyCode==27) destroyFloater();
	});
	var params;
	if(url.indexOf("?")!=-1 && url.split("?").length==2) {
		params = url.split("?")[1];
		url = url.split("?")[0];
	}
	new Ajax.Updater("saved",url,{
		method:'get',
		parameters:params,
		evalScripts:true,
		onComplete: function(t) {
			if(!style) {
				scrollarr = getScrollOffset();
				$("saved").style.top = (scrollarr[1])+"px";
				$("saved").style.left = (scrollarr[0]+((document.documentElement.clientWidth/2))-($("saved").getWidth()/2))+'px';
			} else if($("saved").style.indexOf("display")==-1) $("saved").style = style+" display: none;";
			else $("saved").style = style;
			$("modal_wrapper").setStyle({top:scrollarr[1]+"px",left:scrollarr[0]+"px"});
			new Effect.Appear($("modal_wrapper"),{duration: 0.2,from: 0, to: 0.2});
			new Effect.Appear($("saved"),{duration: 0.2});
		},
		onFailure: function(t) { alert("Failed to load floater! Ajax status: "+t.status); }
	});
}

function openFloater3(url,style) {
	if(!url) {
		alert("Please specify an URL!");
		return false;
	}
	Event.observe(document,"keyup",floater_esc = function(e) {
		if(e.keyCode==27) destroyFloater();
	});
	var params;
	if(url.indexOf("?")!=-1 && url.split("?").length==2) {
		params = url.split("?")[1];
		url = url.split("?")[0];
	}
	new Ajax.Updater("error",url,{
		method:'get',
		parameters:params,
		evalScripts:true,
		onComplete: function(t) {
			if(!style) {
				scrollarr = getScrollOffset();
				$("error").style.top = (scrollarr[1])+"px";
				$("error").style.left = (scrollarr[0]+((document.documentElement.clientWidth/2))-($("error").getWidth()/2))+'px';
			} else if($("saved").style.indexOf("display")==-1) $("saved").style = style+" display: none;";
			else $("error").style = style;
			$("modal_wrapper").setStyle({top:scrollarr[1]+"px",left:scrollarr[0]+"px"});
			new Effect.Appear($("modal_wrapper"),{duration: 0.2,from: 0, to: 0.2});
			new Effect.Appear($("error"),{duration: 0.2});
		},
		onFailure: function(t) { alert("Failed to load floater! Ajax status: "+t.status); }
	});
}

/*
destroyFloater()

Closes an open floater.
*/
function destroyFloater() {
	var modal_window,modal_wrapper;
	if(!$("modal_window").visible()) return false;
	if($("modal_window2")&&$("modal_window2").visible()) {
		modal_window = "modal_window2";
		modal_wrapper = "modal_wrapper2";
	} else {
		modal_window = "modal_window";
		modal_wrapper = "modal_wrapper";
	}
	$$("#"+modal_window+" textarea.modulemce").each(function(o) {
		if(o.id) tinyMCE.execCommand("mceRemoveControl",false,o.id);
	});
	new Effect.Fade($(modal_wrapper),{duration: 0.2});
	new Effect.Fade($(modal_window),{duration: 0.2,afterFinish: function() { $(modal_window).innerHTML=null; }});	
	//Event.stopObserving(document,"keyup",floater_esc);
}

/*
getScrollOffset()

Returns an array with the scroll offset in px.
*/
function getScrollOffset() {
	var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
	var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
	var scrollarr = new Array(scrollx,scrolly)
	return scrollarr;
}

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;
	}
	return [pageWidth,pageHeight];
}
