





function setupMainMenu (){
  
  var nav = $("#nav");
  
  var a = nav.find("a");
  
  var overs = a.find(".over");
  overs.css({opacity:0});
  
  a.mouseenter(function(){
    var over = $(this).find(".over");
    over.stop();
    over.css({top:"0"});
    over.animate({opacity:1}, 250);
  });
  
  a.mouseleave(function(){
    var over = $(this).find(".over");
    over.stop();
    over.animate({opacity:0}, 200);
  });
  
  
  var li = nav.find("> li");
  var flyouts = li.find(".flyout");
  flyouts.each(function(){
    var totalWidth = 0;
    $(this).find("li").each(function(){
      totalWidth += $(this).outerWidth(true);
    });
    $(this).data("openWidth", (totalWidth+10)+"px");
  });
  
  li.mouseenter(function(){
    
    flyouts.stop().animate({width:0}, 150);
    
    var flyout = $(this).find(".flyout");
    
    flyout.stop().animate({width:flyout.data("openWidth")}, 250);
    
  });
  
  li.mouseleave(function(){
    
    $(this).find(".flyout").stop().animate({width:0}, 150);
    
  });
  
}

function setupInfoSlider( $container ){
  
  var container = $( $container );
  var slider = container.find(".slider");
  var items = slider.find(".item");
  var itemWidth = items.outerWidth(true);
  
  slider.css({width: ((itemWidth*items.length)+20)+"px"});
  
  /*items.each(function(){
    var item = $(this);
    item.find("p").filter(":gt(0)").remove();
   
    var p = item.find("p");
    
    var text = p.text();
    if(text.length > 200) text = text.substr(0, 196) + " ...";
    p.text(text);
  });*/
  
  if(items.length < 2) return;
  
  
  var currentItem = 0;
  var pauseLength = 7000;
  var pauseTimeout;
  
  var nextItem = function(){
    
    clearTimeout(pauseTimeout);
    
    currentItem += 1;
    if(currentItem > items.length-1){
      currentItem = 0;
      slider.stop().css({left:-(currentItem * itemWidth)+"px"});
      startPause();
      return;
    }
    
    slider.stop().animate({left:-(currentItem * itemWidth)+"px"}, 500, startPause);
    
  }
  
  var startPause = function(){
    
    clearTimeout(pauseTimeout);
    pauseTimeout = setTimeout(nextItem, pauseLength);
    
  }
  
  container.mouseenter(function(){
    clearTimeout(pauseTimeout);
  });
  container.mouseleave(startPause);
  
  startPause();
  
}

function setupTabs(){
  
  var tabs = $(".tabs .tab");
  var tabContentContainer = $(".tab-content");
  var tabContent = $(".tab-content .tab");
  
  tabs.each(function(i, e){
    $(this).data("tabIndex", i);
    if(i >  0) $(tabContent[i]).css({display:"none"});
    var a = $(this).find("a");
    if(a.hasClass("current")) a.data("current", true);
  });
  
  tabs.click(function(){
    
    var overlay = $("<div></div>").css({
      width: "100%",
      height: "100%",
      position: "absolute",
      top: "0",
      left: "0",
      zIndex: "999",
      backgroundColor: "#000000"
    });
    tabContentContainer.append(overlay);
    
    tabContent.css({display:"none"});
    tabs.find("a").removeClass("current").data("current", false);
    
    $(tabContent[$(this).data("tabIndex")]).css({display:"block"});
    $(this).find("a").addClass("current").data("current", true);
    
    overlay.animate({opacity:0}, 300, function(){
      overlay.remove();
    });
    
  });
  
  tabs.mouseenter(function(){
    tabs.find("a").removeClass("current");
  });
  
  tabs.mouseleave(function(){
    tabs.find("a").each(function(){
      if($(this).data("current") == true) $(this).addClass("current");
    });
  });
  
}

function setupScrollers(){
  $(".scroller").each(function(){
    
    var scroller = $(this);
    
    var up = scroller.find("> .up");
    var down = scroller.find("> .down");
    var content = scroller.find("> .content");
    var offset = 0;
    var scrollSpeed = 5;
    var targetScroll = NaN;
    
    var scrollInterval;
    
    var startScroll = function(direction){
      
      clearInterval(scrollInterval);
      content.stop();
      
      if(scroller.outerHeight() > content.outerHeight()){
        offset = 0;
        content.css({top:"0px"});
        return;
      }
      
      scrollInterval = setInterval(function(){
        
        offset += scrollSpeed*direction;
        
        if(offset > 0 || -offset > content.outerHeight() - scroller.outerHeight()){
          stopScroll();
          return;
        } 
        
        content.css({top:offset+"px"});
        
      }, 40);
    }
    
    var stopScroll = function(){
      
      clearInterval(scrollInterval);
      content.stop();
      
      if(scroller.outerHeight() > content.outerHeight()) return;
      
      if(offset > 0){
        offset = 0;
      } else if (-offset > content.outerHeight() - scroller.outerHeight()){
        offset = -(content.outerHeight() - scroller.outerHeight());
      }
      
      content.css({top:offset+"px"});
    }
    
    var scrollTo = function(o){
      o = Math.abs(o);
      offset = -o;
      if (o > content.outerHeight() - scroller.outerHeight()) {
        offset = -(content.outerHeight() - scroller.outerHeight());
      }
      if(targetScroll == offset) return;
      targetScroll = offset;
      content.stop().animate({top:offset+"px"}, "fast", function(){
        targetScroll = NaN;
      });
    }
    
    up.unbind();
    down.unbind();
    scroller.unbind();
    content.find("a[href^=#]").unbind();
    
    if(scroller.outerHeight() < content.outerHeight()){
      
      up.css({display: "block"});
      down.css({display: "block"});
      
      up.mousedown(function(){
        startScroll(1);
      });
      down.mousedown(function(){
        startScroll(-1);
      });
      $("body").mouseup(stopScroll);
      
      scroller.mousewheel(function(event, delta) {
        var target = offset+(delta*scrollSpeed*3);
        if(target > -1) target = 0;
        scrollTo(target);
        return false;
      });
      
      content.find("a[href^=#]").click(function(){
        var n = this.href.split('#');
        n = n[n.length-1];
        var target = content.find("a[name="+n+"]");
        if(target.length < 1) return;
        scrollTo(target.position().top);
        return false;
      });
      
    } else {
      
      up.css({display: "none"});
      down.css({display: "none"});
      
      content.stop().css({top:0});
      
    }
    
  });
}

function gallery() {
  
  var slideSpeed = 500;
  var direction = -1;
  
  var images;
  
  var currentID = 0;
  var currentImage;
  var nextImage = false;
  
  var container = $("#gallery");
  var data = $("#gallery-data");
  var buttonHolder = $("#gallery-buttons");
  
  var images = data.find("img");
  
  var playing = true;
  var pauseDuration = 6000;
  var restartDuration = 15000;
  
  var nextTimeout;
  
  
  var preloaderTimeout;
  var preloaderDelay = 200;
  var preloader = $('<image id="preloader-ani" src="images/layout/preloader.gif" />').css({display:"none", opacity:"0"});
  container.append(preloader);
  
  this.showImage = showImage;
  
  images.each(function(i, e){
    var img = $(this);
    img.data("src", img.attr("src"));
    img.attr("src", "");
    img.data("id", i);
    
    var button = $('<a></a>');
    buttonHolder.append(button);
    button.css({width: ((buttonHolder.width()/images.length)-4)+"px" });
    button.data("id", i);
    button.click(function(){
      showImage($(this).data("id"));
    })
    button.mouseenter(function(){
      $(this).addClass("over");
    });
    button.mouseleave(function(){
      $(this).removeClass("over");
    });
    
  });
  
  var buttons = buttonHolder.find("a");
  
  container.mouseenter(function(){
    clearTimeout(nextTimeout);
    clearTimeout(preloaderTimeout);
  });
  container.mouseleave(function(){
    nextTimeout = setTimeout(selectNextTimeout, pauseDuration); 
  });
  
  showImage(0);
    
  
  
  
  function showImage(id){
    
    clearTimeout(preloaderTimeout);
    
    preloaderTimeout = setTimeout(function(){
      preloader.css("display", "block");
      preloader.stop().animate({opacity:1}, 200);
    }, preloaderDelay);
  
    clearTimeout(nextTimeout);
    
    if(nextImage) nextImage.remove();
    
    currentID = id;
    
    hiliteButton(id);
    
    var dataImage = $(images[id]);
    var src = dataImage.data("src");
    nextImage = dataImage.clone();
    nextImage.css({left:"100%", opacity:"0"});
    nextImage.attr("src", "");
    
    nextImage.load(function(){
      dataImage.data("loaded", true);
      if($(this).data("id") == nextImage.data("id"))
        slide();
      else
        $(this).remove();
      if(images.length > 1) preloadNextImage();
    });
    
    container.append(nextImage);
    
    nextImage.attr("src", src);
  
  };
  
  function preloadNextImage(){
    for(var i = 0; i<images.length; i++){
      var img = $(images[i]);
      if( img.data("loaded") != true ){
        img.load(function(){
          img.data("loaded", true);
          img.attr("src", "");
          preloadNextImage();
        });
        img.attr("src", img.data("src"));
        break;
      }
    }
  };
  
  function slide(){
    
    nextImage.css({left:(100*direction*-1)+"px"});
    
    if(currentImage) {
      currentImage.animate({left:(200*direction)+"px", opacity:"0"}, slideSpeed, function(){ $(this).remove() } );
      nextImage.animate({left:"0", opacity:"1"}, slideSpeed );
    } else {
      nextImage.css("left", "0");
      nextImage.animate({opacity:"1"}, slideSpeed );
    }
    
    currentImage = nextImage;
    nextImage = null;
    
    clearTimeout(nextTimeout);
    if(images.length > 1){
      var duration = playing ? pauseDuration : restartDuration;
      nextTimeout = setTimeout(selectNextTimeout, duration);
    }
    
    clearTimeout(preloaderTimeout);
    preloader.stop().animate({opacity:0}, 200, function(){
    $(this).css("display", "none");
    });
    
  };
  
  function selectNextTimeout(){
    playing = true;
    if(direction == -1){
      selectNextImage();
    } else {
      selectPrevImage();
    }
  }
  
  function selectNextImage(){
    direction = -1;
    currentID++;
    if(currentID > images.length-1) currentID = 0;
    showImage(currentID);
  };
  
  function selectPrevImage(){
    direction = 1;
    currentID--;
    if(currentID < 0) currentID = images.length-1;
    showImage(currentID);
  };
  
  function hiliteButton(id){
    
    buttons.removeClass("current");
    
    $(buttons[id]).addClass("current");
  }
  
}



$(document).ready(function(){
  
  setupMainMenu();
  
  setupInfoSlider("#fyi-box .slider-container");
  
  setupScrollers();
  
  $("body").fontResize(function(){
    setupScrollers();
  });
  
  setupTabs();
  
  gallery();
  
  $("#small-bike-image").jqzoom({
    title: false,
    zoomWidth: 383,
    zoomHeight: 327,
    xOffset: 25,
    yOffset:-1,
    showEffect: "fadein",
    hideEffect: "fadeout"
  });
  
  
  swfobject.embedSWF("flash/home-banner.swf", "banner-swf", "1031", "346", "9.0.0", null, {}, { wmode: "transparent" });
  
  
});





