﻿/// <reference path="jquery-1.3.2.min.js" />
/// <reference path="globalfuncs.js" />

/* This is a JSON class for the song list.
	----Internal:
	* topSongsUrl			- url of top songs api
	* albumSongsUrl			- url of album songs api
	* listFrom				- either local, worldwide, or album
	* scroller				- scrolling timeout variable
	* createList(xml)		- function to recreate list from xml data
	
	----External:
	* songsToShow			- number of songs to show at a time (default 10)
	* scrollTo(songRow)		- function to scroll to specific row
	* scrollUp(px)			- function to scroll up - will continuously scroll if no pixel amount is given
	* scrollDown(px)		- function to scroll down - will continuously scroll if no pixel amount is given
	* stopScroll(songRow)	- function to stop continouous scrolling
	* refresh(type, params)	- function to refresh list only if needed
	* load(type, params)	- function to always refresh list
	
		* types: 'local', 'worldwide', 'album'
		
		* params for 'local':
			latStart, latStop, longStart, longStop
			
		* params for 'album':
			artistName, albumName
*/
var songList = {
	topSongsUrl: "/Handlers/WebFetch.aspx?getUrl=" + encodeURIComponent("http://lyrics.tunewiki.com/tunewiki/services/getMusicChartsByGenreXML?type=songs&device=6"),
	albumSongsUrl: "/Handlers/WebFetch.aspx?getUrl=" + encodeURIComponent("http://lyrics.tunewiki.com/tunewiki/services/muze_music_search?type=albumtracks&device=6"),
	artistSongsUrl: "/Handlers/WebFetch.aspx?getUrl=" + encodeURIComponent("http://lyrics.tunewiki.com/tunewiki/services/getRecommendationsXML?type=topSongsForArtist&device=6"),
	albumArtist: "",
	listFrom: "worldwide",
	scroller: "",
	preloaded: false,
	songsToShow: 0,
	createList: function(xmlData) {
		var xml;
		if ($.browser.msie) { // this is why we hate IE
			try {
				xml = new ActiveXObject('Microsoft.XMLDOM');
				xml.async = false;
				xml.loadXML(xmlData);
			} catch (err) {
				xml = xmlData;
			}
		} else {
			xml = xmlData;
		};

		//defaults
		var rowHtml = "";
		var i = 1;
		var artist = songList.albumArtist;
		var song = "";
		var duration = "";
		var element = "";
		var songs = [];

		$("#top50Links").html("");
		$("#top50Container").html("");

		// xml parsing / building
		if (songList.listFrom == "album") {
			element = "track";
		} else if (songList.listFrom == "artist") {
			element = "recommendation";
		} else {
			element = "song";
		}
		$(xml).find(element).each(function() {
			if (songList.listFrom != "album") {
				artist = $(this).find("artist").text();
			} else {
				duration = $(this).find("duration").text();
			}

			song = $(this).find("title").text();

			rowHtml += "<div class='top50Row'><div style='width:12px;' class='container'>";
			rowHtml += "<a href=\"javascript:twVideoPlayer.play('";
			rowHtml += twVideoPlayer.formatName(artist);
			rowHtml += "', '";
			rowHtml += twVideoPlayer.formatName(song);
			rowHtml += "')\">"
			rowHtml += "<img src='/static_images/ui/assets/icon_play_white_sm.gif' alt='' />";
			rowHtml += "</a></div><div class='container'><div class='cell rank'>";
			rowHtml += ("00" + i).substr(("00" + i).length - 2, 2);
			rowHtml += "</div></div>";
			if (songList.listFrom != "album") { // Artist Div ----------------------------------------
				rowHtml += "<div class='container'><div class='cell' style='width:90px;'>";
				rowHtml += "<a href=\"/lyrics/";
				rowHtml += toFriendlyUrl(artist);
				rowHtml += ".aspx\">";
				rowHtml += toTitleCase(artist);
				rowHtml += "</a></div></div>"
			} //----------------------------- Title Div -----------------------------------------
			rowHtml += "<div class='container'><div class='cell' style='width:" + (songList.listFrom != "album" ? "124px" : "174px") + ";'>";
			rowHtml += "<a href=\"/lyrics/"
			rowHtml += toFriendlyUrl(artist);
			rowHtml += "/";
			rowHtml += toFriendlyUrl(song);
			rowHtml += ".aspx\">"
			rowHtml += toTitleCase(song);
			rowHtml += "</a></div></div>"
			if (songList.listFrom == "album") { // Duration Div --------------------------------------
				rowHtml += "<div class='container'><div class='cell' style='width:40px;'>";
				rowHtml += parseInt(duration / 60) + ":" + ("00" + parseInt(duration % 60)).substr(("00" + parseInt(duration % 60)).length - 2, 2);
				rowHtml += "</div></div>"
			}
			rowHtml += "</div>";
			songs.push([artist, song]);
			i++;
		});
		// add in resize
		$("#top50Container").height(songList.songsToShow * 20);
		$("#top50Container").html(rowHtml);

		// add in hover
		$(".top50Row").hover(function() { $(this).addClass("top50RowHighlighted"); }, function() { $(this).removeClass("top50RowHighlighted"); });

		$(".top50Control").show();
		
		twVideoPlayer.init({playlist:songs});
	},
	refresh: function() {
		if (this.listFrom == "local" && arguments.length >= 4) {
			this.load("local", arguments[0], arguments[1], arguments[2], arguments[3]);
		} else if (this.listFrom == "album" && arguments.length >= 2) {
			this.load("album", arguments[0], arguments[1]);
		} else if (this.listFrom == "artist" && arguments.length >= 1) {
			this.load("artist", arguments[0]);
		}
	},
	// load based on arguments and type
	load: function() {
		this.listFrom = arguments[0];
		$('#top50Container').prepend("<div style='margin:5px;'><img src='/static_images/ui/assets/ajax_load.gif' alt='Loading...'  /></div>");
		$("#top50Control").hide();
		if (this.listFrom == "local" && arguments.length >= 5) {
			$.get(this.topSongsUrl + encodeURIComponent('&latStart=' + arguments[1] + '&latStop=' + arguments[2] + '&longStart=' + arguments[3] + '&longStop=' + arguments[4]), this.createList, "xml");
			$('.top50Headline').text("Top Songs Today From Map:");
			$('.top50Artist').show();
			$('.top50Duration').hide();
		} else if (this.listFrom == "worldwide") {
			$.get(this.topSongsUrl, this.createList, "xml");
			$('.top50Headline').text("Top Songs Today Worldwide:");
			$('.top50Artist').show();
			$('.top50Duration').hide();
		} else if (this.listFrom == "album" && arguments.length == 5) {
			this.albumArtist = arguments[1];
			$.get(this.albumSongsUrl + encodeURIComponent('&artist=' + arguments[1] + '&album=' + arguments[2] + '&performerId=' + arguments[3]), this.createList, "xml");
			$('.top50Headline').hide() //.text("Tracks in " + arguments[2] + ":");
			this.songsToShow += 2;
			$('.top50Artist').hide();
			$('.top50Duration').show();
		} else if (this.listFrom == "album" && arguments.length == 3) {
			this.albumArtist = arguments[1];
			$.get(this.albumSongsUrl + encodeURIComponent('&artist=' + arguments[1] + '&album=' + arguments[2]), this.createList, "xml");
			$('.top50Headline').hide()//.text("Tracks in " + arguments[2] + ":");
			$('.top50Artist').hide();
			$('.top50Duration').show();
		} else if (this.listFrom == "artist" && arguments.length == 3) {
			this.albumArtist = arguments[1];
			$.get(this.artistSongsUrl + encodeURIComponent('&performerId=' + arguments[2]), this.createList, "xml");
			$('.top50Headline').text("Top Songs By " + toTitleCase(arguments[1]) + ":");
			$('.top50Artist').show();
			$('.top50Duration').hide();
		} else if (this.listFrom == "artist" && arguments.length == 2) {
			this.albumArtist = arguments[1];
			$.get(this.artistSongsUrl + encodeURIComponent('&artist=' + arguments[1]), this.createList, "xml");
			$('.top50Headline').text("Top Songs By " + toTitleCase(arguments[1]) + ":");
			$('.top50Artist').show();
			$('.top50Duration').hide();
		}

	},
	scrollTo: function(place) {
		$('#top50Container').animate({ scrollTop: ((place - 1) * 20) }, 'fast');
		return false;
	},
	scrollUp: function(px) {
		var container = $('#top50Container');
		if (container.scrollTop() > 0) {
			if (px == null) {
				container.scrollTop(container.scrollTop() - 5);
				this.scroller = setTimeout("songList.scrollUp()", 40);
			} else {
				container.scrollTop(container.scrollTop() - px);
			}
		}
	},
	scrollDown: function(px) {
		var container = $('#top50Container');
		if (px == null) {
			container.scrollTop(container.scrollTop() + 5);
			this.scroller = setTimeout("songList.scrollDown()", 40);
		} else {
			container.scrollTop(container.scrollTop() + px);
		}
	},
	stopScroll: function() {
		clearTimeout(this.scroller);
	}
}