jQuery(function($){


  //animate slide
  var SpeedAnimate = 1000;



  $( "#show" ).click(function() {
    if (!$( "#show" ).data('animation_started')) {
      $( "#show" ).data('animation_started', true);
      $("#slide").animate({"left": "+=200%"}, SpeedAnimate, function(){$( "#show" ).data('animation_started', false);});
      $("#show").animate({"left": "-55px"}, SpeedAnimate);
      $(".map").css("visibility","hidden");
      $(".gallery_close").animate({"right": "-=50px"}, SpeedAnimate).css("display","none");
      $(".gallery").css("display","none");
    }
    

  });


  
  $( ".close" ).click(function() {
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate);
    $("#show").animate({"left": "-10px"}, SpeedAnimate);
    $(".gallery_close").animate({"right": "+=45px"}, SpeedAnimate).css("display","block");
  });

  
  $(".body_gall .gallery_close").animate({"right": "+=45px"}, SpeedAnimate).css("display","block");
  
  
  $(".body_gall .gallery_close").click(function(evt){
    evt.preventDefault();
    $(this).animate({"right": "-=50px"}, SpeedAnimate, function () {
    history.back();
    });
  });
  $('.body_map #show').animate({"left": "+=45px"}, SpeedAnimate).css("display","block");
  $('.body_map #show').click(function(evt){
    evt.preventDefault();
    $(this).animate({"left": "-=50px"}, SpeedAnimate, function () {
    history.back();
    });
  });

  $('a.gallery_1').click(function(evt){
    evt.preventDefault();
    var gall_click = $(this);
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate, function(){
    window.location = gall_click.attr('href');
    $("#slide").animate({"left": "+=200%"}, SpeedAnimate);
    });
  });
  
  $('a.map').click(function(evt){
    evt.preventDefault();
    var map_click = $(this);
    $("#slide").animate({"left": "-=200%"}, SpeedAnimate, function(){
    window.location = map_click.attr('href');
    $("#slide").animate({"left": "+=200%"}, SpeedAnimate);
    });
  });


  //resize function
  $(window).resize(function(){

 /* //vertical middle
  var wHeight = $(window).height();
  var hHeight = $('#header').height();
  var cHeight = wHeight - hHeight;
  $('#slide').each(function(){
    var bHeight = $(this).height();

    if ( cHeight > bHeight ){
    $(this).css('margin-top',  Math.floor(((cHeight) - (bHeight))/2) + 'px');
   // $(this).css('margin-bottom',  Math.floor(((cHeight) - (bHeight))/2) + 'px');
    }
    else {
    $(this).css('margin-top', 20 + 'px');
    $(this).css('margin-bottom', 20 + 'px');
    }
  });
   marginTop = parseInt($('#slide').css('margin-top'), 10);   */

     $('.accord_desc').each(function(){
  
    $(this).parent().parent().hover(function(){
    
    
      var accordHeight = $(this).height();
      console && console.log(accordHeight);
      
      $('#slide').css('margin-top', (marginTop - Math.floor(accordHeight)/2) + 'px');
      $('#slide').css('margin-bottom', (marginTop - Math.floor(accordHeight)/2) + 'px');
    
    });  
  });

  // menu
  var menu_topWidth = $('#menu_top').width();
  var logoWidth=$("#logo").width();
  var langWidht=$("#lang").width();
  var findWidth=$("#find_1").outerWidth(true);
  var menuWidth=$("#menu").width();
  var menu =  menu_topWidth - logoWidth - langWidht - findWidth;
  var aSize = $("#menu > ul > li > a").size();
  var x = (menu - menuWidth)/ aSize;

  $('#menu > ul > li > a').each(function(){
    var aWidth = $(this).width();
    $(this).css('width', aWidth + Math.floor(x) - 1 + 'px' );
  });

  //header_title 
  var headerWidth = $('.header_padding').width(); 
  var header_links = $(".header_links > ul > li ").size();
  var liWidth = $(".header_links > ul > li ").width();
  var linksWidth = header_links*liWidth;      

  $('.header_title').each(function(){

  var titleWidth = $(this).outerWidth();
  var titleWidth = headerWidth - linksWidth;

  $(this).css('width', titleWidth - 20 + 'px');
  $(".header_links").css('width', linksWidth + 15 + 'px');
  var headerHeight = $('.header_padding').height();
  var titleHeight = $(this).height() + 5;

  if (headerHeight > titleHeight) {
    $(this).css('padding-top', ((headerHeight - $(this).height()) / 2) + 'px');
    $('.header_padding').css('padding-top', 10 + 'px');
    $('.header_padding').css('padding-bottom', 10 + 'px');

  }
  else {
    $(this).css('padding-top', 0 + 'px');
    $('.header_padding').css('padding-top', 30 + 'px');
    $('.header_padding').css('padding-bottom', 30 + 'px');

  }
  });

  //gallery button




  var descHeight = $(".desc_gall").height();
  var footerHeight = $('.footer_gallery').height();
  var desc_padding_top = ((footerHeight-descHeight > 10)?((footerHeight-descHeight)/2):(5)); 
  $(".desc_gall").css("padding-top",  desc_padding_top+ 'px');
    
  var map = $('.map').height();
  var map_back = $('.body_map #show').height();     




  });

  $(window).resize();
/*
$(".accordion > .accordion_click").each(function () {
$(this).click(function(){
$(this).parent().children('.accordion_desc').slideDown();
$(this).parent().parent().find('.acc_active').removeClass('acc_active').children('.accordion_desc').slideUp().parent().children('.accordion_click').find('span').removeClass('span_active').addClass('span_not_active');
$(this).find('span').addClass('span_active');
$(this).parent().addClass('acc_active');
var c = $(this).parent().height() - $(this).height();
var d = $(this).parent().children('.accordion_desc').find('div').outerHeight(); 
console && console.log(Math.floor(d)/2);
console && console.log(marginTop);
$('#slide').css('margin-top', (marginTop - Math.floor(d))/2 + 'px');
});
});*/



  
  
  $('.login_1').click(function(){
    $('#cboxClose1').addClass('cClose');
  });

  $('.cboxOverlay').click(function(){
    $('#cboxClose').removeClass('cClose');
  });



  //plugins fieldfocus
  $('input.fieldfocus,textarea.fieldfocus').fieldFocus();
  $('.body_form .Nome').fieldFocus();
  $('#login input').fieldFocus();

  //plugins menu
  $('ul.main').superfish();
  $('ul.lang').superfish();


  //Table/add color every second line
  $(".body_table tr:nth-child(odd)").addClass("odd");
  


  //Header Links/remove border from last link   
  $(".header_links li:last-child").addClass("rem");


  //plugins colorbox
  //---$(".inline-gallery a").colorbox();

  $('a.login_1').colorbox({onOpen: function () {$('#colorbox #cboxWrapper').addClass('closebox');}, onClosed: function () {$('#colorbox #cboxWrapper').removeClass('closebox');}});
  
  $(".body_accordion .accord_2 a").click(function(){
  $('body').addClass('body_colorbox');
  });
  $('#cboxClose').click(function(){
  $('body').removeClass('body_colorbox');
  });
  $('#cboxOverlay').click(function(){
  $('body').removeClass('body_colorbox');
  });

  //plugins gallery

  // $( "#accordion" ).accordion({
  // collapsible: true,
  // animated: "fade"
  // });





  //plugins for form page
  // -A121 $('.body_form .selectBlock').sSelect();
  
  //$('.body_form .selectBlock').each(function(i,el){
  $('.selectBlock').each(function(i,el){
    var opt_select = { defaultText: (($(this).attr("title") != "")?($(this).attr("title")):(general_text_for_select)) ,ddMaxHeight: '200px' };
    
    $(this).sSelect(opt_select);
    
  });
  
  // -A121 $(".body_form ul.newList li").first().css('display', 'none');    
  $('.body_form .defaultP input').ezMark();
  $('.body_form .customP input[type="checkbox"]').ezMark({checkboxCls: 'ez-checkbox-green', checkedCls: 'ez-checked-green'});

  //add class custom to checkbox
  $('.defaultP ').last().addClass('custom');
  $('.ez-checkbox-green').prependTo(' .custom span');


  $('.lang > li > a').click(function () {
    return false;
  });



  var c= 0;
  c=$('.gall_img div').size();
  var a = $('li.current').index();
  for( var n = 0 ; n < c ; n++){
    $('<img src="/commons/images/loading_gall.gif" alt="none">').appendTo('.slides_container').wrap($('<div class="slide"><div class="item"></div></div>'));
  };
  $('.item img').eq(0).attr('src',$('span.src').eq(0).text());
  $('.item img').eq(0).attr('alt',$('span.alt').eq(0).text());
  $('.item img').eq(0).attr('mode',$('span.mode').eq(0).text());

  $('#slides').slides({
    generateNextPrev: true,
    generateNextPrevJump: true,
    //vertical center descrizione img
    animationComplete: function(){
        var descHeight = $(".desc_gall").height();
        var footerHeight = $('.footer_gallery').height();
        var desc_padding_top = ((footerHeight-descHeight > 10)?((footerHeight-descHeight)/2):(5));
        $(".desc_gall").css("padding-top", desc_padding_top + 'px');
    }
  });
  
  $('.desc_slides').append('<div class="pagination_div"></div>');
  
  $('.next').clone(true).appendTo(' .desc_slides ');
  var origPager = $('ul.pagination');
  origPager.clone(true).prependTo('.pagination_div');
  origPager.remove();
  $('.prev').clone(true).prependTo('.desc_slides');
  $('#slides .prev').text('');
  $('#slides .next').text('');
  $('#slides .desc_slides .prev').text('<');
  $('#slides .desc_slides .next').text('>');
  $("div.desc_gall").text($("#slides .slide img").attr("alt"));

  $('#slides .next').addClass('next_gall');
  $('#slides .desc_slides .next').removeClass('next_gall');
  $('#slides .prev').addClass('prev_gall');
  $('#slides .desc_slides .prev').removeClass('prev_gall');



  $('.nextJump').clone(true).appendTo(' .desc_slides ');
  $('.prevJump').clone(true).prependTo('.desc_slides');
  $('#slides .prevJump').text('');
  $('#slides .nextJump').text('');
  $('#slides .desc_slides .prevJump').text('<<');
  $('#slides .desc_slides .nextJump').text('>>');
  
  
  if($('.description_gallery').length > 0)
    $('.description_gallery').serialScroll({
		target:'#pagination_control ul',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		
        //prev:'a.pagination_a',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		//next:'a.pagination_b',// Selector to the 'next' button (absolute too)
        
        prev:'a.prevJump',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'a.nextJump',// Selector to the 'next' button (absolute too)
		axis:'xy',// The default is 'y' scroll on both ways
		//navigation:'#pagination_div li',
		duration:300,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		
        
        
        step:num_link_gallery_pag,
        //jump:true,
        
        
        
		//queue:false,// We scroll on both axes, scroll both at the same time.
		//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
		//stop:false,// Each click will stop any previous animations of the target. (false by default)
		//lock:true, // Ignore events if already animating (true by default)		
		//start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )		
		//cycle:true,// Cycle endlessly ( constant velocity, true is the default )
		//step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
		//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		//interval:1000, // It's the number of milliseconds to automatically go to the next
		//constant:true, // constant speed
		
		onBefore:function( e, elem, $pane, $items, pos ){
			/**
			 * 'this' is the triggered element 
			 * e is the event object
			 * elem is the element we'll be scrolling to
			 * $pane is the element being scrolled
			 * $items is the items collection at this moment
			 * pos is the position of elem in the collection
			 * if it returns false, the event will be ignored
			 */
			 //those arguments with a $ are jqueryfied, elem isn't.
			//e.preventDefault();
			//if( this.blur )
			//	this.blur();
		},
		onAfter:function( elem ){
			//'this' is the element being scrolled ($pane) not jqueryfied
		}
	});

    

  //add class on click and hover


  $(".next_gall").mouseover(function(){
    $(this).addClass('next_hover');
  }).mouseout(function(){
    $(this).removeClass('next_hover');
  });

  $(".next_gall").mousedown(function(){
    $(this).addClass('next_click');
  }).mouseup(function(){
    $(this).removeClass('next_click');
  });

  $(".prev_gall").mouseover(function(){
    $(this).addClass('prev_hover');
  }).mouseout(function(){
    $(this).removeClass('prev_hover');
  });

  $(".prev_gall").mousedown(function(){
    $(this).addClass('prev_click');
  }).mouseup(function(){
    $(this).removeClass('prev_click');
  });

});


(function($){
  $.fn.extend({ 
    onShow: function(callback, unbind){
      return this.each(function(){
        var obj = this;
        var bindopt = (unbind==undefined)?true:unbind; 
        if($.isFunction(callback)){
          if($(this).is(':hidden')){
            var checkVis = function(){
              if($(obj).is(':visible')){
                callback.call();
                if(bindopt){
                  $('body').unbind('click keyup keydown', checkVis);
                }
              }                         
            }
            $('body').bind('click keyup keydown', checkVis);
          }
          else{
            callback.call();
          }
        }
      });
    }
  });
})(jQuery);


$(document).ready(function(){
            
   $('.paragrafo_manuali .img_content').hover(function(){ $(this).children('a').css("background-image","url(\"/commons/images/plus_img.png\")") }, function(){ $(this).children('a').css("background-image","none") } ) 
   $('.paragrafo_manuali .img_content a').each(function(){
    
        var $this = $(this);
        
        var plus_w = $this.width();
        var plus_h = $this.height();
        
        $this.css("top", 0);
        $this.css("left", 0);
        
        var pic_real_width = 0;
        var pic_real_height = 0;
        
        if(lessIE8)
        {    
            if(!$(this).siblings('img').is(':visible'))
            {
                var img = new Image();
                img.src = $this.siblings('img').attr("src");
                
                pic_real_width = img.width;
                pic_real_height = img.height;
                
                var top_a = ((img.height / 2) - (plus_h / 2)) + 3;
                var left_a = ((img.width / 2) - (plus_w / 2)) - (img.width + 30 + 37 );
                
                $this.css("width", (pic_real_width) - 1 +"px");
                $this.css("height", (pic_real_height - 1) +"px");
                
                $this.css("top", 0+"px");
                $this.css("left", left_a+"px");
                $this.css({'backgroundPosition': ((pic_real_width / 2) - (plus_w / 2))+'px '+top_a+'px'});
                
                $this.css("display", "block");   
            }
            else
            {
                pic_real_width = $this.siblings('img').width();
                pic_real_height = $this.siblings('img').height(); 
                
                var top_a = ((pic_real_width / 2) - (plus_h / 2)) + 3;
                var left_a = ((pic_real_width / 2) - (plus_w / 2)) - ($this.parent().width() + 7 );
                
                
                $this.css("width", (pic_real_width) - 1 +"px");
                $this.css("height", (pic_real_height - 1) +"px");
                //alert('backgroundPosition:'+ left_a+'px '+top_a+'px')
                //$this.css({'backgroundPosition': left_a+'px '+top_a+'px'});
                
                
                
                //$this.css("top", top_a+"px");
                $this.css({'backgroundPosition': ((pic_real_width / 2) - (plus_w / 2))+'px '+top_a+'px'});
                $this.css("top", 0);
                $this.css("left", left_a+"px");
                $this.css("display", "block");
                    
            }
        }
        else if(isIE)
        {
            if(!$this.siblings('img').is(':visible'))
            {
                var img = new Image();
                img.src = $this.siblings('img').attr("src");
                
                pic_real_width = img.width;
                pic_real_height = img.height;
            }
            else
            {
                pic_real_width = $this.siblings('img').width();
                pic_real_height = $this.siblings('img').height();        
            }
            
            $this.css("width", (pic_real_width) - 1 +"px");
            $this.css("height", (pic_real_height - 1) +"px");
                                
            var top_a = ((pic_real_height / 2) - (plus_h / 2)) + 1;
            var left_a = ((pic_real_width / 2) - (plus_w / 2)) + 1;
            
            $this.css({'backgroundPosition': left_a+'px '+top_a+'px'});
            
        }
        else if(1 || isSafari) // or chrome of ff
        {
            $("<img/>") // Make in memory copy of image to avoid css issues
                .attr("src", $this.siblings('img').attr("src"))
                .load(function() {
                    pic_real_width = this.width;   // Note: $(this).width() will not
                    pic_real_height = this.height; // work for in memory images.

                    //var top_a = ((pic_real_height / 2) - ($this.height() / 2)) + 1;
                    //var left_a = ((pic_real_width / 2) - ($this.width() / 2)) + 1;
                    //$this.css("top", top_a+"px");
                    //$this.css("left", left_a+"px");
                    
                    
                    $this.css("width", (pic_real_width) - 1 +"px");
                    $this.css("height", (pic_real_height - 1) +"px");
                    
                    //$this.css("top", 0);
                    //$this.css("left", 0);
                                        
                    var top_a = ((pic_real_height / 2) - (plus_h / 2)) + 1;
                    var left_a = ((pic_real_width / 2) - (plus_w / 2)) + 1;
                    
                    $this.css({'backgroundPosition': left_a+'px '+top_a+'px'});
                    
                });
        }
         
   });
   
   $('#print').click(function(e){
        e.preventDefault();
        pagePrint();
   });
});

function pagePrint()
{
    window.print();
}



