 $(function() {
	
	$('#lets-fight').click(function() { $('#opponent').focus(); return false; });
	
	$('#submit').click(function() {
		
		var retVal = true;
		var values = [
			{
				'existSelector':'#opponent',
				'valueSelector':'#opponent',
				'error':'Woah there. We need a twitter username!'
			},
			{
				'existSelector':'#fighters',
				'valueSelector':'input[name="fighter"]:checked',
				'error':'You haven\'t chosen your fighter.'
			},
			{
				'existSelector':'#attacks',
				'valueSelector':'input[name="move"]:checked',
				'error':'You haven\'t selected the attack.'
			}
		];
		
		$.each( values, function(i,val){
		
			//if it exists on the page
			if( $( val.existSelector).length > 0 ){
				
				//If it has no value
				if( !$( val.valueSelector ).val() ){
					$( val.existSelector ).after('<p class="error">'+ val.error +'</p>');
					$( val.existSelector ).focus();
					window.location = val.existSelector;
					
					retVal = false;
				}
			}
			
		});
		
		return retVal;
	});
	
	$('#opponent').blur(function() {
		$('.error').empty();

		$.post("/matches/limit", {'username': $('#opponent').val()}, function(d) {
			if(d == "0") {
				$('#opponent').before('<p class="error">Damn, that\'s a a popular fighter, try someone else.</p>');
			};
		});	
	});
	
	$('label').click(function() {
		
		$(this).closest('ul').find('img.selected').removeClass('selected');
		$(this).closest('ul').find('img').css('backgroundColor', 'transparent');
		$(this).closest('ul').find('img').pulse({}, {}).stop();
		
		var img = $(this).children('img').addClass('selected');
		
		img.pulse({ 
			backgroundColor: ['red', 'yellow'],
		}, {
			duration: 75,
			times: 5,
			complete: function() {
				//$(this).css('backgroundColor', 'yellow');
			},
		});
	});
	
	$('input[placeholder]').placeHeld();
	
	// Tabs
	
	$('#tabs').tabs();
	
});
