// JavaScript Document
$(document).ready(function() {
	
	$('#coda-slider-1').codaSlider();
  
    // set your twitter id
    var user = 'the_goodsam';
      
    // using jquery built in get json method with twitter api, return only one result
    $.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=3&callback=?', function(data)      {
          
        // result returned
        //var tweet = data[0].text;
		var tweet = "";
		for (i = 0; i < data.length; i++) {
			tweet = data[i].text;
			
			// process links and reply
			tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
				return '<a href="'+url+'">'+url+'</a>';
			}).replace(/B@([_a-z0-9]+)/ig, function(reply) {
				return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			});
			
			$("#quote" + (i + 1)).html(tweet);
			
		}
		
    }); 
	
	jQuery(".clear_input").val("");	
	
	jQuery("#dialog-contact").dialog({
		autoOpen: false, height: 75,
			buttons: {
				Ok: function() {
					jQuery(this).dialog('close');
					jQuery('#ul_overlay').fadeOut("slow");
					jQuery(".clear_input").val("");
				}
			}

	});	
	
	jQuery("#dialog-mailing").dialog({
		autoOpen: false, height: 75,
			buttons: {
				Ok: function() {
					jQuery(this).dialog('close');
					jQuery('#ul_overlay').fadeOut("slow");
					jQuery(".clear_input").val("");
				}
			}

	});			
      
});


/**
*
*
*
*
**/
function validateForm() {
	
	var name    = document.getElementById("your-name").value;
	var email   = document.getElementById("email").value;
	var phone   = document.getElementById("phone").value;
	var message = document.getElementById("message").value;
	//var quiz    = document.getElementById("quiz").value;
	
	// Loop through and check all the input elements are filled out right
	var FormFilled = true;
	
	// Check if the name input is filled out properly
	if (name == ""){
		jQuery("#your-name").addClass("required_input");
		FormFilled = false;
	} else 
		jQuery("#your-name").removeClass("required_input");
		
	// Check if the email input is filled out properly		
	if (email == ""){
		jQuery("#email").addClass("required_input");
		FormFilled = false;		
	} else
		jQuery("#email").removeClass("required_input");		
	
	// Check if the message input is filled out properly
	if (message == ""){		
		jQuery("#message").addClass("required_input");
		FormFilled = false;		
	} else
		jQuery("#message").removeClass("required_input");
		
/*
	if (quiz != 4) {
		jQuery("#quiz").addClass("required_input");
		FormFilled = false;			
	} else
		jQuery("#quiz").removeClass("required_input");	
*/

	// If any of the required inputs aren't filled out don't submit the form
	if (!FormFilled)
		return false;


	jQuery.ajax({
	   type: "POST",
	   url: "mailer.php",
	   data: jQuery("#contact_form_elem").serialize(),
	   success: function(msg){
		 	jQuery('#ul_overlay').fadeIn("slow");
			jQuery('#dialog-contact').fadeIn("slow").dialog('open');
	   }
	 });
	 
	
	jQuery('#ul_overlay').fadeIn("slow");
	jQuery('#dialog-contact').fadeIn("slow").dialog('open');
	
	return false;
	
	//Submit the input form
	document.getElementById('contact_form_elem').submit();		
	
}


/**
*
*
*
*
**/
function addMailingList() {
	
	var email   = document.getElementById("input-email").value;
	
	// Loop through and check all the input elements are filled out right
	var FormFilled = true;
		
	// Check if the email input is filled out properly		
	if (email == ""){
		FormFilled = false;		
	}

	// If any of the required inputs aren't filled out don't submit the form
	if (!FormFilled)
		return false;


	jQuery.ajax({
	   type: "POST",
	   url: "../components/mail-list.php",
	   data: jQuery("#subForm").serialize(),
	   success: function(msg){
		 	jQuery('#ul_overlay').fadeIn("slow");
			jQuery('#dialog-mailing').fadeIn("slow").dialog('open');
	   }
	 });
	
	return false;
	
}
