/*
    main.js

        TODO:
                - mostFlushes()
                - mostFloats()
                - mostRecent()
                - clearEntries()
            
    
*/

var limit = 10;
var start_record = 0;

function ajaxCall() {
    // returns new XMLHttpRequest
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else {
    // old or new IE, I am not a fan
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}


function postPonderance() {
    var xmlhttp = ajaxCall();
    var ponderToSubmit = document.getElementById('ponder-text').value;
    
    xmlhttp.open("GET","ponderpost.php?ponderance="+ponderToSubmit, true);
    xmlhttp.send();
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState===4 && xmlhttp.status===200) {
            var postInfo = eval('(' + xmlhttp.responseText + ')');
            if (postInfo.success == true) {
                document.location="view.php?id=" + postInfo.id;
                // forward this nucka to their display page
                // if successful, postInfo.id is the page id to load
            }
            else if (postInfo.success == false) {
                // make a call to subModal library
                var errURL = "error.php?msg=" + postInfo.error;
                showPopWin(errURL, 400, 200, null);
                // display error
            }
            
        }
    }
}



function pullPonderances() {

    /* 
        TODO:       when there are no more entries, move #view-more
    */
    
    var arrEntries = undefined;
    var xmlhttp = ajaxCall();
    
    
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState===4 && xmlhttp.status===200) {
            arrEntries = eval('(' + xmlhttp.responseText + ')');
            //displayNewEntry(eval('(' + xmlhttp.responseText + ')'));
            for (var i=0; i<arrEntries.length; i++) {
                displayNewEntry(arrEntries[i]);
            }
        }
    }
    
    /* this returns an array of ponder objects, they are
        id      :   number of post (int)
        floats  :   float count (int)
        flushes :   flush count (int)
        thought :   ponder text (string)
        time    :   UNIX time stamp (TODO: format of this)
    */
    
    xmlhttp.open("GET","getponderances.php?start="+start_record+"&limit="+limit+"&sort=DESC",true);
    xmlhttp.send();
    start_record+=limit;
}

function displayNewEntry(varPonder) {
    /* var varPonder = {
                'id'        : '200',
                'floats'    : '2045',
                'flushes'   : '400',
                'thought'   : 'why do my feet smell so bad?',
                'time'      : '2011-03-03 23:54:03' 
                };
    */
    
    var divArticle  =   document.createElement("div");
    var pThought    =   document.createElement("p");
    var divVote     =   document.createElement("div");
    var linkUp      =   document.createElement("a");
    var linkDown    =   document.createElement("a");
    var linkView    =   document.createElement('a');
    
    
    linkUp.setAttribute('id', 'float-'+varPonder.id);
    linkUp.setAttribute('href', '#');
    linkUp.className        =   'floater-voter';
    linkUp.setAttribute('onClick', 'vote(0,' + varPonder.id + ', ' + varPonder.floats + '); return false;');
    linkUp.onclick = function() { vote(0,varPonder.id,varPonder.floats); };
    linkUp.innerHTML = "<img src='images/arrowup.png' />Floats (" + varPonder.floats + ")";
    
    linkDown.setAttribute('id', 'flush-'+varPonder.id);
    linkDown.setAttribute('href', '#');
    linkDown.className        =   'flusher-voter';
    linkDown.setAttribute('onClick', 'vote(1,' + varPonder.id + ', ' + varPonder.flushes + '); return false;');
    linkDown.onclick=function() { vote(1,varPonder.id,varPonder.flushes); };
    linkDown.innerHTML = "<img src='images/arrowdown.png' />Flushes (" + varPonder.flushes + ")";
    
    linkView.setAttribute('href', 'view.php?id=' + varPonder.id);
    linkView.className        =   'view-this-post';
    linkView.innerHTML = varPonder.time;
    
    divArticle.className    =   "article";
    
    pThought.innerHTML = '<span class="on-the-john">While on the john, I pondered</span> ';
    pThought.innerHTML += varPonder.thought;
    
    divVote.className       =   "float-flush-deets";
    divVote.appendChild(linkUp);
    divVote.appendChild(linkDown);
    divVote.appendChild(linkView);
    
    
    divArticle.appendChild(pThought);
    divArticle.appendChild(divVote);
    
    divArticle.setAttribute('id', 'ponderance-' + varPonder.id);
    
    document.getElementById("entries").appendChild(divArticle);
}
    
    
function vote(mode, id, currentVotes) {
// vote.php?mode=s&id=10
var varResponse;

var xmlhttp = ajaxCall();

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            varResponse = eval('(' + xmlhttp.responseText + ')');
            if (varResponse.hasOwnProperty('success')) {
                // success
                var linkToChange;
                if (mode==0) {
                    linkToChange = document.getElementById('float-' + id);
                    linkToChange.innerHTML = "<img src='images/arrowup.png' />Floats (" + (currentVotes+1) + ")";
                }
                
                else if (mode==1) {
                    linkToChange = document.getElementById('flush-' + id);
                    linkToChange.innerHTML = "<img src='images/arrowdown.png' />Flushes (" + (currentVotes+1) + ")";
                }
                
                
            }
            
            else if (varResponse.hasOwnProperty('error')) {

               // announce error (for now, alert)
                var errURL = "error.php?msg=" + varResponse.error;
                showPopWin(errURL, 400, 200, null);                
            }
            
        }
    }
    
xmlhttp.open("GET","vote.php?mode="+mode+"&id="+id,true);
xmlhttp.send();
} // flush


/* * * * * * * * * * * * * * * * * * * * * * * * * * *
  function stubs, TODO
 * * * * * * * * * * * * * * * * * * * * * * * * * * */
function clearEntries() {
    start_record = 0;
}



function mostFlushes() {}



function mostFloats() {}



function mostRecent() {}



