﻿var img;
var opacity;
var points;

//shortcut
var d = document;

function initStars(star)
{
    /*//set the width
	d.getElementById("stars").style.width = star*20 + "%";
	d.getElementById("stars").style.lineHeight = "10px"; 
	d.getElementById("stars").style.height = "10px"; 
	d.getElementById("stars").style.backgroundPosition = "center left";
	
	if(d.all)
	{      
	    //alert("init ie " + star);
	    //d.getElementById("stars").style.width = "0%";
	    d.getElementById("stars").style.backgroundPosition = "100% 100%";
	    
	    //alert(star);
	    //alert(star*20);
	    d.getElementById("stars").style.width = star*20 + "%";
	    d.getElementById("stars").style.lineHeight = "10px"; 
	    d.getElementById("stars").style.height = "10px"; 
	    
	    
		//d.getElementById("stars").style.backgroundAttachment = "fixed";
	}*/
	
	setStars(star);
}

/*function setStar(star)
{
    //make sure user is logged in
    //get user id
    var user_name = readCookie("isc_key");
    
    if(!user_name || user_name == "anonymous")
    {
        showLogin();
        return;
    }
    
    //set the width
	d.getElementById("stars").style.width = star*20 + "%";
	d.getElementById("stars").style.lineHeight = "10px"; 
	d.getElementById("stars").style.height = "10px"; 
	d.getElementById("stars").style.backgroundPosition = "center left";

	if(d.all)
	{      
	    initStars(0);
	    initStars(star);
	    //alert("set ie " + star);
	    d.getElementById("stars").style.backgroundPosition = "100% 100%";
	    
	    //alert(star);
	    //alert(star*20);
	    d.getElementById("stars").style.lineHeight = "10px"; 
	    d.getElementById("stars").style.height = "10px"; 
	    d.getElementById("stars").style.width = star*20 + "%";
	    
	    //d.getElementById("stars").style.backgroundAttachment = "fixed";
	}
	
	//make ajax call to add rating to db
	addRating(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)
    {
        //do login
        //TODO: reset the stars to 0
        //setStars(0);
        initStars(0);
        showLogin(false);
        return;
    }
    
    var url = "http://" + getDomain() + "/addRating.php";
    
    var params = new ajaxParameters();
    params.add("isc_key", isc_key);
    params.add("song_id", song_id);
    params.add("rating", rating);
    
    //set placeholders for avg and votes
    d.getElementById("spanAvg").firstChild.nodeValue = "--";
    d.getElementById("spanVotes").firstChild.nodeValue = "--------";
	
    makeAjaxPOSTRequest(url, params, rating_callback, true);
}

function rating_callback(xml)
{
    //alert(xml);
    var xmlDoc = PrepareXML(xml);
    
    var avg = xmlDoc.getElementsByTagName("avg")[0];
    var votes = xmlDoc.getElementsByTagName("votes")[0];
    
    //alert(avg.firstChild.nodeValue + ", " + votes.firstChild.nodeValue);
    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(points != "--")
    {
        //showPoints(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();
	}
	
	showNoRate(false);
}

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);
    }
}

	    