window.onload = init;

function init() {
   document.getElementById("bprofile").onclick = profileOpen;
   document.getElementById("bsearch").onclick = searchOpen;
   document.getElementById("bstyle").onclick = toStyle2;
   document.getElementById("bstats").onclick = statsOpen;
   document.getElementById("windowtitle").onclick = windowClose;
   document.getElementById("logo").onclick = showPecadoExp;
   document.getElementById("postwindowtitle").onclick = postWindowClose;
   document.getElementById("bestsins").onclick = bestSinsOpen;
   document.getElementById("famoussins").onclick = famousSinsOpen;
   document.getElementById("bestnewsins").onclick = bestNewSinsOpen;
   document.getElementById("worstsins").onclick = worstSinsOpen;

   setFontSize();
   window.onresize = setFontSize;

   initShowPostLinks();
   initShowFSinLink();
   initSendCommentLinks();
   initShowTextLinks();

   initPecadoNoTwitter();
   initPerguntarPecado();

   document.getElementById("postwindowmain").onclick = postToTop;
   document.getElementById("postwindowcontent").onclick = postToTop;
   document.getElementById("windowmain").onclick = windowToTop;
   document.getElementById("windowcontent").onclick = windowToTop;

   initVotes();
   }

function setFontSize() {
   //window.alert("Entrou");
   wsize = document.body.clientWidth;
   var cont = document.getElementById("mainconteiner");
   var content = document.getElementById("content");
   //window.alert(cont.style.width);
   if (wsize < 800) {
      document.body.style.fontSize = "0.8em";
      cont.style.width = "99%";
      content.style.right = "260px";
      }
   else if (wsize < 1024) {
      document.body.style.fontSize = "0.9em";
      cont.style.width = "96%";
      content.style.right = "270px";
      }
   else if (wsize < 1280) {
      document.body.style.fontSize = "1.0em";
      cont.style.width = "93%";
      content.style.right = "280px";
      }
   else {
      document.body.style.fontSize = "1.1em";
      cont.style.width = "90%";
      content.style.right = "290px";
      }
   }


function initPecadoNoTwitter() {
   var win = document.getElementById("pecadotwittertitulo");
   win.innerHTML = "<H1>Pecando no Twitter</H1>";
   win.style.visibility = "visible";
   win.style.height = "1em";
   win = document.getElementById("pecadotwitterconteudo");
   win.style.visibility = "visible";
   win.style.height = "290px";
   }

function initPerguntarPecado() {
   return;
/* var win = document.getElementById("perguntarpecadotitulo");
   win.innerHTML = "<H1>Perguntar n&atilde;o &eacute; Pecado</H1>";
   win = document.getElementById("perguntarpecadoconteudo");
   win.innerHTML = "Carregando...";
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url="menu/formspring.php";
   request.open("GET",url,true);
   request.onreadystatechange = function() {
      if (request.readyState == 4) {
         if (request.status == 200) {
            var win= document.getElementById("perguntarpecadoconteudo");
            win.innerHTML = request.responseText;
            }
         }
      };
   request.send(null); */
   }

function initShowTextLinks() {
   var links = document.getElementsByName("showtextlink");
   for (k=0; k<links.length; k++) {
      heading = links[k].attributes.getNamedItem('heading').value;
      file = links[k].attributes.getNamedItem('file').value;
      links[k].href = "javascript:showText('" + heading + "','" + file + "')";
      }
   }
function showText(title, textfile) {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>" + title + "</H1>\r\n";
   getWindowData('texts/' + escape(textfile));
   }
function showPecadoExp() {
   showText('Por que Pecado?','pecadoexp.php');
   }
function profileOpen() {
   showText('Perfil','profile.php');
   }

function initSendCommentLinks() {
   var links = document.getElementsByName("sendcomment");
   for (k=0; k<links.length; k++) {
      post = links[k].attributes.getNamedItem('number').value;
      links[k].href = "javascript:sendComment(" + post + ")";
      }
   }
function sendComment(id) {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>Comente!</H1>\r\n";
   getWindowData('postcomment.php?pid=' + escape(id));
   }

function initShowPostLinks() {
   var links = document.getElementsByName("showpostlink");
   for (k=0; k<links.length; k++) {
      post = links[k].attributes.getNamedItem('number').value;
      links[k].href = "javascript:showPost(" + post + ")";
      }
   }
function showPost(post) {
   postWindowOpen();
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url="showpost.php?simple=1&post="+escape(post);
   request.open("GET",url,true);
   request.onreadystatechange = function() {
      if (request.readyState == 4) {
         if (request.status == 200) {
            var win= document.getElementById("postwindowcontent");
            win.innerHTML = request.responseText;
            initVote(1000);
            initShowPostLinks();
            initSendCommentLinks();
            }
         }
      };
   request.send(null);
   }




function initShowFSinLink() {
   var links = document.getElementsByName("showfsinlink");
   for (k=0; k<links.length; k++) {
      post = links[k].attributes.getNamedItem('number').value;
      links[k].href = "javascript:showFSin(" + post + ")";
      }
   }
function showFSin(post) {
   var win = document.getElementById("fsinleiamais");
   win.innerHTML = "<STRONG>Carregando...</STRONG>";
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   //var url="postrender.php?post="+escape(postNumber);
   var url="showfsin.php?open=1&post="+escape(post);
   request.open("GET",url,true);
   request.onreadystatechange = function() {
      if (this.readyState != 4) return;
      if (this.status == 200) {
         var win = document.getElementById("lostcontent");
         win.innerHTML = request.responseText;
         win = document.getElementById("lostsin");
         win.style.fontSize = "1.0em";
         // Reload stars css
         initVotes();
         initSendCommentLinks();
         }
      };
   request.send(null);
   }

function toStyle1() {
   document.getElementById("pagestyle").href="styles/crimson.css";
   document.getElementById("bstyle").onclick = toStyle2;
   }

function toStyle2() {
   document.getElementById("pagestyle").href="styles/crimsoni.css";
   document.getElementById("bstyle").onclick = toStyle1;
   }

function bestSinsOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>20 Melhores Pecados (Bayes)</H1>\r\n";
   getTwentySinsData("4");
   }

function famousSinsOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>20 Pecados mais Falados</H1>\r\n";
   getTwentySinsData("1");
   }

function bestNewSinsOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>20 Melhores Pecados (Bayes)</H1>\r\n";
   getTwentySinsData("3");
   }

function worstSinsOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>20 Melhores Pecados (Bayes)</H1>\r\n";
   getTwentySinsData("5");
   }


function searchOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>Procure Pecados</H1>\r\n";
   getWindowData("search.php");
   }

function statsOpen() {
   windowOpen();
   var win = document.getElementById("windowtitle");
   win.innerHTML = "<H1>Estat&iacute;sticas Pecaminosas</H1>\r\n";
   getWindowData("stats.php");
   }

function windowOpen() {
   var win = document.getElementById("windowmain");
   win.style.left = "50%";
   win.style.visibility = "visible";
   win = document.getElementById("windowcontent");
   win.innerHTML = "<P class=\"centered\">Carregando...</P>";
   win.style.left = "50%";
   windowToTop();
   }

function windowToTop() {
   var win = document.getElementById("windowmain");
   win.style.zIndex = 1000;
   win = document.getElementById("windowcontent");
   win.style.zIndex = 1001;
   win = document.getElementById("postwindowmain");
   win.style.zIndex = 998;
   win = document.getElementById("postwindowcontent");
   win.style.zIndex = 999;
   }

function windowClose() {
   var win = document.getElementById("windowmain");
   win.style.visibility = "hidden";
   win.style.left = "-50%";
   win = document.getElementById("windowcontent");
   win.style.left = "-50%";
   }

function getWindowData(itemName) {
   //window.alert(itemName);
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url = itemName;
   // var url = "getDetails.php?ImageID=" + escape(itemName);
   request.open("GET",url,true);
   request.onreadystatechange = showWindowData;
   request.send(null);
   }

function showWindowData() {
   if (request.readyState == 4) {
      //window.alert(request.status);
      if (request.status == 200) {
         detailDiv = document.getElementById("windowcontent");
         detailDiv.innerHTML = request.responseText;
         }
      }
   }


function searchPost() {
   text=document.postsearch.searchtext.value;
   //var win = document.getElementById("windowtitle");
   //win.innerHTML = "<H1>Pecados Encontrados</H1>\r\n";
   win = document.getElementById("searchresult");
   win.innerHTML = "<P class=\"centered\">Buscando...</P>";
   getSearchData(text);
   }

function getSearchData(itemName) {
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url="showpostlist.php?method=99&text="+escape(text);
   request.open("GET",url,true);
   request.onreadystatechange = showSearchData;
   request.send(null);
   }

function showSearchData() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var win= document.getElementById("searchresult");
         win.innerHTML = request.responseText;
         initShowPostLinks();
         }
      }
   }

function getTwentySinsData(itemName) {
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url="showpostlist.php?method=" + escape(itemName);
   request.open("GET",url,true);
   request.onreadystatechange = showTwentySinsData;
   request.send(null);
   }

function showTwentySinsData() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var win= document.getElementById("windowcontent");
         win.innerHTML = request.responseText;
         initShowPostLinks();
         }
      }
   }

function postWindowOpen() {
   var win = document.getElementById("postwindowmain");
   win.style.left = "50%";
   win.style.visibility = "visible";
   win = document.getElementById("postwindowcontent");
   win.innerHTML = "<P class=\"centered\">Carregando...</P>";
   win.style.left = "50%";
   postToTop();
   }

function postToTop() {
   var win = document.getElementById("postwindowmain");
   win.style.zIndex = 1000;
   win = document.getElementById("postwindowcontent");
   win.style.zIndex = 1001;
   win = document.getElementById("windowmain");
   win.style.zIndex = 998;
   win = document.getElementById("windowcontent");
   win.style.zIndex = 999;
   }

function postWindowClose() {
   var win = document.getElementById("postwindowmain");
   win.style.visibility = "hidden";
   win.style.left = "-50%";
   win = document.getElementById("postwindowcontent");
   win.style.left = "-50%";
   }


function clearPostNome() {
   document.formmessage.nome.className = "";
   }

function clearPostTexto() {
   document.formmessage.texto.className = "";
   }

function addComment(pid) {
   document.formmessage.nome.className = "";
   document.formmessage.texto.className = "";
   document.formmessage.email.className = "";
   if (document.formmessage.nome.value.length == 0)
      {
      document.formmessage.nome.className = "formerror";
      document.formmessage.nome.onblur = clearPostNome;
      return;
      }
   if (document.formmessage.texto.value.length == 0)
      {
      document.formmessage.texto.className = "formerror";
      document.formmessage.texto.onblur = clearPostTexto;
      return;
      }
   if (document.formmessage.email.value.length != 0)
      {
      if (checkemail(document.formmessage.email.value)==false) {
         document.formmessage.email.className = "formerror";
         document.formmessage.email.onblur = clearPostTexto;
         return;
         }
      }

   var button= document.getElementById("subcomment");
   button.value = "Enviando...";
   button.disabled = true;

   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   var url="showallcommentsfull.php?pid=" + escape(pid);
   request.open("GET",url,true);
   request.onreadystatechange = showCommentData;
   request.send(null);
   }

function showCommentData() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var win= document.getElementById("oldcommentarea");
         win.innerHTML = request.responseText;

         request = createRequest();
         if (request == null) {
            window.alert("Unable to create AJAX request");
            return;
            }

         var mform= document.getElementById("formmessage");
         parameters = createQuery(mform);
         request.open('POST', "addcomment.php", true);
         request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         request.setRequestHeader("Content-length", parameters.length);
         request.setRequestHeader("Connection", "close");
         request.onreadystatechange = showCommentData2;
         request.send(parameters);
         }
      }
   }

function showCommentData2() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var win= document.getElementById("oldcommentarea");
         win.innerHTML = request.responseText;
         var button= document.getElementById("subcomment");
         button.value = "Enviar";
         button.disabled = false;
         var comtext= document.getElementById("commenttextarea");
         comtext.value = "";
        }
      }
   }

function createQuery(form)
{
    var elements = form.elements;
    var pairs = new Array();

    for (var i = 0; i < elements.length; i++) {

        if ((name = elements[i].name) && (value = elements[i].value))
            pairs.push(name + "=" + encodeURIComponent(value));
    }

    return pairs.join("&");
}

function initVotes() {
   for (k=0; k<1000; k++) {
      starblock = document.getElementById("stars0" + k);
      if (starblock == null) break;
      post = starblock.attributes.getNamedItem('value').value;
      note = starblock.attributes.getNamedItem('grade').value;
      if (note == 0) starblock.className = "starsvote";
      formatStars(post, note, k);
      }
   }

function initVote(k) {
   starblock = document.getElementById("stars0" + k);
   if (starblock == null) return;
   post = starblock.attributes.getNamedItem('value').value;
   note = starblock.attributes.getNamedItem('grade').value;
   if (note == 0) starblock.className = "starsvote";
   formatStars(post, note, k);
   }

function formatStars(post, note, k) {
   for (i=1; i<=5; i++) {
      star = document.getElementById("stars" + i + k);
      if (star == null) break;
      star.STAR = i;
      star.SEQ = k;
      star.POST = post;
      star.NOTE = note;
      }
   fillVoteStars(k);
   enableVoteEvents(k);
   }

function starout(obj) {
   seq = obj.SEQ;
   fillVoteStars(seq);
   }

function fillVoteStars(k) {
   star = document.getElementById("stars1" + k);
   note = star.NOTE;
   //window.alert(note);
   inote = note;
   for (i=1; i<=5; i++) {
      star = document.getElementById("stars" + i + k);
      if (star == null) break;
      if (note > 0) {
         if (inote >= 1) star.className = "sfull";
         else if (inote >= 0.5) star.className = "spartial";
         else star.className = "sempty";
         }
      else star.className = "snone";
      if (inote >= 1) inote--;
      else if(note>0) inote=0;
      }
   }

function starhover(obj) {
   newnote = obj.STAR;
   postnum = obj.POST;
   postseq = obj.SEQ;
   for (i=1; i<=newnote; i++) {
      star = document.getElementById("stars" + i + postseq);
      if (star == null) break;
      star.className = "shover";
      }
   }

function starclick(obj) {
   newnote = obj.STAR;
   postnum = obj.POST;
   postseq = obj.SEQ;
   request = createRequest();
   if (request == null) {
      window.alert("Unable to create AJAX request");
      return;
      }
   disableVoteEvents(postseq);

   for (i=1; i<=newnote; i++) {
      star = document.getElementById("stars" + i + postseq);
      if (star == null) break;
      star.className = "sloading";
      }

   var url = "postvote.php?pid=" + escape(postnum) + "&vote=" + escape(newnote);
   request.open("GET",url,true);
   request.onreadystatechange = function(post, seq) {
      return function() {
         if (this.readyState != 4) return;
         if (this.status == 200) {
            var win= document.getElementById("vote"+post);
            win.innerHTML = this.responseText;

//            request = createRequest();
//            if (request == null) {
//               window.alert("Unable to create AJAX request");
//               return;
//               }
//            url = "toppostlistsfull.php";
//            request.open("GET",url,true);
//            request.onreadystatechange = reloadBestSinList;

            reloadstar(post);
            // reenable click event
            enableVoteEvents(seq);
            // Reload Top Posts List
            }
         };
      }(postnum, postseq);
   request.send(null);
   }

/*
function reloadBestSinList() {
               window.alert(request.status);
               if (request.readyState != 4) return;
               if (request.status == 200) {
                  window.alert(request.responseText);
                  var win= document.getElementById("bestsinlist");
                  win.innerHTML = request.responseText;
                  }
               }
*/
function enableVoteEvents(k) {
   for (i=1; i<=5; i++) {
      star = document.getElementById("stars" + i + k);
      if (star == null) break;
      star.onmouseout = function onmouseout() {
         javascript:starout(this)
         }
      star.onmouseover = function onmouseover() {
         javascript:starhover(this)
         }
      star.onclick = function onclick() {
         javascript:starclick(this)
         }
      }
   }

function disableVoteEvents(k) {
   for (i=1; i<=5; i++) {
      star = document.getElementById("stars" + i + k);
      if (star == null) break;
      star.onmouseout = "";
      star.onmouseover = "";
      star.onclick = "";
      }
   }

function reloadstar(post) {
   for (k=0; k<1000; k++) {
      starblock = document.getElementById("stars0" + k);
      if (starblock == null) break;
      newpost = starblock.attributes.getNamedItem('value').value;
      if (newpost == post) {
         note = starblock.attributes.getNamedItem('grade').value;
         formatStars(post, note, k);
         starblock.className = "stars";
         }
      }
   }



function checkemail(value) {
   var str=value
   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   if (filter.test(str)) return(true);
   else return(false);
   }
