var PhotoPrizeComments = {
	UploadFile : function (path)
	{
		var fileName = path;
		if (!fileName) return;
		fileName = FileInfo.Path.GetFileName (path);
		$("#photoUploadDummy").attr("value",fileName);
		$("#loadingAnimation").css ("display", "");
		$("#photoUploadDummy").attr("className", "form145px photo-upload-dummy");
		$("#buttonSubmitComment").attr("disabled",true);			
					
		//starting setting some animation when the ajax starts and completes
		$("#loading")
		.ajaxStart(function(){
			$(this).show();
			
		})
		.ajaxComplete(function(){
			$(this).hide();
		});
		$.ajaxFileUpload
		(
			{
				url:'/AjaxImageUpload.aspx', 
				secureuri:false,
				fileElementId:'photoUpload',
				dataType: 'json',
				success: function (data, status)
				{
					$("#loadingAnimation").css ("display", "none");
					$("#loadingAnimation").css ("display", "none");
					$("#buttonSubmitComment").attr("disabled",false);
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							$("#photoUploadDummy").attr("className", "form145px photo-upload-dummy upload-success");
							PhotoPrizeComments.FormData.PhotoName = data.result.fileName;
							PhotoPrizeComments.FormData.PhotoSize = data.result.fileSize;
							PhotoPrizeComments.FormData.TempFileId = data.result.fileId;
							
						}
					}
				},
				error: function (data, status, e)
				{
					$("#loadingAnimation").css ("display", "none");
					$("#photoUploadDummy").attr("className", "form145px photo-upload-dummy upload-failure");
					$("#buttonSubmitComment").attr("disabled",false);
					if (data.responseText && String(data.responseText).indexOf ("0x80004005") >= 0)
					{
						alert ("The uploaded photo exceeds the max file size of 7 MB");
					}
					else 
						alert(e);
				}
			}
		)
		
		return false;

	}  
	
	,FormData:
	{
		TempFileId	: null
		,PhotoName	: null
		,PhotoSize	: 0
		,Email		: null
		,Name		: null
		,Comment	: null		
	}
	
	,Save : function ()
	{
		//Check legacy agreement
		if (!$("#acceptLegalDisclaimer")[0].checked)
			return alert ("Please accept our legal disclaimer first!");
		
		//Get data
		PhotoPrizeComments.FormData.Email  = $("#commentEmail")[0].value;
		PhotoPrizeComments.FormData.Name  = $("#commentAuthor")[0].value;
		PhotoPrizeComments.FormData.Comment  = $("#commentText")[0].value;
		
		//Check email 
		if (!PhotoPrizeComments.FormData.Email) 
			return alert ("Please specify your email address first!");
		
		if (!String(PhotoPrizeComments.FormData.Email).match (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/gi))
		{
			$("#commentEmail").focus();
			return alert ("Please specify a valid email address!");
		}
		
		$("#cboxLoadingGraphic").css ("display","");
		
		//Save
		$.post('SavePhotoComment.aspx', 
			{'name':  PhotoPrizeComments.FormData.Name, 
			'email': PhotoPrizeComments.FormData.Email, 
			'tempFileId': (PhotoPrizeComments.FormData.TempFileId ? PhotoPrizeComments.FormData.TempFileId : ""),
			'comment': escape(PhotoPrizeComments.FormData.Comment),
			'photo': window.PhotoPrize_SelectedPhoto
			}, function(data) {
				try
				{
					var result = window.eval ("result=" + data + ";");				
					if (result.error)
					{
						alert (result.error);
						$("#cboxLoadingGraphic").css ("display","none");
					}
					else if (result.msg == "OK")
					{
						$.fn.colorbox.close();
					}
					else 
					{
						alert (result.msg);
						$("#cboxLoadingGraphic").css ("display","none");
					}
				}catch (e)
				{
					//alert (e.message || e);
					
				}
			});
	}
}
var FileInfo = {};
FileInfo.Path = {};
FileInfo.Path.GetFileName = function (filePath)
{
	if (!filePath) return null;
	filePath = filePath.replace (/\\/ig, "/");
	var lastSlashPos = filePath.lastIndexOf ("/");
	if (lastSlashPos==-1) return filePath;
	var fileName = filePath.substr (lastSlashPos+1);
	return fileName;
}
