/*


EXAMPLE


        
        call({
            url : PH_R+'old/alertVideo/AlertRemove.php',
            formElement : '#idF',
            responseElement : '#call2',
            waitElement : '#call2Wait'
            },
            {
            id_utente : 15
            });
        



*/

var EncriptString = function(){
    
}


var call = function(settings,extraParams){
        
    this.responseHtml = false;
    this.responseScript = false;
    this.response = false;
    this.version = '1.0';
    var ob = this;
    
    this.onEnd = function(){};
    if(settings.onEnd){
        this.onEnd = settings.onEnd;
        settings.onEnd = false;
    }
    
    this.onComplete = function(){};
    if(settings.onComplete){
        this.onComplete = settings.onComplete;
        settings.onComplete = false;
    }
    
    this.onFinish = function(){};
    if(settings.onFinish){
        this.onFinish = settings.onFinish;
        settings.onFinish = false;
    }
    
    this.onStart = function(){};
    if(settings.onStart){
        this.onStart = settings.onStart;
        settings.onStart = false;
    }
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- PARAMETRI */
    
    this.settings = jQuery.extend({
        
            url : false,
            mode : false,
            
            applicationName : 'system',
            applicationSubName : 'system',
            idTask : false,
            multiOpen : true,
            taskNameRef : false,
                        
            responseElement : false,
            formElement : false,
            waitElement : false,
            waitLoading : false,
            actionElement : false,
            
            tasked: true,
            evalScript : true,
            autoStart : true,
            responseView : true,
            animation : false,
            dataSettings : true,
            test : false,
            
            module : false, // params ( module_title // addClass ) 
            dialog : false,
            myDialog : false,
            wrapContent : false,
            scrollTo : false
            
    
    },settings); 
    
    this.extraParams =  jQuery.extend({
        
    },extraParams); 
    
        
    
    
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- COSTRUTTORE */
    
    this.__constructor = function(){
        
        if(this.settings.url){
        
            var data = ''; 
            
            // --------------------------- RESULT NOTIFY
            if(extraParams.result_notify && this.settings.responseElement == false && this.settings.dialog == false){
                
                this.settings.responseView = true;
                this.settings.dialog = { modal : true };
                
            }
            
            // --------------------------- ADD TASK
            this.settings.idTask = core.system.addTask(this);
            if (data.length) data +=  '&';
            var taskName = core.system.getTaskNameIdTask(this.settings.idTask);
            data += jQuery.param({ taskName : taskName });
            
            // --------------------------- TASK NAME REF
            if(this.settings.taskNameRef){ 
                this.settings.formElement = "."+this.settings.taskNameRef+" "+this.settings.formElement;
                this.settings.responseElement = "."+this.settings.taskNameRef+" "+this.settings.responseElement;
            }
                    
            // --------------------------- DATA FORM
            if (this.settings.formElement){
                if (data.length) data +=  '&'; 
                data += jQuery(this.settings.formElement).serialize();
            }
            // --------------------------- DATA SETTINGS
            if (this.settings.dataSettings){
                if (data.length) data +=  '&';
                data += jQuery.param(ob.settings);
            }
            // --------------------------- GET DATA EXTRA
            if (this.extraParams){
                if (data.length) data +=  '&';
                data += jQuery.param(this.extraParams);
            }
            // --------------------------- GET DATA EXTRA
            
            
            data += '&'+jQuery.param({ params :this.extraParams });
            
            
                
            jQuery.ajax  
            ({
                        url: ob.settings.url,
                        mode: ob.settings.mode,
                        cache: false,
                        dataType: "html",
                        type: "POST",
                        data: data,
                        beforeSend:function()
                        { 
                        
                                if (ob.settings.waitElement){
                                    ob.showWait();  
                                }
                                
                                if (ob.settings.waitLoading){
                                    core.css.waitPage();  
                                }
                                                                
                                ob.onStart();
                                
                        },
                        complete:function(){
                        
                                ob.onFinish();
                                
                        },
                        success:function(html,textStatus)
                        {
                                if (ob.settings.waitLoading){
                                    //alert('Before Close');
                                    core.css.waitPage({action : 'close'});
                                }
                                                                
                                if (ob.settings.waitElement){
                                    ob.stopWait();  
                                }
                                                                
                                ob.response = html;
                                
                                var responseSplitted = ob.splitResponse(ob.response);
                                ob.responseHtml = responseSplitted.source;
                                //ob.responseHtml += "<javascript style=\"display:none\">"+responseSplitted.javascript+"</javascript>";
                                ob.responseScript = responseSplitted.javascript;
                                
                                if(ob.settings.test){
                                    alert(html);
                                    
                                }
                                
                                // --------------------------- SE MULTI OPEN 
                                
                                if(ob.settings.multiOpen == false){
                                    core.system.removeTaskEgual(taskName,taskName);
                                    $.validationEngine.closePrompt('.formError',true);
                                } 
                                
                                
                                // --------------------------- VISUALIZZA LA RISPOSTA
                                
                                                                                    
                                if (ob.settings.responseView && jQuery.trim(ob.responseHtml).length > 0){
                                    ob.showResult();
                                }
                                
                                if (ob.settings.evalScript){
                                    ob.evalScript(ob.responseScript);
                                }
                                
                                if (ob.settings.scrollTo){
                                    core.css.scrollTo(ob.settings.scrollTo);
                                }
                                
                                
                                
                                ob.onEnd();
                                ob.onComplete();
                                
                        }                 
            });  
        
        }
            
    }  
    
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- INTERPRETA JAVASCRIPT */                                 
      
    
    this.splitResponse = function(response){
        
        if(response){
            var source = response;
            var scripts = new Array();
            
     
            // Strip out tags
            while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
                var s = source.indexOf("<script");
                var s_e = source.indexOf(">", s);
                var e = source.indexOf("</script", s);
                var e_e = source.indexOf(">", e);
                 
                // Add to scripts array
                scripts.push(source.substring(s_e+1, e));
                // Strip from source
                source = source.substring(0, s) + source.substring(e_e+1);
            }
            
            // Loop through every script collected and eval it
            var javascript = '';
            for(var i=0; i<scripts.length; i++) {
                try {
                    javascript += scripts[i]+";"; 
                }
                catch(ex) {
                    alert('ERROR ON EXECUTION SCRIPT :: '+scripts[i]);
                }
            }
            
            // Split response
            
            
            return { 'source' : source, 'javascript' : javascript };
            
        }
        else return { 'source' : false, 'javascript' : false };         
        
    }
    
    
    this.evalScript = function(javascript){
        
        if(javascript){
            
                            if (window.execScript){
                                window.execScript(javascript);
                            } else {
                                eval(javascript);
                            }
        }
        
    }
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- MOSTRA WAIT */
    
    
    this.showWait = function(){
            jQuery(this.settings.waitElement).html("<span class=\"call-wait\"></span>");
    }
    
    this.stopWait = function(){
            jQuery(this.settings.waitElement).html(""); 
    }
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- MOSTRA IL RISULTATO */
    
    this.showResult = function(){
        
        
        if (this.settings.wrapContent){
            
            $.validationEngine.closePrompt('.formError',true);
            $(".ac_results").css('display' , 'none');
            core.css.wrapContent({ html : ob.responseHtml });
            
            return true;
        }
        
        if (this.settings.myDialog){
            
            $.validationEngine.closePrompt('.formError',true);
            $(".ac_results").css('display' , 'none');
            
            this.settings.myDialog.html = ob.responseHtml;            
            core.css.myDialog(this.settings.myDialog);
            
            return true;
        }
        
            
        if (this.settings.dialog){
                    
            var classDialog = core.system.getTaskNameIdTask(this.settings.idTask);
            
            $("body").append("<div class=\""+classDialog+"\"></div>");
            $("."+classDialog).html(ob.responseHtml);
			
            var title = $("."+classDialog+" > ."+classDialog+"-title").html();
            $("."+classDialog+" > ."+classDialog+"-title").css({display: 'none'});
            
            this.settings.dialog =  jQuery.extend({
                title : title,
                open : function(){
                                            $(".ac_results").css('display' , 'none'); 
                                            $("."+classDialog+" *").blur(); 
                                            },
                dragStart : function(){
                                            $.validationEngine.closePrompt('.formError',true);
                                            },
                beforeClose : function(){                   
                                            $.validationEngine.closePrompt('.formError',true); 
                                            $(".ac_results").css('display' , 'none');
                                            },
                close : function(){         
                                            $("."+classDialog).remove();
                                            }
            },this.settings.dialog);
                        
            
            
            $("."+classDialog).dialog(
                this.settings.dialog
            );
            
            /*core.css = new css();*/

            return true;
        }
        
        
        if (this.settings.responseElement){
            
            var classDialog = core.system.getTaskNameIdTask(this.settings.idTask);          
            
            if (this.settings.module){
                
                if(this.settings.module.addClass){
                    var addClass = ' '+this.settings.module.addClass;
                }
                else{ 
                    var addClass = '' 
                }
                 
                if(this.settings.module.title){
                    ob.responseHtml = "<div class=\"module"+addClass+"\"><div class=\"title\">"+this.settings.module.title+"</div><div class=\"content\">"+ob.responseHtml+"</div></div>";
                }
                else{
                    ob.responseHtml = "<div class=\"module"+addClass+"\"><div class=\"content\">"+ob.responseHtml+"</div></div>";
                }
            } 
            
            if (this.settings.animation){
                jQuery(this.settings.responseElement).fadeOut(500,function(){
                    //jQuery(ob.settings.responseElement).html('');
                    jQuery(ob.settings.responseElement).html("<div class=\""+classDialog+"\">"+ob.responseHtml+"</div>");
                    //jQuery(ob.settings.responseElement).html(ob.responseHtml);
                    /*core.css = new css();*/
                });
                jQuery(this.settings.responseElement).fadeIn(500);
            }
            else{
                //jQuery(this.settings.responseElement).html('');
                if(ob.responseHtml){
                jQuery(this.settings.responseElement).html("<div class=\""+classDialog+"\">"+ob.responseHtml+"</div>");
                }
                //jQuery("."+classDialog).html(ob.responseHtml);
                /*core.css = new css();*/
            }
            
            return true;
        }
    
    }    
    
    /*-------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------- COSTRUTTORE */
    
    jQuery(document).ready(function(){
                
        // --- Autostart
        if (ob.settings.autoStart){ 
            ob.__constructor(); 
        }
        // --- Init Action Element
        if (ob.settings.actionElement){
            jQuery(ob.settings.actionElement).click(function(){
                ob.__constructor();
            })
        }
           
    });
    
}

