
prt = {};

prt.init = function() {
    var newimg = function(src) {
        var img = new Image();
        img.src = src;
        return img;
    }
    var i1 = newimg(MEDIA_URL + 'upmod.png');
    var i2 = newimg(MEDIA_URL + 'downmod.png');
};

prt.get_display_value = function(vote_value, previous_display) {
    var display_value = 0;
    if (previous_display == 0) {
        display_value = vote_value;
	} else if (previous_display == vote_value) {
        display_value = 0;
    } else if (previous_display != vote_value) {
        display_value = vote_value;
    }
    return display_value;
};

prt.get_votebox = function(id) {
    return {
        id: id,
        up: $('#up_' + id).get(0),
        down: $('#down_' + id).get(0),
        score: $('#score_' + id).get(0),
    };
};

prt.get_previous_vote = function(votebox) {
    var current_vote = 0;
    if (votebox.up.className.match("vote upmod")) {
        current_vote = 1;
    } else if (votebox.down.className.match("vote downmod")) {
        current_vote = -1;
    }
    return current_vote;
};

prt.process_vote = function(id, display_value) {
    $.post('/ajax/', {'action':'vote', 's':id, 'n':display_value })
};

prt.set_display_score = function(votebox, vote_value, previous_value) {
    var delta = 0;
    if (votebox.score) {
        var score_val = votebox.score.innerHTML;
        if (vote_value == previous_value) {
            delta = -1*vote_value; //vote was removed
        } else {
            delta = vote_value - previous_value;
        }
        score_val = parseInt(score_val) + delta;
        votebox.score.innerHTML = score_val;
    }
};

prt.set_display_arrows = function(votebox, display_value) {
    if (display_value == 1) {
        votebox.up.className = "vote upmod";
        votebox.up.src = MEDIA_URL + "upmod.png";
        votebox.down.className = "vote down";
        votebox.down.src = MEDIA_URL + "down.png";
    } else if (display_value == 0) {
        votebox.up.className = "vote up";
        votebox.up.src = MEDIA_URL + "up.png";
        votebox.down.className = "vote down";
        votebox.down.src = MEDIA_URL + "down.png";
    } else if (display_value == -1) {
        votebox.up.className = "vote up";
        votebox.up.src = MEDIA_URL + "up.png";
        votebox.down.className = "vote downmod";
        votebox.down.src = MEDIA_URL + "downmod.png";
    }
};

prt.vote = function(id, vote_value) {
    if (LOGGED) {
        var votebox = prt.get_votebox(id);
        var previous_display = prt.get_previous_vote(votebox);
        var display_value = prt.get_display_value(vote_value, previous_display);
        prt.process_vote(id, display_value);
        prt.set_display_arrows(votebox, display_value);
        prt.set_display_score(votebox, vote_value, previous_display);
    } else {
        window.location = '/accounts/login/';
    }
};

prt.logout = function() {
    $('#logoutform').get(0).submit();
};

prt.checksubmit = function() {
    var url = $('#id_url').get(0);
    var title = $('#id_title').get(0);
    if (url && title) {
        var description = $('#id_description').get(0);
        var urlerror = $('#urlerror').get(0);
        var titleerror = $('#titleerror').get(0);
        if (url.value == '') {
            urlerror.innerHTML = LANG.urlmissing;
            return false;
        } else {
            urlerror.innerHTML = '';
        }
        if (title.value == '') {
            titleerror.innerHTML = LANG.titlemissing;
            return false;
        } else {
            titleerror.innerHTML = '';
        }
    }
}

prt.checktalk = function() {
    txt = $('#id_comment').get(0);
    error = $('#talkerror').get(0);
    if (error.innerHTML != '') error.innerHTML = ''
    if (txt == null) { return false; }
    if (!LOGGED) {
        error.innerHTML = LANG.commentlogin;
        return false;
    }
    else if (txt.value == '') {
        error.innerHTML = LANG.missingtext;
        txt.focus();
        return false;
    }
    else if (txt.value.length > 10000) {
        error.innerHTML = LANG.toomuchtext;
        txt.focus();
        return false;
    }
    else {
        if (error.innerHTML != '') error.innerHTML = ''
    }
    return true;
};

$(document).ready(function() {
    $('#logoutlink').bind('click', prt.logout);
    $('#submitform').bind('submit', prt.checksubmit);
    $('#commentform').bind('submit', prt.checktalk);
    $('#login-popup').jqm({overlay:70});
    prt.init();
});
