﻿var img;
var opacity;
var points;

function initStars(star)
{
	setStars(star);
}

function fade()
{   
	if(opacity < .1)
	{
		//img.style.opacity = 0;
		//img.style.filter = "alpha(opacity=" + 0 + ")";
		d.body.removeChild(img);
		
		//show the updated points here?
		if(points != "--")
        {
            showPoints(points);
        }
		return;
	}
	
	img.style.opacity = opacity;
	img.style.filter = "alpha(opacity=" + opacity * 100 + ")";
	opacity = opacity - .03;
	img.style.top = (img.style.top.substring(0,img.style.top.length - 2) - 3) + "px";
	setTimeout(fade, 15);
}

function addRating(rating)
{
    var hid = d.getElementById("hidSongId");
    var song_id = hid.value;
    
    //get user id
    var isc_key = readCookie("isc_key");
    
    if(!isc_key)
    {
        initStars(0);
        openDialog("login");
        return;
    }
    
    $.ajax({
        type: "POST",
        url: "php/action.php",
        data: "action=addRating&isc_key=" + isc_key + "&song_id=" + song_id + "&rating=" + rating,
        success: function(msg)
        {
            rating_callback(msg);
        }
    });
}

function rating_callback(xml)
{
	var xmlDoc = xml;
    
    var avg = xmlDoc.getElementsByTagName("avg")[0];
    var votes = xmlDoc.getElementsByTagName("votes")[0];
    
    avg = avg.firstChild.nodeValue;
    
    d.getElementById("spanAvg").firstChild.nodeValue = roundAvg(avg);
    
    var votes_text = votes.firstChild.nodeValue;
    
    var ratings = "ratings";
    
    if(votes_text == "1")
    {
        ratings = "rating";
    }
    
    d.getElementById("spanVotes").firstChild.nodeValue = votes_text + " " + ratings;
    
    var iscp = getXmlNode(xmlDoc, "iscp");
    points = getXmlNode(xmlDoc, "points");
    
    if(iscp == "true")
	{
	    if(d.getElementById("img"))
	    {
		    d.body.removeChild(document.body.removeChild(document.getElementById("img")));
	    }
	    
	    //create the image
	    img = document.createElement("img");
	    img.id = "img";
	    img.src = "img/plus_1.png";
	    img.style.position = "absolute";
	    img.onload = animatePoint();
	}
}

function animatePoint()
{
    var coords = findPos(d.getElementById("spanRating"));

    img.style.top = coords[1] - 20 + "px";
    img.style.left = coords[0] + 70 + "px";
	
    d.body.appendChild(img);
    opacity = 1;
    fade();
}

var sMax; // Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

function rating(num)
{
     sMax = 0; // Isthe maximum number of stars
     for(n=0; n<num.parentNode.childNodes.length; n++)
     {
         if(num.parentNode.childNodes[n].nodeName.toLowerCase() == "a")
         {
            sMax++;
         }
     }
     
     s = num.id.replace("_", ''); // Get the selected star
     a = 0;
     for(i=1; i<=sMax; i++)
     {
         if(i<=s)
         {
             document.getElementById("_"+i).className = "on";
             holder = a+1;
             a++;
        }
        else
        {
            document.getElementById("_"+i).className = "";
        }
    }
}

function off(me)
{
     if(!preSet)
     {
         for(i=1; i<=sMax; i++)
         {
            document.getElementById("_"+i).className = "";
         }
     }
     else
     {
         rating(preSet);
     }
}

// When you actually rate something
function rateIt(me)
{
    preSet = me;
    sendRate(me);
    rating(me);
}

function sendRate(sel)
{
    var s = sel.id.replace("_", ''); // Get the selected star
    addRating(s);
}

function setNewStar(val)
{
    var star = document.getElementById("_" + val);
    rating(star);
}

function setStars(val)
{
    if(val == 0)
    {
        var star = d.getElementById("_1");
        rating(star);
        
        //now set the class of the first star
        star.className = "";
    }
    else
    {
        var star = document.getElementById("_" + val);
        rating(star);
    }
}

	    
