$(document).ready(function(){
	// Carousel Showcase
	$('#showcase').agile_carousel({
		disable_on_first_last_buttons: "no",
		first_last_buttons: "no",
		hover_next_prev_buttons: "no",
		next_prev_buttons: "no",
		pause_button: "no",
		slide_buttons: "yes",
		slide_captions: "Addictive Mobility wins Best Mobile Marketing 2009|2009 MobileMonday Toronto - Mobile Marketing Awards|Social Media Contesting Application|Interactive Out of Home Technology|Mobile SMS Text to Win Contest", 
		slide_links: "http://www.marketwire.com/press-release/Mobilemonday-Toronto-1042669.html",
		slide_directory: "preview/ui/slides",
		timer_length: "7000",
		transition_duration: 1000,
		transition_easing: "easeInOutExpo",	
		transition_type: "carousel"
	});
	
	var submit_name, submit_email, submit_type;
	$("div#newsletter input#subscribe").click( function() {
		$(this).attr("disabled","disabled");
		submit_name = $("#name").val();
		submit_email = $("#email").val();
		submit_type = $("#type").val();
		var submit_url = "submit/" + submit_type;
		var dataString = 'name='+ submit_name + '&email=' + submit_email;
		$.ajax({  
			type: "POST",  
			url: submit_url,  
			data: dataString,  
			success: function(msg) {  
				if (msg == "success") {
					$('form#newsletter_form').fadeOut( function() {
						$('div#newsletter').html("<span>Thank you for subscribing!</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 
						$("#backtoform").click( function(e) {
							e.preventDefault();
							displayNewsletterForm();
						});
					});
				} else if (msg =="error") {
					$('form#newsletter_form').fadeOut( function() {
						$('div#newsletter').html("<span style=\"color: red\">Error: Please check the form again.</span>");				
						setTimeout(displayNewsletterForm, 2000);						
					});
				}
			}
		});  
	});
	
	$('#name').one('focus', function() { $('#name').val(""); });
	$('#email').one('focus', function() { $('#email').val(""); });
	$('#message').one('focus', function() { $('#message').val(""); });
	
	function displayNewsletterForm() {
		$('div#newsletter span').fadeOut("fast", function() {
			$('div#newsletter').html('<form name="newsletter_form" id="newsletter_form"><input type="hidden" id="type" name="type" value="newsletter" /><input type="text" id="name" name="name" value="'+submit_name+'" /><input type="text" id="email" name="email" value="'+submit_email+'" /><input type="button" name="subscribe" id="subscribe" /></form>');
			$("div#newsletter input#subscribe").click( function() {
				$(this).attr("disabled","disabled");
				submit_name = $("#name").val();
				submit_email = $("#email").val();
				submit_type = $("#type").val();
				var submit_url = "submit/" + submit_type;
				var dataString = 'name='+ submit_name + '&email=' + submit_email;
				$.ajax({  
					type: "POST",  
					url: submit_url,  
					data: dataString,  
					success: function(msg) {  
						if (msg == "success") {
							$('form#newsletter_form').fadeOut("fast", function() {
								$('div#newsletter').html("<span>Thank you for subscribing!</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 
								$("#backtoform").click( function(e) {
									e.preventDefault();
									displayNewsletterForm();
								});
							});
						} else if (msg =="error") {
							$('form#newsletter_form').fadeOut("fast", function() {
								$('div#newsletter').html("<span style=\"color: red\">Error: Please check the form again.</span>");				
								setTimeout(displayNewsletterForm, 2000);						
							});
						}
					}
				});  
			});
		});
	}
	
		
	$("div#download input#signup").click( function() {
		$(this).attr("disabled","disabled");
		submit_name = $("#name").val();
		submit_email = $("#email").val();
		submit_type = $("#type").val();
		var submit_url = "../../submit/" + submit_type;
		var dataString = 'name='+ submit_name + '&email=' + submit_email;
		$.ajax({  
			type: "POST",  
			url: submit_url,  
			data: dataString,  
			success: function(msg) {  
				if (msg == "success") {
					$('form#download_form').fadeOut( function() {
						$('div#download').html("<span>Thank you for signing up. We will notify you when AMO is ready.</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 						
						$("#backtoform").click( function(e) {
							e.preventDefault();
							displayDownloadForm();
						});
					});
				} else if (msg =="error") {
					$('form#download_form').fadeOut( function() {
						$('div#download').html("<span style=\"color: red\">Error: Please check the form again.</span>");				
						setTimeout(displayDownloadForm, 2000);						
					});
				}
			}
		});  
	});
	
	
	function displayDownloadForm() {
		$('div#download span').fadeOut("fast", function() {
			$('div#download').html('<form name="download_form" id="download_form"><input type="hidden" id="type" name="type" value="download" /><input type="text" id="name" name="name" value="'+submit_name+'" /><input type="text" id="email" name="email" value="'+submit_email+'" /><input type="button" name="signup" id="signup" value="Sign Up"/></form>');
			$("div#download input#signup").click( function() {
				$(this).attr("disabled","disabled");
				submit_name = $("#name").val();
				submit_email = $("#email").val();
				submit_type = $("#type").val();
				var submit_url = "../../submit/" + submit_type;
				var dataString = 'name='+ submit_name + '&email=' + submit_email;
				$.ajax({  
					type: "POST",  
					url: submit_url,  
					data: dataString,  
					success: function(msg) {  
						if (msg == "success") {
							$('form#download_form').fadeOut("fast", function() {
								$('div#download').html("<span>Thank you for signing up. We will notify you when AMO is ready.</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 
								$("#backtoform").click( function(e) {
									e.preventDefault();
									displayDownloadForm();
								});
							});
						} else if (msg =="error") {
							$('form#download_form').fadeOut("fast", function() {
								$('div#download').html("<span style=\"color: red\">Error: Please check the form again.</span>");				
								setTimeout(displayDownloadForm, 2000);						
							});
						}
					}
				});  
			});
		});
	}
	
		
	var submit_position, submit_company, submit_website, submit_address, submit_phone, submit_message;
	$("div#inner_content button#contact").click(function(){
		//alert('test');
		$(this).attr("disabled","disabled");
		submit_name = $("#name").val();
		submit_email = $("#email").val();
		submit_type = $("#type").val();
		submit_position = $("#position").val();
		submit_company = $("#company").val();
		submit_website = $("#website").val();
		submit_address = $("#address").val();
		submit_phone = $("#phone").val();
		submit_message = $("#message").val();
		var submit_url = "../submit/" + submit_type;
		var dataString = 'name='+ submit_name + '&email=' + submit_email + '&position=' + submit_position + '&company=' + submit_company + '&website=' + submit_website + '&address=' + submit_address + '&phone=' + submit_phone + '&message=' + submit_message;
		$.ajax({  
			type: "POST",  
			url: submit_url,  
			data: dataString,  
			success: function(msg) {  
				if (msg == "success") {
					$('form#contact_form').slideUp( function() {
						$('div#inner_content').append("<span><br />We have received the following message from "+submit_email+":<p>"+submit_message+"</p>We will contact you within 2 business days. Thank you for your interests in AddictiveMobility&trade;.</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 
						$("#backtoform").click( function(e) {
							e.preventDefault();
							displayContactForm();
						});
					});
					$('form#contact_form').remove();
				} else if (msg =="error") {
					$('form#contact_form').slideUp( function() {
						$('div#inner_content').append("<span style=\"color: red\"><br />Error: Please check the form again.</span>");
						$('form#contact_form').remove();
						displayContactForm();					
					});
				}
			}
		});  
	});
	
	$("div#inner_content button#reset").click(function() {
		$(this).parents("form").children().attr("value", "");
	});
	
	function displayContactForm() {
		$('div#inner_content').append('<form name="contact_form" id="contact_form"><input type="hidden" id="type" name="type" value="contact" /><br /><label for="name" class="required">Name</label><input type="text" id="name" name="name" value="'+submit_name+'" class="required" /><br /><label for="email" class="required">E-mail</label><input type="text" id="email" name="email" value="'+submit_email+'" class="required" /><br /><label for="position">Position</label><input type="text" id="position" name="position" value="'+submit_position+'" /><br /><label for="company">Company</label><input type="text" id="company" name="company" value="'+submit_company+'" /><br /><label for="website">Website</label><input type="text" id="website" name="website" value="'+submit_website+'" /><br /><label for="address">Address</label><input type="text" id="address" name="address" value="'+submit_address+'" /><br /><label for="phone">Phone Number</label><input type="text" id="phone" name="phone" value="'+submit_phone+'" /><br /><label for="position" class="required">Message</label><textarea id="message" name="message" class="required">'+submit_message+'</textarea><br /><button type="button" name="contact" id="contact">Submit</button><button type="button" name="reset" id="reset">Reset</button></form>');
		$('div#inner_content form').hide().slideDown();
		$("div#inner_content button#contact").click(function(){
			$(this).attr("disabled","disabled");
			$('div#inner_content > span').remove();
			submit_name = $("#name").val();
			submit_email = $("#email").val();
			submit_type = $("#type").val();
			submit_position = $("#position").val();
			submit_company = $("#company").val();
			submit_website = $("#website").val();
			submit_address = $("#address").val();
			submit_phone = $("#phone").val();
			submit_message = $("#message").val();
			var submit_url = "../submit/" + submit_type;
			var dataString = 'name='+ submit_name + '&email=' + submit_email + '&position=' + submit_position + '&company=' + submit_company + '&website=' + submit_website + '&address=' + submit_address + '&phone=' + submit_phone + '&message=' + submit_message;
			$.ajax({  
				type: "POST",  
				url: submit_url,  
				data: dataString,  
				success: function(msg) {  
					if (msg == "success") {
						$('form#contact_form').slideUp( function() {
							$('div#inner_content').append("<br /><span>We have received the following message from <strong>"+submit_email+"</strong>:<p>"+submit_message+"</p><br />We will contact you within 2 business days. Thank you for your interests in AddictiveMobility&trade;.</span><br /><span id=\"backtoform\"><a href=\"#\">&lt;&lt; Back to Form</a></span>"); 
							$('form#contact_form').remove();
							$("#backtoform").click( function(e) {
								e.preventDefault();
								$('div#inner_content').html('<h1>Contact Us</h1>');
								displayContactForm();
							});
						});
					} else if (msg =="error") {
						$('form#contact_form').slideUp( function() {
							$('div#inner_content').append("<br /><span style=\"color: red\">Error: Please check the form again.</span>");				
							$('form#contact_form').remove();
							displayContactForm();
						});
					}
				}
			});  
		});		
		$("div#inner_content button#reset").click(function() {
			$(this).parents("form").children().attr("value", "");
		});
		$('#name').one('focus', function() { if ($(this).val() == "(Required)") { $('#name').val(""); }});
		$('#email').one('focus', function() { if ($(this).val() == "(Required)") { $('#email').val(""); }});
		$('#message').one('focus', function() { if ($(this).val() == "(Required)") { $('#message').val(""); }});
	}	
});