/*
	Matteo 2009
	JS Library - matteobovetti.com
*/

function sendmail(){
	
	var name = document.getElementById("name");
	var mail = document.getElementById("mail");
	var txtmail = document.getElementById("txtmail");
	var sname = name.value;
	var smail = mail.value;
	var stxtmail = txtmail.value;
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

	sname = sname.replace(' ', '');
	smail = smail.replace(' ', '');
	stxtmail = stxtmail.replace('\n', '<br/>');
	
	
	/* se un campo è vuoto lancio la segnalazione */
	if (sname == '') showId('#name_err');
	else hideId('#name_err');
	if (smail == '') showId('#mail_err');
	else hideId('#mail_err');
	if (stxtmail == '') showId('#txtmail_err');
	else hideId('#txtmail_err');
	
	/* avendo già gestito le segnalazioni (tutte), posticipo l'uscita dalla funzione */
	if (sname == '' || smail == '' || stxtmail == '')
		return;
	
	/* controllo la forma della mail inserita */

	if (!filter.test(smail)) {
		showId('#mail_err');
		return false;
	}
	
	
	var txt = "smail.php?name=" + sname + "&mail=" + smail + "&txtmail=" + stxtmail;
	
	$.post(txt, function(data){
		hideId('#contact');
  		showId('#contact_mailsent');
	});
}


function showId(id) {
	$(id).slideDown("slow");
}

function hideId(id) {
	$(id).slideUp("fast");
}

function portfolio() {
		$('#portfolio').cycle({
			fx:     'fade', 
			speed:  500, 
			timeout: 4000,
			cleartype:  1,
			pager:  '#thumbnails ul',
			before:  onBefore, 
			after:   onAfter,
			pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#" title="' + slide.alt + '"><img src="' + slide.src + '" alt="' + slide.src + '" border="0" width="180" height="135"/></a></li>';
			}
		});
		
		function onBefore() { 
			$('#description a').show('500');
		}
		
		function onAfter() {
			$('#description a').show('500');
			$('#description').html('<a href="' + this.title + '">' + this.alt + '</a>');
		}
		
		
		$('#thumbnails li a').click(function() { 
			$('#portfolio').cycle('pause'); 
		});
}

function writetext(id) {
	var div = document.getElementById('textlink');
	if (id.title == 'Home')
		div.innerHTML = 'Vai alla home page!';
	else if (id.title == 'Portfolio')
		div.innerHTML = 'I miei lavori';
	else if (id.title == 'iOS')
		div.innerHTML = 'Le mie applicazione per iOS';
		
}

function cleartext(){
	document.getElementById('textlink').innerHTML = '';
}
