jQuery(document).ready(function(){
	var aperta = false;
		
	if (slideshow)
	{
		/** Slideshow Home page **/
		var galleries = $('.ad-gallery').adGallery({slideshow: {
		enable: true,
		autostart: true}});
		/* fine slideshow */
	}
	
	$('.post .entry a img').parent().attr('rel','lightbox');
	
	/* Tasto + per la ricerca avanzata */
	$('a.plus').click(function(){
		$('.rhAdvanced').slideToggle(400);
	},function()
	{
		$('.rhAdvanced').slideToggle(400);
	});
	
	/* Menù a tendina della ricerca avanzata */
	$('.holder').jScrollPane();
	
	$('.holder').css("visibility","visible");
	
	/*Mostra le tendine*/
	$('.voce').click(function () {
		aperta = false;
		if(!$(this).parent().children('.jspScrollable, .holder').is(":visible"))
		{$(".voce").parent().children('.jspScrollable, .holder').slideUp('fast');}
		$(this).parent().children('.jspScrollable, .holder').slideToggle('fast');
	});
	
	$('.jspScrollable').click(function(){
		aperta = false;
	});
	
	$('body').click(function(){
		if( aperta == false )
			aperta = true;
		else
			$(".voce").parent().children('.jspScrollable, .holder').slideUp(10);
	});
	
	$('.holder').css('display','none');
	$('.rhAdvanced').css('display','none');
	$('.rhAdvanced').css('visibility','visible');
	
	/** Scroll dall'header ai contatti e dal footer in alto **/
	$('#page-contatti').click(function() {
  		$('html,body').animate({scrollTop: $('#footer').offset().top}, 1200);
	});
	
	$('#footBottom .footLogo').click(function() {
  		var targetOffset = $('#header').offset().top;
   		$('html,body').animate({scrollTop: targetOffset}, 1200);
       return false;
	});
	/* Fine Scroll dall'header ai contatti e dal footer in alto */
	
	/** SideTab */
	$('#tab-1-inner').css('display','none');
	$('#tab-2-inner').css('display','none');
	
	$('#sideTab a.button').click(function(){
		$(this).addClass('activeTab');
		$('#sideTab a.button').not(this).removeClass('activeTab');
		var activeTab = "#" + $(this).attr('id') + "-inner";
		$('.miniEntryList').not(activeTab).hide('200');
		$(activeTab).show('200');
	});
	/* fine SideTab */
	
	droplinemenu.buildmenu("dropdownCat1");
	$('#dropdownCat1 ul li ul').hover(function(){
		$(this).parent('li').children('a').addClass('activeDropdown');
	},function(){
		$(this).parent('li').children('a').removeClass('activeDropdown');
	});
	
		
	/* About */
	$('#leftContent .aboutImg').hover(function(){
		$(this).children('span').show(150);
		$(this).children('img').css('border-color','#343433');
		$(this).children('img').css('background-color','#fff');
	},function(){
		$(this).children('span').hide(150);
		$(this).children('img').css('border-color','#999999');
		$(this).children('img').css('background-color','#efefef');
	});
	
	$('#leftContent .aboutImg').click(function(){
		location.href = siteUrl + "/mi-presento/" + $(this).attr('id') + "/";
	});
	
	var username='webFandom'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url

	$.getJSON(url,function(tweet){ // get the tweets
		tweet[0].text = tweet[0].text + " ";
		tweet[0].text = tweet[0].text.replace(/(((http\:\/\/)|(www\.)).*(?=[^\.\w\d\/]))/gm,'<a href="$1">$1</a>').replace(/([\w\d\.]+\@[\w\d\.]+(\/)?)/gm,'<a href="mailto:$1$>$1</a>');
		$(".lastTweet").html(tweet[0].text); // get the first tweet in the response and place it inside the div
	});
});


/************************************************************************************
				VALIDAZIONE DEL FORM DEI CONTATTI

************************************************************************************/

/// JavaScript Document

// form validation function //
function validate(form) {
	var name = form.nome.value;
	var email = form.mail.value;
	var message = form.messaggio.value;
	var captcha = form.captcha.value;
	var captchaVal = parseInt(document.getElementById('capHide').innerHTML) - 3;
	
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	if((name == "")||(name == "Nome")) {
		inlineMsg('nome','Devi inserire il nome',2);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('nome','Devi inserire un nome valido',2);
		return false;
	}
	if((email == "")||(email == "Mail")) {
		inlineMsg('mail','<strong>Error</strong><br />Devi inserire il tuo indirizzo Mail',2);
		return false;
	}
	if(!email.match(emailRegex)) {
		inlineMsg('mail','<strong>Error</strong><br />Hai inserito una Mail invalida',2);
		return false;
	}
	if((message == "")||(message == "Messaggio")) {
		inlineMsg('messaggio','Devi inserire il messaggio');
		return false;
	}
	if(message.match(messageRegex)) {
		inlineMsg('messaggio','Hai inserito una messaggio invalido');
		return false;
	}
	if(captcha == "") {
		inlineMsg('captcha','Devi inserire la risposta di sicurezza');
		return false;
	}
	if(captcha != captchaVal) {
		inlineMsg('captcha','Hai inserito una risposta errata');
		return false;
	}
	return true;
}

function validate2(form) {
	var name = form.author.value;
	var email = form.email.value;
	var message = form.comment.value;
	
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	if((name == "")||(name == "nome")) {
		inlineMsg('author','Devi inserire il nome',2);
		return false;
	}
	if(!name.match(nameRegex)) {
		inlineMsg('author','Devi inserire un nome valido',2);
		return false;
	}
	if(email == "") {
		inlineMsg('email','<strong>Error</strong><br />Devi inserire il tuo indirizzo Mail',2);
		return false;
	}
	if(!email.match(emailRegex)) {
		inlineMsg('email','<strong>Error</strong><br />Hai inserito una Mail invalida',2);
		return false;
	}
	if(message == "") {
		inlineMsg('comment','Devi inserire il messaggio');
		return false;
	}
	if(message.match(messageRegex)) {
		inlineMsg('comment','Hai inserito una messaggio invalido');
		return false;
	}
	return true;
}


// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
	var msg;
	var msgcontent;

	if(!document.getElementById('msg')) {
		msg = document.createElement('div');
		msg.id = 'msg';
		msgcontent = document.createElement('div');
		msgcontent.id = 'msgcontent';
		document.body.appendChild(msg);	
		msg.appendChild(msgcontent);
		msg.style.filter = 'alpha(opacity=0)';
		msg.style.opacity = 0;
		msg.alpha = 0;
	} else {
		msg = document.getElementById('msg');
		msgcontent = document.getElementById('msgcontent');
	}
	msgcontent.innerHTML = string;
	msg.style.display = 'block';
	var msgheight = msg.offsetHeight;
	var targetdiv = document.getElementById(target);
	targetdiv.focus();
	var targetheight = targetdiv.offsetHeight;
	var targetwidth = targetdiv.offsetWidth;
	var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
	var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
	msg.style.top = topposition + 'px';
	msg.style.left = leftposition + 'px';
	clearInterval(msg.timer);
	msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
	if(!autohide) {
		autohide = MSGHIDE;
	}
	window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
	var msg = document.getElementById('msg');
	if(!msg.timer) {
		msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
	}
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target, comment) {
  var left = 0;
  var fix = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
	  //alert(left);
	  if ( (fix == 0) && ((left == 18)||(left == 261)) )
	  {
	  	fix = 2;
	  }
	  fix = fix == 0 ? fix + 3 : fix;

      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  left = fix == 2 ? left -288 : left;
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80);
  arrow.src = "http://www.webfandom.com/wp-content/themes/Webfandom%20New%202.0/images/msg_arrow.gif";
}

