jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

$(document).ready(function(){
	
	// png fixes
	//	$('div#header a').pngFix();
	
	$( "#launch-poll" ).click( function (){
		//$( "#bid-layer" ).css( "display", "block" );
		$( "#overlay" ).css( "display", "block" );
		$( "#poll-layer" ).slideToggle("fast");
		return false;
	});
	
	$( ".close-poll" ).click( function(){
		closePoll();
		return false;
	});
	
	/* Show/hide poll form helper text */
	$( "#poll-person #name" ).focus( function(){
		if ( $( this ).val() == "Name" ){
			$( this ).val( "" ).css( "color", "#463031" );
		}
	}).blur( function (){
		if ( $( this ).val() == "" ){
			$( this ).val( "Name" ).css( "color", "#999" );
		}
	});
	$( "#poll-person #phone" ).focus( function(){
		if ( $( this ).val() == "Telephone" ){
			$( this ).val( "" ).css( "color", "#463031" );
		}
	}).blur( function (){
		if ( $( this ).val() == "" ){
			$( this ).val( "Telephone" ).css( "color", "#999" );
		}
	});
	$( "#poll-person #email" ).focus( function(){
		if ( $( this ).val() == "Email" ){
			$( this ).val( "" ).css( "color", "#463031" );
		}
	}).blur( function (){
		if ( $( this ).val() == "" ){
			$( this ).val( "Email" ).css( "color", "#999" );
		}
	});
	
	$( "#enter-draw" ).click( function() {
		
		var poll = $( "#poll-question input:radio:checked" );
		if ( poll.val() == undefined ){
			$( "#poll-error" ).css( "display", "inline" );
			return false;
		}
		$( "#poll-error" ).css( "display", "none" );
		
		// Get form data
		var name = $( "#name" ).val();
		var email = $( "#email" ).val();
		var phone = $( "#phone" ).val();
		var answer = poll.val();
				
		var thankyoumessage = "";
		
		errormessage = false;
		nameError = false;
		phoneError = false;
		emailError = false;
		
		// Validate all required fields have values
		if ( name == "" || name == "Name" ){
			nameError = true;
			errormessage = true;
		}
		if ( phone == "" || phone == "Telephone"){
			phoneError = true;
			errormessage = true;
		}
		if ( email == "" || phone == "Email" ){
			emailError = true;
			errormessage = true;
		}
		else if ( ! isValidEmailAddress( email ) ){
			emailError = true;
			errormessage = true;
		}
		
		datastring = "answer="+answer+"&name="+name+"&phone="+phone+"&email="+email;
		
		// Reset backgrounds
		$( "#phone, #name, #email" ).css( "border", "1px solid #ccc" );
		$( "#error-message" ).css( "display", "none" );
		
		if ( !errormessage ){
			 
			// Ajax call to insert poll data
			$.ajax({
				dataType: "jsonp",
			  	data: datastring,
			  	jsonp: "jsonp_callback",
			  	url: "http://203.109.201.49/lumley/tourdata/poll-entry.php",
			//	url: "http://localhost/lumley.co.nz/broker/poll-entry.php",
			  	success: function (data) {
					if(data != "error" ){
						$( "#poll-layer" ).html( "" ).addClass( "poll-thankyou" ).animate( {opacity: 1.0}, 1000 ).fadeToggle( 2000 );
						$( "#overlay" ).css( "display", "none" );
					}
					else{
						alert(data);
					}
			  	}
			}); 	
		 }
		 else {
		 	if ( nameError ){
		 		$( "#name" ).css( "border", "1px solid #F8040A" );
			}
			if ( emailError ){
		 		$( "#email" ).css( "border", "1px solid #F8040A" );
			}
			if ( phoneError ){
				$( "#phone" ).css( "border", "1px solid #F8040A" );
			}
			
			//Display error message
			$( "#error-message" ).css( "display", "block" ).animate( { "marginTop" : "0" }, 100).animate( { "marginTop" : "5px" }, 100);
		 }
		return false ;
	}); 
	
	$( "#overlay" ).click( function(){
		closePoll();
		return false;
	});
	
});

// Validate that email address is well formed
function isValidEmailAddress( emailAddress ) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function closePoll(){
	$( "#overlay" ).css( "display", "none" );
	$( "#poll-layer" ).slideToggle("fast");
}
