var $j = jQuery.noConflict();

$j.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

function removeError() {
	$j("input#name").removeClass("inputerror");
	$j("input#email").removeClass("inputerror");
	$j("textarea#message").removeClass("txtareaerror");
}


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);
}

jQuery(document).ready(function($j){ 
$j('.error').hide();
removeError();
	// ROLLOVER
	$j("img.ro").hover(
		function()
		{
			this.src = this.src.replace(".gif","-ro.gif");
		},
		function()
		{
			this.src = this.src.replace("-ro.gif",".gif");
		});

		// CYCLE CALL
		$j('#slide').cycle({ 
			fx:      'scrollLeft', 
			speed:    1500, 
			timeout:  3500
		});

		// SOCIAL ROLLOVER
		$j('#fb').hover(function(){
			$j('#text-ro').text('Follow us on Facebook');					
		},
		function(){
			$j('#text-ro').text('');					
		});


		$j('#twitter').hover(function(){
			$j('#text-ro').text('Follow us on Twitter').fadeIn("slow");					
		},
		function(){
			$j('#text-ro').text('');					
		});

		$j('#phone').hover(function(){
			$j('#text-ro').text('Give us a call');					
		},
		function(){
			$j('#text-ro').text('');					
		});

		$j('#slide').hover(function(){
			$j('#text-ro').text('Click on a slide to go to view the Website.');					
		},
		function(){
			$j('#text-ro').text('');					
		});

		$j('#logo').hover(function(){
			$j('#text-ro').text('SScccreeeeeeeaaaaach!!!!');					
		},
		function(){
			$j('#text-ro').text('');					
		});

		$j('#tagline').hover(function(){
			$j('#text-ro').text("That's right, we do it all.");					
		},
		function(){
			$j('#text-ro').text('');					
		});

		$j('img[rel]').hover(function(){
			$j('#text-ro').text("Send us your contact info and we can begin the journey.");					
		},
		function(){
			$j('#text-ro').text('');					
		});

		// JQUERY TOOL CALL
		$j("img[rel]").overlay();


		$j('#contactbtn').hover(function() {
			$j(this).addClass('hover');
		}, function() {
			$j(this).removeClass('hover');
		});

		$j("#submit_btn").click(function() {
			$j('.error').hide();
       		removeError();

			var name = $j("input#name").val();
			if (name == "") {
				$j("label#name_error").show();
				$j("input#name").focus();
				$j("input#name").addClass("inputerror");				
				return false;
			}
			var email = $j("input#email").val();
			if (email == "") {
				$j("label#email_error").show();
				$j("input#email").focus();
				$j("input#email").addClass("inputerror");
				return false;
			}
			
			if ((!isValidEmailAddress(email))) {
				$j("label#email_valid_error").show();
				$j("input#email").focus();
				$j("input#email").addClass("inputerror");
				return false;
			}
			
			var phone = $j("input#phone").val();
			
			if(phone == null){
				phone = "";
			}
			
			var message = $j("textarea#message").val();
			if (message == "") {
				$j("label#message_error").show();
				$j("textarea#message").focus();
				$j("textarea#message").addClass("txtareaerror");
				return false;
			}

			var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&message=' + message;

			$j.ajax({
				type: "POST",
				url: "assets/formValidate/process.php",
				data: dataString,
				success: function() {					
					$j('.details').fadeOut("def", function(){ 
					$j('form #contact_form').clearForm();
					$j('.details').html("<div id='newmessage'></div>");
					$j('#newmessage').html("<h2>Contact Form Submitted!</h2><p>Thank You!<br/>	Your request for more information has been submitted to Screaming Eagle Media.<br/>	We will get back to you ASAP.</p>");
					$j('.details').fadeIn("def");
					});
				}
			});
			return false;
		});

});