jQuery(document).ready(function() {
  Intro.run();
});

$.address.init(function(event){}).change(function(event){
  $.ajax({
    url: event.value,
    success: function(data) {
      xml = $(data);
      document.title = xml.find('#content').attr('title') +
      ' - www.die-mutmachwerkstatt.com';
      $('#content').replaceWith(xml.find('#content'));
      
      $('.navigation a').each(function() {
        $(this).toggleClass('active', $(this).attr('href') == event.value);
      });
      
      $("a[rel=slides]").fancybox({
        'titleShow'     : false,
    		'transitionIn'	: 'elastic',
    		'transitionOut'	: 'elastic',
    		'easingIn'      : 'easeOutBack',
    		'easingOut'     : 'easeInBack'
      });
    }
  });
});


var Intro = {
  current_slide: 0,
  slide_count: 11,
  start_delay: 1500,
  in_duration: 1000,
  out_duration: 300,
  interval: 8154,
  blank_time: 500,
  timeout: null,
  slide_path: '/images/intro/img',
  fade_in: function() {
    $('#intro img').fadeIn(Intro.in_duration, Intro.wait);
  },
  fade_out: function() {
    $('#intro img').fadeOut(Intro.out_duration, Intro.next_slide);
  },
  next_slide: function() {
    Intro.current_slide = Intro.current_slide + 1;
    if(Intro.current_slide < Intro.slide_count) {
      $('#intro img').attr('src', Intro.slide_path + Intro.current_slide + ".jpg");
      setTimeout(Intro.fade_in, Intro.blank_time);
    } else {
      setTimeout(Intro.skip, 500);
    }
  },
  wait: function() {
    Intro.timeout = setTimeout(Intro.fade_out, Intro.interval);
  },
  run: function() {
    if(Intro.should_run()) {
      $('#intro a').click(function(e){
        e.preventDefault();
        Intro.skip();
      });
      $('#intro').show();
      Player.intro();
      setTimeout(Intro.fade_in, Intro.start_delay);
      setTimeout(Intro.preload, Intro.start_delay*2);
    }else{
      Player.page();
    }
  },
  should_run: function() {
    // check cookie
    if($.cookie('intro') == 'skip') {
      return(false);
    }
    // check url
    if(jQuery.url.attr('directory') != '/') {
      return(false)
    }
    if(jQuery.url.attr('directory') == '/' && jQuery.url.attr('anchor')) {
      return(false)
    }
    return(true);
  },
  preload: function() {
    for(i=1;i<=Intro.slide_count;i++) {
      $('<img />')
      .attr('src', Intro.slide_path + i + ".jpg")
      .load(function(){
        $('#preload').append( $(this) );
      });
    }
  },
  skip: function() {
    $.cookie('intro', 'skip');
    $('#intro').fadeOut(500, function() {$('#intro').remove()});
    $('#player').stop();
    clearTimeout(Intro.timeout);
    Player.page();
  }
};

var Player = {
  intro: function() {
    $('#player').jPlayer({
      ready: function () {
        $(this).setFile("/media/intro.mp3").play();
      },
      oggSupport: false,
      volume: 80
    })
    .onProgressChange(function() {});
  },
  page: function() {
    $('#playercontroll').show();
    $('#player').jPlayer({
      ready: function () {
        $(this).setFile("/media/page.mp3").play();
      },
      oggSupport: false,
      volume: 80
    })
    .jPlayerId("play", "play")
  	.jPlayerId("pause", "pause")
  	.onProgressChange(function() {})
    .onSoundComplete(function() {
      $(this).play();
    });
  }
};

