function uri(url) {
	var regExp = /^((\w+):\/\/\/?)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#;\|]+)?([;\|])?([^\?#]+)?\??([^#]+)?#?(\w*)/;
	var r = regExp.exec(url);
	if(!r) return null;
	return (typeof(r[8]) == 'undefined' ? "" : r[8]);
	/* this.url = this._getVal(r,0); this.protocol = this._getVal(r,2); this.username = this._getVal(r,4); this.password = this._getVal(r,5);
	this.host = this._getVal(r,6); this.port = this._getVal(r,7); this.pathname = this._getVal(r,8); this.urlparamseparator = this._getVal(r,9);
	this.urlparam = this._getVal(r,10); this.querystring = this._getVal(r,11); this.fragment = this._getVal(r,12); */
}


var boxstore = new Object();
var uricodi = "";
var boxes=[];
var idContainers=["left","right"];


saveBoxes = function () {
	// save data in store
	boxstore.set(uricodi, JSONstring.make(boxes));
}

redrawBoxes = function () {
	//boxes -> html

	
	jQuery(boxes).each(function () {

		var bx=this;
		var element = jQuery("#"+bx.box);
		//alert(this.id);
		//Si la zona no correspon, canvia-la

		//Si l'ordre no correspon, canvia'l

		//Si l'open/close no correspon, canvia'l
		if (element.hasClass('box-open')!=bx.isopen) {
			if (bx.isopen) {
				element.removeClass('box-closed');
				element.addClass('box-open');
			} else {
				element.removeClass('box-open');
				element.addClass('box-closed');
			}
		}
	
	});

}

cmpBoxes = function (box1,box2) {
	var result=[];
	var resOrder=0;
	var bx1 = {};
	var bx2 = {};
	var trobat = false;

	for (i=0;i<box1.length;i++) {

		bx1=box1[i];
		trobat=false;

		for (j=0;j<box2.length&&!trobat;j++) {
			bx2=box2[j];
			if (bx1.box==bx2.box) {
				trobat=true;
				result.push({box: bx2.box, zone: bx2.zone, order: resOrder, isopen: bx2.isopen });
			}

		}

		if (!trobat) {
			result.push({box: bx1.box, zone: bx1.zone, order: resOrder, isopen: bx1.isopen });
		}	

		resOrder++;
		
	}

	return result;
}

getHtmlBoxes = function () {
	var boxReturn = [];
	var idContainer = 0;
	for (i=0;i<idContainers.length;i++) {
		idContainer=idContainers[i];
		jQuery("#"+idContainer+" div.box").each(function (order) {
			//alert(this.id+" order="+order);
			boxReturn.push({box: this.id, zone: idContainer, order: order, isopen: jQuery(this).hasClass("box-open") });
			//alert(boxes[boxes.length-1].isopen);
		});

	}
	return boxReturn;
}

editBoxes = function () {
	boxes=getHtmlBoxes();
	saveBoxes();
}

switchBox = function(codi) {
	jQuery("#box-"+codi+" .box-content").each(function () {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).slideDown("fast");
				jQuery("#box-"+codi).removeClass("box-closed").addClass("box-open");
			} else {
				jQuery(this).slideUp("fast");
				jQuery("#box-"+codi).removeClass("box-open").addClass("box-closed");
			}
	});
	editBoxes();
}

function fontSize(nivell){

	switch (nivell) {
		case 0:
			document.body.style.fontSize = "1.0em";
		break;
		case 1:
			document.body.style.fontSize = "1.3em";
		break;
		case 2:
			document.body.style.fontSize = "1.6em";
		break;
	}

	boxstore.set("fontSize", nivell);

}


jQuery(document).ready(function() {

	

		
	// first example
	jQuery(".arbre").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});

	var preloadImg=["/img/title_box0.jpg","/img/title_box1.jpg","/img/title_box2.jpg","/img/title_box3.jpg","/img/title_box4.jpg","/img/title_box0_closed.jpg","/img/title_box1_closed.jpg","/img/title_box2_closed.jpg","/img/title_box3_closed.jpg","/img/title_box4_closed.jpg"];

	var temp=new Array();

	for (i=0;i<preloadImg.length;i++) {
		temp.push(new Image());
		temp[temp.length-1].src=preloadImg[i];
	}


	
	// create a new client-side persistent data store
	boxstore = new Persist.Store('Mont-roig Box Status');
	uricodi=uri(document.location).substr(1).replace(/\//g,"__");
	
	//alert(uricodi);
	// get data back from store, and prompt user with it
	boxstore.get(uricodi, function(ok, val) {
		if (ok&&val) {
			
			boxes=cmpBoxes(getHtmlBoxes(),JSONstring.toObject(val));
			redrawBoxes();
	
		} else {
	
			boxes=getHtmlBoxes();
			saveBoxes();
	
		}
	});
	
	boxstore.get("fontSize", function(ok, val) {
		if (ok&&val) fontSize(parseInt(val));
	});
	
	/*jQuery('div.zonaBoxes').Sortable({
			accept: 'box',
			helperclass: 'boxHelper',
			activeclass : 	'boxSortActive',
			hoverclass : 	'boxSortHover',
			handle: 'div.box-header',
			tolerance: 'pointer',
			opacity: 0.8,
			onChange : function(ser) {
			},
			onStart : function() {
				jQuery.iAutoscroller.start(this, document.getElementsByTagName('body'));
			},
			onStop : function() {
				jQuery.iAutoscroller.stop();
			}
	});*/


});

function calendari(mesany) {
	function resetCalendar(o) {
		jQuery("#calendari").html(o);
	}
	jQuery.ajax({ type: "POST", url: '/php/agenda-calendari.php', data: 'mesany='+mesany, success: resetCalendar });

}

function obretancaInfo (codid) {
	jQuery(".infoEvent").each(function () {
		if (codid == this.id) {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).show();
			} else {
				jQuery(this).hide();
			}
		} else {
			if (jQuery(this).css("display")!="none") {
				jQuery(this).hide();
			} 
		}
	});
}




