function addRecordingsToPlaylist(backto)
{
	var entries = $$(".playlist_entry");
	var len = entries.length;
	var to_add = "";
	var i = 0;
	for (i=0; i<len; i++)
	{
		if (entries[i].checked)
			to_add = to_add + entries[i].value + ",";
	}
	if (to_add == "")
		alert("Sorry, you need to use the checkboxes to select recordings before proceeding. Please try again.");
	else
		location.href = "/snap/playlist/add?recording_id=" + to_add + "&backto=" + backto;
}

function updateStatus(id)
{
	new Ajax.Updater("status_text_"+id, "/index.cfm/account/doupdatestatus", {onSuccess: leaveEditStatusMode(id), parameters: { value: $F("new_status_text_"+id) }});
}
function clearStatus(id)
{
	$("new_status_text_"+id).value="";
	updateStatus(id);
	hideStatus(id);
}
function enterEditStatusMode(id)
{
	$("new_status_text_"+id).value="";
	$("status_form_"+id).style.display = "inline";
	$("status_text_area_"+id).style.display = "none";
	Form.focusFirstElement("status_form_"+id);
}
function leaveEditStatusMode(id)
{
	$("status_form_"+id).style.display = "none";
	$("status_text_area_"+id).style.display = "inline";
}
function hideStatus(id)
{
	$("status_form_"+id).style.display = "none";
	$("status_display_area_"+id).style.display = "none";
}

function startCountingCharacters(id, max)
{
	if (typeof(currentlyCountingCharacters) == "undefined" || !currentlyCountingCharacters)
	{
		currentlyCountingCharacters = true;
		characterCountInterval = setInterval("displayCharacterCount('" + id + "'," + max + ")", 1000);
	}
}

function stopCountingCharacters(id, max)
{
	if (typeof(currentlyCountingCharacters) != "undefined" && currentlyCountingCharacters)
	{
		currentlyCountingCharacters = false;
		clearInterval(characterCountInterval);
	}
	displayCharacterCount(id, max);
}

function displayCharacterCount(id, max)
{
	var text = "";
	var left = 0;

	left = max - $(id).value.length;
	if (left < 0)
	{
		if (left == -1)
		{
			text = "letter";
		}
		else
		{
			text = "letters";
		}
		$('characterCount').innerHTML="<span style='color:red;font-weight:bold;'>0</span> (your text is too long, please remove at least " + -left + " " + text + ")";
	}
	else
	{
		$('characterCount').innerHTML=left;
	}
}

function checkCharacterCount(id, max)
{
	if ($(id).value.length <= max)
	{
		return true;
	}
	else
	{
		alert("Your text is too long, please remove some letters before proceeding.");
		return false;
	}
}

function playlistFinishedPlay()
{
	if (typeof(playlist_interval) == "undefined")
		playlist_interval = setInterval("playlistGoToNextUnlessCommenting()", 3000);
}

function playlistSetShuffle(status)
{
	shuffle = status;
}

function playlistSetRepeat(status)
{
	repeat = status;
}

function playlistRedirectTo()
{
	location.href = "/snap/watchandlisten/play/" + recording_id + "?playlist_id=" + playlist_id + "&shuffle=" + shuffle + "&repeat=" + repeat + "&row=" + row + "&pos=" + pos;
}

function playlistJumpTo(jump_to_pos)
{
	pos = jump_to_pos;
	recording_id = ordered_entries[pos-1];
	for (var i=0; i<tot; i++)
	{
		if ((shuffle == 1 && shuffled_entries[i] == recording_id) || (shuffle == 0 && ordered_entries[i] == recording_id))
		{
			row = i+1;
		}
	}
	playlistRedirectTo();
}

function playlistGoTo(direction)
{
	if ((direction == "prev" && row > 1) || (direction == "next" && row < tot) || (repeat == 1))
	{
		if (shuffle == 1)
		{
			if (direction == "next")
			{
				if (row == tot)
					row = 1;
				else
					row = row + 1;
			}
			else
			{
				if (row == 1)
					row = tot;
				else
					row = row - 1;
			}
			recording_id = shuffled_entries[row-1];
			for (var i=0; i<tot; i++)
			{
				if (ordered_entries[i] == recording_id)
				{
					pos = i+1;
				}
			}
			playlistRedirectTo();
		}
		else
		{
			if (direction == "next")
			{
				if (pos == tot)
					pos = 1;
				else
					pos = pos + 1;
			}
			else
			{
				if (pos == 1)
					pos = tot;
				else
					pos = pos - 1;
			}
			recording_id = ordered_entries[pos-1];
			for (var i=0; i<tot; i++)
			{
				if (ordered_entries[i] == recording_id)
				{
					row = i+1;
				}
			}
			playlistRedirectTo();
		}
	}
}

function playlistGoToNextUnlessCommenting()
{
	if ($("new_comment_body") == null || $("new_comment_body").value.length == 0)
	{
		clearInterval(playlist_interval);
		playlistGoTo("next");
	}
}

function changeOpacity(value, obj)
{
	obj2 = document.getElementById(obj);
	shadow = document.getElementById(obj + '_shadow');
	obj2.style.opacity = value/10;
	obj2.style.filter = 'alpha(opacity=' + value*10 + ')';
	if (value==10)
	{
		shadow.style.visibility = 'visible';
		obj2.style.top = '-8px';
	}
	else
	{
		shadow.style.visibility = 'hidden';
		obj2.style.top = '0px';
	}
}

function toggle_checkboxes()
{
	if (typeof(all_checked) == "undefined")
		all_checked = false;
	var all_nodes = document.getElementsByClassName("checkbox_in_list");
	var id_array = new Array();
	for(i = 0; i < all_nodes.length; i++)
	{
		if (all_checked)
	  	{
	  		all_nodes[i].checked = false;
	  	}
	  	else
	  	{
		  	all_nodes[i].checked = true;
			id_array.push($(all_nodes[i]).value);
	  	}
	}
	if (all_checked)
	{
		all_checked = false;
		$("ids").value = "";
	}
	else
	{
		all_checked = true;
		$("ids").value = id_array.join(",");
	}
}


function update_id_list(checked, id)
{
	if ($("ids").value != "")
		var id_array = $("ids").value.split(",");
	else
		var id_array = new Array();
	var action = "";
	if (checked)
	{
		id_array.push(id);
	}
	else
	{
		for (var i = 0; i < id_array.length; i++)
		{
			if (id_array[i] == id)
				id_array.splice(i,1);
		}
	}
	$("ids").value = id_array.join(",");
}

function flag(url, what, id)
{
	var flag_array = "";
	var trick = new Date();

	trick = trick.getTime();
	url = url + "&trick=" + trick;
	new Ajax.Request(url, {method: "get"});
	flag_array = document.getElementsByClassName(what + "_flag_" + id);
	for (i = 0; i < flag_array.length; i++)
	{
		if (flag_array[i].src.indexOf("flag_on") != -1)
			flag_array[i].src="http://songs.singsnap.com/images/small_icon_flag_off.gif";
		else
			flag_array[i].src="http://songs.singsnap.com/images/small_icon_flag_on.gif";
	}
}

function setCookie(name, value, expires, path, domain, secure) 
{
	var name = name.toUpperCase();
	var today = new Date();
	today.setTime(today.getTime());
	if (expires)
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");
}

function getCookie(name)
{
	
	var name = name.toUpperCase();
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) &&	(name != document.cookie.substring(0, name.length)))
	{
		return null;
	}
	if (start == -1)
		return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1)
		end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function deleteCookie(name, path, domain)
{
	if (getCookie(name))
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function check_or_uncheck_all(type)
{
	if (typeof(all_checked) == "undefined")
		all_checked = false;
	var all_nodes = document.getElementsByClassName(type + "_checkbox");
	for(i = 0; i < all_nodes.length; i++)
	{
  	if (all_checked)
  	{
  		all_nodes[i].checked = false;
  	}
  	else
  	{
	  	all_nodes[i].checked = true;
  	}
	}
	if (all_checked)
		all_checked = false;
	else
		all_checked = true;
}

function really_delete_all(action, type)
{
	if (action == "delete" || action == "delete_received" || action == "delete_sent")
	{
		var confirmed = confirm("Are you sure you want to permanently delete all selected " + type + "?");
		if (confirmed)
			return true;
		else
			return false;
	}
	else
	{
		return true;
	}
}