/* check email is valid : alpha_num +  @ + alphanum + . + alpha(lenght 2..4)  =returns false / true*/
function is_valid_email(email){ return (/^[a-z0-9._-]+@+[a-z0-9._-]+\.+[a-z]{2,6}$/i.test(email));}
/* check message no www or http or href in message content */
function is_valid_msg(msg){ return !(/www|http|href/i.test(msg));}
/* no empty fields*/
function is_valid_field(fldClass){
	var ok = true;
	$(fldClass).each(function(){
				 if($(this).attr('value')==""){ok = false;}
	});
	return ok;
}


/* Show / Hide contact form */
function toggleForm(){
	if( !( $('#ContactFrm').is(':visible')) ){
		$('#ContactFrm').slideDown();
	}else{
		$('#ContactFrm').slideUp('slow', 
			function(){$('#ErrorMsg').css({'display':'none'});$('#ErrorMsg').html('');}
		);
	}
}

/*
	function sendError(ErrCode)
		proceed withj errordisplay on form error.
		codes : 1 - empty field, 2 - error in email, 3 - error in message
	@input : ErrCode (Int) Error Code
	@output : feedback message 
*/
function sendError(ErrCode){
	var eMsg="";
	switch(ErrCode){
		case 1 : eMsg = 'Please fill in all the fields'; break;
		case 2 : eMsg = 'Your email is not valid'; break;
		case 3 : eMsg = 'Your message containts invalid data'; break;
	}
	$('#ErrorMsg').html(eMsg).show('slow');
	return false;
}
/* / sendError */



/*
	function sendMail()
		check all fields non empty => continue / exit to error-1
		check mail valid xxx@xxx.xxx => continue / exit to error-2
		check message valid (no http, www, href) => continue / exit to error-3
		send asyns mail
	@input : none
	@output : feedback message if error; feedback msg + async mail() noerror
*/
function sendMail(){
	$('#ErrorMsg').hide();

	if(!is_valid_field(  '.FrmElem' )){
		sendError(1);  return false;
	}
			 
	if(!is_valid_email(  $('#email').attr('value') )){
		sendError(2); return false;
	}

	if(!is_valid_msg( $('#message').attr('value') )){
		sendError(3); return false;
	}
	
	$('#ErrorMsg').hide('slow');
	var qrstr = $('#CtcFrm').serialize();
	$('#CtcFrm').html('<p class="loading"><img src="./images/ajax-loader.gif" alt="Processing" /></p>');

	
	$.ajax({
	   type: "POST",
	   url: "sendmail",
	   data: qrstr,
	   success: function(responseText){
		 $('#CtcFrm').html(responseText);
	   },
	   error: function(){
		 $('#ContactFrm').html('<p>Server Error while processing Ajax Request</p>'); 
	   }
	});
}
/* / sendMail */



/*
	function menu()
		gather menu item text contents, and reformat <br/> inbetween each letter. => verticalize
		animate menu. slide down text on moveover, backup on mouseout.
	@input : none
	@output : screen
*/
function menu(){
	/* verticalize text */
	$('#nav li a').each(function(){
			$str = $(this).html();
			var $stro="";
			for(var i=0; i<$str.length; i++){ $stro += $str.substr(i,1)+'<br/>';}
			$(this).html($stro);
	});
		
	/* animate text */
	$('#nav li').hover(
		function(){
			$(this).find('a').stop().animate({paddingTop:'50px'},200);
		},
		function(){
			$(this).find('a').stop().animate({paddingTop:'0'},200);
		}
	);
		
	/* navigate */
	$('#nav li').click(function(){
					window.location.href=$(this).children('a').attr('href');
			});
}
/* /  menu */


/*
	function make_full_link(evt,el)
		based on a href tag, add title attr cancel link defaut behavior (e.g. navigation) 
		and modify to open into into new window
	@input : event, element
	@output : return false (cancel behavior)
*/
function make_full_link(evt,el){
	lnk = el.attr('title');
	el.attr('title','Visit '+lnk+' - (opens in a new window)');
	el.bind('click',function(event){event.preventDefault; window.open($(this).attr('href')); return false;});
}
/* /  make_full_link */



/*
	function portfolio()
		initiates behaviors and binds on portfolio items
		thumbs mouse enter/leave => opacticity 0.75/0.3. 
		thumbs click => class active + opcaticy 1 &&  hide current then show clicked in main content area
	@input : none
	@output : binds & behaviors
*/
function portfolio(){
	$('#portfolio').find('a').each(function(){

			$link = $(this);
			
			// bind mouseenter / leave =>  opcatity animation
			$link.hover(
				function(){
					if(!($(this).find('img').hasClass('active')) ) {
						$(this).find('img').stop().animate({opacity: 0.75}, 300);
					}
				 },
				function(){
					if( !($(this).find('img').hasClass('active')) ) {
					 $(this).find('img').stop().animate({opacity: 0.3}, 300);
					}
				}
			);
			
			// bind onclick => change main content, opacity animation on thumbs. (restore others + set current)
			$link.click(
				function(event){
					event.preventDefault();
					$lnkid = $(this).attr('href');
					$('.PortVis').removeClass('PortVis').fadeOut(300,function(){$($lnkid).addClass('PortVis').fadeIn(300);});
					$('#portfolio').find('a').find('img').removeClass('active').css('opacity',0.3);
					$(this).find('img').toggleClass('active').css('opacity',1);
				}
			);
	});
}
/* /  portfolio() */




/*
	function innit(page)
		initiates behaviors and binds on page with body class="page"
	@input : page (string) page body class attr
	@output : binds & behaviors
*/
function innit(page){

	menu();
	
	switch (page)
	{
		
		case 'home':
			$('#BtnContact').click(function(){toggleForm();return false;});
			$('#BtnSendMsg').click(function(){sendMail();return false;});
		break;
		
		case 'present':
		
		break;
		
		case 'past':
			$(".external").each(
				function(event){
					make_full_link(event,$(this));
				}
			);
			portfolio();
		break;
		
		case 'future':
			$('#BtnContact').click(function(){toggleForm();return false;});
			$('#BtnSendMsg').click(function(){sendMail();return false;});
			$('#BtnContact').trigger('click');
		break;	
	
	}
}
/* /  innit() */

