$(document).ready(function() {
	
	$('#add_story').click(function() {
		
		$('#dialog').dialog("option", "width", 520);
		$('#dialog').dialog("option", "height", 210);
		$('#dialog').dialog("option", "resizable", false);
		$('#dialog').dialog("option", "closeOnEscape", false);
		$('#dialog').dialog('open');

		loadNewStory(true);
	});
});

function loadNewStory(newSession) {
	
	if(!newSession) newSession = false;
	
	$('#dialog').empty();
	$('#dialog').load(homeDir + 'stories/new-story/', 
			{'newSession': newSession}, 
			function() {	
				$('#song_field').ezpz_hint();
				$('#dialog').bgiframe();
				$('#dialog').unbind('dialogbeforeclose');
				
				$('#set_song').click(function() {
					
					if ( result && result != '' ) {
						if(checkRequired()) {
							$(this).parent().ajaxSubmit(function() {loadStoryDetails();});	
						}
						
						result = '';
					} else {
						alert( 'Please choose an artist, song, or album from the dropdown list.' );
					}
					
					return false;
				});
				$('#close_new_story_dialog').click(function() {
					$('#dialog').dialog('close');
					return false;
				});
			});	
	
	$('#dialog').bind('close', function() {
		$('#song_field').unautocomplete();
		return true;
	});
}

function loadStoryDetails() {
	$('#dialog').empty();
	$('#dialog').dialog("option", "height", 400);
	$('#dialog').dialog("option", "position", "center");
	$('#dialog').css('height', '360px');
	
	

		
	$('#dialog').dialog('open');
	$('#dialog').load(homeDir + 'stories/add-details/', 
			{}, 
			function() {
					
				$('#dialog').bgiframe();
				$('#story_date').mask("99/99");


				$('#set_details').unbind('click');
				$('#set_details').click(function() {
					if(checkRequired()) {
						
						$(this).parent().ajaxSubmit(function() {
							//publishNewSection($('#story_title').value(), $('#story_id').value());
							loadAddImages();
						});	
					}
				});
				
				$('#close_new_story_dialog').unbind('click');
				$('#close_new_story_dialog').click(function() {
					$('#dialog').dialog('close');
					return false;
				});
				
				$('#dialog').unbind('dialogbeforeclose');
				$('#dialog').bind('dialogbeforeclose', function() {	
					if(confirm('Closing now will delete this story. Do you still want to close?')){	
						$.get(homeDir + 'stories/delete-current/', {}, 
							  function(data) {});
						
					} else {
						return false;	
					}
					
				});	
				
				initBack(loadNewStory);
			});		
}

function loadAddImages() {
	$('#dialog').empty();
	$('#dialog').dialog("option", "height", 527);	
	$('#dialog').dialog("option", "position", "center");
	$('#dialog').css('height', '527px');	
	$('#dialog').load(homeDir + 'stories/add-images/', 
			{}, 
			function() {
				
				$('#dialog').bgiframe();
				
				var that = this;
				
				$('#add_images').click(function() {
					$(this).parent().ajaxSubmit(function() {loadAddVideos();});
				});

				$('#close_new_story_dialog').click(function() {
					$('#dialog').dialog('close');
					return false;
				});
				
				initBack(loadStoryDetails);
			});		
}


function loadAddVideos() {
	$('#dialog').empty();
	$('#dialog').dialog("option", "height", 527);	
	$('#dialog').dialog("option", "position", "center");
	$('#dialog').css('height', '540px');	
	$('#dialog').load(homeDir + 'stories/add-videos/', 
			{}, 
			function() {
				
				$('#dialog').bgiframe();
				
				var that = this;
				
				$('#add_videos').click(function() {
					var title = $('#story_title').val();
					var image = $('#album_art').val();
					var song = $('#song').val();
					var artist = $('#artist').val();
					var story_id = $('#story_id').val();
					//publishNewStory(title, image, song, artist, story_id);
					$('#add_videos_form').submit();
				});

				$('#close_new_story_dialog').click(function() {
					$('#dialog').dialog('close');
					return false;
				});
				
				initAddVideos();
				initBack(loadAddImages);
			});		
}





function initAddVideos() {
	$('#add_videos_form input[name=type]').click(function() {
		
		if ( 'embed' == $(this).val() ) {
			$('#embed').show();
			$('#upload,#upload_form').hide();
		} else if ( 'upload' == $(this).val() ) {
			$('#embed').hide();
			$('#upload,#upload_form').show();
		} else {
			$('#upload,#upload_form,#embed').hide();
		}
	});
	
	$('#upload_form').ajaxForm({
		success: upload_video_callback,
		dataType: 'json',
		beforeSubmit: function( data, form, options ) {
			// first parse the form options into a keyed array.
			var parsed = {};
			for( var i in data ) {
				parsed[data[i].name] = data[i].value;
			}
			
			// Make sure the video is not empty.
			if ( '' == parsed.file ) {
				alert( 'Please choose a video to upload.' );
				$('#loading').hide();
				$('#add_videos_form .video_upload').css('visibility', 'visible');
				return false;
			}

			options.url = parsed.upload_url;
			
			return true;
		}
		
	});

	
	$('#add_videos_form').ajaxForm({
		beforeSubmit: function( data, form, options ) { 
		
			// first parse the form options into a keyed array.
			var parsed = {};
			for( var i in data ) {
				parsed[data[i].name] = data[i].value;
			}
			
			// If we have completed all the steps we'll have a youtube id.
			if ( '' != parsed.youtube_id ) {
				options.success = done_callback;
				return true;
			}
			

			//  See if we're parsing embed code
			if ( 'embed' == parsed.type ) {
				options.url = homeDir + 'media/parse-embed/format/json/';
				options.success = parse_code_callback;
				options.dataType = 'json';
				return true;
				
			// Otherwise see if we're doing youtube uploading
			} else if ( 'upload' == parsed.type ) {
				$('#loading').show();
				$('#add_videos_form .video_upload').css('visibility', 'hidden');
				
				// First we need an upload token
				if ( '' == $('#token').val() ) {
					
					// Do this one manually with an ajax post so we don't 
					// transfer the file needlessly.
					$.getJSON(
						homeDir + 'media/upload-token/format/json/',
						{
							name: parsed.name,
							description: parsed.description,
							category: parsed.category,
							tags: parsed.tags
						},
						upload_token_callback
					);
					
					return false;
					
				// If we already have a token, post to youtube.
				} else {
					$('#upload_form').submit();
					return false;
				}
				
				
			// Otherwise go to the next step 	
			} else {
				loadNewSection(0);
			}
			
			
			return false;
		}
	});
}



function parse_code_callback( data ) {
	if ( true == data.error ) {
		alert( 'There was an error parsing the embed code you entered.  Please enter a YouTube URL or the YouTube embed code located on the video\'s page.' );
	} else {
		$('#youtube_id').val( data.youtube_id );
		$('#add_videos_form').submit();
	}
}

function upload_token_callback( data ) {
	if ( true == data.error ) {
		$('#loading').hide();
		$('#add_videos_form .video_upload').css('visibility', 'visible');
	} else {
		$('#upload_url').val( data.url );
		$('#token').val( data.token );
		$('#add_videos_form').submit();
	}
}

function upload_video_callback( data ) {
	$('#youtube_id').val( data.youtube_id );
	$('#add_videos_form').submit();
}

function done_callback( data ) {
	loadNewSection(0);
}


function initBack(callback) {
	
	$('.back_btn').click(function() {
		
		callback();
		
	});
}



