//nascondo le news solo a chi ha javascript abilitato
if (document.getElementById) {
  document.write('<style type="text/css">.hidden{position:absolute;left:-10000px;top:-10000px;width:1px;height:1px;overflow:hidden}<\/style>');
}
//imposta target _blank ai link esterni
function linkext(){
	$("a.ext, a.link").each(function () {
		 $(this).attr({ 
          target: "_blank",
          title: $(this).attr("title")+" (link esterno, si apre in una nuova finestra)"
        });
	});
}
//animazione background menu
function animateBackground(){
	$('#menu2 h3 a').not('.perche #menu2 li.perche h3 a, .cosa #menu2 li.cosa h3 a, .interlocutori #menu2 li.interlocutori h3 a, .teniamo #menu2 li.teniamo h3 a')
	.css( {backgroundPosition: "0 0"} )
	.bind("mouseover focus", function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 -25px)"}, 
			200)
		})
	.bind("mouseout blur", function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 0)"}, 
			200)
		})
	$('.home #menu2 h3 a')
	.css( {backgroundPosition: "0 0"} )
	.bind("mouseover focus", function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 -100px)"}, 
			200)
		})
	.bind("mouseout blur", function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 0)"}, 
			200)
		})
	$('#menu1 li a').not('#menu1 li a.active')
	.css( {height: "40px"} )
	.bind("mouseover focus", function(){
		$(this).stop().css("opacity","0.5").animate(
			{height:"80px",
			opacity:1}, 
			200)
		})
	.bind("mouseout blur", function(){
		$(this).stop().animate(
			{height:"40px"}, 
			200)
		})
}
//inizializzazione jquery.pagination
var items_per_page = 2;

function pageselectCallback(page_index, jq){
	var max_elem = Math.min((page_index+1) * items_per_page, $('#hiddenresult li.result').length);
	$('#searchresult').empty();
	$('#searchresult').hide();
	for(var i=page_index*items_per_page;i<max_elem;i++)
                {
                    var new_content = $('#hiddenresult li.result:eq('+i+')').clone();
                	$('#searchresult').append(new_content);
                }
	
	$('#searchresult').fadeIn("slow");
	initFancybox();
	return false;
}
function initPagination() {
	var num_entries = $('#hiddenresult li.result').length;
	$("#pagination").pagination(num_entries, {
		num_edge_entries: 0,
		num_display_entries: 4,
		items_per_page:items_per_page,
		callback: pageselectCallback
	});
 }
//toggle corsi
function initToggle() {
	$(".corsi").hide();
	$("h3.categoria").css("cursor","pointer").toggle(function(){
		$(this).addClass("active").attr("title","meno informazioni");
		}, function () {
		$(this).removeClass("active").attr("title","pił informazioni");
	});
	$("h3.categoria").click(function(){
		$(this).next(".corsi").eq(0).slideToggle();
	});
 }
 

 
//fancybox
function initFancybox() {
  //se esistono link alle news
  if ($("a.iframe").length){
	  $("a.iframe.more, a.iframe.corso").fancybox({
	   'hideOnContentClick': false,
	   'frameWidth': 485,
	   'frameHeight': 580,
	   'padding': 0,
	   'centerOnScroll': false
	  });
	  $("a.iframe.map").fancybox({
	   'hideOnContentClick': false,
	   'frameWidth': 1000,
	   'frameHeight':700,
	   'padding': 0,
	   'centerOnScroll': false
	  });
  }
 }

//init
$(document).ready(function(){
  //se esiste contenuto da paginare
  if ($('#hiddenresult').length){
	  $("#hiddenresult").before("<ul id='searchresult'></ul>");
	initPagination();
  }
  //se esistono link con tooltip
  if ($('a.bus').length || $('a.cooperative').length || $('a.professionisti').length){
	  //stile globale tooltip
	  $.fn.qtip.styles.stileTooltip = {
			width: 180,
			border: {
			 width: 3,
			 radius: 3,
			 color: '#EB8603'
			 },
			tip: { size: { x: 15, y: 9 }, corner: 'bottomMiddle' },
			title: { 'background': '#FFF1DF' },
			name: 'light'
	  }
	  
	  $('div.bus, #cooperative, #professionisti').hide();
	  
	  $('a.bus').each(function(){
		$(this).qtip({
		  content: {
			  text:$(this).parent().next().html(),
			  title: {
			  text: '<span class="bus">mezzi pubblici</span>',
			  button: 'x'
			  }
		  },
		  position: {
			  corner: {
				 target: 'topLeft',
				 tooltip: 'bottomLeft'
			  },
			 adjust: { y: 2 }
		   },
		  style: { 
			name: 'stileTooltip'
		  },
		  show: {
			  delay: 0,
			  when: { event: 'click' }
			  },
		  hide:'click'
		});
		$(this).click(function(){return false;});
	  });
	  
	  $('a.cooperative').click(function(){return false;}).qtip({
		  content: {
			  text:$('#cooperative').html(),
			  title: {
			  text: 'Cooperative sociali',
			  button: 'x'
			  }
		  },
		  position: {
			  corner: {
				 target: 'topMiddle',
				 tooltip: 'bottomMiddle'
			  }
		   },
		  style: { 
			width: 355,
			name: 'stileTooltip'
		  },
		  show: {
			  delay: 0,
			  when: { event: 'click' }
			  },
		  hide:'click'
		});
  
	  $('a.professionisti').click(function(){return false;}).qtip({
		  content: {
			  text:$('#professionisti').html(),
			  title: {
			  text: 'Professionisti accreditati',
			  button: 'x'
			  }
		  },
		  position: {
			  corner: {
				 target: 'topMiddle',
				 tooltip: 'bottomMiddle'
			  }
		   },
		  style: { 
			width: 355,
			name: 'stileTooltip'
		  },
		  show: {
			  delay: 0,
			  when: { event: 'click' }
			  },
		  hide:'click'
		});
  }
  //se ci sono corsi
  if ($('.corsi').length){
	initToggle();
  }
  if ($('#aule a').length){
	  
	$("#aule a").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	100, 
		'speedOut'		:	50, 
		'overlayShow'	:	true
	});

	
  }
	initFancybox();
	linkext();
	animateBackground();
});