/**
* @author geppo
*/

function Fin() {

    this.azione = "";
    this.azione_gen = "";
    var varFinAct = $('#idx_fin').attr('value');
    this.varFin = varFinAct;
    varFinAct ++;
    $('#idx_fin').attr('value', varFinAct);
    //var varZAct = $('#zIdx_fin').attr('value');
    this.varZ = 0;
    this.varTit = "Caricamento in corso ...";
    var posX = 100;
    posX += (layout == 0) ? 95 : 0;
    posX = posX + this.varFin * 15;
    var posY = 85 + this.varFin * 10;
    
    this.apri = function(azione){
        
        if (!fin_check(azione, this.varFin)) {
            /** Creo la finestra **/
            $('#sfondo').append('<div class="wfin fin_attiva idfin' + this.varFin + '" onmousedown="arr_fin[' + this.varFin + '].zIndC();">' +
            '<div id="fin' + this.varFin + '_content"><div class="caricamento"><img src="/images/stili_imgs/small_loading.gif" alt="loading" /></div></div>' +
            '<div class="wfin_headL"></div><div class="wfin_headM" id="fin' + this.varFin + '_drag"><h1>' +this.varTit +'</h1>' +
            '<div class="wfin_bott"><img src="/images/stili_imgs/bott_reload.png" alt="Ricarica" title="Ricarica" onclick="arr_fin[' +this.varFin +'].setContent()" />' +
            //'<img src="/images/stili_imgs/bott_min.gif" alt="Minimizza" title="Minimizza" onclick="arr_fin[' +this.varFin +'].minOut()" />' +
            '<img src="/images/stili_imgs/bott_chiudi.png" alt="Chiudi" title="Chiudi" onclick="arr_fin[' +this.varFin + '].chiudi();" /></div>' +
            '</div><div class="wfin_headR"></div><div class="wfin_bodyL"></div><div class="wfin_bodyR"></div><div class="wfin_bottL"></div>' +
            '<div class="wfin_bottR"></div><div class="wfin_bottM"></div></div>');
            
            if (layout == 0) $('#fin_pannelli').append('<div class="idfin'+ this.varFin +'"></div>');
            
            this.setContent(azione);
            
            /** Gestisco il drag **/
            var theHand = document.getElementById("fin" + this.varFin + "_drag");
            var theRoot = document.getElementById("fin" + this.varFin + "_content").parentNode;
            
            if(Drag) { Drag.init( theHand, theRoot, 19, posX, posY ); } // Previene il blocco del javascript
            
            this.zIndC();
        }
    }
    
    this.apriPanel = function(azione){
        
        if (!fin_check(azione, this.varFin)) {
            
            /** Creo il pannello **/
            $('#sfondo').append('<div class="panel idfin' + this.varFin + '">' + '<div id="fin' + this.varFin + '_drag"><h1>' +this.varTit +'</h1></div>' +
            '<div id="fin' + this.varFin + '_content" class="panel_content"></div>' +
            '<div class="wlog_headL"></div><div class="wlog_headM"></div><div class="wlog_bodyL"></div><div class="wlog_bottL"></div>' +
            '<div class="wlog_bottM"></div></div>');
            
            if (layout == 0) $('#fin_pannelli').append('<div class="idfin'+ this.varFin +'"></div>');
            
            this.setContent(azione, 'pan');
        }
    }
    
    this.mostra = function() {
        var $fin = $('#fin' + this.varFin + '_content').parent();
        if ($fin.hasClass('wfin')) {
            $fin.show();
        } else {
            $fin.width(($('.cbox:eq(0)',$fin).css('width')).replace('px','') - (-11) + 'px'); // fix FF2 looong panel
            $fin.animate({ right:"0px" }, 1000, "swing");
        }
        // nascondo il loading
        Load.hide();
        // rimuovo la classe oscura se presente
        $('.oscura', $fin).remove();
    }

    this.chiudi = function(speed) {
        var $fin = $('#fin' + this.varFin + '_content').parent();
        if ($fin.hasClass('wfin')) {
            if (speed == "slow") {
                $fin.fadeOut('slow', function() { $(this).remove(); });
            } else {
                $fin.remove();
            }
            
        } else {
            if (speed == "fast") {
                $fin.remove();
            } else {
                $fin.animate({
                    right:"-500px"
                }, 1000, "swing", function() { $(this).remove(); });
            }
        }
        
        if (layout == 0) {
            $('.idfin'+this.varFin+':eq('+eq_pan+')').remove();
        }
        location.hash = '/home'; //imposto alla homepage #/home siccome '' fa refresh...
        arr_fin[this.varFin] = null;
    }
    
    this.zIndC = function() {
        var li_varZ = $('#zIdx_fin').attr('value');
        var $fin = $('#fin' + this.varFin + '_content').parent();
        if ( this.varZ < li_varZ ) {
            this.varZ = parseInt(li_varZ) + 1;
            $fin.css('z-index', this.varZ);
            $('#zIdx_fin').attr('value', this.varZ);
            
            if (layout == 0) {
                // faccio diventare la finestra arancione
                $('div.fin_attiva').removeClass('fin_attiva');
                $fin.addClass('fin_attiva');
                // mostro il pannello corretto
                $('#fin_pannelli .pan_attivo').removeClass('pan_attivo');
                $('.idfin'+this.varFin+':eq('+eq_pan+')').addClass('pan_attivo');
            }
            
            location.hash = this.azione;
        }
    }
    
    this.setTitolo = function(title) {
        this.varTit = title;
        $('#fin' + this.varFin + '_drag h1').remove();
        $('#fin' + this.varFin + '_drag').append('<h1>' + this.varTit + '</h1>')
    }
    
    this.lettSempl = function() {
        var ins = $('#fin' + this.varFin + '_content .post_text').html();
        //var tit = this.varTit.replace('[B] ', '');
        $('#sfondo').append('<div id="oscura"><div id="fin_ls"><div class="ls_box"><div class="ls_text"><h1>' + this.varTit + '</h1>'  + ins + '</div></div>' +
        '<div class="wlog_headL"></div><div class="ls_headR" onclick="gRm(\'oscura\');"></div><div class="wlog_headM"></div><div class="wlog_bodyL">' +
        '</div><div class="wlog_bodyR"></div><div class="wlog_bottL"></div><div class="wlog_bottR"></div><div class="wlog_bottM"></div></div></div>');
        $('#oscura').fadeIn('slow');
    }
    
    this.caricamento = function() {
        if(Load) { Load.show(); }
        $('#fin' + this.varFin + '_content').parent().append('<div class="oscura"></div>');
    }
    
    this.setContent = function(action, tipo) {
        
        arr_fin[this.varFin].caricamento();
        if (action != null) { //se cambia azione -> non voglio un reload
            this.azione = action;
            this.azione_gen = get_azione_gen(action);
        }
        var arr_ajax = $.ajax({
                fin: this.varFin,
                method: "get",
                url: this.azione,
                global: false,
                error: function(obj_req) {
                    parseStatus(obj_req.status, this.fin);
                },
                success: function(html){
                    parseAjax(html, this.fin);
                }
            });
        
        if (tipo != 'pan') location.hash = this.azione; // quando apro nella stessa finestra
    }
    
    this.fotoFulls = function() {
        t = "";
        current_img = $('div.idfin'+this.varFin+':eq('+eq_pan+') > .nav > h4 > span').html() - 1;
        var lista = $('.idfin'+this.varFin+':eq('+eq_pan+') input[name=lista_foto]').attr('value');
        array_imgs = lista.split(',');
        array_imgs.pop();
        
        $('#sfondo').append('<div id="oscura"><div id="fin_ls" style="width:32px; height:32px; text-align:center; background:#000; color:#FFF;"><div class="ls_box">'
            + '<img src="/images/stili_imgs/fulls_load.gif" alt="loading" />'
            + '<img id="image_big" src="/album/read_foto?id='+ array_imgs[current_img] +'&size=big" /></div>'
            + '<table id="fotoFulls_tb"><tr><td style="cursor:pointer;" onclick="fotoFulls_control(\'indietro\')"><img src="/images/stili_imgs/fulls_indietro.png" alt"indietro" /></td>'
            + '<td></td><td style="cursor:pointer;" onclick="fotoFulls_control()"><img src="/images/stili_imgs/fulls_pause.png" alt="play-pause" /></td>'
            + '<td></td><td style="cursor:pointer;" onclick="fotoFulls_control(\'avanti\')"><img src="/images/stili_imgs/fulls_avanti.png" alt"avanti" /></td>'
            + '</tr></table>'
            + '<div class="wlog_headL"></div><div class="ls_headR" onclick="gRm(\'oscura\');"></div><div class="wlog_headM"></div><div class="wlog_bodyL">'
            + '</div><div class="wlog_bodyR"></div><div class="wlog_bottL"></div><div class="wlog_bottR"></div><div class="wlog_bottM"></div></div></div>');
        $('#oscura').fadeIn('slow');
        
        $('#image_big').load(function() {
            $('#fin_ls img[alt=loading]').hide();
            $('#image_big').height('');
            $('#image_big').width('');
            var docum_h = $(document).height();
            var docum_w = $(document).width();
            var img_h = $('#image_big').height();
            var img_w =  $('#image_big').width();
            var box_h;
            var box_w;
            (docum_h < img_h) ? $('#image_big').height('100%') : $('#image_big').width('100%');
            box_h = "90%";
            box_w = "95%";
            $('#fin_ls').css('max-height', img_h+'px');
            $('#fin_ls').css('max-width', img_w+'px');
            $('#fin_ls').height(box_h).width(box_w);
            $('#image_big').show();
            
            if (fotoFulls_stat == "play")
                t = setTimeout("fotoFulls_control('avanti')", 5000);
                
        });
        
        $('#fotoFulls_tb td').hover(function() {
            $(this).find('img').fadeIn('slow');
        }, function() {
            $(this).find('img').fadeOut('fast');
        });
        
    }
    
    this.updateAzione = function(mod, variable, value) {
        switch (mod) {
            case 'aggiungi' : // aggiunge value
                var inx_v = this.azione.indexOf(variable);
                if ( inx_v != -1) { //se esiste giˆ la variabile
                    var p1 = this.azione.slice(0, inx_v+variable.length); // /home/casa?var1=
                    this.azione = p1 + value + ',' + this.azione.slice(inx_v+variable.length);
                } else { // se non esiste la variabile
                    var prepender = (this.azione.lastIndexOf("?") != -1) ? "&": "?";
                    this.azione = this.azione + prepender + variable + value;
                }
                break;
            case 'sostituisci' : // sostituisce il valore della variabile
                var inx_v = this.azione.indexOf(variable);
                if ( inx_v != -1) { //se esiste giˆ la variabile
                    var regex = eval("/"+variable+"([0-9a-z\,\-\_]+)/i");
                    this.azione = this.azione.replace(regex, variable+value);
                } else { // se non esiste la variabile
                    var prepender = (this.azione.lastIndexOf("?") != -1) ? "&": "?";
                    this.azione = this.azione + prepender + variable + value;
                }
                break;
            case 'rimuovi' : // rimuove un dato valore nella variabile
                var inx_v = this.azione.lastIndexOf(variable);
                if ( inx_v != -1) { //se esiste la variabile
                    var p1 = this.azione.slice(0, inx_v-1); // /home/casa
                    var p2 = this.azione.slice(inx_v-1); // ?var1=1,3,4&var2=3,19
                    var prepender = p2.slice(0, variable.length+1); // ?var1=
                    p2 = p2.slice(variable.length+1); // 1,3,4&var2=3,19
                    if (p2.indexOf('&') != -1) {
                        values = p2.slice(0, p2.lastIndexOf('&')); // 1,3,4
                        p2 = p2.slice(p2.indexOf('&')); // &var2=3,19
                    } else {
                        var values = p2;
                        p2 = '';
                    }
                    values = values.replace(value+',', ''); // se non e' l'ultima
                    values = values.replace(','+value, ''); // se la variabile e' ultima
                    this.azione = (values != value) ? p1+prepender+values+p2 : p1+p2;
                }
                break;
            case 'pulisci' : //rimuove tutte le variabili
                var inx_p = this.azione.indexOf('?');
                if ( inx_p != -1) { //se esistono variabli
                    this.azione = this.azione.slice(0, inx_p);
                }
                if (value != null && value != '') { 
                    this.azione = this.azione + "?" + variable + value;
                }
                break;
        }
        location.hash = this.azione;
    }
}

var current_img = 0;
var array_imgs = new Array;
var fotoFulls_stat = "play";
var t = "";