/*
 * Script di base per il funzionamento delle finestre di overlay
 * In linea teorica, QUESTO FILE NON DOVREBBE MAI ESSERE MODIFICATO
 */
var timeout = null;

function WindowWidget() {
	this.seconds_before_hiding = seconds_before_hiding;
	this.showResponse = true;
	this.yui_window = null;
  	
  	var me = null;
	
	this.initialize = function (name, properties) {
		this.yui_window = new YAHOO.widget.Panel(name, properties);
		
		this.yui_window.hideEvent.subscribe(this.close);
  	};
  	
    this.setHeader = function (header){
  		this.yui_window.setHeader(header);
  	};
  	
    this.setBody = function (body){
  		this.yui_window.setBody(body);
  	};
  	
    this.setFooter = function (footer){
  		this.yui_window.setFooter(footer);
  	};
  	
    this.show = function () {
  		this.yui_window.render(document.body);
  		this.yui_window.show();
  	};
  	
    this.hide = function () {
  		this.yui_window.hide();
  	};
  	
    this.setClose = function (closing) {
  		this.yui_window.cfg.setProperty("close", closing);
  	};
  	
    this.setPostHiding = function (seconds_before_hiding) {
  		this.seconds_before_hiding = seconds_before_hiding;
  	};
  	
    this.setShowResponse = function (showResponse) {
  		this.showResponse = showResponse;
  	};
  	
  	this.setObjectName = function(obj_name) {
  		this.obj_name = obj_name;
  	};
  	
    this.ajaxStaticCall = function (me, win_text, script_url, actionString) {
    	var callback = {
		    success : function (o) {
				try {
		        	me.staticAjaxWindow(me, win_text.header, o.responseText, win_text.footer);
		        }
		        catch(e) {
		        	me.staticAjaxWindow(me, errorWindow["json"].header, errorWindow["json"].body, errorWindow["json"].footer);
		        }
		    },
		    failure : function (o) {
		    	me.staticAjaxWindow(me, errorWindow["connection"].header, errorWindow["connection"].body, errorWindow["connection"].footer);
		    }
		};

  	    var conn = YAHOO.util.Connect.asyncRequest("POST", script_url, callback, actionString);
  	};
  	
  	this.ajaxCall = function (actionString) {
    	me = this;
  		var callback = {
		    success : function (o) {
				try {
		        	var messages = YAHOO.lang.JSON.parse(o.responseText);
		        	
					// redirect
			        if(messages.redirect != ''){
			        	location.href = messages.redirect;
			        }

		        	if(me.showResponse) me.responseWindow(messages.header, messages.body, messages.footer);
		        	else me.hide();
		        }
		        catch(e) {
		        	if(me.showResponse) me.responseWindow(errorWindow["json"].header, errorWindow["json"].body, errorWindow["json"].footer);
		        	else me.hide();
		        }
		    },
		    failure : function (o) {
		    	if(me.showResponse) me.responseWindow(errorWindow["connection"].header, errorWindow["connection"].body, errorWindow["connection"].footer);
		    	else me.hide();
		    }
		};

  	    var conn = ((method == "GET") ?
  	    			(
  	    				(action_query) ?
  	    					(YAHOO.util.Connect.asyncRequest("GET", base_url + actionString, callback)) :
  	    					(YAHOO.util.Connect.asyncRequest("GET", php_script + "?" + actionString, callback))
  	    			) :
  	    			(
	    				(action_query) ?
	    					(YAHOO.util.Connect.asyncRequest("POST", base_url + actionString, callback)) :
	    					(YAHOO.util.Connect.asyncRequest("POST", php_script, callback, actionString))
    	    		)
  	    		);
  	};
  	
    this.timedClose = function () {
    	if(this.seconds_before_hiding > 0) {
  			me = this;
  			timeout = window.setTimeout(this.close, this.seconds_before_hiding * 1000);
  		}
  	};
  	
  	this.close = function () {
  		me.hide();
  	};
  	
  	this.staticWindowBase = function (obj, header, body, footer) {
  		timeout = null;
  		
  		obj.fillWindow(header, body, footer, obj);
  		obj.show();
  		
  		if(obj.closeAfter == "static") obj.timedClose();
  	};
  	  	
  	this.staticWindow = function (obj, header, body, footer) {
  		obj.staticWindowBase(obj, header, body, footer);
  	};
  	
  	this.staticAjaxWindow = function (obj, header, body, footer) {
  		obj.staticWindowBase(obj, header, body, footer);
  	};
  	
  	this.preStaticWindow = function (obj, pre_win, post_win, script_url, actionString) {
  		obj.fillWindow(pre_win.header, pre_win.body, pre_win.footer, obj);
    	obj.show();
    	
    	obj.ajaxStaticCall(obj, post_win, script_url, actionString);
  	};
  	
    this.loadingWindow = function (obj, actionString) {
    	obj.setClose(false);
    	obj.fillWindow(wait.header, wait.body, wait.footer, obj);
    	obj.show();
    	
    	obj.ajaxCall(actionString);
    	if(obj.closeAfter == "loading") obj.timedClose();
    };
    
    this.loadingCustomWindow = function (obj, actionString, custom_loading_text) {
    	obj.setClose(false);
    	obj.fillWindow(custom_loading_text.header, custom_loading_text.body, custom_loading_text.footer, obj);
    	obj.show();
    	
    	obj.ajaxCall(actionString);
    	if(obj.closeAfter == "loading") obj.timedClose();
    };
  	
    this.responseWindow = function (header, body, footer) {
  		this.setClose(true);
  		this.fillWindow(header, body, footer, this);
    	this.timedClose();
  	};
  	
  	this.fillWindow = function (header, body, footer, obj) {
  		obj.setHeader(header);
  		obj.setBody(body);
  		obj.setFooter(footer);
  	};
  	
  	this.setCloseAfter = function (step_name) {
  		this.closeAfter = step_name;
  	};
};

function getHiddenArguments(argumentsName, startingPoint, args)
{
	var hiddenArguments = new Array();
	var j = 0;
	
	for(var i=startingPoint; i<args.length; i++)
		if(args[i])
			hiddenArguments[argumentsName[j++]] = args[i];
	
	return hiddenArguments;
}

/* E' già nello script per le notifiche */
function isArray(obj)
{
    return (obj != null && obj.constructor == new Array().constructor);
}