function shareOn(network)
{
	var u,t,url;
	u=location.href;
	t=document.title;
	if (network == "facebook")
		url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(u) + "&t=" + encodeURIComponent(t);
	else if (network == "myspace")
		url = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + encodeURIComponent(u) + "&t=" + encodeURIComponent(t) + "&c=" + '<a href="' + encodeURIComponent(u) + '">' + encodeURIComponent(t) + "</a>";
	else if (network == "twitter")
		url = "http://twitter.com/home?status=" + encodeURIComponent(t) + " " + encodeURIComponent(u);
	window.open(url,'sharer','toolbar=0,status=0,width=626,height=436');
}

function ajaxPagination(link, page, perPage, totalRecords, load)
{
	var tmp = "";
	var ajaxContent = $("ajax-content");
	var pagination = "";
	var span = "";
	var text = "";
	var elm = "";
	var div = "";
	var windowSize = 0;
	var totalPages = 0;
	var i = 0;
	var timer = "";

	if (typeof(window.snap$initialized) == "undefined")
	{
		window.snap$link = link;
		window.snap$page = page;
		window.snap$perPage = perPage;
		window.snap$totalRecords = totalRecords;
		window.snap$load = load;
		window.snap$initialized = true;
	}
	if (link == null) {link = window.snap$link;} else {window.snap$link = link;}
	if (page == null) {page = window.snap$page;} else {window.snap$page = page;}
	if (perPage == null) {perPage = window.snap$perPage;} else {window.snap$perPage = perPage;}
	if (totalRecords == null) {totalRecords = window.snap$totalRecords;} else {window.snap$totalRecords = totalRecords;}
	if (load == null) {load = window.snap$load;} else {window.snap$load = load;}

	if (typeof(window.snap$ajaxLoadInProgress) == "undefined")
	{
		window.snap$ajaxLoadInProgress = false;
	}

	if (!window.snap$ajaxLoadInProgress)
	{
		if (load)
		{
			window.snap$ajaxLoadInProgress = true;
			timer = setTimeout("window.snap$ajaxLoadInProgress = false", 1000);
			tmp = (ajaxContent.getWidth() / 2) - 50;
			tmp = new Element("img", {src: "/images/ajax_large.gif"}).setStyle({marginRight: tmp + "px", marginLeft: tmp + "px"});
			if (ajaxContent.viewportOffset().top < 0)
			{
				tmp.setStyle({marginTop: (ajaxContent.getHeight()-110) + "px", marginBottom: "10px"})
			}
			else
			{
				tmp.setStyle({marginTop: "10px", marginBottom: (ajaxContent.getHeight()-110) + "px"})
			}
			ajaxContent.update(tmp);		
			new Ajax.Request(link + "?page=" + page + "&perPage=" + perPage + "&totalRecords=" + totalRecords, {onComplete: function(parameters){
				ajaxContent.update(parameters.responseText);
				window.snap$ajaxLoadInProgress = false;
				clearTimeout(timer);
			}});
		}
	
		windowSize = 5;
		totalPages = Math.ceil(totalRecords/perPage);
		
		if (totalRecords > perPage)
		{
			pagination = new Element("div", {className: "ajax-pagination"});
		
			// add "previous" link
			span = new Element("span", {className: "prevnext prev"});
			if (page > 1)
			{
				elm = new Element("a", {href: "javascript:ajaxPagination(null," + (page-1) + ",null,null,true);"});
				span.setStyle({backgroundImage: "url(/images/prev_bg.gif)"});
			}
			else
			{
				elm = new Element("span").setStyle({color: "#e6e6e6"});
				span.setStyle({backgroundImage: "none"});
			}
			pagination.insert(span.insert(elm.update("Previous")));
		
			// create div for links
			div = new Element("div", {className: "links"});
		
			// add page links
			for (i=1; i<=totalPages; i++)
			{
				if ((i >= (page-windowSize) && i <= (page+windowSize)) || i == 1 || i == totalPages)
				{
					if (i == page)
					{
						elm = new Element("span");
					}
					else
					{
						elm = new Element("a", {href: "javascript:ajaxPagination(null," + i + ",null,null,true);"});
					}
					div.insert(elm.insert(i));
				}
				if (i < (page-windowSize) && i > 1)
				{
					div.insert("...");
					i = page-(windowSize+1);
				}
				else if (i > (page+windowSize) && i != totalPages)
				{
					div.insert("...");
					i = totalPages-1;
				}
			}
		
			// add all links
			pagination.insert(div);
		
			// add "next" link
			span = new Element("span", {className: "prevnext next"});
			if (totalPages > page)
			{
				elm = new Element("a", {href: "javascript:ajaxPagination(null," + (page+1) + ",null,null,true);"});
				span.setStyle({backgroundImage: "url(/images/next_bg.gif)"})
			}
			else
			{
				elm = new Element("span").setStyle({color: "#e6e6e6"});
				span.setStyle({backgroundImage: "none"})
			}
			pagination.insert(span.insert(elm.update("Next")));
			div = new Element("div").setStyle({clear: "both"});
			pagination.insert(div);
			tmp = document.getElementsByClassName("ajax-pagination");
			if (tmp.length == 0)
			{
				ajaxContent.insert({"before" : pagination.cloneNode(true)});
				ajaxContent.insert({"after" : pagination.cloneNode(true)});
			}
			else
			{
				for (i=0; i<tmp.length; i++)
				{
					tmp[i].replace(pagination.cloneNode(true));
				}
			}
		}	
	}
}

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 delete all selected " + type + "?");
		if (confirmed)
			return true;
		else
			return false;
	}
	else
	{
		return true;
	}
}