function toggleSliderFormCommenti(ev) {
    YAHOO.util.Event.stopEvent(ev);
    var content = document.getElementById('vidz_commenti');
    var toggle = document.getElementById('toggleFormCommenti');
    if (arguments.callee.status == 'hidden') {
        // show
        var heightToGo = content.scrollHeight;
        var statusToGo = 'shown';
        //var toggleClassToGo = 'gal_hideform';
    } else {
        // hide
        var heightToGo = 0;
        var statusToGo = 'hidden';
        //var toggleClassToGo = '';
    }
    var anim = new YAHOO.util.Anim('vidz_commenti', { height: {to: heightToGo} }, 0.5, YAHOO.util.Easing.easeNone);
    if (statusToGo == 'shown') {
        anim.onComplete.subscribe(
            function() {
                content.style.height = 'auto';
            }
        );
    }
    anim.animate();
    //toggle.className = toggleClassToGo;
    arguments.callee.status = statusToGo;
}

function initSliderFormCommenti() {
    var content = document.getElementById('vidz_commenti');
    content.style.overflow = 'hidden';
    content.style.height = 0;
    toggleSliderFormCommenti.status = 'hidden';
}

var onCommentoInserito = {
    success: function(o) {
        var response = eval( '(' + o.responseText + ')');
        if ('errore' in response) {
            inserisciErroreCommento(response.errore);
        } else if ('htmlCommento' in response) {
            inserisciNuovoCommento(response.htmlCommento, response.codiceCommento);
        }
    },
    failure: function(o) {
        inserisciErroreCommento('errore di comunicazione');
    }
}

function inserisciErroreCommento(msg)
{
    var tmp = document.createElement('div');
    var contenitoreCommenti = YAHOO.util.Dom.get('inizio_lista_commenti'); 
    tmp.innerHTML = '<li class="erroreCommento">' + msg + '</li>';
    tmp = tmp.firstChild;
    tmp.style.display = 'block';
    if (contenitoreCommenti.hasChildNodes) {
        contenitoreCommenti.insertBefore(tmp, contenitoreCommenti.firstChild);
    } else {
        contenitoreCommenti.appendChild(tmp);
    }
}

function inserisciNuovoCommento(html, newCodice)
{
    YAHOO.util.Dom.get('testoCommento').value = '';
    YAHOO.util.Dom.get('codiceCommento').value = newCodice;
    // clean info messages
    var toDelete = YAHOO.util.Dom.getElementsByClassName('infoCommento', 'li', YAHOO.util.Dom.get('inizio_lista_commenti'));
    for (var i=0; i < toDelete.length; i++) {
        toDelete[i].parentNode.removeChild(toDelete[i]);
    }
    // add comment
    var tmp = document.createElement('div');
    var contenitoreCommenti = YAHOO.util.Dom.get('inizio_lista_commenti'); 
    tmp.innerHTML = html;
    tmp = tmp.firstChild;
    tmp.style.display = 'block';
    tmp.style.opacity = 0;
    if (contenitoreCommenti.hasChildNodes) {
        contenitoreCommenti.insertBefore(tmp, contenitoreCommenti.firstChild);
    } else {
        contenitoreCommenti.appendChild(tmp);
    }
    var properties = { opacity: {from: 0, to: 1}};
    var anim = new YAHOO.util.Anim(tmp, properties, 1.5, YAHOO.util.Easing.easeNone);
    anim.animate();   
}

function inserisciCommentoDaForm(ev) {
    YAHOO.util.Event.stopEvent(ev);
    YAHOO.util.Connect.setForm('formInserimentoCommento');
    YAHOO.util.Connect.asyncRequest('POST', '/service/inserisciCommento.php', onCommentoInserito);  
}


YAHOO.util.Event.onAvailable('vidz_commenti', initSliderFormCommenti);
YAHOO.util.Event.onAvailable('toggleFormCommenti',
    function() { YAHOO.util.Event.on(this, 'click', toggleSliderFormCommenti); }
);
YAHOO.util.Event.addListener('formInserimentoCommento', 'submit', inserisciCommentoDaForm);