//jquery.slider_pxl.js
//author: 			Bjoern Sellnau @ PIXELTEX
//project:			jQuery Plugin 'SliderPXL'
//description:	this is the main class for the SliderPXL Plugin
//added:				17-04-2009
//modified:			30-04-2009
//version				0.75
//=====================================================

(function($)
{

	// define sliderPXL object with some default config settings.
	$.sliderPXL = {
		defaults: {
			contentSize : 185,
			direction: 'right',	//left,right,top,bottom
			debugMode : false,
			animationDurration : 500,
			easing : 'linear',
			sliderInterval:5000
		}		
	};
	
	// extend jQuery with the plugin.
	// -- the constructor --
	$.fn.extend({
		sliderPXL:function(config)
		{
			
			// use defaults or properties supplied by user.
			var config = $.extend({}, $.sliderPXL.defaults, config);
			
			config.slider_id = this.attr("id");

		  if(config.debugMode) 
		    console.log("before init() ["+config.name+"]");
		    
		    if(config.contentSize.indexOf('px') != -1)
		    {
			    config.contentSize = String(config.contentSize);
			    config.contentSize = config.contentSize.substring(0,config.contentSize.length-2);
		    }
		    
		  if(config.debugMode){ 
		    console.log("- config of "+config.name+" -");
		    console.log(config);
			}		    
			
			init(config,doSlideInterval,this);
			
			// return the jQuery object for chaining.
			return this;		
		}
	});

	$.fn.sliderPXL.stopSlider=function(scope_id){
			var interval = $("#"+scope_id).data('slider_slideInterval');
			clearInterval(interval);
	}

	$.fn.sliderPXL.slideBackward=function(scope_id)
	{
		var config = $("#"+scope_id).data('slider_config');
		var slider = $("#"+scope_id).get(0);
		var minRange = 0;
		var maxRange = $("#"+scope_id).data('slider_maxRange');
		var currentContent = $("#"+scope_id).data('slider_currentContent');
		var contentNum = $("#"+scope_id).data('slider_contentNum');




	  if(config.debugMode) 
	    console.log("undoSlideInterval() called ["+config.name+"]");

		if(config.debugMode)
			console.log("["+config.name+"] -> "+slider.style.right.substring(0,slider.style.right.length-2)+" = "+(minRange+(config.contentSize*2)));
			
 	 	if(slider.style.right.substring(0,slider.style.right.length-2) <= minRange)
  	{
    	slider.style.right = (maxRange-(config.contentSize*2))+"px"; 
    	currentContent = contentNum-3;
  	} else {
    	currentContent--; 
  	}
  	
  	$("#"+scope_id).data('slider_currentContent', currentContent);

  	var position = (currentContent * config.contentSize);
  
  	if(config.debugMode)
  		console.log(position+" = "+slider.style.right.substring(0,slider.style.right.length-2)+" ["+config.name+"]");
  
  	$("#"+config.slider_id).animate({right:position},config.animationDurration,config.easing);	
	};

	$.fn.sliderPXL.slideForward=function(scope_id)
	{
		var config = $("#"+scope_id).data('slider_config');
		var slider = $("#"+scope_id).get(0);
		var maxRange = $("#"+scope_id).data('slider_maxRange');
		var currentContent = $("#"+scope_id).data('slider_currentContent');




	  if(config.debugMode) 
	    console.log("doSlideInterval() called ["+config.name+"]");

		if(config.debugMode)
			console.log("["+config.name+"] -> "+slider.style.right.substring(0,slider.style.right.length-2)+" = "+(maxRange-(config.contentSize*2)));
			
 	 	if(slider.style.right.substring(0,slider.style.right.length-2) >= (maxRange-(config.contentSize*2)))
  	{
    	slider.style.right = "0px"; 
    	currentContent = 1;
  	} else {
    	currentContent++; 
  	}
  	
  	$("#"+scope_id).data('slider_currentContent', currentContent);

  	var position = (currentContent * config.contentSize);
  
  	if(config.debugMode)
  		console.log(position+" = "+slider.style.right.substring(0,slider.style.right.length-2)+" ["+config.name+"]");
  
  	$("#"+config.slider_id).animate({right:position},config.animationDurration,config.easing);	
	};

	function doSlideInterval(scope_id)
	{
		var config = $("#"+scope_id).data('slider_config');
		var slider = $("#"+scope_id).get(0);
		var maxRange = $("#"+scope_id).data('slider_maxRange');
		var currentContent = $("#"+scope_id).data('slider_currentContent');




	  if(config.debugMode) 
	    console.log("doSlideInterval() called ["+config.name+"]");

		if(config.debugMode)
			console.log("["+config.name+"] -> "+slider.style.right.substring(0,slider.style.right.length-2)+" = "+(maxRange-(config.contentSize*2)));
			
 	 	if(slider.style.right.substring(0,slider.style.right.length-2) >= (maxRange-(config.contentSize*2)))
  	{
    	slider.style.right = "0px"; 
    	currentContent = 1;
  	} else {
    	currentContent++; 
  	}
  	
  	$("#"+scope_id).data('slider_currentContent', currentContent);

  	var position = (currentContent * config.contentSize);
  
  	if(config.debugMode)
  		console.log(position+" = "+slider.style.right.substring(0,slider.style.right.length-2)+" ["+config.name+"]");
  
  	$("#"+config.slider_id).animate({right:position},config.animationDurration,config.easing);	
	};

// initialize sliderPXL.
	function init(config,dointervalfunc,scope)
	{
		  if(config.debugMode) 
		    console.log("init() called ["+config.name+"]");

		var slider = $("#"+config.slider_id).get(0);
		var contentNum = 0;
		var trappedDivs = 0;
		
		for(var i=0;i<=slider.childNodes.length-1;i++)
		{
		  if(slider.childNodes[i].tagName == "DIV" && trappedDivs < 2)
		  {
		      if(config.debugMode) { 
		      	console.log("["+config.name+"]");
		      	console.log(slider.childNodes[i]);
		      	}
		      
		      var cloned = slider.childNodes[i].cloneNode(true);
		      cloned.innerHTML = slider.childNodes[i].innerHTML;
		      slider.appendChild(cloned);
		      trappedDivs++;
		      contentNum++;   
		  }
		   else if(slider.childNodes[i].tagName == "DIV")
		  {
		     contentNum++;
		  }
		}
		
		if(config.direction == 'right')
		{
			slider.style.right = 0
		}
		
		if(config.debugMode) 
			console.log(contentNum+" ["+config.name+"]");

		var maxRange = contentNum * config.contentSize;
		scope.data('slider_maxRange', maxRange);
		scope.data('slider_currentContent', 0);
		scope.data('slider_contentNum', contentNum);
				
		var slideInterval = setInterval(function(){dointervalfunc(config.slider_id)},config.sliderInterval);
		scope.data('slider_slideInterval', slideInterval);
		
		scope.data('slider_config', config);
		
	};

})(jQuery);