/************************************************************						
 *	Author		:	Wim Selles						
 *	Licenced by	:	wswebcreation.nl				
 *-----------------------------------------------------------
 *	Version		:	0.11								
 *	Date		:	2010-01-26
 *	History		:	Added:
 *					- Lightbox for portfoliopage
 *					- Slide menu for portfoliopage
 *					Changed:
 *					- Changed effect from slide to fade
 *					  for the tabs
 *----------------------------------------------------------				
 *-----------------------------------------------------------
 *	Version		:	0.1								
 *	Date		:	2010-01-01
 *	History		:	Initial version
 *----------------------------------------------------------
 ************************************************************/

$(document).ready(function(){
	//---- The navigationmenu
	$('ul#navigationMenu li a').click(adminTabs);
	$('#about a.tabLink').click(adminTabs);
	//---- The lightbox on the portfoliopage
	$('#portfolio li a.portfolioLightbox').lightBox();

	//---- Setting up the slideviewer for the portfoliopage
	var currentPosition 	= 	0;
	var slideWidth 			= 	596;			// Size of each slide
	var slides 				= 	$('.slide');
	var numberOfSlides 		= 	slides.length;
	
	//---- Set #portfolioSlideInner width equal to total width of all slides
	$('#portfolioSlideInner').css('width', slideWidth * numberOfSlides);
	
	//---- Hide left arrow control on first load
	manageControls(currentPosition);
	
	//---- Create event listeners for .controls clicks
	$('.control').bind('click', function(){
		//---- Determine new position
		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
		//---- Hide / show controls
		manageControls(currentPosition);
		//---- Move portfolioSlideInner using margin-left
		$('#portfolioSlideInner').animate({
			'marginLeft' : slideWidth*(-currentPosition)
		});
	});
	
	//---- manageControls: Hides and Shows controls depending on currentPosition
	function manageControls(position){
		//---- Hide left arrow if position is first slide
		if(position==0){ 
			$('#leftControl').hide() 
		}else{ 
			$('#leftControl').show() 
		}
		//---- Hide right arrow if position is last slide
		if(position==numberOfSlides-1){ 
			$('#rightControl').hide() 
		}else{ 
			$('#rightControl').show() 
		}
	}
	//---- End of slideviewer
	
	//---- The contactform
	$('#submitContact').click(submitContactForm);
	//---- Hover effect on the contact-images
	xOffset = 60;
	yOffset = 30;
	$('.social-icon').hover(hoverIn,hoverOut);
	$(".social-icon").mousemove(mouseMove);
});

function adminTabs(){
	//---- Get the tab the has been clicked
	var tab	=	$(this).attr('href');
	//---- Only change tabs if tabs isn't visible
	if($(tab).is(":hidden")){
		//---- Remove selected class
		$('ul#navigationMenu li').removeClass('selected');
		//---- Add selected class to clicked tab
		$(this).parent('li').addClass('selected');
		//---- Remove visible class from active tab and fade it out
		$('.tab-visible').removeClass('tab-visible').fadeOut("slow",function(){
			//---- Add visible class to clicked tab and fade in
			$(tab).addClass('tab-visible').fadeIn("slow");
		})
	}
	return false;
}

function submitContactForm(){
	//---- Set all vars
	hasError 			= 	false;
	var contactName		=	$('#contactName').val();
	var contactEmail	=	$('#contactEmail').val();
	var emailReg 		= 	/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;	// check if email is like name@url.com
	var contactMessage	=	$('#contactMessage').val();
	if(contactName==''){
		$('.contactName').show();
		hasError = 	true;	
	}else{
		$('.contactName').hide();		
	}if(contactEmail == '' || !emailReg.test(contactEmail)){
		$(".contactEmail").show();
		hasError 	= 	true;
	}else{
		$('.contactEmail').hide();
	}
	if(contactMessage==''){
		$('.contactMessage').show();
		hasError = 	true;	
	}else{
		$('.contactMessage').hide();		
	}	

	if(!hasError){
		$.post("include/sendEmail.php",{ 
			contactName:	contactName,
			contactEmail:	contactEmail,
			contactMessage:	contactMessage
		},
		function(data){
			if(data==''){
				$('#contactMailForm').fadeOut("slow",function(){
					$('#succesContactMessage').fadeIn("slow");
				});
				emptyFields();
				setTimeout(function() {
					$('#succesContactMessage').fadeOut("slow",function(){
						$('#contactMailForm').fadeIn("slow");
					});
				}, 3000);
			}else{
				$('#contactMailForm').fadeOut("slow",function(){
					$('#errorContactMessage').fadeIn("slow");
				});
				setTimeout(function() {
					$('#errorContactMessage').fadeOut("slow",function(){
						$('#contactMailForm').fadeIn("slow");
					});
				}, 3000);
			}
		});
		return false;
	}
	return false;	
}
function emptyFields(){
	$('input').val('');
	$('#contactMessage').val('');	
}

 function hoverIn(e){
	//-- Get the title
	this.t 		= 	this.title;
	var c 		= 	this.t
	//---- Empty the tile attr
	this.title 	= 	"";
	//---- Add hoverPopup to the body
	$("body").append("<p id='hoverPopup'>"+ c +"</p>");
	//---- Add startpoint of mouse to the hoverPopup
	$("#hoverPopup")
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px")
		.fadeIn("fast");
}
  
function mouseMove(e){
	//---- Let hoverPopup move with the mouse in the image
	$("#hoverPopup")
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px");
}
 function hoverOut(e){
	//---- Put back the title
	this.title = this.t;
	//---- Remove the hoverPopup
	$("#hoverPopup").remove();
 }	

