/*
 * Script di base per il funzionamento delle finestre di overlay
 * In linea teorica, QUESTO FILE NON DOVREBBE MAI ESSERE MODIFICATO
 * 
 * VERSIONE 2.1
 */
var timeout = null;

function WindowWidget() {
	this.seconds_before_hiding = seconds_before_hiding;
	this.showResponse = true;
	this.yui_window = null;
	this.method = "GET";	// Metodo per il passaggio dei dati
	this.php_script = "";	// Nome dello script PHP che esegue le azioni (da usare solo se action_query è falso)
	this.action_query = true; // Se false significa che gli script vengono raggiunti tramite normali chiamate a script php
	
  	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, parameters) {
    	me = this;
  		var callback = {
		    success : function (o) {
				try {
		        	var messages = YAHOO.lang.JSON.parse(o.responseText);

		        	var body = '<div id="popup_window_feedback"><div style="text-align : center; padding-left : 100px; padding-bottom:30px; width : 240px;">' + messages.body + '</div></div>';
		        	
		        	if(me.showResponse) me.responseWindow(messages.header, 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();
		        }
				
				// redirect
		        if(messages.redirect != ''){
		        	location.href = messages.redirect;
		        }
		        
		        // reload 
		        if(messages.section == 'personal_photo_gallery'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/galleria/dyn_foto/' + aux[0], 'last-my-photo-list');
		        	reload('http://' + location.hostname + '/galleria/dyn_foto_dettaglio/' + aux[0] + '/' + aux[1], 'photo-list');
		        }
		         
		        if(messages.section == 'status_update'){
		 
		        	reload('http://' + location.hostname + '/main/dyn_status_update', 'status-update-list');
		        }
		        
		        if(messages.section == 'personal_video_gallery'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/galleria/dyn_video/' + aux[0], 'last-my-video-list');
		        	reload('http://' + location.hostname + '/galleria/dyn_video_dettaglio/' + aux[0] + '/' + aux[1], 'video-list');
		        }
		        
		        if(messages.section == 'buddylist'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/amici/dyn_vedi_amici/' + aux[0], 'buddy-list');
		        }
		        
		        if(messages.section == 'personal_buddylist'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/amici/dyn_vedi_amici_personal/' + aux[0], 'buddy-list');
		        }
		        
		        if(messages.section == 'blacklist'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/listanera/dyn_vedi_listanera/' + aux[0], 'black-list');
		        }
		        
		        if(messages.section == 'message_out'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/messaggi/dyn_inviati/' + aux[0], 'last-sent-messages-list');
		        }
		        
		        if(messages.section == 'message_in'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/messaggi/dyn_ricevuti/' + aux[0], 'last-messages-list');
		        }
		        
		        if(messages.section == 'blog'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/blog/dyn_vedi/' + aux[0], 'blog-posts-list');
		        }
		        
		        if(messages.section == 'post'){
		        	var aux = messages.params.split('-');
		        	reload('http://' + location.hostname + '/blog/dyn_post/' + aux[0] + '/' + aux[1], 'last-comments-list');
		        }
		    },
		    failure : function (o) {
		    	if(me.showResponse) me.responseWindow(errorWindow["connection"].header, errorWindow["connection"].body, errorWindow["connection"].footer);
		    	else me.hide();
		    },
		    upload : function(o){
		    	alert(o.responseText);
		    }
		    
		};

  	    var conn = ((this.method == "GET") ?
  	    			(
  	    				(this.action_query) ?
  	    					(YAHOO.util.Connect.asyncRequest("GET", base_url + actionString, callback)) :
  	    					(YAHOO.util.Connect.asyncRequest("GET", this.php_script + "?" + actionString, callback))
  	    			) :
  	    			(
	    				(this.action_query) ?
	    					(YAHOO.util.Connect.asyncRequest("POST", base_url + actionString, callback, parameters)) :
	    					(YAHOO.util.Connect.asyncRequest("POST", this.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, parameters) {
    	obj.setClose(false);
    	obj.fillWindow(wait.header, wait.body, wait.footer, obj);
    	obj.show();
    	
    	obj.ajaxCall(actionString, parameters);
    	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);
}
