﻿(function($)
{
	$.fn.customFadeIn = function(speed, callback)
	{
		$(this).fadeIn(speed, function()
		{
			if (jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if (callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback)
	{
		$(this).fadeOut(speed, function()
		{
			if (jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if (callback != undefined)
				callback();
		});
	};
})(jQuery);

$(document).ready(function()
{ 
	
});

function btnEditInfo_Click(sender)
{
	var editForm = $('#EditMyInfo');

	if (editForm.is(':visible'))
	{
		editForm.customFadeOut('slow');
	}	
	else
	{
		editForm.customFadeIn('slow');
		if (CURRENT_GUEST_ID != EMPTY_GUID)
		{
			DisplayEditForm(CURRENT_GUEST_ID);
		}
		$('form').validate(
		{
			rules: 
			{
				tbEmailEdit: { required: true, email: true },
				tbNameEdit: { required: true },
				tbDisplayNameEdit: { required: true }
			} 
		});
	}
}

function btnSignOut_Click(sender)
{
	SignOut();
}

function hypSignIn_Click(sender)
{
	$('#SignUpForm').hide().find('input[type = "text"]').rules("remove");
	var signInForm = $('#SignInForm');

	if (signInForm.is(':visible'))
	{
		signInForm.customFadeOut('slow');
	}
	else
	{
		signInForm.customFadeIn('slow');
		$('form').validate(
		{
			rules:
			{
				tbEmailSignIn: { required: true, email: true }
			}
		});
	}	
}

function hypSignUp_Click(sender)
{
	$('#SignInForm').hide().find('input[type="text"]').rules("remove");
	var signUpForm = $('#SignUpForm');

	if (signUpForm.is(':visible'))
	{
		signUpForm.customFadeOut('slow');		
	}
	else
	{
		signUpForm.customFadeIn('slow');
		$('form').validate(
		{
			rules:
			{
				tbEmail: { required: true, email: true },
				tbName: { required: true },
				tbDisplayName: { required: true }
			}
		});
	}
}

function btnSignUp_Click(senderElem)
{
	if ($('form').valid())
	{
		var senderParent = $(senderElem).parent();
		senderParent.ajaxStart(function()
		{
			senderParent.html('<strong>Signing up...</strong><img src="/images/ajax-loader1.gif" style="margin:5px; vertical-align:middle;" />');
			//sender.attr('disabled', 'disabled');
			//sender.css('background-color', '#E2E2E2');
			//sender.css('cursor', 'default');
			//sender.after("&#160;<strong>Signing up...</strong><img src=\"/images/ajax-loader1.gif\" style=\"margin:5px; vertical-align:middle;\" />");
		});

		SignUpForCurrentEvent($('#tbName').val(), $('#tbDisplayName').val(), $('#tbEmail').val());

		senderParent.unbind('ajaxStart');
	}
}

function btnSignIn_Click(senderElem)
{
	if ($('form').valid())
	{
		var senderParent = $(senderElem).parent();
		senderParent.ajaxStart(function()
		{
			senderParent.html('<strong>Signing in...</strong><img src="/images/ajax-loader1.gif" style="margin:5px; vertical-align:middle;" />');
		});

		SignIn($('#tbEmailSignIn').val());

		senderParent.unbind('ajaxStart');
	}
}

function btnUpdateGuestInfo_Click(senderElem)
{
	if ($('form').valid())
	{
		var senderParent = $(senderElem).parent();
		senderParent.ajaxStart(function()
		{
			senderParent.html('<strong>Updating...</strong><img src="/images/ajax-loader1.gif" style="margin:5px; vertical-align:middle;" />');
			//sender.attr('disabled', 'disabled');
			//sender.css('background-color', '#E2E2E2');
			//sender.css('cursor', 'default');
			//sender.after("&#160;<strong>Signing up...</strong><img src=\"/images/ajax-loader1.gif\" style=\"margin:5px; vertical-align:middle;\" />");
		});

		UpdateGuestInfo($('#tbNameEdit').val(), $('#tbDisplayNameEdit').val(), $('#tbEmailEdit').val());

		senderParent.unbind('ajaxStart');
	}	
}

function SignUpForCurrentEvent(name, displayName, email)
{
	$.ajax({
		type: "POST",
		url: "webservices/GuestList.asmx/SignUpForCurrentEvent",
		data: "{'email':'" + email + "','name':'" + name + "','displayName':'" + displayName + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg)
		{
			if (msg.d)
			{
				//alert('success');
				setTimeout(function() { $('#SignUpForm').html('<h3 class="message">You are now on the Guest List</h3>'); }, 1000);
				setTimeout(function() { location.reload(true); }, 1000);
			}
		}
	});
}

function UpdateGuestInfo(name, displayName, email)
{
	$.ajax({
		type: "POST",
		url: "webservices/GuestList.asmx/UpdateGuestInfo",
		data: "{'guestId':'" + CURRENT_GUEST_ID + "','email':'" + email + "','name':'" + name + "','displayName':'" + displayName + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg)
		{
			if (msg.d)
			{
				//alert('success');
				setTimeout(function() { $('#EditMyInfo').html('<h3>Your information has been updated</h3>'); }, 1000);
				setTimeout(function() { location.reload(true); }, 500);
			}
		}
	});
}

function DisplayEditForm(guestId)
{
	$.ajax({
		type: "POST",
		url: "webservices/GuestList.asmx/GetGuestInfo",
		data: "{'guestId':'" + guestId + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg)
		{
			var data = $.evalJSON(msg.d);
			$('#tbNameEdit').val(data.Name);
			$('#tbDisplayNameEdit').val(data.DisplayName);
			$('#tbEmailEdit').val(data.Email);
		}
	});
	
	$('#UploadButton').upload({
		name: 'file',
		method: 'post',
		enctype: 'multipart/form-data',
		action: '/handlers/Upload.ashx',
		onSubmit: function()
		{
			$('#UploadStatus').html('Uploading file... <img src="/images/ajax-loader1.gif" style="vertical-align:middle;" />');
			$('#UploadStatus').show();
		},
		onComplete: function(data)
		{
			var statusHtml = '';
			if (data == 'success')
			{
				statusHtml = 'File uploaded successfully';
			}
			else
			{
				statusHtml = 'Oops, something went wrong...<br />';
				if (data.indexOf('failure') != -1)
				{
					if (data.indexOf('^invalid file type') != -1)
					{
						statusHtml += 'You are attempting to upload a file that is not an image. You can upload .jpg, .gif, .bmp, .png files.';
					}
					else
					{
						statusHtml += data.substr(data.indexOf('^') + 1);
					}
				}
				else if (data.indexOf('404.13'))
				{
					statusHtml += "The file you are trying to upload is too large";
				}

			}
			$('#UploadStatus').html(statusHtml);
			$('#UploadStatus').show();
		}
	});
}

function SignIn(email)
{
	$.ajax({
		type: "POST",
		url: "webservices/UserService.asmx/SignIn",
		data: "{'email':'" + email + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg)
		{
			var data = $.evalJSON(msg.d);
			CURRENT_GUEST_ID = data.GuestId;
			setTimeout(function() { location.reload(true); }, 0);
		}
	});
}

function SignOut()
{
	$.ajax({
		type: "POST",
		url: "webservices/UserService.asmx/SignOut",
		data: "{}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg)
		{
			if (msg.d)
			{
				CURRENT_GUEST_ID = EMPTY_GUID;
				setTimeout(function() { location.reload(true); }, 0);
			}
		}
	});
	
}