
$(document).ready(function(){

  var cur = null;
  var wait = null;
  var off = $('body').hasClass('front') ? 1 : -1;

  $('#submenu').find('ul').each(function(){
    var offset = $('#topmenu').find('.' + this.id).data('sub', this).position();
    var self = this;
    $(this).css({left:offset.left + off + (this.id == 'menu_comp' ? 1 : 0) + 'px'});
    $(this).mouseenter(function() { clearTimeout(wait); });
    $(this).mouseleave(function() {
      wait = setTimeout(function() {
        if (cur == self) { $(cur).hide(); cur = null; wait = null; }
      }, 300);
    });
  });

  $('#topmenu').find('li').each(function(){
    var sub = $(this).data('sub');
    if (sub) {
      $(this).mouseenter(function(){
        clearTimeout(wait);
        if (cur != sub) {
          $(cur).hide();
          cur = sub;
          $(cur).show(0);
        }
      }).mouseleave(function() {
        wait = setTimeout(function() {
          if (cur == sub) { $(cur).hide(); cur = null; wait = null; }
        }, 300);
      });
    }
  });

  var img = $('#media .view img').eq(0);
  var thumb = $('#media .thumb').eq(0);
  $('#media .thumb').each(function(){
    $(this).mouseenter(function(){
      if(!+"\v1")
      {
	thumb = this;
      	$(img).attr('src', $(thumb).find('img').attr('src'));
      }
      else
      {
	$(thumb).removeClass('active');
        $(this).addClass('active');
	thumb = this;
      	$(img).attr('src', $(thumb).find('img').attr('src'));
      }
    });
  });

  $('.rbox').each(function() {
    var wrap = $(this).find('.wr').eq(0);
    setTimeout(function() { yscroll(wrap); }, 3000);
  });
  
  var timers = new Array();
  var backs = $('#b00').find('.back');
  $('#b00').find('.back').each(function(i){
    timers[i] = fadeback(this, 72000 - i * 12000);
  });

});

function fadeback (el, time) {
  return setTimeout(function(){
    $(el).fadeTo(2000, 0, function() {
      var zindex = $(el).css('z-index');
      $(el).css('z-index', zindex - 1).fadeTo(0, 1, function() { fadeback(el, 72000); });
    });
  }, time);
}

function yscroll(el) {
  $(el).animate({top: '-120px'}, 800, 'swing', function() {
    var ctop = $(el).children().eq(0);
    $(el).css({top:0}).detach(ctop);
    $(el).append(ctop);
    setTimeout(function() { yscroll(el); }, 10000);
  });
}

