

$(document).ready(function()
{	


	/***
		Turn Status dropdowns into XHR mechanisms
	***/
	//
	//var statuses = '<button class="Approved" value="8" title="Item is OK for the site">OK</button>';
	//statuses += '<button class="NotApproved" value="2" title="Item is not suitable for the site -- don\'t show it">Not OK</button>';
	//statuses += '<button class="Unapproved" value="4" title="Item must be reviewed">Not Reviewed</button>';
	//statuses += '<button class="Deleted" value="1" title="Item will be removed from the site">Deleted</button>';
	
	// Hide the submit button -- we're doin XHRs
	$('#mediaListForm .buttons').hide();
	
	// Add message holders to each item
	$('.mediaList .mediaAdmin').append('<span class="xhrMessage"></span>');
	
	$('.mediaList .mediaAdmin').change(function()
	{
		var id = $(this).parent().attr('id');
		var stat = $('#' + id + " option:selected").attr('value');
		
		var act = $('#mediaListForm').attr('action');
		var meth = $('#mediaListForm').attr('method');
		
		var type = $(this).parent().parent().attr('id');
		
		
		$.ajax({
			   url: act,
			   type: meth,
			   data: 'isXhr=1&' + id + '=' + stat,
			   dataType: 'json',
			   complete: function(obj, textSuccess)
			   {
					// This could likely use a significant amount of cleanup, but it mostly works. (only 2-3 people using it anyway)
					var msg = '';
					if(obj.status == 200)
					{
						// Parse the response
						var resp = eval('(' + obj.responseText + ')');
						
						msg = resp.msg;
						
						// Check for error sent from PHP
						if(resp.success)
						{
							msgclass = 'message_success';
							
							if($('form#filter').length > 0)
							{
								if($('form#filter select.statusSelect option:selected').val() != stat)
								{
									$('li#row' + resp.id).hide('slow');
									$('#' + type + 'Count').html(($('#' + type + 'Count').text()-1).toString());
									return;
								}
							}
						} else {
							msgclass = 'message_failure';
						}
					} else {
						msg = 'Could not update the approval. There was a communications error.';
						msgclass = 'message_failure';
						
						// TODO: Reset combobox to original value if the filter combobox exists (like on /media.php)
					}
					
					
					$('#row' + resp.id + ' .xhrMessage').removeClass('message_failure').removeClass('message_success').addClass(msgclass).html(msg);
					//alert('OBJECT\n' + obj.getAllResponseHeaders() + '\nTEXT\n' + textSuccess);
					$(this).blur();
			   }
		});
		
	});
	
	
	
	
	/***
		PICTURES
	***/
	
	
	
	
	
	/***
		VIDEOS
	***/
	// Modify videos to play in place
	// Move the thumbnail image to the background and add a class that displays the play button
	$('.mediaListVideos li').each(function() {
		// Gather required info
		var id = $(this).attr('id');
		var vidURL = $('#' + id + ' a.vidLink').attr('href');
		var vidThumb = $('#' + id + ' .thumbnail img').attr('src');
		//alert('' + '\n\nID: ' + id + '\nTHUMBNAIL: ' + vidThumb + '\nURL: ' + vidURL);
		
		// Make the changes
		$('#' + id + ' .thumbnail').css('background', 'transparent url(' + vidThumb + ') no-repeat scroll left top');
		$(this).addClass('transformed');
	});
	
	
	// Play the videos in place
	$(".mediaListVideos a[@href*=youtube.com]").click( function()
	{
		
		//	Add the class
		$(this).parent().parent().addClass('playing');
		
		// Build the player
		var href = $(this).attr('href');
		var vidPlayer  = '<div class="videoFull">';
			vidPlayer += '<a href="#" class="videoCloseButton" onclick="return closePlayer(this);">Close</a>';
			vidPlayer += '<object width="300" height="250"><param name="movie" value="' + href + '&autoplay=1" />';
			vidPlayer += '<param name="autoplay" value="true" />';
			vidPlayer += '<param name="wmode" value="transparent" />';
			vidPlayer += '<param name="allowFullScreen" value="true" />';
			vidPlayer += '<embed src="' + href + '&autoplay=1" type="application/x-shockwave-flash" autoplay="true" width="300" height="250" wmode="transparent" /></object>';
			vidPlayer += '</div>';
		
		// Add the player
		$(this).parent().append(vidPlayer);
		
		// Remove the background
		$(this).parent().css('background', 'none');
		
		return false;
	});
});

function closePlayer(el)
{
	// Gather required info
	var id = $(el).parent().parent().parent().attr('id');
	var vidThumb = $('#' + id + ' img.vidThumb').attr('src');
	//alert('' + '\n\nID: ' + id + '\nTHUMBNAIL: ' + vidThumb + '\nURL: ' + vidURL);
	$('#' + id + ' .thumbnail').css('background', 'transparent url(' + vidThumb + ') no-repeat scroll left top');
	
	$(el).parent().parent().parent().removeClass('playing');
	$('#' + id + ' .videoFull').remove();
	return false;
}

function setCombobox(id, value)
{
}