// JavaScript Document

// Page Load Functions
$(document).ready(function(){
// Element Animations
	$(window).resize(function(){
		var documentHeight = $(document).height();
		var winH = $(window).height();  
		var winW = $(window).width();
		$('#modalMask').height(documentHeight);
		$('#modalBox').css('top',  winH/2-$('#modalBox').height()/2);  
		$('#modalBox').css('left', winW/2-$('#modalBox').width()/2);
	});
	
// Click Functions
	$('#modalMask')
		.click(function(){
			$('#modalBox').fadeOut('fast',function(){
				$('#modalMask').hide();
			});
		});
// Form Functions
	$('input')
		.focus(function(){
			if ($(this).css('color') == '#888888')
			{
				$(this).val('');
			}
		});
});
//End Page Load Functions


// Image Upload Functions
function imageLink()
{
	var imagePath = $('#imagePath').val();
	$('#newImageForm').fadeOut('fast',function(){
		$('#loading').fadeIn('fast');
	});
	$.ajax({
		type: 'POST',
		url: 'scripts/linkImage.php',
		cache: false,
		async: true,
		dataType: 'json',
		data: 'imagePath=' + imagePath,
		success: function(response)
		{
			if (response[0].internalErrorCode == 0) {
				createThumb(response[0].localPath);
				setTimeout(function()
				{
					$('#loading').fadeOut('fast',function(){
						$('#mainContainer').fadeOut('fast',function(){
							$('#mainContainer').load('includes/editImage.php?link='+response[0].localThumb+'', function(){
								$('#mainContainer').fadeIn('fast');
							});
						});
					});
				}, 1000);
			} else {
				setTimeout(function()
				{
					$('#loading').fadeOut('fast',function(){
						$('#newImageResult').load('includes/addImage_failure.php', function(){
							if (response[0].internalErrorCode > 0) {
								$('#errorMessage').append('<div>'+response[0].internalErrorMessage+'</div>');
							}
							$('#newImageResult').fadeIn('fast');
						});
					});
				}, 1000);
			}
		},
		error: function()
		{	
		}
	});
return false;
}

// Local Upload Completion Check
var imageUploading = 0;
function imageUpload()
{
	if (imageUploading == 0)
	{
		$('#newImageForm').fadeOut('fast',function(){
			$('#loading').fadeIn('fast', function(){
				imageUploading = 1;
			});
		});
	}
	var imagePath = $('#imagePath').val();
	var session = $('#session').val();
	$.ajax({
			type: 'POST',
			url: 'scripts/checkUploadedImage.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'imagePath=' + imagePath + '&session=' + session,
			success: function(response)
			{
				if (response[0].uploadStatus == 1)
				{
					setTimeout(function()
					{
						imageUpload();
					}, 1500);
				}
				else
				{
					imageUploading = 0;
					if (response[0].internalErrorCode == 0)
					{
						createThumb(response[0].localPath);
						setTimeout(function()
							{
								$('#loading').fadeOut('fast',function(){
									$('#newImageResult').load('includes/addImage_success.php', function(){
										$('#newImageResult').fadeIn('fast');
									});
								});
							}, 1000);
					}
					else
					{
						setTimeout(function()
						{
							$('#loading').fadeOut('fast',function(){
								$('#newImageResult').load('includes/addImage_failure.php', function(){
									if (response[0].uploadErrorCode > 0)
									{
										$('#errorMessage').append('<div>'+response[0].uploadErrorMessage+'</div>');
									}
									else if (response[0].internalErrorCode > 0)
									{
										$('#errorMessage').append('<div>'+response[0].internalErrorMessage+'</div>');
									}
									$('#newImageResult').fadeIn('fast');
								});
							});
						}, 1000);
					}
				}
			},
			error: function()
			{	
			}
		});
}

// Create Thumbnails
function createThumb(imagePath)
{
	$.ajax({
		type: 'POST',
		url: 'scripts/createThumb.php',
		cache: false,
		async: true,
		dataType: 'json',
		data: 'filePath=' + imagePath,
		success: function(response)
		{
		},
		error: function()
		{	
		}
	});
}
// Interactivity Functions
function loadItem(itemLocation, requestedItem)
{
	$('#' + itemLocation).load(requestedItem);
}
// Modal Functions
function showPopup(requestedItem)
{
	var documentHeight = $(document).height();
	var winH = $(window).height();  
	var winW = $(window).width();
	$('#modalMessage').load(requestedItem);
	$('#modalMask').height(documentHeight);
	$('#modalBox').css('top',  winH/2-$('#modalBox').height()/2);  
	$('#modalBox').css('left', winW/2-$('#modalBox').width()/2);
	$('#modalMask').fadeIn('fast',function(){
		$('#modalBox').fadeIn('fast');
	});
}
function hidePopup()
{
	$('#modalBox').fadeOut('fast',function(){
		$('#modalMask').fadeOut('fast');
	});
}
// Main Content
function showContent(requestedItem)
{
	$('#mainContainer').fadeOut('fast', function(){
		$('#mainContainer').load(requestedItem, function(){
			$('#mainContainer').fadeIn('fast');
		});
	});
}