﻿/**
* @ elation JavaScript (jQuery) API
*
* @ fornisce i metodi per la gestione del sistema.
* @ 1.0.00
*/
var elationJS = function() {
    /**
    * @ ID dell'oggetto HTML usato.
    * @ public var string
    * @ 1.0.00
	*/
    var object_id = "";
    
    /**
    * @ metodo di passaggio dei dati (GET, POST).
    * @ public var string
    * @ 1.0.00
	*/
    var method = "GET";
    
    /**
    * @ indirizzo della pagina AJAX server side.
    * @ public var string
    * @ 1.0.00
	*/
    var php_page = "";
    
    /**
    * @ nome della funzione AJAX PHP.
    * @ public var string
    * @ 1.0.00
	*/
    var php_function = "";
    
    /**
    * @ array dei campi da validare.
    * @ public var array
    * @ 1.0.00
	*/
    var params = new Array();
    
    /**
    * @ secondi di durata di un timeout.
    * @ public var int
    * @ 1.0.00
	*/
    var time = 0;
    
    /**
    * @ mostra o meno il caricamento.
    * @ public var string
    * @ 1.0.00
	*/
    var loading = "";
    
    /**
    * @ indirizzo di una pagina.
    * @ public var string
    * @ 1.0.00
	*/
    var address = "";
    
    /**
    * @ numero di una tab di un sistema a tab.
    * @ public var int
    * @ 1.0.00
	*/
    var tab = 0;
    
    /**
    * @ numero di una slide di un sistema a slide.
    * @ public var int
    * @ 1.0.00
	*/
    var slide = 0;
    
    /**
    * @ nome di un form HTML.
    * @ public var string
    * @ 1.0.00
	*/
    var form_name = "";
    
    /**
    * @ nome di un campo di un form HTML.
    * @ public var string
    * @ 1.0.00
	*/
    var input_name = "";
    
    /**
    * @ ID di un campo di un form HTML.
    * @ public var string
    * @ 1.0.00
	*/
    var input_id = "";
    
    /**
    * @ suggerimento da inserire in un campo di un form.
    * @ public var string
    * @ 1.0.00
	*/
    var suggestion = "";
    
    /**
    * @ tempo di attesa del redirect.
    * @ public var int
    * @ 1.0.00
	*/
    var time = 0;
    
    /**
    * @ valore di una option di una select HTML.
    * @ public var string
    * @ 1.0.00
	*/
    var value = "";
    
    /**
    * @ stringa per tinyMCE.
    * @ public var string
    * @ 1.0.00
	*/
    var string = "";
    
    /**
    * @ tag (BBCode, HTML) di apertura.
    * @ public var string
    * @ 1.0.00
	*/
    var open_tag = "";
    
    /**
    * @ tag (BBCode, HTML) di chiusura.
    * @ public var string
    * @ 1.0.00
	*/
    var close_tag = "";
    
    /**
    * @ metodo di validazione dei campi di un form.
    * @ public function
    * @ 1.0.00
	*/
    this.validates_form = function(object_id, method, php_page, php_function, params, loading) {
        // Object HTML
        var object = jQuery("#" + object_id);
        
        if( loading == "yes" ) {
            // mostro un caricamento
            object.html("Caricamento...");
        }
        
        // chiamata AJAX
        jQuery.ajax({
            type: method,
            url: php_page,
            data: {
                "function": php_function,
                "params": params
            },
            cache: false,
            success: function(response) {
                // mostro il risultato
                object.html(response);
            },
            error: function() {
                object.html("Qualche problema con la chiamata AJAX :(");
            }
        });
    }
    
    /**
    * @ metodo di inizializzazione di un nuovo sistema a tab.
    * @ public function
    * @ 1.0.00
	*/
    this.tabs_init = function(object_id, address) {
        var tabs = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").siblings();
        var button = jQuery("#" + object_id + " > #tab-nav > ul > li.tab-button").first();
        var tab = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").first();
        
        // nascondo tutte le tab
        tabs.addClass("corpo-tab-inattivo");
        
        // mostro la prima tab
        button.addClass("active");
        tab.fadeIn("slow").removeClass("corpo-tab-inattivo").addClass("corpo-tab-attivo");
        
        if( address != "" ) {
            tab.html("caricamento...");
            tab.load(address);
        }
    }
    
    /**
    * @ metodo di selezione di una tab in un sistema a tab.
    * @ public function
    * @ 1.0.00
	*/
    this.tabs = function(object_id, tab, address) {
        var old_button = jQuery("#" + object_id + " > #tab-nav > ul > li.active");
        var old_tab = jQuery("#" + object_id + " > #tab-corpo > div.corpo-tab-attivo");
        var new_button = jQuery("#" + object_id + " > #tab-nav > ul > li.tab-button").slice(tab, Number(tab) + 1);
        var new_tab = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").slice(tab, Number(tab) + 1);
        
        // nascondo la tab attualmente attiva
        old_button.removeClass("active");
        old_tab.hide().removeClass("corpo-tab-attivo").addClass("corpo-tab-inattivo");
        
        // mostro la tab selezionata
        new_button.addClass("active");
        new_tab.fadeIn("slow").removeClass("corpo-tab-inattivo").addClass("corpo-tab-attivo");
        
        if( address != "" ) {
            new_tab.html("caricamento...");
            new_tab.load(address);
        }
    }
    
    /**
    * @ metodo di inizializzazione di un nuovo sistema a tab.
    * @ public function
    * @ 1.0.00
    * @MOD CFAN
	*/
    this.tabs_initCFAN = function(object_id, address) {
        var tabs = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").siblings();
        var button = jQuery("#" + object_id + " > #tab-nav > ul > div.userbar-sx > li.tab-button").first();
        var tab = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").first();
        
        // nascondo tutte le tab
        tabs.addClass("corpo-tab-inattivo");
        
        // mostro la prima tab
        button.addClass("active");
        tab.fadeIn("slow").removeClass("corpo-tab-inattivo").addClass("corpo-tab-attivo");
        
        if( address != "" ) {
            tab.load(address);
        }
    }
    
    /**
    * @ metodo di selezione di una tab in un sistema a tab.
    * @ public function
    * @ 1.0.00
    * @MOD CFAN
	*/
    this.tabsCFAN = function(object_id, tab, address) {
        var old_button = jQuery("#" + object_id + " > #tab-nav > ul > div.userbar-sx > li.active");
        var old_tab = jQuery("#" + object_id + " > #tab-corpo > div.corpo-tab-attivo");
        var new_button = jQuery("#" + object_id + " > #tab-nav > ul > div.userbar-sx > li.tab-button").slice(tab, Number(tab) + 1);
        var new_tab = jQuery("#" + object_id + " > #tab-corpo > div.tab-panel").slice(tab, Number(tab) + 1);
        
        // nascondo la tab attualmente attiva
        old_button.removeClass("active");
        old_tab.hide().removeClass("corpo-tab-attivo").addClass("corpo-tab-inattivo");
        
        // mostro la tab selezionata
        new_button.addClass("active");
        new_tab.fadeIn("slow").removeClass("corpo-tab-inattivo").addClass("corpo-tab-attivo");
        
        if( address != "" ) {
            new_tab.load(address);
        }
    }
    
    /**
    * @ metodo di inizializzazione di un nuovo sistema a slide.
    * @ public function
    * @ 1.0.00
	*/
    this.slide_init = function(object_id, address) {
        var slides = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").siblings();
        var button = jQuery("#" + object_id + " > #slide-nav > ul > li.slide-button").first();
        var slide = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").first();
        
        // nascondo tutte le slide
        slides.addClass("corpo-slide-inattivo");
        
        // mostro la prima slide
        button.addClass("active");
        slide.fadeIn("slow").removeClass("corpo-slide-inattivo").addClass("corpo-slide-attivo");
        
        if( address != "" ) {
            slide.html("caricamento...");
            slide.load(address);
        }
        
        this.timer_slides = setTimeout("eJS.slides('" + object_id + "', '1', '" + address + "')", 5000);
    }
    
    /**
    * @ metodo di selezione di una slide in un sistema a slide.
    * @ public function
    * @ 1.0.00
	*/
    this.slides = function(object_id, slide, address) {
        var old_button = jQuery("#" + object_id + " > #slide-nav > ul > li.active");
        var old_slide = jQuery("#" + object_id + " > #slide-corpo > div.corpo-slide-attivo");
        var new_button = jQuery("#" + object_id + " > #slide-nav > ul > li.slide-button").slice(slide, Number(slide) + 1);
        var new_slide = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").slice(slide, Number(slide) + 1);
        
        // nascondo la slide attualmente attiva
        old_button.removeClass("active");
        old_slide.hide().removeClass("corpo-slide-attivo").addClass("corpo-slide-inattivo");
        
        // mostro la slide selezionata
        new_button.addClass("active");
        new_slide.fadeIn("slow").removeClass("corpo-slide-inattivo").addClass("corpo-slide-attivo");
        
        if( address != "" ) {
            new_slide.html("caricamento...");
            new_slide.load(address);
        }
        
        // cambio slide automatico
        clearTimeout(this.timer_slides);
        
        var next_slide = ( slide == 3 ) ? 0 : Number(slide) + 1;
        this.timer_slidestimer_slides = setTimeout("eJS.slides('" + object_id + "', '" + next_slide + "', '" + address + "')", 5000);
    }
    
    /**
    * @ metodo di inizializzazione di un nuovo sistema a slide.
    * @ public function
    * @ 1.0.00
    * @MOD CFAN
	*/
    this.slide_initCFAN = function(object_id, address) {
        var slides = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").siblings();
        var button = jQuery("#" + object_id + " > #slide-nav > ul > li.slide-button").first();
        var slide = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").first();
        
        // nascondo tutte le slide
        slides.addClass("corpo-slide-inattivo");
        
        // mostro la prima slide
        button.addClass("active");
        slide.fadeIn("slow").removeClass("corpo-slide-inattivo").addClass("corpo-slide-attivo");
        
        if( address != "" ) {
            slide.load(address);
        }
        
        this.timer_slides = setTimeout("eJS.slides('" + object_id + "', '1', '" + address + "')", 7000);
    }
    
    /**
    * @ metodo di selezione di una slide in un sistema a slide.
    * @ public function
    * @ 1.0.00
    * @MOD CFAN
	*/
    this.slidesCFAN = function(object_id, slide, address) {
        var old_button = jQuery("#" + object_id + " > #slide-nav > ul > li.active");
        var old_slide = jQuery("#" + object_id + " > #slide-corpo > div.corpo-slide-attivo");
        var new_button = jQuery("#" + object_id + " > #slide-nav > ul > li.slide-button").slice(slide, Number(slide) + 1);
        var new_slide = jQuery("#" + object_id + " > #slide-corpo > div.slide-panel").slice(slide, Number(slide) + 1);
        
        // nascondo la slide attualmente attiva
        old_button.removeClass("active");
        old_slide.hide().removeClass("corpo-slide-attivo").addClass("corpo-slide-inattivo");
        
        // mostro la slide selezionata
        new_button.addClass("active");
        new_slide.fadeIn("slow").removeClass("corpo-slide-inattivo").addClass("corpo-slide-attivo");
        
        if( address != "" ) {
            new_slide.load(address);
        }
        
        // cambio slide automatico
        clearTimeout(this.timer_slides);
        
        var next_slide = ( slide == 3 ) ? 0 : Number(slide) + 1;
        this.timer_slides = setTimeout("eJS.slidesCFAN('" + object_id + "', '" + next_slide + "', '" + address + "')", 7000);
    }
    
    /**
    * @ metodo di Autocomplete Suggestions di un input.
    * @ public function
    * @ 1.0.00
	*/
    this.autocomplete_suggestions = function(form_name, input_name, object_id, method, php_page, php_function) {
        // Object HTML
        var object = jQuery("#" + object_id);
        // Object Input Field
        var input = document.forms[form_name].elements[input_name];
        
        if( input.value.length > 0 ) {
            // mostro l'oggetto
            object.show();
            
            // chiamata AJAX
            jQuery.ajax({
                type: method,
                url: php_page,
                data: {
                    "function": php_function,
                    "params": input.value
                },
                cache: false,
                success: function(response) {
                    // mostro il risultato
                    object.html(response);
                },
                error: function() {
                    object.html("Qualche problema con la chiamata AJAX :(");
                }
            });
        } else {
            // nascondo l'oggetto
            object.hide();
        }
    }
    
    /**
    * @ metodo di inserimento di un suggerimento in un input.
    * @ public function
    * @ 1.0.00
	*/
    this.post_suggestion = function(object_id, input_id, suggestion) {
        // Object HTML
        var object = jQuery("#" + object_id);
        // Object Input HTML
        var input = jQuery("input:text#" + input_id);
        
        // inserisco il suggerimento
        input.val(suggestion);
        // nascondo l'oggetto (Object HTML)
        object.hide();
    }
    
    /**
    * @ metodo di reindirizzamento.
    * @ public function
    * @ 1.0.00
	*/
    this.redirect = function(address, time) {
        if( time == "" ) {
            time = 0;
        }
        
        setTimeout(function() { location = address; }, time);
    }
    
    /**
    * @ metodo di visualizzazione di un oggetto HTML.
    * @ public function
    * @ 1.0.00
	*/
    this.show_object = function(object_id) {
        // Object HTML
        var object = jQuery("#" + object_id);
        
        // mostro l'oggetto
        object.show();
    }
    
    /**
    * @ metodo di visualizzazione/occultamento di un oggetto HTML.
    * @ public function
    * @ 1.0.00
	*/
    this.hide_object = function(object_id) {
        // Object HTML
        var object = jQuery("#" + object_id);
        
        // nascondo l'oggetto
        object.hide();
    }
    
    /**
    * @ metodo di occultamento di un oggetto HTML.
    * @ public function
    * @ 1.0.00
	*/
    this.show_hide_object = function(object_id) {
        // Object HTML
        var object = jQuery("#" + object_id);
        
        // nascondo l'oggetto
        object.toggle("slow");
    }
    
    /**
    * @ metodo di scorrimento della pagina.
    * @ public function
    * @ 1.0.00
	*/
    this.scroll_page = function(object_id) {
        target = jQuery("#" + object_id);
        
        jQuery("html, body").animate({scrollTop: target.offset().top}, 1000);
    }
    
    /**
    * @ metodo di selezione di una option di una select HTML.
    * @ public function
    * @ 1.0.00
	*/
    this.select_option = function(object_id, value) {
        jQuery("#" + object_id).val(value);
    }
    
    /**
    * @ metodo di selezione di tutte le checkbox.
    * @ public function
    * @ 1.0.00
	*/
    this.check = function(form_name, input_name) {
        for( var i = 0; i < document.forms[form_name].elements[input_name].length; i++ ) {
            document.forms[form_name].elements[input_name][i].checked = true;
        }
    }
    
    /**
    * @ metodo di deselezione di tutte le checkbox.
    * @ public function
    * @ 1.0.00
	*/
    this.uncheck = function(form_name, input_name) {
        for( var i = 0; i < document.forms[form_name].elements[input_name].length; i++ ) {
            document.forms[form_name].elements[input_name][i].checked = false;
        }
    }
    
    /**
    * @ metodo di inserimento di una stringa in tinyMCE.
    * @ public function
    * @ 1.0.00
	*/
    this.insert_to_tinyMCE = function(string) {
        tinyMCE.activeEditor.selection.setContent(string, {format : "html"});
    }
    
    /**
    * @ metodo di inserimento di tag (BBCode, HTML).
    * @ public function
    * @ 1.0.00
	*/
    var insert_tag = function(open_tag, close_tag) {
        var Text = document.getElementById("text");
        
        Text.focus();
        
        // Internet Explorer
        if( typeof document.selection != "undefined" ) {
            var interval = document.selection.createRange();
            var internal_text = interval.text;
            
            interval.text = open_tag + internal_text + close_tag;
            interval = document.selection.createRange();
            
            (internal_text.length == 0) 
                ? interval.move("character", -close_tag.length)
                : interval.moveStart("character", open_tag.length + internal_text.length + close_tag.length);
                
            interval.select();
        } else if( typeof Text.selectionStart != "undefined" ) {
            // Gecko Browser
            var begin = Text.selectionStart;
            var end = Text.selectionEnd;
            var internal_text = Text.value.substring(begin, end);
            
            Text.value = Text.value.substr(0, begin) + open_tag + internal_text + close_tag + Text.value.substr(end);
            
            var position;
            
            (internal_text.length == 0)
                ? position = begin + open_tag.length
                : position = begin + open_tag.length + internal_text.length + close_tag.length;
                
            Text.selectionStart = position;
            Text.selectionEnd = position;
        } else {
            var position;
            var re = new RegExp("^[0-9]{0,3}$");
            
            while( !re.test(position) ) {
                position = prompt("Inserisci in posizione (0.." + Text.value.length + "):", "0");
            }
        }
        
        if( position > Text.value.length ) {
            position = Text.value.length;
        }
    }
}

/**
* @ oggetti "elation" da utilizzare.
*
* @ object eJS - oggetto breve
* @ object elation - oggetto completo
*/
var eJS = new elationJS();
var elation = new elationJS();
