function popUpConfirmVoteUp(typeElement2,id_objet2) {

    $(function() {
        //On incremente le vote
        $.post("/moduleJaime_pas/incrementeVote.php",
        {
            id_objet2:id_objet2,
            typeElement2:typeElement2
        },
        function(data){
            
            if(data==1){
                //passage de neutre à aime
                $('<div class="dialogDestroy" title="Le vote est pris en compte">\n\
                        <p>Tu aimes cette news, merci pour ton vote</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:350,
                    modal: true,
                    minHeight:0
                });

                var nbVotes = $("#voteUpScoop"+id_objet2).html();
                nbVotes = parseInt(nbVotes);
                var nbVotesNew = nbVotes + 1;
                var zoneStopVote = '<a class="zoneVote dejaVote" href="javascript:popUpConfirmVoteDown(\'scoop\','+id_objet2+');"><span id="voteUpScoop'+id_objet2+'">'+nbVotesNew+'</span></a>';
                $("#zoneVoteScoop"+id_objet2).html(zoneStopVote);
                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }
            
            if(data==2) {
                //vote existant
                $('<div class="dialogDestroy" title="Vote déjà effectué">\n\
                        <p>Tu as déjà voté pour cette news</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:300,
                    modal: true,
                    minHeight:0
                });
                
                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }

            if(data==0) {
                //si le serveur retourne une erreur
                $('<div class="dialogDestroy" title="Erreur serveur">\n\
                        <p>Veuillez nous excuser, une erreur serveur est survenue, recommencez plus tard</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:400,
                    modal: true,
                    minHeight:0
                });

                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }

            if(data==5) {
                //si le membre n'est pas connecté
                popUpConnexion();
            }

            if(data==6) {
                $('<div class="dialogDestroy" title="Attention">\n\
                    <p>Vous avez trop voté aujourd\'hui, attendez 24h</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:300,
                    modal: true,
                    minHeight:0
                });

                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }
        }
        );
    });
}

function popUpConfirmVoteDown(typeElement2,id_objet2) {

    $(function() {
        //On incremente le vote
        $.post("/moduleJaime_pas/decrementeVote.php",
        {
            id_objet2:id_objet2,
            typeElement2:typeElement2
        },
        function(data){
            if(data==2) {
                //vote existant
                $('<div class="dialogDestroy" title="Vote déjà effectué">\n\
                        <p>Tu as déjà voté pour cette news</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:'auto',
                    modal: true,
                    minHeight:0
                });

                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }

            if(data==3) {
                //passage de aime à neutre
                $('<div class="dialogDestroy" title="Merci pour ton vote">\n\
                    <p>Tu n\'aime plus cette news</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:'auto',
                    modal: true,
                    minHeight:0
                });

                var nbVotes = $("#voteUpScoop"+id_objet2).html();
                nbVotes = parseInt(nbVotes);
                var nbVotesNew = nbVotes - 1;
                var zoneStopVote = '<a class="zoneVote" href="javascript:popUpConfirmVoteUp(\'scoop\','+id_objet2+');"><span id="voteUpScoop'+id_objet2+'">'+nbVotesNew+'</span></a>';
                $("#zoneVoteScoop"+id_objet2).html(zoneStopVote);
                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }

            if(data==0) {
                //si le serveur retourne une erreur
                $('<div class="dialogDestroy" title="Erreur serveur">\n\
                        <p>Veuillez nous excuser, une erreur serveur est survenue, recommencez plus tard</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:300,
                    modal: true,
                    minHeight:0
                });

                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }

            if(data==5) {
                //si le membre n'est pas connecté
                popUpConnexion();
            }

            if(data==6) {
                $('<div class="dialogDestroy" title="Attention">\n\
                    <p>Vous avez trop voté aujourd\'hui, attendez 24h</p></div>').dialog({
                    bgiframe: true,
                    resizable: false,
                    height:'auto',
                    width:300,
                    modal: true,
                    minHeight:0
                });

                setTimeout("popUpTimerClose('.dialogDestroy')",2000);
            }
        }
        );
    });
}

function popUpTimerClose(popup) {
    $(popup).dialog('destroy');
}

function initCompteurVotes(typeElement2,id_objet2,votesNegatifs) {
    //initialise les votes au chargement
    $.post("/moduleJaime_pas/recupNotePositive.php",
    {
        id_objet2:id_objet2,
        typeElement2:typeElement2
    },
    function(data){
        $("#voteUpScoop"+id_objet2).text(data);
    }
    );

    if(votesNegatifs == 'oui') {
        //si les votes negatifs sont autorisés on fait la requete
        $.post("/moduleJaime_pas/recupNoteNegative.php",
        {
            id_objet2:id_objet2,
            typeElement2:typeElement2
        },
        function(data){
            $("#voteDown").text(data);
        }
        );
    }
}

function loadZoneVotes(typeElement2,id_objet2) {

    $.post("/moduleJaime_pas/testSiRelation.php",
    {
        id_objet2:id_objet2,
        typeElement2:typeElement2
    },
    function(data){

        if(data==1) {
            var nbVotes = $("#voteUpScoop"+id_objet2).html();
            var zoneStopVote = '<a class="zoneVote dejaVote" href="javascript:popUpConfirmVoteDown(\'scoop\','+id_objet2+');"><span id="voteUpScoop'+id_objet2+'">'+nbVotes+'</span></a>';
            $("#zoneVoteScoop"+id_objet2).html(zoneStopVote);
        }
    }
    );
}

function loadZonesVotesMulti(typeElement2,tab_id_objet2) {

    $.post("/moduleDiggLike/recupListeVotes.php",
    {
        tab_id_objet2:tab_id_objet2,
        typeElement2:typeElement2
    },
    function(tabRelations){
        
        var tabRelationUnserialized = unserialize(tabRelations);

        for (var id_objet2 in tabRelationUnserialized) {
            var nbVotes = $("#voteUpScoop"+tabRelationUnserialized[id_objet2]).html();
            var zoneStopVote = '<a class="zoneVote dejaVote" href="javascript:popUpConfirmVoteDown(\'scoop\','+tabRelationUnserialized[id_objet2]+');"><span id="voteUpScoop'+tabRelationUnserialized[id_objet2]+'">'+nbVotes+'</span></a>';
            $("#zoneVoteScoop"+tabRelationUnserialized[id_objet2]).html(zoneStopVote);
        }
    }
    );
}

function unserialize(txt){
    var level=0,arrlen=new Array(),del=0,final=new Array(),key=new Array(),save=txt;
    while(1){
        switch(txt.substr(0,1)){
            case 'N':
                del = 2;
                ret = null;
                break;
            case 'b':
                del = txt.indexOf(';')+1;
                ret = (txt.substring(2,del-1) == '1')?true:false;
                break;
            case 'i':
                del = txt.indexOf(';')+1;
                ret = Number(txt.substring(2,del-1));
                break;
            case 'd':
                del = txt.indexOf(';')+1;
                ret = Number(txt.substring(2,del-1));
                break;
            case 's':
                del = txt.substr(2,txt.substr(2).indexOf(':'));
                ret = txt.substr( 1+txt.indexOf('"'),del);
                del = txt.indexOf('"')+ 1 + ret.length + 2;
                break;
            case 'a':
                del = txt.indexOf(':{')+2;
                ret = new Array();
                arrlen[level+1] = Number(txt.substring(txt.indexOf(':')+1, del-2))*2;
                break;
            case 'O':
                txt = txt.substr(2);
                var tmp = txt.indexOf(':"')+2;
                var nlen = Number(txt.substring(0, txt.indexOf(':')));
                name = txt.substring(tmp, tmp+nlen );
                //alert(name);
                txt = txt.substring(tmp+nlen+2);
                del = txt.indexOf(':{')+2;
                ret = new Object();
                arrlen[level+1] = Number(txt.substring(0, del-2))*2;
                break;
            case '}':
                txt = txt.substr(1);
                if(arrlen[level] != 0){
                    alert('var missed : '+save);
                    return undefined;
                };
                //alert(arrlen[level]);
                level--;
                continue;
            default:
                if(level==0) return final;
                alert('syntax invalid(1) : '+save+"\nat\n"+txt+"level is at "+level);
                return undefined;
        }
        if(arrlen[level]%2 == 0){
            if(typeof(ret) == 'object'){
                alert('array index object no accepted : '+save);
                return undefined;
            }
            if(ret == undefined){
                alert('syntax invalid(2) : '+save);
                return undefined;
            }
            key[level] = ret;
        } else {
            var ev = '';
            for(var i=1;i<=level;i++){
                if(typeof(key[i]) == 'number'){
                    ev += '['+key[i]+']';
                }else{
                    ev += '["'+key[i]+'"]';
                }
            }
            eval('final'+ev+'= ret;');
        }
        arrlen[level]--;//alert(arrlen[level]-1);
        if(typeof(ret) == 'object') level++;
        txt = txt.substr(del);
        continue;
    }
}
