/******************
 * tags.js
 ******************
 */
var tgIF;
var lastTID = new Array();

function saveTag(pid,tagTxt) {
    if (!pid || !tagTxt) { return; }
    if (tagTxt.match(/,/)) { alert('Commas are not allowed in tags, Please only add one tag at a time.'); return; }
    tagTxt = tagTxt.replace(/[",]/g,'');
    var qString = 'p=' + pid + '&t=' + escape(tagTxt);
    new Ajax.Request('/ajax/tag.cgi', {
        parameters: qString,
        onComplete: function(transport) {
            if (transport.status == 200) {
                addTag(pid);
                $('tags'+pid).update(transport.responseText).innerHTML;
                new Effect.Highlight($('tags'+pid), { startcolor: '#ffff99', endcolor: '#ffffff' });
             }
            else if (transport.status == 403) {
                addTag(pid);
            }
             else if (transport.status == 401) {
                if (pAct == 2) {
                    var goLogin = confirm("You aren't logged in! - You must log in to tag 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 addTag(pid) {
    var pnDisp = $('tgtx'+pid).getStyle('display'); tgIF = $('tgtf' + pid);
    if (pnDisp == 'none') {
        if ($('rmtd'+pid).getStyle('display') != 'none') { togRT(pid); }
        Effect.SlideDown('tgtx'+pid, { duration: 0.15 });
        window.setTimeout("tgIF.focus();",16);
    }
    else if (pnDisp != 'none') { Effect.SlideUp('tgtx'+pid, { duration: 0.15 }); tgIF.value = ''; }
}

    function togRT(pid) {
        if ($('tgtx'+pid).getStyle('display') != 'none') { addTag(pid); }
        var spID = $('rmtd'+pid);
        Effect.toggle(spID,'slide',{duration:0.25});
        if (spID.getStyle('display') != 'none') { if (lastTID[pid]) { $(lastTID[pid]).setStyle({backgroundColor:''}); }}
        else { if (lastTID[pid]) { $(lastTID[pid]).setStyle({backgroundColor:'#DEDEDE'}); } }
    }
    
    function showRmTag(pid,tnum) {
        var spID = $('rmtd'+pid); if (spID.getStyle('display') == 'none') { return; }
        var parentTag = $('t'+pid+'a'+tnum);
        spID.setStyle({left: parentTag.offsetLeft + 'px', width:'50px'}); 
        $('rmtb'+pid).update('<a class="deltag" href="#" onclick="rmTag('+pid+','+tnum+');return false;"><b>&times; Remove </b></a>').innerHTML;
        if (lastTID[pid]) {$(lastTID[pid]).setStyle({backgroundColor:''}); }
        parentTag.setStyle({backgroundColor:'#DEDEDE'});
        lastTID[pid] = 't'+pid+'a'+tnum;
    }
    
    function rmTag(pid,tnum) {
        var parentTag = $('t'+pid+'a'+tnum);
        var tagTxt = parentTag.innerHTML.replace(/[",]/g,'');
        if (!pid || !tagTxt) { return; }
        var qString = 'a=1&p=' + pid + '&t=' + escape(tagTxt);
        new Ajax.Request('/ajax/tag.cgi', {
            parameters: qString,
            onComplete: function(transport) {
                if (transport.status == 200) {
                    togRT(pid);
                    $('tags'+pid).update(transport.responseText).innerHTML;
                    new Effect.Highlight($('tags'+pid), { startcolor: '#ffff99', endcolor: '#ffffff' });
                    lastTID[pid] = undefined;
                } else {
                     alert("We're so sorry... there's been an error trying to process your request.\n Please try again!");
                     window.location = window.location;
                }
            }
        });
    }
