function sndReq(vote, id_num)
{
	$('#star_rating_ul').html('<div class="loading"></div>');
	$.ajax({
		type: 'GET',
		url: '/process_rating_db.php',
		data: ({
			vote: vote,
			vid: id_num,
			redirect: 'n'
		}),
		success: function(xhr) {
                    var response = xhr;
                    var update = new Array();
                    if(response.indexOf('|') != -1) {
                        update = response.split('|');
                        changeText(update[0], update[1]);
                        //ratingDescTmp = ratings[update[2]-1];
                        displayMsg(update[2]);
                    }
		}
	});
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0;
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    } else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

function ajaxStarRatingSystem(theVote, theratingID){
    sndReq(theVote,theratingID);
}

//this one works by the numbers calculated using rating data
function displayMsg(tab){
    document.getElementById('TabBlock3').innerHTML = tab;
    ratingDescription = document.getElementById('ratingDesc');
    if(tab>0){
        ratingDescription.innerHTML = '';

        if(tab >= '0' && tab <= '18'){
            msg = 'Poor';
        }else if(tab >= '19' && tab <= '36'){
            msg = 'Nothing special';
        }else if(tab >= '37' && tab <= '54'){
            msg = 'Worth watching';
        }else if(tab >= '55' && tab <= '72'){
            msg = 'Pretty cool';
        }else if(tab >= '73' && tab <= '90'){
            msg = 'Awesome!';
        }

        ratingDescription.innerHTML = msg;
    }else{
        ratingDescription.innerHTML = ratingDescTmp;
    }
}

// this one works by position
function displayRatingMsg(tab){
    ratingDescription = document.getElementById('ratingDesc');
    if(tab>0){
        ratingDescription.innerHTML = '';
        switch(tab){
            case 1:
                msg = 'Poor';
                break;
            case 2:
                msg = 'Nothing special';
                break;
            case 3:
                msg = 'Worth watching';
                break;
            case 4:
                msg = 'Pretty cool';
                break;
            case 5:
                msg = 'Awesome!';
                break;
            default:
                msg = 'Poor';
                break;
        }

        ratingDescription.innerHTML = msg;
    }else{
        ratingDescription.innerHTML = ratingDescTmp;
    }
}

function registerToVote(){
    registerHtml = '<a href="/getiton.php">Sign In to Rate!</a>';
    document.getElementById('ratingDesc').innerHTML = registerHtml;
    ratingDescTmp = registerHtml;
}

$(document).ready(function() {
    $("#star_rating_ul").find("li").each(function(){
        $(this).hover(
          function () {
            switch(parseInt($(this).attr("id"))){
                case 0:
                case 1:
                    $("#ratingDesc").html("poor");
                break;
                case 2:
                    $("#ratingDesc").html("Nothing special");
                break;
                case 3:
                    $("#ratingDesc").html("Worth watching");
                break;
                case 4:
                    $("#ratingDesc").html("Pretty cool");
                break;
                case 5:
                    $("#ratingDesc").html("Awesome!");
                break;
                default:
                    $("#ratingDesc").html("poor");
            }
          },
          function () {
            $("#ratingDesc").html("poor");
          }
        );
    });
});
