var playerWindow = null;

function popupPlayer(url){
	var plugin = $('#player-plugin');
	if(plugin){
		plugin.hide();
	}
	
	playerWindow = window.open(url, 'DetachedPlayer', 'left=20,top=20,height=300,width=560,menubar=no,status=no,toolbar=no,location=no,directories=no,resizable=yes,scrollbars=no');
	playerWindow.focus();
}

function setupPlaylistControl(control_selector, item_id, item_table, playlist_add_url){
	$(document).ready(function(){
		$(control_selector).change(function(){
			var data = {
				'add-item[item_id]':item_id,
				'add-item[item_table]':item_table,
				'add-item[submit]':'add'
			};
			
			var response = null;
			if($(this).val() == '__new__'){
				response = prompt("What do you want to name the new playlist?");
				if(response != null && response != ''){
					data['add-item[new_playlist]'] = response;
				}
			}
			
			var callback = function(data, textStatus){};
			jQuery.post(playlist_add_url + '/' + $(this).val(), data, callback, 'text');
			$(this).val('');
			
			if(response){
				alert("This " + item_table + " has been added to a new playlist called '" + response + "'.");
				window.location.reload();
			}
			else{
				alert("This " + item_table + " has been added to your playlist.");
			}
		});
	});
}

function embeddedPlayer(){
	if(playerWindow){
		playerWindow.close();
		playerWindow = null;
	}
	var plugin = document.getElementById('player-plugin');
	plugin.style.display = 'block';
	alignSelectionArrow();
}

function alignSelectionArrow(){
	var selected_item = $('.item-row.selected');
	var arrow = $('#selected-' + item_type + '-arrow');
	var offset = selected_item.offset();
	
	if(offset){
		arrow.css('top', offset.top);
		arrow.css('left', offset.left - 150); 
		arrow.show();
	}
}

function exposeSelectionArrow(){
	var arrow = $('#selected-' + item_type + '-arrow:visible');
	var offset = arrow.offset()
	if(offset){
		window.scroll(0, offset.top - 100);
	}
}

