$(function(){slideShow.init()});

slideShow = {
  
  slides: new Array(),
  len: '',
  duration: 6000,
  fade_in_duration: 400,
  timer: '',
  current: 0,
  pause_on_mouse: false,
  
  init: function()
  {
    slideShow.get_slides();
    slideShow.len = slideShow.slides.length;
    slideShow.add_slides(); 
    slideShow.start();
    
    if(slideShow.pause_on_mouse)
    {
      $('.slide_show_wrapper').mouseenter(function()
      {
        slideShow.stop();
        slideShow.current = slideShow.current -1;
      });
      
      $('.slide_show_wrapper').mouseleave(function()
      {
        slideShow.start();
      })
    }
  },
  
  get_slides: function() 
  {
    slideShow.slides[0] = 'content/img/slides/usord.jpg';
    slideShow.slides[1] = 'content/img/slides/turkey-trot.jpg';
    slideShow.slides[2] = 'content/img/slides/mr-glass.jpg';
  },

  add_slides: function()
  {
    // add the slides to the slide_show_wrapper
    for(var i = 0; i < slideShow.len; i++)
    {
      // add slides to the DOM
      $('.slide_show_wrapper').append("<li id='qs_"+i+"' class='quik_slide'><img src='"+slideShow.slides[i]+"'/></li>");
      
      // add navigation dots to the DOM
      $('.slide_nav').append("<li id='nav_dot_"+i+"' class='nav_icon'></li>");
    }
    
    // bind click event to the newly added nav_icons
    $('.nav_icon').bind('click', function(event){
      slideShow.nav_icon_click(event.target);
      });
  },
  
  set_current: function(cur)
  {
    if(cur < (slideShow.len -1))
    {
      ++slideShow.current;
    } 
    else
    {
      slideShow.current = 0;
    }
  },
  
  start: function()
  {
    slideShow.show_slide(slideShow.current);
    slideShow.change_nav(slideShow.current);
    slideShow.set_current(slideShow.current);
      
    slideShow.timer = window.setInterval(function(){
      // change the slide
      slideShow.change_slide(slideShow.current);
      
      // change the navigation indicator
      slideShow.change_nav(slideShow.current);
      
      // adjust the pointer
      slideShow.set_current(slideShow.current);
      
    }, slideShow.duration);
  },
  
  stop: function()
  {
    window.clearInterval(slideShow.timer);
  },
  
  show_slide: function(slide)
  {
    // immediately show the slide without fadeIn timer
    $('.slide_show_wrapper').children('li').fadeOut(0).eq(slide).fadeIn(0);  
  },
  
  change_slide: function(slide)
  {
    // hide all slides except the current slide
    $('.slide_show_wrapper').children('li').fadeOut('slow').eq(slide).fadeIn('fast');
  },
  
  change_nav: function(nav)
  {
    // make all nav_icons default except for the current one
    $('.slide_nav').children('li').removeClass('current').eq(nav).addClass('current');
  },
  
  nav_icon_click: function(target)
  {
    // stop the timer
    slideShow.stop();
    
    // set current to value of clicked nav icon
    slideShow.current = $(target).attr('id').substr(-1);
    
    // restart the timer
    slideShow.start();
  }
}
