/*
	Copyright 2009 Florian Strzelecki <florian.strzelecki@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*	slidernews.start	*/
jQuery.fn.slidernews = function(options)
{

	var settings = jQuery.extend(
		{
			/*	general	*/
			debug: false,

			/*	css	*/
			list: 'list',
			visual: 'visual',
			selected: 'selected',

			/*	initial value	*/
			position: 0,

			/*	animate	*/
			animate: true,
			smouth: 500,

			/*	cycle	*/
			autostart: true,
			step: 1,
			time: 3000

		}, options);

	return this.each(function()
	{

		/*	move to the position	*/
		this._move = function(position)
		{
			if(this.position != position)
			{
				var visual_selector = '.' + this.settings.visual;
				var list_selector = '.' + this.settings.list;

				var visual_children = jQuery(this).children(visual_selector);
				var list_children = jQuery(this).children(list_selector);

				var hide_children = jQuery(visual_children).children('li[position='+this.position+']');
				var show_children = jQuery(visual_children).children('li[position='+position+']');

				if(this.settings.animate == false)
				{
					hide_children.css('display', 'none');
					show_children.css('display', 'block');
				}
				else
				{
					hide_children.stop(false, true);
					hide_children.css('display', 'none');
					show_children.fadeIn(this.settings.smouth);
				}

				jQuery(list_children).children('li[position='+this.position+']').removeClass(this.settings.selected);
				jQuery(list_children).children('li[position='+position+']').addClass(this.settings.selected);

				this.position = position;
			}
		}

		/*	init function */
		this.init = function(settings)
		{
			this.settings = settings;

			var marked = false;
			var position = 0;
			var slider = this;

			/*	init visual	*/
			var visual_selector = '.' + this.settings.visual;		
		    var visual_children = jQuery(this).children(visual_selector);

			this.length = 0;

			for(var i = 0; i < visual_children.length; i++)
			{
				var children = jQuery(visual_children[i]).children('li');
				var li = null;

				this.length = children.length;
				for(var i = 0; i < children.length; i++)
				{
					li = jQuery(children[i]);
					li.attr('position', i);
					if(i == this.settings.position)
					{
						marked = true;
						li.css('display', 'block');
					}
					else
					{
						li.css('display', 'none');
					}
				}
			}

			/*	init list	*/
			var list_selector = '.' + this.settings.list;
			var list_children = jQuery(this).children(list_selector);

			for(var i = 0; i < list_children.length; i++)
			{
				var children = jQuery(list_children[i]).children('li');
				var li = null;

				for(var i = 0; i < children.length; i++)
				{
					li = jQuery(children[i]);
					li.attr('position', i);

					li.mouseover(function(e)
					{
						slider.goto(jQuery(this).attr('position'));
					});
				}
			}

			if(marked)
			{
				position = this.settings.position;
			}
			else if(this.settings.debug == true)
			{
				alert('Warning : no visual at position ' + this.settings.position);
			}

			if(this.settings.autostart)
			{
				this.start(position);
			}
			else
			{
				this.goto(position);
			}
		};

		this.start = function(position)
		{
			if(position != null)
			{
				this._move(position);
			}
			this.manual = false;
			var slider = this;
			this.timer = setTimeout(function() {	slider.cycle();	}, this.settings.time);
		}

		this.cycle = function()
		{
			if(!this.manual)
			{
				var slider = this;
				var new_position = this.position + this.settings.step;

				if(new_position < 0)
				{
					new_position = this.length -1;
				}
				else if(new_position >= this.length)
				{
					new_position = 0;
				}

				this._move(new_position);
				this.timer = setTimeout(function() {	slider.cycle();	}, this.settings.time);
			}
		}

		/*	stop 	*/
		this.stop = function()
		{
			if(this.manual == false)
			{
				this.manual = true;
				clearTimeout(this.timer);
			}
		}

		/*	manual move to position	*/
		this.goto = function(position)
		{
			this._move(position);
			this.stop();
		}

		/*	indicate version	*/
		this.version = function() {
			alert('Version 0.0.1');
		}

		this.init(settings);

	});

};
/*	slidernews.end	*/
