﻿// Funzione base per le pagine
// richiede jquery?

var APP = (function(){

    var that = this;

    //array delle funzioni di inizializzazione
    this.__INIT = [];
    
    //array delle funzioni del post-inizializzazione
    this.__INITCOMPLETE = [];
	
	//array delle risorse
	this.__RESOURCES = [];
    
   
    
    this.addInitFunction = function(fn, args){
        var c = __INIT.length;
        __INIT[c] = {
            fn: fn,
            args: args
        };         
    }
    
    this.addInitCompleteFunction = function(fn, args){
        var c = __INITCOMPLETE.length;
        __INITCOMPLETE[c] = {
            fn: fn,
            args: args
        };         
    }
    
        
        
    return {
		pagetitle: function() {
			if (jQuery) {  
				//return jQuery("head title").html();
				return $(document).attr("title");
			}
		},
		require: function(url, cache, async){
			//callback di registrazione script
			var register = function(){
				__RESOURCES[url] = true;	
			
			}
						
			cache = cache || false;
			async = async || false;
			
			$.ajax({
				async: async,
				type: "GET", 
				url: url, 
				dataType: "script", 
				cache: cache,
				success: register
			}); 
			
		},
		requireOnce: function(url, cache, async){
		
			if(!__RESOURCES[url]){
				this.require(url, cache, async);
			}
		},
        addInit: function(f){
            var args = arguments;
            if(args && args.length>1){
                args.slice(1);
            } else {
                args = undefined;
            }
            addInitFunction(f, args);
        },
        runInit: function(){
            //runInitFunctions();
            for(j=0;j<__INIT.length;j++){
                var F = __INIT[j];
                if(F.fn && typeof(F.fn) == "function"){
                    F.fn(F.args);
                }
            }           
        },
        addInitComplete: function(f){
            var args = arguments;
            if(args && args.length>1){
                args.slice(1);
            } else {
                args = undefined;
            }
            addInitCompleteFunction(f, args);
        },
        runInitComplete: function(){
            for(j=0;j<__INITCOMPLETE.length;j++){
                var F = __INITCOMPLETE[j];
                if(F.fn && typeof(F.fn) == "function"){
                    F.fn(F.args);
                }
            }           
        },
        raiseEvent: function(group, name){
            //eventuali argomenti dell'evento
            var args = this.Utils.argsShift(arguments,2);
            var e = this.Utils.getEventName(group, name);
            //TODO: usare chiamate native del dom, renderebbe app.js indipendente da jquery
            this.globalEventEmitter.trigger(e, args);        
        },
        globalEventEmitter:  $(document),
        Data: {},
        Utils: {              
            argsShift: function(args, shift){  //utility che eseguo lo shift degli argomenti di una funzione
                if(args && args.length>shift){
                    a = [];
                    for(i=shift; i<args.length; i++){
                        a[i-shift] = args[i];
                    }
                    return a;
                    
                } else {
                    return undefined;
                }    
            },        
            getEventName: function(group, name){
                return group + '_globalevent_' + name;  
            },
            newGuid: function(){
                var s = [];
                var hexDigits = "0123456789ABCDEF";
                for (var i = 0; i < 32; i++) {
                    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
                }
                s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
                s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

                var uuid = s.join("");
                return uuid;
            }
        }
        
    
    }


})();


function RUN(){

    
    //esegue le inizializzazioni
    APP.runInit();
    
    //... ed il post
    APP.runInitComplete();
	

}


$(function(){
	RUN();
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(){
		RUN();
		if (jQuery) {
			jQuery("head title").text(APP.pagetitle());
		}
	});
});


