var ratings = new Array;

var rating_handlerFunc = function(t) {
	var res = t.responseText.split(",");
	var id = res[0];
	var index = res[1];
	var message = res[2];
	document.getElementById('votediv_' + id).innerHTML = message;
	document.getElementById('currentrating_' + id).style.width = (Math.round(index)*25)+'px';
}

var errFunc = function(t) {
    alert('Din stemme kunne ikke registreres.');
}

function rating_vote(id, index) {
	var q = rating_getcookie('blogquarantine');
	if (q.length > 0) {
        alert('Du har fået midlertidig karantæne for misbrug af rating funktionen.');
        return;
	}
	if (getCookie('tv2login') == null) {
        alert('Du skal være logget på TV 2 Login for at bruge rating funktionen.');
        return;
	}
    if (rating_havevoted(id)) {
        alert('Du har allerede afgivet din stemme.');
        return;
    }
    document.location.href = rating_url+'?entryid='+id+'&rating='+index;
}

function rating_getcookie(name) {
    var _vote_cookies = document.cookie.split('; ');
    for (var i=0; i<_vote_cookies.length; i++) {
       var _vote_c = _vote_cookies[i];
       var _vote_cc = _vote_c.split('=');
       if (_vote_cc[0] == name) {
           return unescape(_vote_cc[1]);
       }
    }
    return '';
}

function rating_havevoted(id) {
    var cookie = rating_getcookie('blogentryrating');
    var _vote_cids = cookie.split(',');
    for (var ci=0; ci<_vote_cids.length; ci++) {
        if (_vote_cids[ci] == id) {
            return true;
        }
    }
    return false;
}

function rating_setcookie(id) {
    var cookie = rating_getcookie('blogentryratings');
    var _vote_cids = cookie.split(',');
    if (_vote_cids.include(id)) {
        return
    }
    _vote_cids.push(id);
    _vote_cids = _vote_cids.slice(-10);
    var date = new Date();
	date.setTime(date.getTime()+(3*24*60*60*1000));
	var value = 'blogentryratings='+escape(_vote_cids.join(','))+'; expires='+date.toGMTString()+'; domain=tv2.dk; path=/';
    document.cookie = value;
}
