var playList = new Array();
var index = 0;
var mobileMode = false;
var showing = false;		

/*
	this function plays a list of songs by getting them from the given url
	jSonUrl = the json source for the list
	listUrl = the url back to the page for this list
	title = the title of this list
*/
function playItems(jSonUrl, listUrl, title)
{
	$.get(jSonUrl, function(data) {
		playList = new Array();
		
		for (key in data.Results)
			playList.push(data.Results[key]);
			
		index = 0;
		startPlaying(listUrl, title);
	});
}

/*
	Same as above, but starts from a given itemId
*/
function playItemsFrom(jSonUrl, listUrl, title, itemId)
{
	$.get(jSonUrl, function(data) {
		playList = new Array();
		var i = 0;
		index = 0;
		for (key in data.Results)
		{
			playList.push(data.Results[key]);
			if (itemId == data.Results[key]["Id"])
				index = i;
			i = i + 1;
		}
		startPlaying(listUrl, title);
	});
}

function playItemOfList(itemId)
{
	var source = $("#playListSource").attr("value");
	var url = $("#playListUrl").attr("value");
	var title = $("#playListTitle").attr("value");
	playItemsFrom(source, url, title, itemId);
}

function startPlaying(listUrl, title)
{
	var url = playList[index]["Url"];
	var id = youtubeIDextract(url);
	var link = "<a href='" + listUrl + "' class='mpn content' title='" + title + "'>";
	var artistLink = playList[index]["Artist"];
	var itemLink = playList[index]["Title"];
	var likeLink = '<a id="player_like_link" itemId="' + playList[index]["Id"] + '" href="/items/like/?id=' + playList[index]["Id"] + '&value=';
	likeLink += (playList[index]["Like"] ? 'False' : 'True');
	likeLink += '" onClick="toggleLikeState()" class="mpa ' + (playList[index]["Like"] ? 'stat_love_on' : 'stat_love_off');
	likeLink += '" alt="Click to show your love" title="Click to show your love"></a>';
	
	$("#like_item").html(likeLink);		
	
	$("#playing_song_title").text(itemLink + " by " + artistLink);
	$("#playing_theme_title").html(link + title + "</a>");
	
	ensurePlayerIsOpen();
	
	$("#youtube-player-container").tubeplayer('play',id);
}
function toggleLikeState()
{
	var element = $("#player_like_link");
	var url = element.attr("href");

    var spanCounter = element.children('span');

    if (element.hasClass('stat_love_on')) {
        //change class
        element.removeClass('stat_love_on');
        element.addClass('stat_love_off');
        //change link
        element.attr('href', url.replace('False', 'True'));
		playList[index]["Like"] = false;
    }
    else {
        //change class
        element.removeClass('stat_love_off');
        element.addClass('stat_love_on');
        //change link
        element.attr('href', url.replace('True', 'False'));
    }

    $.ajax({
        cache: false,
        url: url,
        success: function(result) {
			playList[index]["Like"] = !playList[index]["Like"];
        },
        error: function(result) {
            alert('Error(MPA371):Could not follow.');
        }
    });

    return false;
}

function play()
{
	var url = playList[index]["Url"];
	var id = youtubeIDextract(url);
	var link = "<a id='theme_" + playList[index]["ThemeID"] +"' href='/Themes/Details/" + playList[index]["ThemeID"] + "' class='mpn content' title='" + playList[index]["ThemeName"] + "'>";
	var likeLink = '<a id="player_like_link" itemId="' + playList[index]["Id"] + '" href="/items/like/?id=' + playList[index]["Id"] + '&value=';
	likeLink += (playList[index]["Like"] ? 'False' : 'True');
	likeLink += '" onClick="toggleLikeState()" class="mpa ' + (playList[index]["Like"] ? 'stat_love_on' : 'stat_love_off');
	likeLink += '" alt="Click to show your love" title="Click to show your love"></a>';
	
	$("#like_item").html(likeLink);		
	$("#playing_song_title").text(playList[index]["Title"] + " by " + playList[index]["Artist"]);
	
	ensurePlayerIsOpen();
	
	$("#youtube-player-container").tubeplayer('play',id);
}

function failAndNext()
{
	$('#youtube-player').css("height", 0);
	$("#youtube-player-container-fail").show();
	setTimeout ("proceed()", 2000);
}

function proceed()
{
	$('#youtube-player').css("height", 225);
	$("#youtube-player-container-fail").hide();
	next();
}

function ensurePlayerIsOpen()
{
	if (!showing)
		$("#open_close_player").click();
}

function next()
{
	index+=1;
	if (index < playList.length)
		play();
}

function prev()
{
	if (index > 0)
		index-=1;

	if (index < playList.length)
		play();
}

function youtubeIDextract(url) 
{ 
    var re = new RegExp('v=([^&]*)');
    var matches = re.exec(url);
    if (matches == null || matches.length < 2)
        return '';
    else
        return matches[1];
}

function shuffleArray(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function shuffle()
{
	if (playList != null) {
		playList = shuffleArray(playList);
		index = -1;
		next();
	}
}

