/*
 * jQuery Slider plugin v1.0
 * File Date: 5/7/2009
 *
 * Copyright (c) 2009 Jim Salyer
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

$.fn.extend({
  slider: function(locals)
  {
    // initialize the global properties and add in the given properties
    var globals = {
      animationDuration: 500,
      itemSelector: "li.slider-item",
      menuSelector: "",
      onAfterSlide: function(slider, fromSlide, toSlide){},
      onBeforeSlide: function(slider, fromSlide, toSlide)
      {
        // trigger the slide action
        this.onSlide(slider, fromSlide, toSlide);
      },
      onInit: function(slider)
      {
        // set up the array of slide ID's to check
        var props = this;
        slider.find(".slider-items " + this.itemSelector).each(function()
        {
          props.items.push($(this).attr("id"));
        });
        
        // start playing (if applicable)
        if (this.playOnLoad)
          slider.slider_startTimer();
        else
          this.stopped = true;
      },
      onSlide: function(slider, fromSlide, toSlide)
      {
        if (fromSlide != toSlide)
        {
          // run the slide action
          var pos = $("#" + this.items[toSlide]).position(), props = this;
          slider.find(".slider-items").stop().animate({
            marginLeft: "-=" + pos.left,
            marginTop: "-=" + pos.top
          }, this.animationDuration, function()
          {
            props.menu.find("a.slider-slide.slider-slide-active").removeClass("slider-slide-active");
            props.menu.find("a.slider-slide").eq(toSlide).addClass("slider-slide-active");
            slider.slider_startTimer();
            props.onAfterSlide(slider, fromSlide, toSlide);
          });
        }
      },
      playOnLoad: true,
      timerDelay: 5000
    };
    $.extend(globals, locals);
    
    // loop through the given elements
    return $(this).each(function()
    {
      // initialize the global variables and properties of this element
      var slider = $(this).css("overflow", "hidden");
      var props = {
        items: [],
        counter: 0,
        menu: typeof(globals.menuSelector) == "string" && globals.menuSelector != "" ? $(globals.menuSelector) : slider,
        timer: null,
        stopped: false
      };
      $.extend(props, globals);
      props.menu.find("a.slider-slide:first").addClass("slider-slide-active"); // activate first slide link
      
      // set up the start link
      props.menu.find("a.slider-start").click(function()
      {
        slider.slider_start();
        return false;
      });
      
      // set up the previous link
      props.menu.find("a.slider-previous").click(function()
      {
        slider.slider_previous();
        return false;
      });
      
      // set up the paging links
      props.menu.find("a.slider-slide").click(function()
      {
        slider.slider_slide($(this).attr("href"));
        return false;
      });
      
      // set up the next link
      props.menu.find("a.slider-next").click(function()
      {
        slider.slider_next();
        return false;
      });
      
      // set up the stop link
      props.menu.find("a.slider-stop").click(function()
      {
        slider.slider_stop();
        return false;
      });
      
      // 1. store the properties
      // 2. run any custom initialization actions
      slider.data("slider_props", props);
      props.onInit(slider);
    });
  },
  // go to the next slide
  slider_next: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      // 1. stop the timer
      // 2. if we're at the end, tell the slider to go to the beginning
      slider.slider_stopTimer();
      var fromSlide = props.counter;
      if (++props.counter >= props.items.length) props.counter = 0;
      props.onBeforeSlide(slider, fromSlide, props.counter);
    });
  },
  // go to the previous slide
  slider_previous: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      // 1. stop the timer
      // 2. if we're at the beginning, tell the slider to go to the end
      slider.slider_stopTimer();
      var fromSlide = props.counter;
      if (--props.counter < 0) props.counter = props.items.length - 1;
      props.onBeforeSlide(slider, fromSlide, props.counter);
    });
  },
  // go to a specific slide
  slider_slide: function(url)
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      // split the given URL by the anchor or pound (#) symbol
      var linkTarget = url.split("#"); 
      if (linkTarget.length > 1)
      {
        // if an anchor has been specified, loop through the slides to find the appropriate one
        $.each(props.items, function(i, n)
        {
          if (n == linkTarget[1])
          {
            // stop the timer and tell the slider to go to the current slide
            slider.slider_stopTimer();
            var fromSlide = props.counter;
            props.counter = i;
            props.onBeforeSlide(slider, fromSlide, props.counter);
            return false;
          }
        });
      }
    });
  },
  // start playing
  slider_start: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      props.stopped = false;
      slider.slider_startTimer();
    });
  },
  // start the timer
  slider_startTimer: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      if (props.stopped) return; // if play is currently stopped, don't do anything
      props.timer = setTimeout(function()
      {
        slider.slider_next();
      }, props.timerDelay);
    });
  },
  // stop playing
  slider_stop: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      slider.slider_stopTimer();
      props.stopped = true;
    });
  },
  // stop the timer
  slider_stopTimer: function()
  {
    // loop through the given elements
    return $(this).each(function()
    {
      // 1. get the global variables and properties
      // 2. check for a valid slider element
      var slider = $(this);
      var props = slider.data("slider_props");
      if (!props) return true;
      
      clearTimeout(props.timer);
      props.timer = null;
    });
  }
});
