/* 
* containerId: id dell'element HTML in cui verrà in serito il file
* fp: path del file da aprire via ajax
* menuId: id della voce di menù da attivare
*/ 
function loadPage(containerId, fp, menuId){
  //se non viene passato nessun id pr il menu non iene cambiata la voce
  if(menuId){
    activateMenuLink(menuId);
  }
  var containerEl = $(containerId); 
  activateLoader(containerEl);
  //setTimeout("fake()", 3000);
  openAjaxRequest(fp, containerEl);
}

function activateMenuLink(menuId){
  deactivateMenuLink();
  $(menuId).addClassName('menuSelected');
}

function deactivateMenuLink(){
  var l = document.getElementsByClassName('menuSelected');
  l.each(function(el){el.removeClassName('menuSelected');});
}

function activateLoader(containerEl){
  //$(containerEl).update('<span><img src="../utils/img/loading.gif"/></span>');
  //$(containerEl).update('<span><img src="../img/loading.gif"/></span>'); // dentro al tema
  $(containerEl).update('<span><img src="../loading.gif" /></span>'); //sulla root
}

function deactivateLoader(containerEl, errorMessage){
  $(containerEl).update('<span>'+errorMessage+'</span>');
}


function updatePage(containerEl, html){
  containerEl.update(html);
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}


/** AJAX ******/

function openAjaxRequest(url, containerEl){
	//var pars = serializeForm(form, uuid);
	var onCompleteFunc = function(){processAjaxResponse(arguments[0], containerEl);};
	var onFailureFunc = processAjaxFailure;
	//var header = ["Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1"];
	var header = ["Content-Type", "text/html; charset=iso-8859-1"];
	
	new Ajax.Request(url, { method: 'post', onComplete: onCompleteFunc, onException: onFailureFunc, requestHeaders: header});
}

function processAjaxFailure(request, exception) {
	//log("ajax response failure start");
	if(exception.message)
		alert("An error has occurred: " + exception.message);
	else
		alert("An error has occurred: " + exception);
	if(window.uxForm)
		window.uxForm.submitted = false;
	deactivateLoader('content_body', exception.message);
}

function processAjaxResponse(request, containerEl) {
	//log("ajax response start");
	//log(request.responseText);
	//alert("request.responseText -> " + request.responseText);
  updatePage(containerEl, request.responseText);
	submitting = false;
	//();
	//log("ajax response done");
}


/* alcedo_doScroll 
* 
* ESEMPI Invocazione:
*  alcedo_doScroll("down", "containerDivId", 500); 
*  alcedo_doScroll("up", "containerDivId", 500); 
*/
var alcedo_moving = false;
var alcedo_scrolling = null;
function alcedo_doScroll(dir, elId, movement, step, i) {    
  if(alcedo_moving==false){
    alcedo_moving = true;
    // Velocità di movimento
  	if(!i){var i=0;}
  	if(!step){var step=10;}
  	var timeout = 20;
  	alcedo_scroll(elId, dir, step);
   	var i2 = i + step;
   	if (i2<movement) {
  		var func = "alcedo_continueScrolling('" + dir + "','" + elId + "'," + movement + "," + step + "," + i2 + ")";
  		setTimeout(func, timeout);
  	}
  	return false;
	}
}

function alcedo_continueScrolling(dir, elId, movement, step, i) {
    alcedo_scroll(elId, dir, step);
    if(!i){var i=0;}
  	if(!step){var step=10;}
    var timeout = 20;
  	var i2 = i + step;
	if(i2>movement-step){step = movement-i2;}
   	if (i2<movement) {
  		var func = "alcedo_continueScrolling('" + dir + "','" + elId + "'," + movement + "," + step + "," + i2 + ")";
  		alcedo_moving = false;
  		setTimeout(func, timeout);
   }else{
     alcedo_moving=false;
   }
}

function alcedo_startScrolling(elId, dir, step){
		if(alcedo_moving==false){
      alcedo_moving = true;
      alcedo_scroll(elId, dir, step);
    	if(alcedo_scrolling == null){
    		//METTERE DIRETTAMENTE ALCEDO_SCROLL IN setInterval ???
    		var func = "alcedo_scroll('" + elId + "','" + dir + "'," + step + ")";
    		//var func = "alcedo_startScrolling('" + elId + "','" + dir + "'," + speed + ")";
    		alcedo_scrolling = setInterval(func, 20);
    	}
  	}
}

function alcedo_stopScrolling(el){
	clearInterval(alcedo_scrolling);
	alcedo_scrolling = null;
	alcedo_moving = false;
}

function alcedo_scroll(el, dir, step){
	if(!step){var step=5;}
	var e = $(el);
	if(dir=='up')
		e.scrollTop-=step;
	if(dir=='down')
		e.scrollTop+=step;
	if(dir=='left')
		e.scrollLeft-=step;
	if(dir=='right')
		e.scrollLeft+=step;
}

