$(document).ready(function() {
	
	// Disable Enter Key
	$("form").keypress(function(e) {
		if (e.which == 13) {
			return false;
		}
	});
	
	// "Bump" Animation on Focus
	$('input').focus(function () {
		$(this).animate({ 
		padding: "5px"
		}, 100 );
	});
	
	$('input').blur(function () {
		$(this).animate({ 
		padding: "3px"
		}, 100 );
	});
	
	// Upload Files
	var filenumone = 1;
	var filenumtwo = 1;
	$('#fileInput1').uploadify({
		'uploader'  : 'script/uploadify.swf',
		'script'    : 'script/uploadify.php',
		'cancelImg' : 'images/cancel.png',
		'auto'      : true,
		'folder'    : 'uploads',
		'onComplete': function(event, queueID, fileObj, response, data) {
			$('#filesUploaded1').append('<input type="hidden" name="upload'+filenumone+'" value='+response+'/><span class="filename">' +fileObj.name+ ' </span>Uploaded Successfully<br/>');
			filenumone++;
		}
	});
	
	$('#fileInput2').uploadify({
		'uploader'  : 'script/uploadify.swf',
		'script'    : 'script/uploadify.php',
		'cancelImg' : 'images/cancel.png',
		'auto'      : true,
		'folder'    : 'uploads',
		'onComplete': function(event, queueID, fileObj, response, data) {
			$('#filesUploaded2').append('<input type="hidden" name="upload'+filenumtwo+'" value='+response+'/><span class="filename">' +fileObj.name+ ' </span>Uploaded Successfully<br/>');
			filenumtwo++;
		}
	});
	
	// Add Players
	var current1 = 1;
	var current2 = 1;
	function addPersonOne() {
		current1++;
		var strToAdd = '<label for="player'+current1+'">Player '+current1+':</label><span class="float-right"><input id="player'+current1+'" name="player'+current1+'" value="Name" class="playername" maxlength="64"/><input type="text" value="No." class="number" name="player'+current1+'number" maxlength="4"/></span><br/>';
		$('#mainField1').append(strToAdd);
	}
	
	function addPersonTwo() {
		current2++;
		var strToAdd = '<label for="player'+current2+'">Player '+current2+':</label><span class="float-right"><input id="player'+current2+'" name="player'+current2+'" value="Name" class="playername" maxlength="64"/><input type="text" value="No." class="number" name="player'+current2+'number" maxlength="4"/></span><br/>';
		$('#mainField2').append(strToAdd);
	}
	$('.addPerson1').click(addPersonOne);
	$('.addPerson2').click(addPersonTwo);
	
	// Activate Date Picker
	$(".datepicker").datepicker();
	
	// Show and Hide The Different Forms
	$('#new-form').hide(0);
	$('#existing-form').hide(0);
	
	$('#new').click(function () {
		$('#new-form').show(350);
		$('#existing-form').hide(350);
	});
	
	$('#existing').click(function () {
		$('#existing-form').show(350);
		$('#new-form').hide(350);
	});
	
	// Input value switcher: First Name
	$('.first').focus(function () {
		if ($(this).val() == 'First') {
			$(this).val('');
		}
	});
	
	$('.first').blur(function () {
		if ($(this).val() == '') {
			$(this).val('First');
		}
	});
	
	// Input value switcher: Last Name
	$('.last').focus(function () {
		if ($(this).val() == 'Last') {
			$(this).val('');
		}
	});
	
	$('.last').blur(function () {
		if ($(this).val() == '') {
			$(this).val('Last');
		}
	});
	
	// Input value switcher: Player Name
	$('.playername').live('focus', function () {
		if ($(this).val() == 'Name') {
			$(this).val('');
		}
	});
	
	$('.playername').live('blur', function () {
		if ($(this).val() == '') {
			$(this).val('Name');
		}
	});
	
	// Input value switcher: Player Number
	$('.number').live('focus', function () {
		if ($(this).val() == 'No.') {
			$(this).val('');
		}
	});
	
	$('.number').live('blur', function () {
		if ($(this).val() == '') {
			$(this).val('No.');
		}
	});
	
	// Validate form
	$("#new-form").validationEngine();
	$("#existing-form").validationEngine();
	
	// Fade Effect for Buttons
	if (jQuery.browser.msie) {
	} else {
		$('.fadeThis').append('<span class="hover"></span>').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function () {
				$span.stop().fadeTo(200, 1);
			}, function () {
				$span.stop().fadeTo(300, 0);
			});
		});
	}
	
	// Character Limit on Textarea
	function limitChars(textid, limit, infodiv) {
		var text = $('#'+textid).val();	
		var textlength = text.length;
		if(textlength > limit) {
			$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
			$('#'+textid).val(text.substr(0,limit));
				return false;
		} else {
		$('#' + infodiv).html((limit - textlength) +' characters remaining.');
		return true;
		}
	}

	$('#notes').keyup(function(){
		limitChars('notes', 512, 'charlimitinfo');
	});
});
