/**
 * @author Anders Mattson
 */

// Must load this here since some of the other libraries use the $ object.
jQuery.noConflict();

// Creating a direct function for loading content into a dom element.
jQuery.fn.extend({
	loadContent: function(path, fn){
		var self = this;
		var path = (path.indexOf("{%") === 0) ? path : (path == "startup") ? "{% startup %}" : "{% layout:"+path+" %}";
		JSide.query(path,function(d,s){
			jQuery(self).html(d);
			if(fn) {
				fn.apply(self, [d,s]);
			}
		});
	}
});

/*
 * Standard rpc error handling
 */
jQuery.extend(jQuery.rpc,{
	error: function(e){

		jQuery("#rpc_status").hide();
		if(typeof e == "object") {
			if(e.error == 898) {
				window.location.href = window.location.href;
				window.location.reload();
			}
			jQuery("body").html(jQuery.toJSON(e));
		}
		else {
			jQuery("body").html(e);
		}
		jQuery("body").css("color","");
	}
});

jQuery.fn.extend({
	getValues: function(){
		var out = {};
		jQuery(":input[rel=" + jQuery(this).attr("id") + "]",this).not(":submit").each(function(i){
			if (jQuery(this).is(":checkbox")) {
				out[jQuery(this).attr("name")] = jQuery(this).is(":checked") ? jQuery(this).val() : "0";
			}
			else {
				out[jQuery(this).attr("name")] = jQuery(this).val();
			}
		});
		return out;
	}
});

var EXECUTE_ONLOAD = function(d){
	jQuery("body").append(jQuery(d));
};

// An attempt at making this installation folder independant.
(function(){
	var url = document.location.href.split("#")[0];
	jQuery.rpc.settings.baseurl = url.split("index.php")[0];
	jQuery.rpc.settings.url = url.split("index.php")[0] + "xmlrpc/index.php";
})();

JSide.__initfunc__ = function(){
	JSide.query("{% startup %}", function(d){
		if (typeof d == "object") {
			jQuery("body").prepend(jQuery("<span>An error occured:" + d.errmsg + '</span>'));
		}
		else {
			jQuery("body").prepend(jQuery(d));
		}
	});
}

// Initiation script. This needs to check for some of the js libraries that needs initialisation.

jQuery(function(){
	
	// The token always has to be present.
	// TODO: determine if the token approach should be used at all.
	if (JSide.token || true) {
		JSide.__initfunc__();
	}

});

// Adds the token to the request header before sending the xhr
jQuery.rpc.settings.beforeSend = function(xhr){
	xhr.setRequestHeader("X-token",JSide.token);
}

JSide.fill = function(sel, parent, comp){
	comp = comp || 0;
	jQuery(window).resize(function(){
		jQuery(sel).each(function(){
			var self = jQuery(this);
			var ph = 0;
			var sp;
			if (parent) {
				sp = jQuery(parent).eq(0);
				ph = sp.height();
			}
			else {
				var sp = self;
				while (!ph) {
					sp = sp.parent();
					ph = sp.height();
				}
			}

			self.height(0);

			var pot = sp.offset().top;
			var pbt = sp.css("border-top-width").replace("px","")*1;
			var ppt = sp.css("padding-top").replace("px","")*1;
			var pbb = sp.css("border-bottom-width").replace("px","")*1;
			var sot = self.offset().top;

			var sbt = self.css("border-top-width").replace("px","")*1;
			var sbb = self.css("border-bottom-width").replace("px","")*1;
			
			var h = ph - ((sot + sbt + sbb) - (pbt + pot + ppt)) + comp;
			self.height(h);
		});
	});
//	jQuery(window).resize();
}

jQuery(function(){
	jQuery(".ci-more-link").live("click", function(e){
		jQuery(this).next().toggle();
		return false;
	});
});
