/* Author: 
	Marco Lisci - BadShark Communications
*/

// Skills Stripes
$('section.skills ul li:nth-child(2n)').addClass('even');
// Works Styles
$('section.works ul li:nth-child(3n+1)').addClass('first');

$(document).ready(function() {
	
	// Add Classes for tooltip
	$('header a').addClass('btip');
	$('footer nav a').addClass('btip');
	$('.prefooter .clients a:not(em a)').addClass('ttip');
	// Generate Bottom ToolTip
	$('a.btip').qtip({ 
		position: {
			corner: {
		    	target: 'bottomMiddle',
		    	tooltip: 'topMiddle'
			},
			adjust: {
				y: 12
			}
		},
		style: { 
			name: 'light',
			tip: { 
				corner: 'topMiddle',
				color: '#dadada',
				size: {
					x: 12, 
					y : 6 
				}
			},
			border: {
		 		width: 3,
				radius: 3,
				color: '#dadada'
			},
			width: {
				max: 200
			}
		}
	});
	// Generate Top ToolTip
	$('a.ttip').qtip({ 
		position: {
			corner: {
		    	target: 'topMiddle',
		    	tooltip: 'bottomMiddle'
			},
			adjust: {
				y: -6
			}
		},
		style: { 
			name: 'light',
			tip: { 
				corner: 'bottomMiddle',
				color: '#dadada',
				size: {
					x: 12, 
					y : 6 
				}
			},
			border: {
		 		width: 3,
				radius: 3,
				color: '#dadada'
			},
			width: {
				max: 200
			}
		}
	});
	
	// ColorBox
	$('.thumb:not(.ext) a').colorbox({
		transition: "elastic",
		close: "close",
		maxWidth: "70%",
		initialWidth: 100,
		initialHeight: 100,
		scalePhotos: true
	});
	
	// Work Title Click
	$('section.works h3 a').click(function(event) {
		// Open lightbox
		$(this).parent().parent().find('.thumb a').click();
		// Play Video
		player = $(this).parent().parent().find('iframe');
		$f(player[0]).api('play');
		return false;
	});
	
	// Reset inputs on focus
	$("#contact input, #contact textarea").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// Go To Form and focus.
	$("a[href='#contact']").bind("click",function(e) {
		$('html, body').animate( { scrollTop: $(document).height()-$(window).height() }, 'slow', function () {
			$("#contact #name").focus().val('Name').addClass('animated').addClass('pulse');
		});
		return false;
	});
	
	// Contact Form
	$("#contact #submit").click(function(){
		
		$("#contact input").removeClass('error');
		
		 // show the red text spinner
		 $('#contact .validate').html('Checking fields...');
		
		 // Get and Check Name field 
		var name = $("#contact #name").val();
		if (name == "") {  
		      $("#contact #name").addClass('error');  
		      $("#contact #name").focus();
				$('#contact .validate').addClass('red').html('Please check your name.');
		      return false;  
		    }
		
		 // Get and Check Message field 
		var message = $("#contact #message").val();
		if (message == "") {  
		      $("#contact #message").addClass('error');  
		      $("#contact #message").focus();
				$('#contact .validate').addClass('red').html('Please write a message.');
		      return false;  
		    }
		
		 // Get and Check Email field 
		var email = $("#contact #email").val();
		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);
		}
		if (email == "" || (isValidEmailAddress(email) == false) ) {  
		      $("#contact #email").addClass('error');  
		      $("#contact #email").focus();
				$('#contact .validate').addClass('red').html('Please check your email.');
		      return false;  
		    }
		
		 // Get Budget field 
		var budget = $("#contact #budget").val();
		
		 // Create dataString for Ajax call 
		var dataString = 'name='+ name + '&email=' + email + '&message=' + message + '&budget=' + budget + '&submit=submit';  
		
		 //the main ajax request
		 $.ajax({
				type: "POST",
				data: dataString,
				url: "/wp-content/themes/badshark/contact.php",
				success: function(data, message) {
					$('#contact').html('<div class="messageOut"></div>');
					$('#contact .messageOut').html(data);
				}
		  });
		return false;
	});
	
});	

