/******************
 * postaction.js - ajax functionality for posts
 ******************
 */
    function thup(pid) {
        $('tu'+pid).src = '/imgs/tupress.gif';
        ajaxCheer(0,pid);
    }
    
    function thdn(pid) {
        $('td'+pid).src = '/imgs/tdpress.gif';
        ajaxCheer(1,pid);
    }
    
    function addFav(pid) { postAct(2,pid); }

    function flagAbuse(pid) { postAct(3,pid); }
    
    
    function ajaxCheer(cheerAct,postID) {
        userType = parseInt(userType);
        var qString = 'p=' + postID + '&c=' + cheerAct;
        new Ajax.Request('/ajax/postAction.cgi', {
            parameters: qString,
            onComplete: function(transport) {
                if (transport.status == 200) {
                     var voteCt = $('t'+cheerAct+postID).innerHTML;
                     if (voteCt == 'None') { voteCt = 0; }
                     else { voteCt = parseInt(voteCt); }//Numerifiy
                     voteCt = voteCt + userType; 
                     $('t'+cheerAct+postID).innerHTML = voteCt;
                 }
                 else if (transport.status == 403) {
                     var alertMsg = "You already voted on this post!\nYou can only vote once for each post";
                     if (userType != 2) { alertMsg = alertMsg + ", but if you want more clout, become a Soapbox Aristocrat!"; }
                     else { alertMsg = alertMsg + '.'; }
                     alert(alertMsg);
                 }
                 else if (transport.status == 401) {
                     var goLogin = confirm("You aren't logged in! - You must log in to vote on posts on Soapbox Nation\nWould you like to go to the log in page or create an account?\nIt's free!");
                     if (goLogin) { window.location = '/login.cgi'; }
                 }
                 else if (transport.status == 500) {
                     alert("We're so sorry... there's been an error trying to add your vote.\n Please try again!");
                     window.location = window.location;
                 }            
                unclick(cheerAct,postID);
            }
        });
    }
 
    function postAct(pAct,postID) {
        userType = parseInt(userType);
        var qString = 'p=' + postID + '&c=' + pAct;
        new Ajax.Request('/ajax/postAction.cgi', {
            parameters: qString,
            onComplete: function(transport) {
                if (transport.status == 200) {$('rfa'+postID).setStyle({backgroundColor:'#000066'});}
                else if (transport.status == 202) { $('rfa'+postID).setStyle({backgroundColor:'#770000'});}
                else if (transport.status == 401) {
                    if (pAct == 2) {
                        var goLogin = confirm("You aren't logged in! - You must log in to add favorite posts on Soapbox Nation\nWould you like to go to the log in page or create an account?\nIt's free!");
                        if (goLogin) { window.location = '/login.cgi'; }
                    }
                 }
                 else if (transport.status == 500) {
                     alert("We're so sorry... there's been an error trying to process your request.\n Please try again!");
                     window.location = window.location;
                 }
            }
        });
    }
    
    function delPost(pid,typ) {
        var wMsg;
        if (typ == 0) { wMsg = 'Post'; }
        else if (typ == 1) { wMsg = 'Shoutback'; }
        else { return; }
        if (!confirm('HEY!\n If you delete this '+wMsg+', you can never get it back again!\nAre you SURE you wish to PERMANENTLY delete this?')) { return; }
        var qString = 'p=' + pid + '&c=4';
        new Ajax.Request('/ajax/postAction.cgi', {
            parameters: qString,
            onComplete: function(transport) {
                if (transport.status == 200) {
                    alert(wMsg + ' Deleted');
                    if (typ == 0) { history.back(); }
                    else { window.location = window.location; }
                 }
                 else if (transport.status == 500) {
                     alert("We're so sorry... there's been an error trying to process your request.\n Please try again!");
                     window.location = window.location;
                 }
            }
        });

    }
 
    function unclick(cheerAct,pid) {
        var typ;
        if (cheerAct == 0) { typ = 'tu'; }
        else { typ = 'td'; }
        $(typ+pid).src = '/imgs/'+typ+'.gif';
    }
    
    function scrollSB(pid) {
        var sbID = 'sb' + pid;
        if (!$(sbID)) { return false; }
        else {
            Effect.ScrollTo($(sbID), { duration: 0.00 });
            window.setTimeout("new Effect.Highlight(sbtxt"+pid+", { startcolor: '#ffff99', endcolor: '#ffffff' });",200);
        }
        return false;
    }
    
    function expandSB(pid) { $('sbml'+pid).replace($('sbht'+pid).innerHTML).innerHTML; }

