var madebyping = {
	init: function() {
		this.home_js();
		this.about_js();
		this.our_work_js();
		this.contact_js();
		this.email();
	},
	
	home_js: function() {
		var count = 2;
		setInterval(function() {
			$('body > .content > .slider > img').each(function(index) { $(this).fadeOut(200); });	// value for the fade out
			$('body > .content > .slider > img.img_' + count).fadeIn(1500); // value for the fade in (in milliseconds)
			count = count >= 3 ? 1 : count + 1;
		}, 7000);
	},
	
	about_js: function() {
		var length = $('body > .about > .slider > .element').length;
		var count = 1;
		
		$($('body > .about > .slider > .element').get(0)).html();
		setInterval(function() {
			$('body > .about > .slider > .element').each(function(index) { $(this).fadeOut(0); });	// value for the fade out
			$($('body > .about > .slider > .element').get(count)).fadeIn(1500); // value for the fade in (in milliseconds)
			count = count >= length - 1 ? 0 : count + 1;
		}, 7000);
	},
	
	our_work_js: function() {
		if($('body > .our_work > .wrapper > section > .img_wrapper').html())
			$('body > .our_work > .wrapper > section > .img_wrapper').fancybox();
	},
	
	contact_js: function() {
		if($('.contact > form > .button').html()) {
			$('.contact > form > .button').click(function() {
				var ok = true;
				var regex = /^[ a-zA-Z1-9\'\.,-]{2,100}$/;
				if( $('body > .contact > form > .name').val() === 'YOUR NAME' ||
					!regex.test($('body > .contact > form > .name').val())) {
					$('body > .contact > form > .star_1').css({'background-position': '-166px -39px'});
					ok = false;
				} else
					$('body > .contact > form > .star_1').css({'background-position': '-132px -39px'});

				regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
				if( $('body > .contact > form > .email').val() === 'EMAIL' ||
					!regex.test($('body > .contact > form > .email').val())) {
					$('body > .contact > form > .star_2').css({'background-position': '-166px -39px'});
					ok = false;
				} else
					$('body > .contact > form > .star_2').css({'background-position': '-132px -39px'});
				
				regex = /^[ a-zA-Z0-9\.,\\$]{2,30}$/;
				if( $('body > .contact > form > .budget').val() === 'BUDGET' ||
					!regex.test($('body > .contact > form > .budget').val())) {
					$('body > .contact > form > .star_3').css({'background-position': '-166px -39px'});
					ok = false;
				} else
					$('body > .contact > form > .star_3').css({'background-position': '-132px -39px'});

				regex = /^[^<>#\*\/\\]{2,300}$/;
				if( $('body > .contact > form > textarea').val() === 'YOUR MESSAGE' ||
					!regex.test($('body > .contact > form > textarea').val())) {
					$('body > .contact > form > .star_4').css({'background-position': '-166px -39px'});
					ok = false;
				} else
					$('body > .contact > form > .star_4').css({'background-position': '-132px -39px'});
				
				if(ok) {
					$.ajax({
						type : 'POST',
						url : 'email.php',
						dataType : 'json',
						data: {
							name  : $('body > .contact > form > .name').val(),
							email : $('body > .contact > form > .email').val(),
							budget: $('body > .contact > form > .budget').val(),
							text  : $('body > .contact > form > textarea').val()
						},
						success : function(data){
							$('body > .contact > form').html('<h5>Thank you for your message</h5>');
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							alert('Server error');
						}
					});
				}
				return false;
			});
			$('body > .contact > form > input, body > .contact > form > textarea').focusin(function() {
				var that = $(this);
				if(that.val() === that.attr('title'))
					that.val('');
			});
			$('body > .contact > form > input, body > .contact > form > textarea').focusout(function() {
				var that = $(this);
				if (that.val() === '')
					that.val(that.attr('title'));
			});
		}
	},
	
	email: function() { $('footer > .contact').append('<a href=\"javascript:location.href=\'mailto:info@madebyping.com\';\" target="_blank" title=\"Contact Us - MadeByPing WEBDESIGN MARKETING\">info@madebyping.com</a>'); }
	
};
$(function() {
	madebyping.init();
});
