$(document).ready(function(){

  $.fn.maphilight.defaults = {
    fill: true,
    fillColor: 'ffffff',
    fillOpacity: 0.3,
    stroke: true,
    strokeColor: 'a01840',
    strokeOpacity: 1,
    strokeWidth: 3,
    fade: true,
    alwaysOn: false    
  }  

  $('#states-map').maphilight();
  
  $('#states-map-small').maphilight();
  

  /* Dropdown menus code */
  var timeouts = new Object(), duration = 200;
  $("a.dropdown").click(function(){
    return false;
  });
  $("a.dropdown").mousemove(function(){
    clearTimeout(timeouts[$(this).attr("rel")]);
    $("#" + $(this).attr("rel")).slideDown();
  });  
  $("a.dropdown").mouseleave(function(){
    timeouts[$(this).attr("rel")] = setTimeout('$("#' + $(this).attr("rel") + '").slideUp()', duration);
  });
  $("ul.dropdown-menu").mouseleave(function(){
    timeouts[$(this).attr("id")] = setTimeout('$("#' + $(this).attr("id") + '").slideUp()', duration);
  });
  $("ul.dropdown-menu").mousemove(function(){
    clearTimeout(timeouts[$(this).attr("id")]);
  });

  $('#mycarousel').jcarousel({
      wrap: 'both'
  });

  $('.jcarousel-ctn').jcarousel({wrap: 'both', scroll: 1});
  $('.jcarousel-container').jcarousel({scroll: 1,
    wrap: 'both',
    itemLastInCallback: {
      onAfterAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeIn();
		$('.right-part').height($('#item-'+idx).height());
      }
    },
    itemLastOutCallback: {
      onBeforeAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeOut();
      }
    }
  });

/*  $('#passives-carousel').jcarousel({wrap: 'both', scroll: 1});
  $('#systems-carousel').jcarousel({wrap: 'both', scroll: 1});*/

  $('#timeline-carousel').jcarousel({
    scroll: 1,
    itemLastInCallback: {
      onAfterAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeIn();
      }
    },
    itemLastOutCallback: {
      onBeforeAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeOut();
      }
    }
  });
  
/*  $('#lines-carousel').jcarousel({
    scroll: 1,
    wrap: 'both',
    itemLastInCallback: {
      onAfterAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeIn();
      }
    },
    itemLastOutCallback: {
      onBeforeAnimation: function(carousel, item, idx, state){
        $('#item-'+idx).fadeOut();
      }
    }
  });*/
  
  $('.tabs a').click(function(){
    $('.panel-content').hide();
    $('.tabs li').removeClass('current');
    $('#'+$(this).attr('rel')).show();
    $(this).parent().addClass('current');
    return false;
  });
  
  $('.category-title a').click(function(){
    $(this).toggleClass('up');
    $('#'+$(this).attr('rel')).toggle('fast');
    return false;
  });
  
  $('.contact-link').click(function(){
    $('.email').val($(this).attr('rel'));
  });
  
  $('.mail-form').submit(function(){
    if ($('#fullname').val() == '' || $('#subject').val() == '' || $('#message').val() == '' ) {
      alert('All fields are required.');
      return false;
    }
    $('#mailer-answer').addClass('loading');
    $.ajax({
      type: 'POST',
      url: $(this).attr('action'),
      data: $(this).serialize(),
      success: function (data) {
        $('#mailer-answer').removeClass('loading');
        if (data != 'success') {
          alert("Error. Can't send an email.");
        } else {
          $('#mailer-answer').text('Email sent.');
        }
      }
    });
    return false;
  });  
  
});
