/**
 * @author ralph
 */

function slugify(s) {
	var t = s.toLowerCase();
	t = t.replace(/ /g, "-");
	t = t.replace(/[^a-z0-9-]/g, "");
	return t;
}

var SPlayer = 
{	
	fListid: function() {
		var lUrl = "/listener/artists/" + listid + "/"; 
		if (skin != undefined) lUrl += "?skin=" + skin;
		if (brandid != undefined) {
			if (brandid != "") {
				lUrl += "&b=" + brandid;
			}
		}
		return lUrl;
	},
	
	removeArtist: function(id) {
		$("#span_banned_text").hide();
		
		try {
			id = parseInt(id);
		} catch (ex) { id = false; }
		
		if (id) {
			$.ajax({
				type: "POST",
				url: this.fListid(),
				data: "addid=" + id,
				success: function(msg) {
					SPlayer.loadDeselects();
				}
			});
		}
	},
	
	_removeArtistByDisp: function(disp) {
		$.ajax({
			type: "POST",
			url: this.fListid(),
			data: "adddisp=" + escape(disp),
			success: function(msg) {
				SPlayer.loadDeselects();
			}
		});
	},
	
	removeArtistByDisp: function(disp) {
		$("#span_banned_text").hide();
		
		// Still problems caused by un-slugifyable things like '!!!'
		$.ajax({
			type: "POST",
			url: this.fListid(),
			data: "adddisp=" + escape(disp),
			success: function(msg) {
				// This probably should actually check the msg and
				// make sure the server returned the right thing
				
				var trId = "#tr" + slugify(disp);
				var t = $(trId);
				var atable = $("#tb_banned_artists");
				
				// Replace the +- image
				var i = t.find("img");
				var newSrc = i.attr("src").replace("artist_remove", "artist_add");
				i.attr("src", newSrc);
				i.attr("alt", "+");
				
				// Replace the onclick function
				var a = t.find("a");
				a.unbind('click');
				a.click(function() {
					SPlayer.addArtistByDisp(disp);
				});
				
				// Find the right alphabetical place
				//var c = $("#tb_banned_artists").children();
				var isSet = false;
				var c = atable.find("tr");
				if (c.length > 0) {
					var localTrId = trId.replace("#", "");
					c.each(function() {
						if ($(this).attr("id") > localTrId) {
							$(this).before(t);
							isSet = true;
							return false;	// Breaks out of the each
						} else {
							return true;
						}
					});
					if (!isSet) {
						// Goes at the end
						atable.append(t);
					}
				} else {
					// Nothing in the table yet
					atable.append(t);
				}
			}
		});
	},
	
	addArtist: function(id) {
		try {
			id = parseInt(id);
		} catch (ex) { id = false; }
		
		if (id) {
			$.ajax({
				type: "POST",
				url: this.fListid(),
				data: "remid=" + id,
				success: function(msg) {
					SPlayer.loadDeselects();
				}
			});
		}
	},
	
	_addArtistByDisp: function(disp) {
		$.ajax({
			type: "POST",
			url: this.fListid(),
			data: "remdisp=" + escape(disp),
			success: function(msg) {
				SPlayer.loadDeselects();
			}
		})
	},
	
	addArtistByDisp: function(disp) {
		$.ajax({
			type: "POST",
			url: this.fListid(),
			data: "remdisp=" + escape(disp),
			success: function(msg) {
				// This probably should actually check the msg and
				// make sure the server returned the right thing
				
				var trId = "#tr" + slugify(disp);
				var t = $(trId);
				var atable = $("#tb_playing_artists");
				
				// Replace the +- image
				var i = t.find("img");
				var newSrc = i.attr("src").replace("artist_add", "artist_remove");
				i.attr("src", newSrc);
				i.attr("alt", "+");
				
				// Replace the onclick function
				var a = t.find("a");
				a.unbind('click');
				a.click(function() {
					SPlayer.removeArtistByDisp(disp);
				});
				
				// Find the right alphabetical place
				//var c = $("#tb_banned_artists").children();
				var isSet = false;
				var c = atable.find("tr");
				if (c.length > 0) {
					var localTrId = trId.replace("#", "");
					c.each(function() {
						if ($(this).attr("id") > localTrId) {
							$(this).before(t);
							isSet = true;
							return false;	// Breaks out of the each
						} else {
							return true;
						}
					});
					if (!isSet) {
						// Goes at the end
						atable.append(t);
					}
				} else {
					// Nothing in the table yet
					atable.append(t);
				}
			}
		});
	},
	
	
	
	loadDeselects: function() {
		$('#div_tab_artists').load(this.fListid());
	}
}

$(document).ready(function() {
	//$("#div_content > ul").tabs();
	
	/*
	// Fix the tab layer positioning and sizes...	
	var dc = $('#div_tab_info');
	dc.css("position", "absolute");
	
	var cc = $('#div_tab_artists');
	cc.height(dc.height());
	cc.width(dc.width());
	
    // This is broken in ie6 and safari
	var cc = $('#div_tab_channels');
	cc.css("position", "absolute");
	cc.css("top", dc.css("top"));
	cc.height(dc.height());
	cc.width(dc.width());
	*/
	
	// Load the deselects
	SPlayer.loadDeselects();
	
});

// Set some variables
var plpath_override = "/playlist/xspf96-runspot/";
