// JavaScript Document
// For fan-art submissions

jQuery(document).ready(function($){
	
	// make user info form disabled
	$('#user_info_submit_form input').attr('disabled','disabled');

	// image upload form behavior
	$('#art_submit_form').ajaxForm({
		url: 'ajax/submit_art.cfc?method=upload_art_image&returnFormat=json',
		type: 'post',
		dataType: 'json',
		beforeSubmit: function(){
			// load spinner
			$('#image_container div').html('<div class="spinner" align="center"><img src="/aaaemily/fans/submit_loading.gif" style="padding:4px;"/></div>');
		},
		success: function(data, statusText){
			if(statusText == 'success' && data.SUCCESS == true) {
				
				// add image to page, hidden
				$('#image_container').append('<img class="art_image_preview" style="display:none;" src="'+data.FILE_SRC+'">');
				
				// pre load the image 
				$('.art_image_preview').preload({onFinish:function(){
					// after image is loaded...
					
					// fade out old content
					var old_content = $('#image_container *:first');
					old_content.fadeOut(function(){
						// after fade out...
						old_content.remove();
						$('.art_image_preview').fadeIn();
						
						// make user info form fields editable
						$('#user_info_submit_form input').attr('disabled','');
					});
				}});
				
				// copy image src to user info form hidden field
				$('#image_src').val(data.FILE_SRC);
			}
			else if(data.SUCCESS != true) {
				// an error occurred, display notification
				alert(data.MESSAGE);
			}
			else {
				// an unknown error occurred, display notification
				alert('Oops, something went wrong! Please try again.');
			}
		}
	});
	
	// whenever the a new image is selected in the image upload form...
	$('#art_image').change(function(){
		// submit the form automatically
		$('#art_submit_form').submit();
	});

	// user info form behavior
	$('#user_info_submit_form').ajaxForm({
		url: 'ajax/submit_art.cfc?method=complete_submission&returnFormat=json',
		type: 'post',
		dataType: 'json',
		success: function(data, statusText){
			if(statusText == 'success' && data.SUCCESS == true) {
				// success... 
				
				// fade out forms
				$('#art_submit_form,#user_info_submit_form').fadeOut(function(){
				
					// remove forms from page
					$('#art_submit_form,#user_info_submit_form').remove();
					
					// display success notification
					$('#fan_art_success_message').fadeIn();
				});
			}
			else if(data.SUCCESS != true) {
				// an error occurred, display notification
				alert(data.MESSAGE);
			}
			else {
				// an unknown error occurred, display notification
				alert('Oops, something went wrong! Please try again.');
			}
		}
	});
});

function openTerms() {
	width = 400;
	height = 600;
	left = screen.width - width - 20;
	win = window.open('http://www.emilystrange.com/beware/termsofuse.cfm','_terms','toolbar=no,scrollbars=yes,width=' + width + ',height=' + height + ',top=20,left=' + left);
}
