$(document).ready(function(){

// post in evidenza hp

$("#p1").click(function(event){
	event.preventDefault();
	$("#foot_mainbox a").removeClass("on");
	$(this).addClass("on");
	$("#mainbox_content").animate({"left":"0px"},300);
});

$("#p2").click(function(event){
	event.preventDefault();
	$("#foot_mainbox a").removeClass("on");
	$(this).addClass("on");
	$("#mainbox_content").animate({"left":"-906px"},300);
});

$("#p3").click(function(event){
	event.preventDefault();
	$("#foot_mainbox a").removeClass("on");
	$(this).addClass("on");
	$("#mainbox_content").animate({"left":"-1812px"},300);
});

// eventi in evidenza in home page

$("#pallino_spot01").click(function(event){
	event.preventDefault();
	$(".pallino_spot").css({"background-position":"0 0"});
	$(this).css({"background-position":"0 -19px"});
	$(".spot_event").fadeOut("fast");
	$("#spot_event01").fadeIn("fast");
});

$("#pallino_spot02").click(function(event){
	event.preventDefault();
	$(".pallino_spot").css({"background-position":"0 0"});
	$(this).css({"background-position":"0 -19px"});
	$(".spot_event").fadeOut("fast");
	$("#spot_event02").fadeIn("fast");
});

$("#pallino_spot03").click(function(event){
	event.preventDefault();
	$(".pallino_spot").css({"background-position":"0 0"});
	$(this).css({"background-position":"0 -19px"});
	$(".spot_event").fadeOut("fast");
	$("#spot_event03").fadeIn("fast");
});

// eventi in evidenza in sez. eventi

var num_eventi = $("#eventi a").size();
var curr_position = 0;
var step_eventi = 200;
var curr_step_eventi = 1;
var tot_steps = Math.ceil(num_eventi/4);
var spaiato = num_eventi%4;
if (spaiato == 0) { spaiato = 4; }

$("#arrow_eventi_right").click(function(event){
	event.preventDefault();
	
	if (curr_step_eventi<tot_steps) {
		if (curr_step_eventi==tot_steps-1) {
			curr_position = curr_position - (spaiato*step_eventi);
			$("#arrow_eventi_right").css({"opacity":"0.5"});
		} else {
			curr_position = curr_position - (4*step_eventi)
		}
		$("#eventi").animate({"left": curr_position + "px"},300);
		curr_step_eventi++;
	}
	
	$("#arrow_eventi_left").css({"opacity":"1"});
	
});

$("#arrow_eventi_left").click(function(event){
	event.preventDefault();

	if (curr_step_eventi>1) {
		if (curr_step_eventi==2) {
			curr_position = 0;
			$("#arrow_eventi_left").css({"opacity":"0.5"});
		} else {
			curr_position = curr_position + (4*step_eventi)
		}
		$("#eventi").animate({"left": curr_position + "px"},300);
		curr_step_eventi--;
	}
	
	$("#arrow_eventi_right").css({"opacity":"1"});
	
});

/*

$("#arrow_eventi_right").click(function(event){
	event.preventDefault();
	if(curr_step_eventi > tot_steps_eventi) {
	curr_step_eventi --;
	$("#eventi").animate({"left": (curr_step_eventi*step_eventi) + "px"},300);
	$("#arrow_eventi_left").css({"opacity":"1"});
	if(curr_step_eventi == tot_steps_eventi) {
			$("#arrow_eventi_right").css({"opacity":"0.5"});
		}
	}
});

$("#arrow_eventi_left").click(function(event){
	event.preventDefault();
	if(curr_step_eventi < 0) {
		curr_step_eventi ++;
		$("#eventi").animate({"left": (curr_step_eventi*step_eventi) + "px"},300);
		$("#arrow_eventi_right").css({"opacity":"1"});
		if(curr_step_eventi == 0) {
			$("#arrow_eventi_left").css({"opacity":"0.5"});
		}
	}
});

*/

// espansione slot

$(".more, .less").live('click', function(event){
	event.preventDefault();
	if ($(this).hasClass("less")) {
		$(this).next().hide();
	} else {
		$(this).next().css({"display":"inline"});
	}
	$(this).toggleClass("less");
	$(this).toggleClass("more");
});

$(".action.reply").live('click', function(event){
  event.preventDefault();
  $tweetbox = $('.tweetbox', $(this).parent().parent()).toggle();

  if ($('iframe.twitter-anywhere-tweet-box', $tweetbox).css('width') == '0px') {
    $('iframe.twitter-anywhere-tweet-box', $tweetbox).css({'width': '620px', 'height' : '130px'});
    if (jQuery.support.boxModel) {
      $('.tweetbox', $tweetbox).css({'margin-left':'98px','display':'inline'});
      $('iframe.twitter-anywhere-tweet-box', $tweetbox).css('height', '200px');
    }
    
      $('.tweetbox', $tweetbox).css({'display':'inline !important'});
  }
  
});

// tab scheda evento

$("#btn_tab_about").live('click', function(event){
	event.preventDefault();
	$(".btn_tab_scheda").removeClass("active");
	$(this).addClass("active");
	$("#tab_video, #tab_foto").hide();
	$("#tab_about").show();
});

$("#btn_tab_video").live('click', function(event){
	event.preventDefault();
	$(".btn_tab_scheda").removeClass("active");
	$(this).addClass("active");
	$("#tab_about, #tab_foto").hide();
	$("#tab_video").show();
});

$("#btn_tab_foto").live('click', function(event){
	event.preventDefault();
	$(".btn_tab_scheda").removeClass("active");
	$(this).addClass("active");
	$("#tab_about, #tab_video").hide();
	$("#tab_foto").show();
});

// apertura bottom bar

var bottombar_open = 0;

$("#test_bottombar").click(function(event){
	event.preventDefault();
	if (bottombar_open == 0) {
		$("#main_container .spacer,#nav_menu_wrapper,#nav_menu_wrapper .nav_menu").animate({"height":"200px"},250,"swing");
		bottombar_open = 1;
	} else {
		$("#main_container .spacer,#nav_menu_wrapper,#nav_menu_wrapper .nav_menu").animate({"height":"45px"},250,"swing");
		bottombar_open = 0;
	}
});

// focus commento FB

$(".field_fb_comment").live('focus', function(event){
	if (($(this).val() == "per lasciare un commento, clicca prima su \'mi piace\'")||($(this).val() == "") ) {
	$(this).val("").css({"font-style":"normal","color":"#000"});
	}
});

});

// mail validator

function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	return false
}
var user=matchArray[1]
var domain=matchArray[2]
 
if (user.match(userPat)==null) {
    return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
			return false
	    }
    }
    return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   return false
}
if (len<2) {
   return false
}
return true;
}

// controllo form commenti

function controlla_commenti() {
	var flag = 0;
	if (document.form_comment.comment_author_name.value == "" ) {
		$("#comment_author_name").addClass("error");
		$("#campo_author_name span.help").text("Campo obbligatorio");
		flag=1;
	} else {
		$("#comment_author_name").removeClass("error");
		$("#campo_author_name span.help").text("");
	}
	if((document.form_comment.comment_author_email.value != "") && (!emailCheck(document.form_comment.comment_author_email.value))) {
			$("#comment_author_email").addClass("error");
			$("#campo_author_email span.help").text("E-mail non valida");
			flag = 1;
	} else {
			if (document.form_comment.comment_author_email.value == "" ) {
				$("#comment_author_email").addClass("error");
				$("#campo_author_email span.help").text("Campo obbligatorio");
				flag=1;
			} else {
				$("#comment_author_email").removeClass("error");
				$("#campo_author_email span.help").text("");
			}
	}
	if (document.form_comment.comment_body.value == "" ) {
		$("#comment_body").addClass("error");
		$("#campo_body span.help").text("Campo obbligatorio");
		flag=1;
	} else {
		$("#comment_body").removeClass("error");
		$("#campo_body span.help").text("");
	}
	if(flag==1) { return false; }
}


