$(document).ready(function(){	
	// GENERAL NAVIGATION
	function goContact(){
		$('#header-shadow').fadeIn(1000);
		$('#contact')
			.animate({
				opacity: 1
			}, 500)
			.animate({
				top: '0px'
			},{
				duration: 1200,
				easing: 'easeOutBack'
			});
	};
	
	function closeContact(){
		$('#header-shadow').fadeOut(1000);
		$('#contact')
			.animate({
				top: '-400px'
			});
	}
	
	function goAbout(){
		$('#about-bg').fadeIn(750);
		$('#about')
			.animate({
				opacity: 1
			}, 750)
			.animate({
				top: '100px'
			});
		
		$('#about h2').fadeOut();
		
		$('#about p, #about h4')
			.animate({
				opacity: 1
			}, 900)
			.fadeIn(750);
	}
	
	function closeAbout(){
		$('#about')
			.animate({
				top: '480px'
			});
		$('#about h2').fadeIn();
		$('#about-bg, #about p, #about h4').fadeOut();
	}
	
	$('#nav a')
		.click(
			function(){
				// VIEW OUR WORK
				if ($(this).attr('href') == '?p=portfolio'){
					closeContact();
					closeAbout();
				};
				
				// ABOUT THE DUDES
				if ($(this).attr('href') == '?p=about'){
					closeContact();
					goAbout();
				};
				
				// GET A QUOTE
				if ($(this).attr('href') == '?p=quote'){
					closeAbout();
					goContact();
				};
				
				// GET IN TOUCH
				if ($(this).attr('href') == '?p=contact'){
					closeAbout();
					goContact();
				};
				
				if ($(this).attr('rel') == 'out'){
					url = $(this).attr('href');
					window.location = url;
				}
				$('#nav a').removeAttr('id');
				$(this).attr('id','current');
				
				return false;
			}
		);
	
	$('#contact-form')
		.submit(
			function(){
				name = $('#name').val();
				email = $('#email').val();
				subject = $('#subject').val();
				message = $('#message').val();
				
				$.ajax({
					type: "POST",
					url: "ajax/contact.php",
					data: {name:name, email:email, subject:subject, message:message, sendemail:'yes'},
					success: function(){
						$('a[href="?p=contact"]').text('Get in Touch Again');
						closeContact();
						$('#name').val('');
						$('#email').val('');
						$('#subject').val('');
						$('#message').val('');
						$('#nav a').removeAttr('id');
						$('a[href="?p=portfolio"]').attr('id','current');
					}
				})
				
				return false;
			}
		);
	
	
	// SLIDER FUNCTIONALITY
	hold = true;
	animate = true;
    
    w = $('#slider').width();
	h = $('#slider').height();
	
	n = $('#slider li').size();
	c = 1;
	
	$('#slider')
		.hover(
			function(){
				hold = false;
			},
			function(){
				hold = true;
			}
		);
	
	$('#controls')
		.click(
			function(){
				if ($(this).attr('class') == 'pause'){
					$(this).attr('class','play');
					animate = false;
				} else {
					$(this).attr('class','pause');
					animate = true;
				}
			}
		);
	
	$('#slider ul')
		.css({
			width: w*n+'px'
		});
	
	$('#slider li')
		.css({
			width: w+'px',
			height: h+'px'
		});
	
	for(i=0; i < n; i++){
		num = i+1;
		
		$('#thumbs').append('<a href="'+num+'" class="t'+num+'" rel="'+num+'">'+num+'</a>');
		$('#slider li:nth-child('+num+')').attr('id',num);
	};
	
	$('a.t'+c).attr('id','selected');
	
	$('#thumbs a')
		.click(
			function(){
				numb = $(this).attr('rel')-1;
				$('#thumbs a').removeAttr('id');
				c = parseInt($(this).attr('rel'));
				
				$(this).attr('id','selected');
					
					$('#slider ul')
						.css({
							left: '-'+(w*numb)+'px'
						});
				
				animate = false;
				$('#controls').attr('class','play');
				return false;
			}
		);
	
	bcolor = '#'+$('#slider li:nth-child(1)').attr('rel');
	
	$('#slider').css('borderColor',bcolor);
	
	function slideIt(){
			
		if (animate){
			if (hold){
			x = $('#slider ul').css('left');
			
			$('#thumbs a').removeAttr('id');
			
			// IF IT REACHES THE END
			if (x === '-'+w*(n-1)+'px'){
				c = 1;
				$('#slider li blockquote')
					.animate({
						opacity: 0
					}, 1000)
					.animate({
						opacity: 0
					}, 2000)
					.animate({
						opacity: .75
					});
					
				$('#slider ul')
					.animate({
						opaicty: 1
					}, 1000)
					.animate({
						left: 0
					},{
						duration: 1000
					});
			} else {
				c = c+1;
				
				$('#slider li blockquote')
					.animate({
						opacity: 0
					}, 1000)
					.animate({
						opacity: 0
					}, 750)
					.animate({
						opacity: .75
					}, 1000);
				
				$('#slider ul')
					.animate({
						opacity: 1
					}, 1000)
					.animate({
						left: '-='+w+'px'
					},{
						duration: 750,
						easing: 'easeOutBack'
					});
			}
			
			$('a.t'+c).attr('id','selected');
			
			bcolor = '#'+$('#slider li:nth-child('+c+')').attr('rel');
			$('#slider')
				.animate({
					borderBottomColor: bcolor,
					borderTopColor: bcolor,
					borderLeftColor: bcolor,
					borderRightColor: bcolor
				});
		}
	}
		
	}
	
	setInterval(slideIt, 6000);

});