var interval = 0;
var DEPTH = '/';

function play() {
	interval = setInterval('next()', 5000);
}

function pause() {
	clearInterval(interval);
}

function next() {
	$('#nav-right').click();
}

$(function(){
	var begin = 0;
	var scroll = $('#scroller');
	var width = $('.slide:first').width();
	var numslides = $('.slide').length;
	
	scroll.width(width*numslides);
	
	$('#nav-right').click(function(){
		begin++;
		if ( begin<numslides ) {
			scroll.animate({'margin-left': '-='+width}, 500);
		} else {
			scroll.fadeOut(500, function(){
				scroll.css('margin-left', 0);
				scroll.fadeIn(500);
				begin = 0;
			});
		}
	});
	
	var move = '';
	var timeId = 0;
	$('#slide-visible').hover(
		function(){
			pause();
		},
		function(){
			play();
		}
	);
	
	play();
	
	$('#nav-left').click(function(){
		begin--;
		if ( begin>=0 ) {
			scroll.animate({'margin-left': '+='+width}, 500);
		} else {
			scroll.fadeOut(500, function(){
				scroll.css('margin-left', -width*numslides+width);
				scroll.fadeIn(500);
				begin = numslides-1;
			});
		}
	});
	
	// contact form validation
	$('#ContactForm').submit(function(){
		var b = '';
		$(this).find('*:input').each(function(){
			var a = $(this);
			if ( !a.val() && a.attr('required') ) {
				a.css('background', '#FF9F9F');
				b = 'set';
			}
			if ( a.attr('id') == 'ContactEmail' ) {
				if ( !a.val().match(/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) ){
					a.css('background', '#FF9F9F');
					b = 'set';
				}
			}
			if ( a.attr('id') == 'ContactPhone' && a.val() ) {
				if ( !a.val().match(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/) ){
					a.css('background', '#FF9F9F');
					b = 'set';
				}
			}
		});
		if (b)
			return false;
		
		$('#ContactSubmit')
			.attr('disabled', true)
			.after('<img src="'+DEPTH+'img/img_loading_ccc.gif" height="16px" width="16px" />')
			.next('img')
			.css({position:'absolute', top:'6px', left:'86px'});
		
		
		$.post(
			DEPTH+'send_form_email.php',
			$('#ContactForm').serialize(),
			function(){
				$('.submit').fadeOut(function(){
					$('#ContactSubmit').next('img').remove();
					$('#ContactSubmit').attr('disabled', false);
					$('#ContactMessage').val('');
					$(this).after('<p class="success no-space">Woah nelly! We read you loud and clear!</p>');
				});
				
				setTimeout(
					function(){
						$('.success').fadeOut(function(){
							setTimeout(function(){ $('.submit').fadeIn(); }, 1000);
						});
					},
					5000
				);
			}
		);	
		
		return false;
		
	}).find('*:input:not(:submit)').focus(function(){ // reset form fields to white background when it gains focus
		$(this).css('background', '#FFF');
	});
	
	
	// get tweets
	getTwitters('tweet', { 
		id: 'nologydesign',
		count: 1, 
		enableLinks: true,
		newwindow: true,
		ignoreReplies: true, 
		template: '"%text%"',
		callback: function(){ $('#tweet').removeClass('center'); }
	});
	
	
	// quote form
	var quote = $('#quote');
	var quoteSlide = $('#quote-slide');
	var normal = '0';
	var hover = '-40px';
	var click = '-80px';
	var current = '0';
	
	quote.click(function(){
		quoteSlide.slideToggle();
	});
	
	quote.toggle(function(){
		quote.css('background-position', '0 -80px');
		current = '-80px';
	}, function(){
		quote.css('background-position', '0 0');
		current = '0';
	});
	
	quote.hover(function(){
		quote.css('background-position', '0 -40px');
	}, function(){
		quote.css('background-position', '0 '+current);
	});
	
	$('#QuoteForm').submit(function(){
		var b = '';
		$(this).find('*:input').each(function(){
			var a = $(this);
			if ( !a.val() && a.attr('required') ) {
				a.css('background', '#FF9F9F');
				b = 'set';
			}
			if ( a.attr('id') == 'QuoteEmail' ) {
				if ( !a.val().match(/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) ){
					a.css('background', '#FF9F9F');
					b = 'set';
				}
			}
			if ( a.attr('id') == 'QuotePhone' && a.val() ) {
				if ( !a.val().match(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/) ){
					a.css('background', '#FF9F9F');
					b = 'set';
				}
			}
		});
		if ( !$(this).find('*:checked').length ) {
			alert('We love new projects; however, it would be right lovely if you told us what kind of project you are looking to do. Please check a service of interest.');
			b = 'set';
		}
		if (b)
			return false;

		$.post(
			DEPTH+'send_quote_email.php',
			$('#QuoteForm').serialize(),
			function(){
				quoteSlide.slideToggle(function(){
					$('#QuoteForm').hide();
					$('#QuoteForm').after('<p class="quote-success">We have received your message and will get back to you soon! In the meantime... would you like a cookie?</p>');
					quoteSlide.slideToggle(function(){
						setTimeout(function(){ 
								quoteSlide.slideToggle(function(){
									$('.quote-success').remove();
									document.getElementById('QuoteForm').reset();
									$('#QuoteForm').show();
									quote.css('background-position', '0 0');
									current = '0';
								})
							}, 
								5000
						);
					});
				});
			}
		);	
		
		return false;
	});
	// reset form fields to white background when it gains focus
	$('#QuoteForm').find('*:input:not(:submit)').focus(function(){
		$(this).css('background', '#FFF');
	});
	
});