$(function()
{
  $('#header .title').click(function(){document.location='/';});

  $('#categorie a.categoria').click(openMenu);

 $('#categorie').css({'margin-top': Math.floor(($('#menu').innerHeight()-$('#categorie').outerHeight())/2)+'px'});

  if($('#d-body').hasClass('home'))
    {$('#ssHome').cycle({ fx: 'fade', speed: 2000 });}
  else if($('#d-body').hasClass('bio') || $('#d-body').hasClass('references'))
    {$('#scroll-text').jScrollPane({enableKeyboardNavigation: false});}
  else if($('#d-body').hasClass('servizio'))
  {
    $('#content').jScrollPane({enableKeyboardNavigation: false});
    $('#content').append('<div id="s-photos-big-c"><ul id="s-photos-big"></ul><div id="photo-alt"></div><div id="photo-alt-bg"></div></div>');
    $('#photo-alt-bg').css('opacity',0.5);
    $('#s-photos > li > img').click(function (){$('#s-photos-big').cycle(parseInt($(this).parent().attr('id').substr(6)));$('#s-text-box, #s-photos').fadeOut('fast',function(){$('#content').jScrollPane({enableKeyboardNavigation: false});$('#s-photos-big-c').fadeIn('fast');});}).each(function(index){$('#s-photos-big').append('<li id="p-big-'+index+'"><img src="'+this.src.replace('_tn.','_medium.')+'" alt="'+$(this).attr('alt')+'" /></li>');});
    $('#s-photos-big-c').append('<div id="pager"><a id="p-prev"></a><div><span id="p-n">1</span> / '+$('#s-photos > li').size()+'</div><a id="p-next"></a><a id="p-close">Close</a></div>');
    $('#p-close').click(function (){$('#s-photos-big').cycle('pause');$('#s-photos-big-c').fadeOut('fast',function(){$('#s-text-box, #s-photos').fadeIn('fast',function(){$('#content').jScrollPane({enableKeyboardNavigation: false});});});});
    $('#s-photos-big').cycle({ fx: 'fade', speed: 1000, before: function(currSlideElement,nextSlideElement){$('#p-n').text(parseInt(nextSlideElement.id.substr(6))+1); $('#photo-alt').text($(nextSlideElement).children('img').attr('alt'));showPhotoAlt();} }).cycle('pause');
    $('#s-photos-big li img').click(function(){$('#s-photos-big').cycle('next');}).mousemove(function(){showPhotoAlt();});
    $('#p-prev').click(function(){$('#s-photos-big').cycle('prev');});
    $('#p-next').click(function(){$('#s-photos-big').cycle('next');});
    $('#show-all').click(sText);
  }
});

function openMenu()
{
  c=$('#s-'+$(this).attr('id').substr(2));
  if(c.is(':hidden'))
  {
    $('#categorie ul:visible').slideUp('slow');
    $('#categorie > li > a.sel').removeClass('sel');
    c.slideDown('slow',vAlignMenu);
    $(this).addClass('sel');
  }
  else
  {
    c.slideUp('slow',vAlignMenu);
    $(this).removeClass('sel');
  }
}

function sText()
{
  if($('#s-text-c').hasClass('open'))
  {
    $('#s-text-c').animate({'height': '63px'},'fast',function(){$('#content').jScrollPane({enableKeyboardNavigation: false});});
    $(this).text('Show all...');
    $('#s-text-c').removeClass('open');
  }
  else
  {
    $('#s-text-c').animate({'height': $('#s-text').outerHeight(true)+'px'},"slow",function(){$('#content').jScrollPane({enableKeyboardNavigation: false});});
    $(this).text('Close');
    $('#s-text-c').addClass('open');
  }
}

function vAlignMenu()
{
  $('#categorie').animate({'margin-top': Math.floor(($('#menu').innerHeight()-$('#categorie').outerHeight())/2)+'px'},'slow');
}

var photoAltTimer;
var photoAltVis=true;

function showPhotoAlt()
{
  clearTimeout(photoAltTimer);
  if($('#photo-alt').text()!='')
  {
    if(!photoAltVis)
    {
      $('#photo-alt-bg, #photo-alt').stop();
      $('#photo-alt-bg').fadeTo('slow',0.5);
      $('#photo-alt').fadeTo('slow',1);
      photoAltVis=true;
    }
    photoAltTimer=setTimeout(hidePhotoAlt,2000);
  }
  else
    {hidePhotoAlt();}
}

function hidePhotoAlt()
{
  if(photoAltVis)
  {
    $('#photo-alt-bg, #photo-alt').stop().fadeTo('slow',0);
    photoAltVis=false;
  }
}
