// JavaScript Document
var projectsHover = {    
	 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
	 interval: 50, // number = milliseconds for onMouseOver polling interval    
	 over: slideRight, // function = onMouseOver callback (REQUIRED)    
	 timeout: 50, // number = milliseconds delay before onMouseOut    
	 out: slideLeft // function = onMouseOut callback (REQUIRED)    
};

$(function(){	
	$('.container #featured_homepage').animate({opacity:1},500);
	$("ul#projects li div span").animate({opacity: 0.2},300).show();
	$("ul#thumbs li span").not(':eq(0)').animate({opacity: 0.7},500).show();
	$("ul#thumbs li span:eq(0)").animate({opacity: 0.0},10).show();
	$("ul#mainImages li:eq(0)").fadeIn();
	$("ul#projects li div").hoverIntent(projectsHover);
	//Project view page, change main image 
	$('ul#thumbs li').click(function(){
		if($('span', this).css('opacity') == '0.7'){
			var imageId = $(this).attr('id').split('_');
			imageId = imageId[1];
			$('ul#mainImages li:visible').fadeOut();
			$('ul#mainImages li#main_'+imageId).fadeIn();
			var lis = $('ul#thumbs li span');
			$.each(lis, function(){
				if($(this).css('opacity') == 0){
					$(this).animate({opacity: 0.7},500);
				}
			});
			$('span', this).animate({opacity: 0},500);
		}
	});
	
	// Contact form validation
	if($('#contactForm').length > 0){
		$('#contactForm').submit(function(){
			var continueMe = true;
			if($('#PostMessage').val() == ''){
				$('#PostMessage').addClass('outline').focus();
				continueMe = false;
			} else {
				$('#PostMessage').removeClass('outline')
			}				
			if(!validateEmail($('#PostEmail').val())){
				$('#PostEmail').addClass('outline').focus();
				continueMe = false;
			} else {
				$('#PostEmail').removeClass('outline')
			}		
			if($('#PostName').val() == ''){
				$('#PostName').addClass('outline').focus();
				continueMe = false;
			} else {
				$('#PostName').removeClass('outline')
			}
			if(continueMe == false){
				$('#error', this).fadeIn();
				return false;
			}
		});
	}
	
	if($('#examples').length > 0){
		$("#examples").CloudCarousel(	
		{			
			xPos: 370,
			yPos: 113,
			yRadius:-75,
			minScale: 0.02,
			reflHeight:40,
			reflOpacity:0.3,
			buttonLeft: $("#left"),
			buttonRight: $("#right"),
			autoRotate: 'right',
			autoRotateDelay: 8000
		}
	);
	}
});

function rand(n){
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}


function slideRight(){
	$('span', this).animate({left: '292px', width: '0px', opacity: 0},300);
	$(this).animate({borderTopColor: '#c75908', borderLeftColor: '#c75908', borderBottomColor: '#c75908', borderRightColor: '#c75908'},300);
}

function slideLeft(){
	$('span', this).animate({left: '5px', width: '292px', opacity: 0.2},200);
	$(this).animate({borderTopColor: '#491d18', borderLeftColor: '#491d18', borderBottomColor: '#491d18', borderRightColor: '#491d18'},200);
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
			return true;
	 }
}
