/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */

jQuery.timer = function(interval, callback)
{
  /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

  var interval = interval || 100;

  if (!callback)
    return false;

  _timer = function(interval, callback)
  {
    this.stop = function()
    {
      clearInterval(self.id);
    };

    this.internalCallback = function()
    {
      callback(self);
    };

    this.reset = function(val)
    {
      if (self.id)
        clearInterval(self.id);

      var val = val || 100;
      this.id = setInterval(this.internalCallback, val);
    };

    this.interval = interval;
    this.id = setInterval(this.internalCallback, this.interval);

    var self = this;
  };

  return new _timer(interval, callback);
};

$(document).ready(function()
{

  // Starting number of slide show.
  num = 0;
  $('.featured .item-1').fadeIn();
  slide_show_start();

  $('.featured .item a.overlay').hover(
		function() { $(this).css('background-position', '0 0') },
		function() { $(this).css('background-position', '0 320px') }
	);

  $('ul.slideshow li a.one').click(function()
  {
    num = 0;
    timer.reset(15000);
    $('.featured .item').hide();
    $('.featured .item-1').fadeIn();
    $('ul.slideshow li a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  });

  $('ul.slideshow li a.two').click(function()
  {
    num = 1;
    timer.reset(5000);
    $('.featured .item').hide();
    $('.featured .item-2').fadeIn();
    $('ul.slideshow li a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  });
  $('ul.slideshow li a.three').click(function()
  {
    num = 2;
    timer.reset(5000);
    $('.featured .item').hide();
    $('.featured .item-3').fadeIn();
    $('ul.slideshow li a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  });
  $('ul.slideshow li a.four').click(function()
    {
      num = 3;
      timer.reset(5000);
      $('.featured .item').hide();
      $('.featured .item-4').fadeIn();
      $('ul.slideshow li a').removeClass('selected');
      $(this).addClass('selected');
      return false;
   });
   
  
  function slide_show_start()
  {
    timer = $.timer(10000, function(i)
    {
      num++;
      if (num > 3) { num = 0; }
      if (num == 0) $('ul.slideshow li a.one').click();
      if (num == 1) $('ul.slideshow li a.two').click();
      if (num == 2) $('ul.slideshow li a.three').click();
      if (num == 3) $('ul.slideshow li a.four').click();
    });
  }

  function slide_show_stop()
  {
    $(document).stopTime();
  }

})
