var classic=false;
var text_focused=false;

function recolorVoteButtons(n,to)
{
    var nb="";
    var color, tcolor;
    if(to<2) {
        color="r";
        tcolor="#FFAAAA";
    } else if (to<4) {
        color="or";
        tcolor="#FFCCAA";
    } else if (to<7) {
        color="o";
        tcolor="#EEDDBB";
    } else if (to<10) {
        color="yg";
        tcolor="#DDFFBB";
    } else if (to==10) {
        color="bg";
        tcolor="#AAFFAA";
    } else {
        color="d";
        tcolor="#FFF5F5";
    }
    for(var i=0;i<11;i++) {
        if(i<(to+1)) {
            document.getElementById('vb_'+n+'_'+i).src="http://bnware.ndforums.com/l9/quotearchive/img/"+i+"_"+color+".png";
        } else {
            document.getElementById('vb_'+n+'_'+i).src="http://bnware.ndforums.com/l9/quotearchive/img/"+i+"_d.png";
        }
    }
    document.getElementById('vp_'+n).innerHTML="<span style=\"color:"+tcolor+";\">"+to+"/10</span>";
}

function recolorVoteButtons_new(n,to)
{
    var nb="";
    var tcolor, bcolor, fcolor;
    if(to<2) {
        tcolor="#FFBBBB";
        bcolor="#EEAAAA";
        fcolor="#995555";
    } else if (to<4) {
        tcolor="#FFCCAA";
        bcolor="#EEBB99";
        fcolor="#996644";
    } else if (to<7) {
        tcolor="#EEDDBB";
        bcolor="#DDCCAA";
        fcolor="#887755";
    } else if (to<10) {
        tcolor="#DDFFBB";
        bcolor="#CCEEAA";
        fcolor="#779944";
    } else if (to==10) {
        tcolor="#AAFFAA";
        bcolor="#99EE99";
        fcolor="#449944";
    } else {
        tcolor="#FFF5F5";
        bcolor="#EEE5E5";
        fcolor="#999595";
    }
    for(var i=0;i<11;i++) {
        if(i<(to+1)) {
            document.getElementById('vb_'+n+'_'+i).style.backgroundColor=bcolor;
            document.getElementById('vb_'+n+'_'+i).style.borderBottom="1px solid "+tcolor;
            if(i==to) {
                document.getElementById('vb_'+n+'_'+i).style.backgroundColor=tcolor;
            }
            document.getElementById('vb_'+n+'_'+i).style.color=fcolor;
        } else {
            break;
        }
    }
    document.getElementById('vp_'+n).innerHTML="<span style=\"color:"+tcolor+";\">"+to+"/10</span>";
}

function revertVoteButtons(n)
{
    for(var i=0;i<11;i++) {
        document.getElementById('vb_'+n+'_'+i).src="http://bnware.ndforums.com/l9/quotearchive/img/"+i+"_d.png";
    }
    document.getElementById('vp_'+n).innerHTML="";
}

function revertVoteButtons_new(n)
{
    for(var i=0;i<11;i++) {
        document.getElementById('vb_'+n+'_'+i).style.backgroundColor="#B5B5C5";
        document.getElementById('vb_'+n+'_'+i).style.borderColor="#B5B5C5";
        document.getElementById('vb_'+n+'_'+i).style.color="#666677";
    }
    document.getElementById('vp_'+n).innerHTML="";
}

function msg_alert(msg)
{
    if(!classic)
        unpress_button();
    else
        unpress_button_classic();
    document.getElementById('ab_text').innerHTML=msg;
    $('#alert_bg').fadeIn("fast");
    $('#ab_shadow').fadeIn("fast");
    $('#alertbox').fadeIn("fast");
}

function press_button()
{
    var a_b=document.getElementById('alert_button');
    a_b.style.paddingTop='5px';
    a_b.style.paddingBottom='4px';
    a_b.style.backgroundColor='#A5A5C5';
    a_b.style.borderTopColor='#9595B5';
    a_b.style.borderBottomColor='#A5A5C5';
    a_b.style.outlineColor='#8888A8';
}

function press_button_classic()
{
    var a_b=document.getElementById('alert_button_classic');
    a_b.style.paddingLeft='9px';
    a_b.style.paddingRight='7px';
    a_b.style.borderTopColor='#7F6F5F';
    a_b.style.borderLeftColor='#7F6F5F';
    a_b.style.borderRightColor='#EFDFCF';
    a_b.style.borderBottomColor='#EFDFCF';
}

function unpress_button()
{
    var a_b=document.getElementById('alert_button');
    a_b.style.paddingTop='4px';
    a_b.style.paddingBottom='5px';
    a_b.style.backgroundColor='#BFBFDF';
    a_b.style.borderTopColor='#BFBFDF';
    a_b.style.borderBottomColor='#CFCFEF';
    a_b.style.outlineColor='#AFAFCF';
}

function unpress_button_classic()
{
    var a_b=document.getElementById('alert_button_classic');
    a_b.style.paddingLeft='8px';
    a_b.style.paddingRight='8px';
    a_b.style.borderTopColor='#EFDFCF';
    a_b.style.borderLeftColor='#EFDFCF';
    a_b.style.borderRightColor='#7F6F5F';
    a_b.style.borderBottomColor='#7F6F5F';
}

function enter_unpress_button(ev)
{
    var kcode=(ev.which) ? ev.which : event.keyCode
    if(kcode==13) {
        if(!classic) 
            unpress_button();
        else
            unpress_button_classic();
    }
}

function close_ma()
{
    $('#alert_bg').fadeOut();
    $('#ab_shadow').fadeOut();
    $('#alertbox').fadeOut();
}

function enter_close_ma(ev)
{
    var kcode=(ev.which) ? ev.which : event.keyCode
    if(kcode==8) {
        if(!text_focused)
            history.back(1);
    } else if(kcode==13) {
        if(!classic)
            press_button();
        else
            press_button_classic();
        close_ma();
    }
}

function reload_quote(q,m)
{
    if(!document.getElementById('quote_'+q)) return;
    var ajax=new_ajax();
    var args="action=refresh&q="+q+"&m="+m;
    ajax.open("POST","action.php",true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", args.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange=function() {
        if(ajax.readyState==4) {
            document.getElementById('quote_'+q).innerHTML=ajax.responseText;
        }
    }
    ajax.send(args);
}

function ajax_action(q,args)
{
    if(args.length==0) return;
    var ajax=new_ajax();
    ajax.open("POST","action.php",true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", args.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange=function() {
        if(ajax.readyState==4) {
            msg_alert(ajax.responseText);
            reload_quote(q,'0');
        }
    }
    ajax.send(args);
}

function qvote(q,v)
{
    ajax_action(q,"action=vote&q="+q+"&v="+v);
}

document.onkeydown=enter_close_ma;
document.onkeyup=enter_unpress_button;
