/*
Site-wide JS
*/

$(function(){
  // $("#SearchQ").hint();
  $('marquee').marquee();
  
  // do some javascript trickery with the section nav.
  $("#section_nav").each(function(){
    // + $("#section_nav a.currentSection").next('ul').html()
    var 
      link = $('<div id="section_title"></div>').append($("#section_nav a.currentSection").clone()),
      sectionNav = $("#section_nav a.currentSection").next('ul').clone();
    $(this).empty().append(link).append(sectionNav);
  })
    // .html($("#section_nav a.currentSection").wrap('<div></div>').html() + $("#section_nav a.currentSection").next('ul').html())
    // every li but the last one
    .find('li').not(':last').append('<span class="bullet">&#183;</span>').end()
    // section_nav itself
    .end().css('visibility', 'visible');
  
  $(".image_block:odd").addClass('last');
  
  // create new accordion
  var content_accordion = new Accordion("#content > h3:not(.no_accordion)");
  $(content_accordion.options.excerpt_separator, content_accordion.contentareas).remove();
  // collapse all by default
  content_accordion.collapseAll();
  
  $("#home_slides").each(function(){
    var ref = this;
    $("dt", $(this)).each(function(){
      if(typeof($(ref).data('slides')) == 'undefined'){
        $(ref).data('slides', []);
      }
      var data = $(ref).data('slides');
      data[data.length] = {
        title: $(this).html(),
        content: $(this).next('dd').html()
      };
      $(ref).data('slides', data);
    });
    // console.log($(this).data('slides'));
    
   $.extend(this, {
     interval: false,
     timer: 4000,
     paused: false,
     theIndex: -1,
     choose: function(index){
       this.theIndex = index;
       var data = this.getSlide(index);
       var ref = this;
       $(this).each(function(){
         // $("#home_main_image").html(data.title);
         $("#koa_ridge_banner").html(data.content);
       });
     },
     nextSlide: function(){
       if(!this.paused){
         this.choose(this.theIndex + 1);
       }
     },
     pause: function(){
       // handle pause/play link.
        // $(".featured_pauseplay", $(this)).text('play').unbind('click').bind('click', function(e){
        //  e.preventDefault();
        //  featured.play();
        // });
       // do pause action.
       this.paused = true;
       clearInterval(this.interval);
     },
     play: function(){
       // handle pause/play link.
        // $(".featured_pauseplay", $(this)).text('pause').unbind('click').bind('click', function(e){
        //  e.preventDefault();
        //  featured.pause();
        // });
       // do play action.
       this.paused = false;
       if(this.interval != false){
         clearInterval(this.interval);
       }
       this.setTheInterval();
     },
     getSlide: function(index){
       return $(this).data("slides")[((index-1) % $(this).data("slides").length)];
     },
     setTheInterval: function(){
       var ref = this;
       this.interval = setInterval(function(){
         ref.nextSlide();
       }, this.timer);
     }
   });
   // choose the first one on page load.
   this.choose(1);
    // this.play();
  });
});

