window.onAjaxLoad = function() {};

$(document).ready(function(){	
						   
						   
	 $('a.spip_out, a.out').click(function(){	
		this.target = "_blank";	
	});
	
	//Specialità Header

  	var tot = $("#specialita li").length;
  	var larghezza = $("#specialita li").width();
  	var ultimo = tot-1;
  	var conta = 0;
  	$("#specialita ul").css('width',tot*larghezza);	
  	$("#specialita #prev a").click(function(){
        	conta= (conta<=0) ? ultimo : conta-1;
      	posizione = (conta*larghezza*-1)  + 'px';
  		$("#specialita ul").animate(
  			{ marginLeft: posizione }, 
  			800
  			);
      });	
  	$("#specialita #next a").click(function(){
  		conta = (conta>=ultimo) ? 0 : conta+1;		
      	posizione = (conta*larghezza*-1)  + 'px';
  		$("#specialita ul").animate(
  			{ marginLeft: posizione }, 
  			800
  			);	
  		
  	});	 
	
	
	
	//Specialità Pagina Rubrica 1
	window.init_prev_next = function() {	
		$("#specialita #window #prev a").click(function(){
			$("#specialita #window .img").animate(
				{ marginLeft: 0 }, 
				800
				);
			$(this).parent().parent().find(".description").fadeTo(500,1);
			$(this).parent().parent().find("#next").css("display","block").fadeTo(500,1);
			$(this).parent().fadeTo(500,0);
		});	
		$("#specialita #window #next a").click(function(){	
														
			$("#specialita #window .img").animate(
				{ marginLeft: -500 }, 
				800
				);	
			$(this).parent().parent().find(".description").fadeTo(500,0);
			$(this).parent().parent().find("#prev").css("display","block").fadeTo(500,1);
			$(this).parent().fadeTo(500,0);
		});	
	}	
	
	init_prev_next();
	
	//apertura menu specialità
	$("a[name^=aprimenu]").click(function(){
      $(".subopen").hide();
      $(this).next().show();
	  $(this).addClass("on");
      return false;
    });
	
	//apertura menu specialità
	$("a.speclick[name^=view]").click(function(){
	  var athumb = $(this).attr("name");	
	  $(".subopen").hide();
	  $("a[name="+athumb+"]").parent().parent().show();
      return false;
    });
	
	
	
	//apertura articoli news
	$("a[name^=news]").click(function(){	 
      $(".newsopen").hide();
	  $(this).parent().parent().parent().find("a").removeClass('on');
      $(this).parent().parent().find(".newsopen").show();
	  $(this).addClass("on");
      return false;
    });
	
	//click dei tabs nel footer
	$("#other a").click(function(){			 
		$(this).parent().parent().find("img").css({zIndex:1});
		$(this).parent().parent().find("div.nascondi").css({zIndex:1});
		$(this).parent().parent().parent().find("a").removeClass("on");						 
		$(this).addClass("on");
		$(this).nextAll().css({zIndex:10});
    $("div.zoomoutmenu>div").hide();
	
	});
	
	$('.zoomoutmenu #tabgall').click(function(){
	  $("div.zoomoutmenu>div").show();
		$(this).parent().parent().prev().find("img").css("z-index","0");
		$(this).parent().parent().prev().find("div.nascondi").css("z-index","0");
		$(this).parent().parent().prev().find("a").removeClass("on");						 
		$(this).addClass("on");
	});
	
	
	//Gallery nel footer
	$('.zoomoutmenu').zoomtabs(15);
   
   	//facebox
	$('.tabs a.galleria').click(function(){
		$(this).next().click();
	});
	
	//facebox con la possibilità di inserire il testo alle immagini
	$("a[rel*=facebox]").facebox().facebox_gallery().
  		bind("update_facebox_gallery",function(e,index){
    	$("#facebox div.info").prepend("<div class=\"testofacebox\">"+this.title+"<p>"+this.name+"</p></div>");
  	});
	


});
	//Radio 1
	
	$.fn.zoomtabs = function (zoomPercent, easing) {
        if (!zoomPercent) zoomPercent = 2;
        
        return this.each(function () {
            var $zoomtab = $(this);
            var $tabs = $zoomtab.find('.tabs');
            var height = $tabs.height();
            
            var panelIds = $tabs.find('a.galleria').map(function () {
                return this.hash;
            }).get().join(',');
            
            $zoomtab.find('> div').scrollTop(0);
            
            var $panels = $(panelIds);
            var images = [];
            
            $panels.each(function () {
                var $panel = $(this),
                    bg = ($panel.css('backgroundImage') || "").match(/url\s*\(["']*(.*?)['"]*\)/),
                    img = null;
                
                if (bg !== null && bg.length && bg.length > 0) {
                    bg = bg[1];
                    img = new Image();
                    
                    $panel.find('*').wrap('<div style="position: relative; z-index: 2;" />');                    
                    $panel.css('backgroundImage', 'none');
                    
                    $(img).load(function () {
						
                        var w = this.width / 10;
                        var wIn = w / 100 * zoomPercent;
                        var h = this.height / 10;
                        var hIn = h / 100 * zoomPercent;
                        var top = 0;
                        
                        var fullView = {
                            height: h + 'em',
                            width: w + 'em',
                            top: top,
                            left: 0
                        };
                                                
                        var zoomView = {
                            height: (h + hIn) + 'em',
                            width: (w + wIn) + 'em',
                            top: top,
                            left: '-' + (wIn / 2) + 'em'
                        };
                        
                        $(this).data('fullView', fullView).data('zoomView', zoomView).css(zoomView);

                    }).prependTo($panel).css({'position' : 'absolute', 'top' : 0, 'left' : 0 }).attr('src', bg);
                    
                    images.push(img);
                }
            });
            
            function zoomImages(zoomType, speed) {
                $(images).each(function () {
                    var $image = $(this);
                    if ($image.is(':visible')) {
                        $image.stop().animate($image.data(zoomType), speed, easing);
                    } else {
                        $image.css($image.data(zoomType));//, speed);
                    }
                });
            }
                        
            $tabs.height(0).hide(); // have to manually set the initial state to get it animate properly.
            
            // this causes opear to render the images with zero height and width for the hidden image
            // $panels.hide().filter(':first').show();
            var speed = 200;
            
            $zoomtab.hover(function () {
                // show and zoom out
                zoomImages('fullView', speed);
                $tabs.stop().animate({ height : height }, speed, easing);
            }, function () {
                // hide and zoom in
                zoomImages('zoomView', speed);
                $tabs.stop().animate({ height : 0 }, speed, easing, function () {
                  $tabs.hide();
                });
            });
            
            var hoverIntent = null;
            $tabs.find('a').hover(function () {
                clearTimeout(hoverIntent);
                var el = this;
                hoverIntent = setTimeout(function () {
                    $panels.hide().filter(el.hash).show();
                }, 100);
            }, function () {
                clearTimeout(hoverIntent);
            }).click(function () {
                return false;
            });
        });
   		};
