// Fonctions d'affichage

function voteOver(obj, actif) {
    if (actif) {
        obj.style.background = '#FF9800';
        if (obj.childNodes[1].innerHTML=='--') obj.childNodes[1].innerHTML='VOTER';
        else obj.firstChild.style.display = '';
    } else {
        obj.style.background = 'url(../images/fond-chiffre.gif) left top repeat-x'; // Impossible de lire la propriété background sous IE...
        if (obj.childNodes[1].innerHTML=='VOTER') obj.childNodes[1].innerHTML='--';
        else obj.firstChild.style.display = 'none';
    }
}

function tipOver(obj, actif) {
    if (actif) {
        obj.style.background = '#FFFFFF';
    } else {
        obj.style.background = '#CCFF99';
    }
}

function tipswitchOver(obj,actif) {
    if (actif) {
        obj.className = 'chiffre1';
    } else {
        obj.className = 'chiffre1 alpha40';
    }
}

// Handy JavaScript to measure the size taken to render the supplied text;
// you can supply additional style information too if you have it to hand.

function measureText(pText) {
  var lDiv = document.createElement('lDiv');

  document.body.appendChild(lDiv);

  lDiv.className = 'textetip';

  lDiv.style.padding = '2px 1px';
  lDiv.style.margin = '0px';
  lDiv.style.letterSpacing = '0px';
  lDiv.style.fontWeight = 'normal';
  lDiv.style.fontSize = "11px";
  lDiv.style.position = "absolute";
  lDiv.style.visibility = "hidden";
  lDiv.style.left = -1000;
  lDiv.style.top = -1000;

  lDiv.innerHTML = pText;

  var lResult = {
   width: lDiv.clientWidth,
   height: lDiv.clienHeight
  };

  document.body.removeChild(lDiv);
  lDiv = null;

  return lResult;
}

function checkInputWidth(obj) {
    var w = measureText(obj.value);
    while (w.width > 615) {
        var str = obj.value;
        obj.value = str.substring(0, str.length-1);
        w = measureText(obj.value);
    }
}

function emptyText(obj) {
    obj.onclick = '';
    obj.value = '';
    obj.style.fontSize = '11px';
    obj.style.fontWeight = 'normal';
}

function unemptyText(obj,txt) {
    if (obj.value == '') {
        obj.style.fontSize = '13px';
        obj.style.fontWeight = 'bold';
        obj.onclick = function onclick(event) { emptyText(this); };
        obj.value = txt;
    }
}

function hltip(id){
    new Effect.Highlight(id);
}

// Actions de l'utilisateur si connecté

function actionVote(obj,eid,tid) {
    if (access_level < 2) {
        show_message_popup();
        return 0;
    }

    if(obj) {
        obj.onclick = '';
        obj.onmouseover = '';
        obj.onmouseout = '';
        voteOver(obj,1);
    }

    var url = '/php/tips.php';
    var rand=Math.random();
    new Ajax.Request(url, {
      method: 'post',
      parameters: {id: eid, action: 'vote', param: tid, rand:rand},
      onSuccess: function(transport) {
        var html = transport.responseText;
        var id = transport.headerJSON.id;
        var d = document.getElementById('TIP'+id);
        d.innerHTML = html;
        if (transport.headerJSON.result == 0) {
            alert("Vous ne pouvez pas voter pour cet évènement.");
            return false;
        }
        hltip(transport.headerJSON.tiptohl);
      }
    });
}

function actionSpam(obj,eid,tid) {
    if (access_level < 2) {
        show_message_popup();
        return 0;
    }

    if (confirm("Êtes-vous sûr(e) de vouloir signaler ce tuyau (spam, message offensant...) ?")) {
        if (obj) obj.onclick = '';

        var url = '/php/tips.php';
        var rand=Math.random();
        new Ajax.Request(url, {
          method: 'post',
          parameters: {id: eid, action: 'spam', param: tid, rand:rand},
          onSuccess: function(transport) {
            var html = transport.responseText;
            var id = transport.headerJSON.id;
            var d = document.getElementById('TIP'+id);
            d.innerHTML = html;
            if (transport.headerJSON.result == 0) {
                alert("Vous ne pouvez pas marquer cet évènement comme spam.");
                return false;
            }
            hltip(transport.headerJSON.tiptohl);
          }
        });
    }
}

function actionPost(obj,eid) {
    if (access_level < 2) {
        show_message_popup();
        return 0;
    }

    var d = document.getElementById('POSTTIP'+eid);
    if ((d.value == '') || (d.value == 'Saisissez votre tuyau ici')) {
        alert("Vous n'avez rien écrit !");
        return false;
    }
    if (confirm("Vous validez la publication de ce tuyau (pas de spam, pas de message offensant...) ?")) {
        if (obj) obj.onclick = '';

        var url = '/php/tips.php';
        var rand=Math.random();
        new Ajax.Request(url, {
          method: 'post',
          parameters: {id: eid, action: 'post', param: d.value, rand:rand},
          onSuccess: function(transport) {
            var html = transport.responseText;
            var id = transport.headerJSON.id;
            var d = document.getElementById('TIP'+id);
            d.innerHTML = html;
            if (transport.headerJSON.result == 0) {
                alert("Vous ne pouvez pas poster deux tuyaux sur le même évènement.");
                return false;
            }
            var d = document.getElementById('NBTIPS'+id);
            d.innerHTML = transport.headerJSON.tipslink;
            hltip(transport.headerJSON.tiptohl);
          }
        });
    }
}

// Actions de l'utilisateur si déconnecté

function setCook(nom,valeur) {
    document.cookie = nom + "=" + escape(valeur);
}

function setCook2(nom,valeur,jours) {
    var expDate = new Date();
    expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000));
    document.cookie = nom + "=" + escape(valeur) + ";expires=" + expDate.toGMTString();
}

function getCook(nom) {
    deb = document.cookie.indexOf(nom + "=");
    if (deb >= 0) {
        deb += nom.length + 1;
        fin = document.cookie.indexOf(";",deb);
        if (fin < 0) fin = document.cookie.length;
        return unescape(document.cookie.substring(deb,fin));
    }
    return "";
}

function delCook(nom) { setCook2(nom,"",-1) }

function actionDelayVote(obj,eid,tid) {
    setCook('TIPID',eid);
    setCook('TIPACTION',"vote");
    setCook('TIPPARAM',tid);

    document.location="/register.php?from=tips";
}

function actionDelaySpam(obj,eid,tid) {
    setCook('TIPID',eid);
    setCook('TIPACTION',"spam");
    setCook('TIPPARAM',tid);

    document.location="/register.php?from=tips";
}

function actionDelayPost(obj,eid) {
    var d = document.getElementById('POSTTIP'+eid);
    if ((d.value == '') || (d.value == 'Saisissez votre tuyau ici')) {
        alert("Vous n'avez rien écrit !");
        return false;
    }

    setCook('TIPID',eid);
    setCook('TIPACTION',"post");
    setCook('TIPPARAM',d.value);

    document.location="/register.php?from=tips";
}

function checkCookie() {
    if (!document.getElementById('comptelogin')) {
        var tipid = getCook('TIPID');
        if (tipid == '') return false;
        var tipaction = getCook('TIPACTION');
        var tipparam = getCook('TIPPARAM');
        delCook('TIPID');
        delCook('TIPACTION');
        delCook('TIPPARAM');

        document.location = '#e'+tipid;
        document.getElementById('TIP' + tipid).style.display = '';

        if (tipaction == 'vote') {
            actionVote(0,tipid,tipparam);
        }
        if (tipaction == 'spam') {
            actionSpam(0,tipid,tipparam);
        }
        if (tipaction == 'post') {
            var posttip = document.getElementById('POSTTIP'+tipid);
            if (posttip) {
                emptyText(posttip);
                posttip.value = tipparam;
                actionPost(0,tipid);
            } else alert("Vous ne pouvez pas poster deux tuyaux sur le même évènement.");
        }
    }
}

// Code Addthis (adapté)

var addthis_config = {"data_track_clickback":true};

function updateAddThis() {
    var headID = document.getElementsByTagName("body")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = 'http://s7.addthis.com/js/250/addthis_widget.js#username=betabondieu';
    headID.appendChild(newScript);
}

