$(document).ready(function() {
  $('#gallery-nav').mousemove(function(event) {
    var offset = $('#gallery-nav').offset();
    var coordX = event.pageX - offset.left;
    var border = $('#gallery-nav').width() / 2;
    
    if( coordX >  border ) {
      $('#gallery-nav-next').css('display', 'block');
      $('#gallery-nav-prev').css('display', 'none');
    } else {
      $('#gallery-nav-next').css('display', 'none');
      $('#gallery-nav-prev').css('display', 'block');
    }
  });
  $('#gallery-nav').mouseout(function(event) {
    $('#gallery-nav-next').css('display', 'none');
    $('#gallery-nav-prev').css('display', 'none');
  });
  
  $('#gallery-container').slides({
    pagination: true,
    generatePagination: false,
    effect: 'fade, fade',
    crossfade: true,
    fadeSpeed : 1000,
    play: 3000
  });
  
  $('#gallery-play').click(function() {
    $(this).css('display', 'none');
    $('#gallery-pause').css('display', 'block');
    return false;
  });
  $('#gallery-pause').click(function() {
    $(this).css('display', 'none');
    $('#gallery-play').css('display', 'block');
    return false;
  });
  
  rotateHomeImages(5000, 1000);
  
  var speakerSoundPlay = false;
  $(".speaker-sound").jPlayer({
    ready: function () {
      $(this).jPlayer("setMedia", {
        mp3 : "/files/" + $(this).attr('id') + ".mp3"
      });
      $(this).jPlayer("play");
    },
    swfPath: "/files",
    supplied: "mp3",
    solution: "flash",
    play: function(e){
      speakerSoundPlay = true;
    },
    pause: function(e){
      speakerSoundPlay = false;
    }
  });
  
  $('#speaker-icon').click(function() {
    if(speakerSoundPlay) {
      $(".speaker-sound").jPlayer("stop");
    } else {
      $(".speaker-sound").jPlayer("play");
    }
    return false;
  });
});


function rotateHomeImages(time, speed) {
  var list = $('#wrapper-images img');
  if(list.length > 0) {
    var current_index = -1;
    list.each(function(index, item) {
      if($(item).hasClass('current')) {
        $(item).removeClass('current');
        current_index = index;
      }
    });
    
    if((current_index + 1) >= list.length) {
      var current_item = $(list[0]);
    } else {
      var current_item = $(list[current_index + 1]);
    }
    $(list[current_index]).fadeOut(speed);
    current_item.addClass('current').fadeIn(speed, function() {
      setTimeout('rotateHomeImages('+time+','+speed+')', time);
    });
  }
}

