$.fn.wait = function(time, type)
{
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function()
  {
    var self = this;
    setTimeout(function()
    {
      $(self).dequeue();
    }, time);
  });
};
$(document).ready(function()
{
  var prevText;
  $('.section ul li a').hover(function()
  {
    section = $(this).parent().parent().parent();
    prevText = section.children('.section_text').text();
    thisText = $(this).next().text();
    section.children('.section_text').text(thisText);
  }, function()
  {
    section = $(this).parent().parent().parent().children('.section_text').text(prevText);
  });

  if ($.browser.msie)
  {
    $('a').css('cursor', 'pointer');
    $('a').click(function()
    {
      window.location = $(this).attr('href');
    });
  }

  var prevText;
  var prevImg;
  $('.elemento').hover(function()
  {
    updateText = $(this).children('span').children('a').text();
    updateImg = $(this).children('div').text();
    prevText = $('#grey_column_image_container').children('span').text();
    prevImg = $('#grey_column_image_container').children('img').attr('src');
    $('#grey_column_image_container').children('span').text(updateText);
    $('#grey_column_image_container').children('img').attr('src', updateImg);
  }, function()
  {
    $('#grey_column_image_container').children('span').text(prevText);
    $('#grey_column_image_container').children('img').attr('src', prevImg);
  });

  var background_color;
  $('.category_bar ul li').hover(function()
  {
    background_color = $(this).css('backgroundColor');
    newBackground = $(this).children('.background_color').text();
    $(this).css('backgroundColor', newBackground);
  }, function()
  {
    $(this).css('backgroundColor', background_color);
  });

  $('.invia_link').click(function(event)
  {
    event.preventDefault();
    var left = Math.floor((screen.width - 500) / 3);
    var top = Math.floor((screen.height - 360) / 3);
    window.open($(this).attr('href'), 'popup', 'width=500,height=360,top=' + top + ',left=' + left + ',scrollbar=yes');
  });

  $('#change_menu').change(function()
  {
    window.location.href = $(this).attr('value');
  });


  $('table#serie td a').mousemove(function(event)
  {
    height = $(document).height();
    elementHeight = $(this).offset().top;
    $('#tooltip img').attr('src', $(this).children('.image_container').children('img').attr('src'));
    $('#tooltip h4').text($(this).children('.element_container').text());
    if (height - elementHeight < 300)
    {
      myHeight = $(this).children('span').children('img').height();
      $('#tooltip').css({top: (event.pageY - 65 - myHeight) + 'px', left: (event.pageX - 70) + 'px'});
    }
    else
    {
      $('#tooltip').css({top: (event.pageY + 25) + 'px', left: (event.pageX - 70) + 'px'});
    }
  });

  $('table#serie td a').hover(function(event)
  {
  }, function()
  {
    $('#tooltip').css({top: '-500px', width: ''});
    $('#tooltip h4').text('');
  });

  if (!$.browser.msie)
  {
    $('.scarica_pdf').each(function()
    {
      $(this).attr('target', '_blank');
    });
  }

  var ricercaText = $('#ricerca_input').val();

  $('#ricerca_input').click(function()
  {
    if ($(this).val() == ricercaText)
    {
      $(this).val('');
    }
  });

  $('#ricerca_input').blur(function()
  {
    if ($(this).val().replace(' ', '') == '')
    {
      $(this).val(ricercaText);
    }
  });

});
