soundManager.url = 'http://spiritualstudio.com/wp-content/themes/ss/swf/';

$(document).ready(function() {
  var $images = $('.slides img');
  var $thumbs = $('.thumbs img');
  
  $images.fadeIn(0);

  // show menu when hovering on header, hide a couple of shortlty after hovering off
  $('.home #header, .single #header').hover(
    function() {
      $('#menu').stop(true, true).fadeIn(100);
      $('#logo').stop(true, true).fadeTo(0, 1);
    },
    function() {
      $('#menu').stop(true, true).fadeOut(400);
      $('#logo').stop(true, true).fadeTo(1000, 0);
    }
  ).mouseover(function() {
    $('#menu, #logo').addClass('idle');
  });
  
  // show sidebar controls when hovering on footer, hide a couple of seconds after hovering off
  $('#footer').hover(
    function() {
      $('#sidebar').stop(true, true).animate({ opacity: 1 });
    },
    function() {
      $('#sidebar').animate({ opacity: 0 });
    }
  ).mouseover(function() {
    $('#sidebar').addClass('idle');
  });
  
  // autohide menu and sidebar on load if appropriate
  $('.home #menu.idle, .single #menu').delay(2000).fadeOut(800, function() { $(this).removeClass('idle') });
  $('.home #logo.idle, .single #logo').delay(2000).fadeTo(1000, 0, function() { $(this).removeClass('idle') });
  $('#sidebar.idle').delay(2400).animate({ opacity: 0 }).removeClass('idle');
  
  // slideshow
  var playing = 1, tune;
  
  if($images.length > 1) {
    var ss = new slideshow();
    var mp3 = $('.post').data('mp3');
    
    if(mp3) {
      soundManager.onready(function() {
        
        tune = soundManager.createSound({
          id: "tune",
          url: mp3,
          onplay: function() {
            if($(".thumbs").is(":hidden")) {
              ss.play();
            }
          },
          onfinish: function() {
            loopSound(this);
          }
        });
        tune.play();
      });
    } else {
      ss.play();
    }

    // next ticks to next image, pauses the slideshow, hides grid, deselects basement button
    $('#next a').click(function(e) {
      e.preventDefault();
      
      if(playing == 1) {
        $('#pp a').trigger('click');
      }
      hideGrid();
      tick("next", 100, 100);
    });
    
    // prev ticks to prev image, pauses the slideshow, hides grid, deselects basement button
    $('#prev a').click(function(e) {
      e.preventDefault();
      
      if(playing == 1) {
        $('#pp a').trigger('click');
      }
      hideGrid();
      tick("prev", 100, 100);
    });

    $("#pp a").click(function(e) {
      e.preventDefault();
      $('#basement a').removeClass('on');
      
      if(playing == 1) {
        if(tune) {
          tune.pause();
        }
        
        ss.stop();
        playing = 0;
      } else {
        if(tune) {
          tune.play();
        }
        
        ss.play();
        hideGrid();
        playing = 1;
      }
    });
  }
  
  // show/hide thumbs
  $('#basement a').click(function(e) {
    e.preventDefault();
    if($('.thumbs').is(":hidden")) {
      if(ss) {
        if(tune) {
          tune.pause();
        }
        
        ss.stop();
        playing = 0;
      }
      showGrid();
    } else {
      if(ss) {
        if(tune) {
          tune.play();
        }
        
        ss.play();
        playing = 1;
      }
      hideGrid();
    }
  });
  
  // stylish hover on thumbs
  $thumbs
  .fadeTo(50, 0.4)
  .hover(
    function() {
      $(this).stop(true, true).css('opacity', 1);
    },
    function() {
      $(this).stop(true, true).fadeTo(500, 0.4);
    }
  );
});

function showGrid() {
  $('#basement a').removeClass('on');
  var $slides = $('.slides'), $thumbs = $('.thumbs');
  $slides.fadeOut(400, function() {
    $thumbs.show();
  });
}

function hideGrid() {
  $('#basement a').removeClass('on');
  var $slides = $('.slides'), $thumbs = $('.thumbs');
  $thumbs.fadeOut(0, function() {
    $slides.fadeIn(400);
    $slides.show();
  });
  jump();
}

function slideshow() {
  var timer_id;
  this.play = function() {
    $('#main').addClass('playing');
    $("#pp span.tt").text('pause');
    $("#pp a").addClass('on');

    timer_id = setInterval(function() {
      tick("next", 800, 1000);
    }, 5000);
  },
  this.stop = function() {
    $('#main').removeClass('playing');
    $("#pp span.tt").text('play');
    $("#pp a").removeClass('on');
    
    clearInterval(timer_id);
    jump();
  }
}

function tick(direction, fadeOutTime, fadeInTime) {
  var $current = $('.current').length ? $('.current') : $('.slide').first();
  var $next = $current.next().length ? $current.next() : $('.slide').first();

  if(direction == "prev") {
    $current = $('.current').length ? $('.current') : $('.slide').last();
    $next = $current.prev().length ? $current.prev(): $('.slide').last();
  }
  var $currentImg = $current.find('span img');
  var $nextImg = $next.find('span img').css('opacity', 0);
  
  var target = $next.offset().top;
  var off = $('#main').scrollTop() + target;

  $currentImg.stop(true).animate({ opacity: 0 }, fadeOutTime, function() {
    $next.addClass('current');
    $current.removeClass('current');
    $('html, body, #main').animate({ scrollTop: off }, 0, function() {
      $nextImg.animate({ opacity: 1}, fadeInTime, function() {
        $currentImg.css('opacity', 1);
        tick("next", 800, 1000);
      });
    });
  });
};

function jump() {
  var $current = $('.current').length ? $('.current') : $('.slide').first();

  var offset = $current.offset().top;
  $('html, body, #main').animate({ scrollTop: offset }, 0);
}

