$(document).ready(function(){

	// Tablesorter
	$('table#users th').each(function(){
		$(this).html('<a href="javascript:void(0);">' + ($(this).html()) + '</a>');
	});
	$('table#users').tablesorter();

	// Section
	var Section = '';
	var ImageInUse		= '';
	var FileNameInUse	= '';
	var initializer 	= '';

	// Media Manager
	$('.boot_media_manager').live('click', function(){
		if($(this).html() == 'Choose')
		{
			$(this).html('Change Image');
		}
		Section = $(this).attr('title');
		$('#media #folder').selectedIndex = 0;
		showMedia();
		return false;
	});

	$('.boot_return_media').live('click', function(){
		if($(this).html() == 'Choose')
		{
			$(this).html('Change Image');
		}

		initializer = $(this).attr('id');

		Section = $(this).attr('title');
		$('#media2 #folder2').selectedIndex = 0;
		showMedia2();
		return false;
	});

	$('.boot_return_video').live('click', function(){
		if($(this).html() == 'Choose')
		{
			$(this).html('Change File');
		}

		initializer = $(this).attr('id');

		Section = $(this).attr('title');
		$('#media3 #folder3').selectedIndex = 0;
		showMedia3();
		return false;
	});

	$('.boot_prev').live('click', function(){
		$(this).prev('input').click();
		return false;
	});

	$('.clear_prev').live('click', function(){
		$(this).prev('a').prev('input').val('');
		return false;
	});

	$('.boot_upload').live('click', function(){
		Section = $(this).attr('title');
		$('#media select').each(function(){
			$(this).selectedIndex = 0;
		});
		showUpload();
		return false;
	});

	// Media Manager - User selected a folder from the existing category
	$('#folder').change(function(){
		var gallery = $(this).parent();
		if($(this).val() != '')
		{
			jQuery.post('/media/retrieve_images/'+$(this).val()+'/true', null, function(data, status){
				$('#image').html(data);
			}, "html");
		}
	});

	$('#folder2').change(function(){
		var gallery = $(this).parent();
		if($(this).val() != '')
		{
			jQuery.post('/media/retrieve_images/'+$(this).val()+'/true', null, function(data, status){
				$('#image2').html(data);
			}, "html");
		}
	});

	$('#folder3').change(function(){
		var gallery = $(this).parent();
		if($(this).val() != '')
		{
			jQuery.post('/media/retrieve_files/'+$(this).val()+'/true', null, function(data, status){
				$('#image3').html(data);
			}, "html");
		}
	});

	$('#image').change(function(){
		var thumbnail = $(this).val();
		ImageInUse = thumbnail;
		FileNameInUse = $('option:selected', this).html() + '(' + $('option:selected', this).val() + ')';
		$('#image .preview').html('<img src="'+ thumbnail + '" alt="'+FileNameInUse+'" width="50" height="50" />');
	});

	$('#image2').change(function(){
		var thumbnail = $(this).val();
		ImageInUse = thumbnail;
		FileNameInUse = $('option:selected', this).html() + '(' + $('option:selected', this).val() + ')';
		$('#image2 .preview').html('<img src="'+ thumbnail + '" alt="'+FileNameInUse+'" width="50" height="50" />');
	});

	$('#image3').change(function(){
		var thumbnail = $(this).val();
		ImageInUse = thumbnail;
		FileNameInUse = $('option:selected', this).html() + '(' + $('option:selected', this).val() + ')';
	});

	// Media Manger Dialogs
	$('#media').dialog({
		autoOpen:false,
		bgiframe: true,
		resizable: false,
		width: 500,
		position: 'center',
		modal: true,
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Use Photo': function() {
				usePhoto();
			},
			Upload: function() {
				showUpload();
			},
		}
	});

	$('#media2').dialog({
		autoOpen:false,
		bgiframe: true,
		resizable: false,
		width: 500,
		position: 'center',
		modal: true,
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Use Photo': function() {
				usePhotoInline();
			},
			Upload: function() {
				showUpload2();
			},
		}
	});

	$('#media3').dialog({
		autoOpen:false,
		bgiframe: true,
		resizable: false,
		width: 500,
		position: 'center',
		modal: true,
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Use File': function() {
				useFileInline();
			},
			Upload: function() {
				showUpload3();
			},
		}
	});

	$('#upload').dialog({
		autoOpen:false,
		bgiframe: true,
		width: 500,
		height: 430,
		resizable: false,
		position: 'center',
		modal: true,
		buttons: {
			Close: function() {
				showMedia();
			}
		}
	});

	$('#upload2').dialog({
		autoOpen:false,
		bgiframe: true,
		width: 500,
		height: 430,
		resizable: false,
		position: 'center',
		modal: true,
		buttons: {
			Close: function() {
				showMedia2();
			}
		}
	});

	$('#upload3').dialog({
		autoOpen:false,
		bgiframe: true,
		width: 500,
		height: 430,
		resizable: false,
		position: 'center',
		modal: true,
		buttons: {
			Close: function() {
				showMedia3();
			}
		}
	});

	// Media Manager Functions
	function usePhoto()
	{
		var image	= ImageInUse;
		var name	= FileNameInUse;
		$('#set_'+Section+' input').val(name);
		$('#set_'+Section+' .preview').html('<img src="'+image+'" alt="" /><div class="info">File: '+name+'</div>');
		$('#media').dialog('close');
	}

	function usePhotoInline()
	{
		var image	= ImageInUse;
		$('#' + initializer).val(FileNameInUse);
		var thumb = image.replace('/files/', '/files/thumbs/');
		$('#' + initializer).next('div').html('<img src="'+ thumb +'" alt="" />');
		$('#media2').dialog('close');
	}

	function useFileInline()
	{
		var file	= ImageInUse;
		$('#' + initializer).val(FileNameInUse);
		$('#media3').dialog('close');
	}

	function showMedia()
	{
		$('#upload').dialog('close');
		$('#media').dialog('open');
		$('#media option:first').attr('selected', 'selected');
		return false;
	}

	function showMedia2()
	{
		$('#upload2').dialog('close');
		$('#media2').dialog('open');
		$('#media2 option:first').attr('selected', 'selected');
		return false;
	}

	function showMedia3()
	{
		$('#upload3').dialog('close');
		$('#media3').dialog('open');
		$('#media3 option:first').attr('selected', 'selected');
		return false;
	}

	function showUpload()
	{
		$('#media').dialog('close');
		$('#upload').html('<iframe src ="/media/inline/'+Section+'" width="100%" height="100%"></iframe>')
		$('#upload').dialog('open');
		return false;
	}

	function showUpload2()
	{
		$('#media2').dialog('close');
		$('#upload2').html('<iframe src ="/media/inline/'+Section+'" width="100%" height="100%"></iframe>')
		$('#upload2').dialog('open');
		return false;
	}

	function showUpload3()
	{
		$('#media3').dialog('close');
		$('#upload3').html('<iframe src ="/media/inline/'+Section+'" width="100%" height="100%"></iframe>')
		$('#upload3').dialog('open');
		return false;
	}

});
