/*******************************************************************************
 jQuery.jMenu
 Copyright (c) 2010 Aporie
 email: contact@aporie.biz

 Licences: MIT, GPL
 http://www.opensource.org/licenses/mit-license.php
 http://www.gnu.org/licenses/gpl.html
 ******************************************************************************/

/*
 * Name:jquery.jscroller
 * Version: 0.1
 * usage : $(selector).jScroll(options);
 */
(function($) {

$.jScroll = {
	index:0,
	count:0,
	options: {
		pause : 5000,
		duration : 1000,
		number:1,
		sens:'left',
		animate : []
	},

  jScroll : function(options){
		return $(this).each(function(){
		   var d=document.body.style.display;
		   document.body.style.display='';
		   
		   var opt = {};
			$.extend (opt, $.jScroll.options);
			$.extend (opt, options);
			
			options=opt;
			id=$(this).attr('id');

			w=$(this).css('width');
		   h=$(this).css('height');

		   $(this).css({position:'absolute',overflow:'hidden',width:w,height:h});

			// Div
			var n=0;
			$(this).find('div').each(function(index,element){
			   $(this).attr('id',id+'_'+index).css({position:"absolute"}).hide();
			   n++;
			});
			if(options.number>n-2) options.number=n-1;

			// Position initale
			h=0; w=0;
			for(i=0;i<options.number;i++) {
			   if(options.sens=='left') { l=w; t=0;}
			   if(options.sens=='top') { l=0; t=h;}
				$('#'+id+'_'+i).css({top:t,left:l}).show();
				h+=$('#'+id+'_'+i).outerHeight();
				w+=$('#'+id+'_'+i).outerWidth();
				}

			if(w<$(this).css('width')) $(this).css({width:w});

			$(this).data('options',options);
			$(this).data('count',n);
			$(this).data('index',0);
			if($.browser.msie)  setInterval("$.jScroll.change('"+id+"')",options.pause);
		 	else setInterval(function(id){$.jScroll.change(id);},options.pause,id);
		 	
		 	document.body.style.display=d;
		});
	},
	change : function(id)
	{  options=$('#'+id).data('options');
 		index=$('#'+id).data('index');
		count=$('#'+id).data('count');
		id=$('#'+id).attr('id');

		olddiv=index;
	   lastdiv=(olddiv+options.number-1)%count;
	   newdiv=(olddiv+options.number)%count;
		if(options.sens=='left') {t=$('#'+id+'_'+olddiv).css('top'); l=parseInt($('#'+id+'_'+lastdiv).css('left'))+$('#'+id+'_'+lastdiv).outerWidth();};
		if(options.sens=='top') {l=$('#'+id+'_'+olddiv).css('left'); t=parseInt($('#'+id+'_'+lastdiv).css('top'))+$('#'+id+'_'+lastdiv).outerHeight();};
		$('#'+id+'_'+newdiv).css({top:t,left:l}).show();
		$('#'+id).data('index',(index+1)%count);

	   es={}; eh={}
		for(i in options.animate)
		{	es[options.animate[i]]='show';
			eh[options.animate[i]]='hide';
		}
		w=$('#'+id+'_'+olddiv).outerWidth();
		h=$('#'+id+'_'+olddiv).outerHeight();

		if(options.sens=='left') effect={left:'-='+w};
		if(options.sens=='top') effect={top:'-='+h};

		$('#'+id+'_'+olddiv).animate(effect,options.duration);
		for(i=0;i<options.number;i++)	{
			newdiv=((olddiv+i+1)%count);
			$('#'+id+'_'+newdiv).animate(effect,options.duration);
		}
	}
};
$.fn.jScroll = $.jScroll.jScroll;
$.fn.change = $.jScroll.change;
})(jQuery);

