

/***
 * Slideshow object
 * requires dataContainer for iterating the data
 * @return
 */
function qSlideshow(dataContainer)
{
	this.dataContainer 	= dataContainer;
	this.activeIndex	= -1;
	
	this.speedInterval	= 4500;		// The interval between 2 slides
	this.speedRunning 	= 1000;		// The animation duration when running slideshow
	this.speedManual	   = 200;		// The animation duration during user intervention
	this.running		   = true;		// Slideshow running?
	this.runningTimer;				// Timer for the next slide
	this.animateTimer;            // Delay for starting the animation 
	this.animateTimeout  = 500;   // Delay for starting the animation
	
	this.imagePath = 'files/image/';
}


/**
 * Start the slideshow. Sets a timeout for the next slide-call
 */
qSlideshow.prototype.start = function(skip)
{
	var instance = this;
	this.running = true;
	var delay = (skip == true)  ?  500  :  this.speedInterval;
	
   clearTimeout(this.runningTimer);
   this.runningTimer = setTimeout(function(){ instance.switchTo() }, delay);
}


/**
 * Stops the slideshow and clears timers
 */
qSlideshow.prototype.stop = function()
{
	this.running = false;
	clearTimeout(this.runningTimer);
}


/**
 * Stops the slideshow and clears timers
 */
qSlideshow.prototype.resume = function()
{
   var instance = this;
   clearTimeout(this.runningTimer);
   this.runningTimer = setTimeout(function(){ instance.switchTo() }, this.speedInterval);
}


/**
 * Switch to another slide, uses next when none defined
 */
qSlideshow.prototype.switchTo = function(newIndex)
{
	var instance = this;
	if (newIndex == undefined)
	{
		var newItem = this.dataContainer.setNext();
		newIndex = this.dataContainer.pointer;
	}
	
	// Set timeout for delay
	clearTimeout(this.animateTimer);
	this.animateTimer = setTimeout(function(){ instance.animateTo(newIndex) }, this.animateTimeout);
}


/**
 * Animate to the next slide
 */
qSlideshow.prototype.animateTo = function(newIndex)
{
   if (newIndex == this.activeIndex)
      return false;
   this.activeIndex = newIndex;
   var instance = this;
   this.dataContainer.set(newIndex);
   var aniSpeed = (this.running)
      ?	this.speedRunning
      :	this.speedManual;
   
   /// Animate floating bar
   // Hide previous
   $('#container-balk').animate({ opacity:0 }, aniSpeed/2, function()
   {
      var direction      = (newIndex % 2 == 0)  ?  'right' :  'left';
      var otherDirection = (newIndex % 2 == 0)  ?  'left'  : 'right';
      
      // Reset bar position
      var cssAttr = {opacity: 1};
      cssAttr[ direction ]				= '150%';
      cssAttr[ otherDirection ]			= 'auto';
      $('#container-balk').css(cssAttr);
      
      $('#container-balk').addClass( 'home-balk-'+direction );
      $('#container-balk').removeClass( 'home-balk-'+otherDirection );
      
      // Set active class
      $('#menu-themes li').removeClass('active');
      $('#container-balk li').removeClass('active');
      $('#menu-themes li:eq('+ newIndex +')').addClass('active');
      $('#container-balk li:eq('+ newIndex +')').addClass('active');
      
      $('#container-balk li:eq('+ newIndex +')')
         .mouseenter(function(){ instance.stop(); })
         .mouseleave(function(){ instance.start(true); });
      
      // Start animation new item
      var anim = {};
      anim[direction] = '50%';
      anim['filter'] = '';
      $('#container-balk').animate(anim, aniSpeed, function(){
         // If new, add cycle
         if (instance.running)
            instance.start();
      });
      
      // Switch the images
      var active = $('#container-home-backgrounds .active');
      if (active.length == 0)
         active = $('#container-home-backgrounds img:first');
      active.addClass('last-active');

      var nextImage = $('#menu-themes li:eq('+ newIndex +') a').attr('id')
                        .replace(/(menu-theme)-(.*)/, "home-background-$2");
      $('#'+nextImage).css({opacity: 0})
         .addClass('active')
         .animate({opacity: 1.0}, 1750, function() {
            // Fix IE fonts
            active.removeClass('active last-active');
         });
   });
}


/**
 * Get the image. If not exist, create it
 *
qSlideshow.prototype.getImage = function(index)
{
   var imageId = 'home-background-'+ index;
   if ($('#'+imageId).length == 0)
   {
      var row = this.dataContainer.get( index );
      $('<img id="'+ imageId +'" src="'+ this.imagePath + row.background +'.jpg" alt="">')
         .appendTo('#container-home-backgrounds');
   }
   return '#'+ imageId;
}


qSlideshow.prototype.getNextImage = function()
{
   var next = $('#menu-themes li:eq('+ (this.activeIndex + 1) +') a');
   return getImage( next.attr('href') );
}
*/
