/*
* DashCoincoin
* Developed and designed by Thenon David
* www.sveetch.biz
* 
* HTML parser
*
* Sert à installer les évènements pour les éléments html déja en place.
*/

Tribune_HTML_Parser = {
    /*
    * Parser d'une liste initiale HTML pour les messages
    */
    get_backend : function() {
        if(DEBUG) console.log("get_backend -> HTML() started");
        var processedPosts = 0;
        var postList = $("#tribune_list ul li");
        if( postList.length > 0 ) {
            // On mappe la liste du backend sur une liste html mise en forme
            $.each( postList, function(i){
                var is_anonymous = $(this).find("span.ua_login").length > 0;
                var sender = "";
                
                if(!is_anonymous) {
                    sender = $(this).find("span.login").text();
                    var ua = $(this).find("span.login").attr("title");
                    var truncated_ua = ua.substr(0,30);
                } else {
                    var ua = truncated_ua = $(this).find("span.ua_login").text();
                }
                var n = {
                    "pk": parseInt( $(this).attr("rel") ),
                    "clock": $(this).find("span.horloge").text(),
                    "clockclass": ClockstampManipulator.get_from_clock($(this).find("span.horloge").text()),
                    "sender": sender,
                    "is_anonymous": is_anonymous,
                    "timestamp": $(this).attr("class").split(' ')[0].substr(2),
                    "ua": ua,
                    "truncated_ua": truncated_ua,
                    "text": $(this).find("span.post_content").html()
                };
                // vérification anti-doublon au cas ou
                if( n['pk'] > LAST_POSTED_ID ) {
                    processedPosts += 1;
                    
                    // Note le timestamp et calcul son indice
                    if( TS_CLOCKS[ n['timestamp'] ] ) {
                        TS_CLOCKS[ n['timestamp'] ][0] += 1;
                    } else {
                        TS_CLOCKS[ n['timestamp'] ] = [1, false];
                    }
                    // Ajouter un indice à l'horloge et à sa class si 
                    // elle est supérieure à 1 sans preter attention à l'indice 
                    // déja existant dessus
                    if( TS_CLOCKS[ n['timestamp'] ][0] > 1 ){
                        var indice = TS_CLOCKS[n['timestamp']][0];
                        
                        n['clock'] = n['clock'].substr(0,8) + indice;
                        $(this).find("span.horloge").html( n['clock'].substr(0,8)+'<sup>'+ indice +'</sup>' );
                        // Modifier la class itemHORLOGE pour mettre à jour son indice
                        $(this).removeClass('item'+n['clockclass']);
                        n['clockclass'] = n['clockclass'].substr(0,6) + ClockIndicer.lpadding(indice);
                        $(this).addClass('item'+n['clockclass']);
                    }
                    
                    // Stock l'horloge du post comme celle de l'utilisateur, 
                    // uniquement pour les authentifiés
                    if( n['sender'] != "" && wrapPreference.getPref("username") != "" && n['sender'] == wrapPreference.getPref("username")) {
                        // Horloge sur trois segments
                        if(DEBUG) console.log("USER_CLOCK => " + ClockIndicer.indice_to_number(n['clock']));
                        USER_POSTS_CLOCK[ ClockIndicer.indice_to_number(n['clock']) ] = true;
                        // Meme Horloge en version sur deux segments
                        USER_POSTS_CLOCK[ n['clock'].substr(0,5) ] = true;
                    }
                    
                    //Evènements sur les objets du message
                    tribuneParser.attach_message_events(n, true);
                    
                    // Récupère le dernier id du backend
                    LAST_POSTED_ID = n['pk'];
                }
            });
            
            // Positionne le scroll de la liste des messages au bas de la liste
            if( processedPosts>0 ) $("#tribune_list").scrollTop( $("#tribune_list ul").height() );
        }
        return true;
    }
};
