(function($){	
	//declare an array to store the keys of the options object
	$.dssOptionsList = [];
	var timer;

	
	//main function
	$.fn.divSlideShow = function(customOptions, callback){	
	
	
		//default options
		var options = 
		{ 
			width:200, 
			height:100, 
			arrow:"begin", 			
			delay:5000, 
			loop:1,
			leftArrowClass:"",
			rightArrowClass:"",
			//leftArrowClass:"ui-icon ui-icon-circle-triangle-w",
			//rightArrowClass:"ui-icon ui-icon-circle-triangle-e",
			slideContainerClass:"", 
			controlClass:"",
			controlActiveClass:"",
			//controlClass:"ui-state-default ui-corner-all", 
			//controlActiveClass:"ui-state-hover",
			controlHoverClass:"",
			controlContainerClass:"",
			separatorClass:"",
			callback: function() {}		
		};
		//override options
		if(customOptions)
			$.extend(options, customOptions);
			
		//store the keys of the options object
		for(var key in options)
			$.dssOptionsList.push(key);
		
		//limit loop amount
		if(options.loop > 30)
			options.loop = 30;
			
		return this.each(function(){
			
			for(key in options)
				$(this).attr(key, options[key])
				
			//make slideshow
			$.divSlideShow(this);
			
						
		});				
		
	};
	
	$.divSlideShow = function(slideShow)
	{		
		var options = $.divSlideShow.getOptionsObject(slideShow)		
		
		var numSlide = $(slideShow).children('.slide').length;			
		$(slideShow).css( { 'width':options.width ,  'overflow':'hidden', 'display':'block' } );		
		
		//wrap all slides with inner conatiner
		$(slideShow).children(".slide")			
			.wrap('<div class="dssSlide"></div>')			
		$(slideShow).children('.dssSlide').wrapAll('<div class="dssSlideContainer" page=0 max='+numSlide+'/>');
		$(slideShow).find('.dssSlide')
			.css( {'float':'left', 'width':options.width, 'height':options.height, 'overflow-y':'auto' } );

		
		
		//styles for inner container and controls
		$(slideShow).find('.dssSlideContainer').css( {'width':options.width * numSlide, 'height':options.height, 'overflow':'hidden'} ).addClass(options.slideContainerClass);
		if(options.loop > 0)
		{
			var slideCount = 0;
			timer = setInterval(function()
										 {
											slideCount++;
											if(slideCount == numSlide) slideCount = 0;
											$.divSlideShow.slideTo(slideShow, slideCount, true); 
										 }, 5500);						 
		
					/*
			//auto-slide: queue a sequence of animation with delay
			for(i = 1; i < Math.floor(numSlide*options.loop); i++)
			{
				$(slideShow).find('.dssSlideContainer').delay(options.delay);				
				$.divSlideShow.slideTo(slideShow, i % numSlide, true);
			}
			*/
			
			//initialize controls look
			$.divSlideShow.manageControls(slideShow, 0);
		}
		
		
	};

	$.divSlideShow.Stop = function()
	{
		clearInterval(timer);
		
	}

	$.divSlideShow.slideTo = function(slideShow, gotoPage, queue)
	{
		//remove auto-slide
		if( !queue )
		{
			$.divSlideShow.Stop();
			$(slideShow).find('.dssSlideContainer').clearQueue();
		}
			
		
		var options = $.divSlideShow.getOptionsObject(slideShow)
		
		//validate gotoPage
		var max = $(slideShow).find('.dssSlideContainer').children().length ;

		if(gotoPage >= max)
			gotoPage = max - 1;
		if(gotoPage < 0)
			gotoPage = 0;
		
		//get width
		var width = $(slideShow).find('.dssSlideContainer .dssSlide').width();		
		
		//manage control look and store current page as attribute, to be executed just before animation
		$(slideShow).find('.dssSlideContainer').queue(function(){
			$.divSlideShow.manageControls(slideShow, gotoPage);
			$(this).attr('page', gotoPage);
			$(this).dequeue();
		});
		
		//animate
		$(slideShow).find('.dssSlideContainer').animate(	{'margin-left':-gotoPage*width}	);		
		
	};
	
	$.divSlideShow.getOptionsObject = function(slideShow)
	{
		var options = {};
		var optionsList = $.dssOptionsList;
		for (var i in optionsList)
		{
			var attribute = $(slideShow).attr(optionsList[i]);
			if( isNaN( parseInt(attribute) ) )
				options[optionsList[i]] = attribute;
			else
				options[optionsList[i]] = parseInt(attribute);			
		}
		return options;
	}
		

	$.divSlideShow.manageControls = function(slideShow, page)
	{
		$("#slide-control").children(".active").removeClass("active"); // remove the "active" class from the one that has it
		$("#slide-control").children(".slide-picker").eq(page).removeClass("inactive").addClass("active"); // set the "active" class to the appropriate slide
	};

})(jQuery);
