var currentRatingLength = 30;
var last_days = 0;
var last_type = 0;
function changeType(type)
{
    loadRatings(type, last_days);
}

function changeDays(daysCount)
{
    loadRatings(last_type, daysCount);
}

function buildButtons(type, daysCount)
{
    var text = "";
    var typesBlock = dojo.byId("typesBlock");
    var daysBlock = dojo.byId("daysBlock");
    typesBlock.innerHTML = "";

    for(var i=0; i<types.length; i++)
    {
        if(type != types[i])
        {
            text = "<span class=\"grey\"><span><span><a href=\"javascript:changeType('" + types[i] + "');\" class=\"nounder\">" + texts[types[i]] +  "</a></span></span></span>";
        }
        else
        {
            text = "<span class=\"grey\"><span><span>" + texts[types[i]] + "</span></span></span>";
        }
        typesBlock.innerHTML += ((i>0) ? "&nbsp;" : "")+text;
    }

    daysBlock.innerHTML = to;
    for(var i=0; i<days.length; i++)
    {
        if(daysCount != days[i])
        {
            text = "<span id=\"menu_" + days[i] + "\"><a href=\"javascript:changeDays(" + days[i] + ");\">" + texts[days[i]] + "</a></span>";
        }
        else
        {
            text = "<span id=\"menu_" + days[i] + "\">" + texts[days[i]] + "</span>";
        }
        daysBlock.innerHTML += ((i>0) ? " | " : "")+text;
    }
}

/*
function showRating(type, len)
{
    if (len==0)len = currentRatingLength;
    currentRatingLength = len;
    
	var days = new Array(1, 7, 30);
	
	var display = "";
	for(var i=0; i<types.length; i++)
	{
		display = (types[i]==type) ? "block" : "none";
		text = (types[i]==type) ? texts[types[i]] : "<a href=\"javascript:showRating('"+types[i]+"', 0);\" id=\"nounder\">"+texts[types[i]]+"</a>";		
		
		dojo.style(types[i], "display", display);		
		dojo.byId("menu_"+types[i]).innerHTML = text;
		
		for(var j=0; j<days.length; j++)
		{
			var id = types[i]+"_"+days[j];
			display = (id == type+"_"+len) ? "block" : "none";
			text = (id == type+"_"+len) ? texts[days[j]] : "<a href=\"javascript:showRating('"+types[i]+"', "+days[j]+");\">"+texts[days[j]]+"</a>";
			dojo.style(id, "display", display);
			dojo.byId("menu_"+types[i]+"_"+days[j]).innerHTML = text;
		}
	}
}
*/
function loadRatings(type, daysCount)
{
    if(!ratings[type] || !ratings[type][daysCount])
    {
        return;
    }

    buildButtons(type, daysCount);
    last_days = daysCount;
    last_type = type;
    var ratingBlock = dojo.byId("ratingsBlock");
    ratingBlock.innerHTML = "";
    var maxNumItems = 100;
    if (window.ad_homePage==true) {
        if (type=='tracks')
            maxNumItems = 5;
        else
            maxNumItems = 3;
        var height = 355;
        dojo.query('.songRatingBlock').style('height', '50px');
        dojo.query('#best-rating-block').style('height', height+'px');
        dojo.query('#best-rating-block .content').style('height', (height-20)+'px');
    }
    for(var j=0; j<ratings[type][daysCount].length && j<maxNumItems; j++)
    {
        data = ratings[type][daysCount][j];
        var blockText = "";
        var i = j+1;
        if(type == 'tracks')
        {
            blockText = "<div class=\"songRatingBlock\">";
            blockText += "<a href=\"javascript:mp3Player.playSong('" + data[0] + "',0,0,true)\" class=\"play\"><img src=\"/_front/images/icons/play.gif\"/></a>";
            blockText += "<span style=\"display: block\"><b>" + i + ".</b>&nbsp;<strong class=\"songName\">" + data[1] + "</strong> (" + data[2] + ")</span>";
            blockText += "<div class=\"smallArts\">" + by + "&nbsp;<span class=\"songArtists\">" + data[3] + "</span></div></div>";
        }
        else if(type == 'artists')
        {
            blockText = "<table class=\"artRatingTable\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
            blockText += "<td class=\"cov\"><a href=\"" + data[2] + "\" id=\"nounder\">";
            blockText += "<img height=\"128\" width=\"128\" src=\"" + data[0] + "\" style=\"width:64px;height:64px;\" class=\"artistSmallBorder\" alt=\"" + data[1]+ "\" title=\"" + data[1] + "\" /></a></td>";
            blockText += "<td><a href=\"" +data[2]+ "\" id=\"nounder\">" + data[1] + "</a></td>";
            blockText += "</tr></table>";
        }
        else
        {
            blockText = "<table class=\"ratingBlock albrate\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
            blockText += "<td class=\"cov\">";
            blockText += "<a href=\"" + data[5] + "\"><img height=\"128\" width=\"128\" src=\"" + data[0] + "\" style=\"height:64px;width:64px;\" class=\"albumBorder\" alt=\"" + data[1] + "\" title=\"" + data[1] + "\" /></a>";
            blockText += "</td><td>";
            blockText += "<a class=\"albName nounder\" href=\"" + data[5] + "?norating=true\">" + data[1] + "</a><br/>";
            if(data[3] == 'album' || data[3] == 'one')
            {
                blockText += "<span class=\"albumProp smallArts\">" + by + " " + data[4] +"</span>";
            }
            blockText += "<div class=\"albPlay\"><a href=\"javascript:mp3Player.playAlbum('" + data[2] + "',0,0,true)\"><img src=\"/_front/images/icons/play.gif\" class=\"trackPlay\" /></a></div>";
            blockText += "</td></tr></table>";
        }

        ratingBlock.innerHTML += blockText;
        i++;
    }    
	ratings_js();
//	alert(1);
}



function ratings_js() {
//    sLoader.addOnLoad(function() {
        var trackTextSizer = new textSizeUtil({fontSize: '13px'});
         dojo.query('.songRatingBlock .songName').forEach(function(el, k){
             var maxWidth = 170;
             var text = el.innerHTML;
             if (text.length>9) {
                 var newText = text;
                 for(var pos=10;pos<=text.length;pos++) {
                    newText = text.substring(0, pos);
                    var w = trackTextSizer.getTextWidth(newText)
                    if (w>maxWidth) {
                        break;
                    }
                 }
                 if (newText.length<text.length) {
                     text=newText+'...';
                 }
                 el.title = el.innerHTML;
                 el.innerHTML = text;
             }
           //alert(info);
       });
       try {
       dojo.query('.songRatingBlock .songArtists').forEach(function(el, k){
           var artists = el.innerHTML;
           var loverArtists = artists.toLowerCase();
           var pos = loverArtists.indexOf("</a>");

           var re= /<\S[^><]*>/g
           var title = artists.replace(re, "");
           if (pos!=-1) {
               artists = artists.substring(0, pos+4);
               if (loverArtists.indexOf("feat")!=-1) {
                   artists += ' feat...';
               }
               else if (loverArtists.indexOf('</a>', pos+4)!=-1) {
                   artists += ' ...';
               }
           }
           el.title = title;
           el.innerHTML = artists;

       });
       }
       catch(e) {
       }

       dojo.query('.ratingBlock .albName').forEach(function(el, k){
           var text = el.innerHTML;

               var re= /<\S[^><]*>/g
               el.title = el.innerHTML.replace(re, "");
               var length = 55;
               if (el.innerHTML.length>length) {
                    el.innerHTML = el.innerHTML.substring(0, length-3)+'...';
                }

       });

        dojo.query('.ratingBlock .albumProp.smallArts').forEach(function(el, k){
           var artists = el.innerHTML;
           var loverArtists = artists.toLowerCase();
           var pos = loverArtists.indexOf("</a>");

           var re= /<\S[^><]*>/g
           var title = artists.replace(re, "");
           if (pos!=-1) {
               artists = artists.substring(0, pos+4);
               if (loverArtists.indexOf("feat")!=-1) {
                   artists += ' feat...';
               }
               else if (loverArtists.indexOf('</a>', pos+4)!=-1) {
                   artists += ' ...';
               }
           }
           el.title = title;
           el.innerHTML = artists;
       });


//    });
}

