/*
 * Slider - jQuery Plugin
 * Copyright(c) 2011 sophide
 * MIT Licensed.
 */
;(function ( $, window, document, undefined ) {

	var pluginName = 'slider',
		defaults = {
			'duration' : 3000,
			'speed' : 500,
			'vector' : 'left',
			'auto' : 1,
			'data' : null,
			'action' : null
		};

	function Plugin( element, options ) {
		this.element = element;
		this.options = $.extend( {}, defaults, options) ;

		this._defaults = defaults;
		this._name = pluginName;

		//addition
		this.flg = 0;
		this.move = {};
		this.timer = {};
		
		this.init();
	}

	Plugin.prototype = {
		init: function(){
			var self = this,
				op = this.options;

			if(op.data){
				var html = '<ul>';
				for(var i=0; i<op.data.length; i++){
					html += '<li>' + op.data[i] + '</li>';
				}
				html += '</ul>';
				$(this.element).html(html);
			}

			this.$wrap = $(this.element).find('ul');
			this.$items = this.$wrap.find('li');
			var width = this.$items.width(),
				height = this.$items.height();

			if(op.vector === 'left'){
				this.$wrap.css('width', this.$items.length * width + 'px');
				this.$items.css('float', 'left');
				this.move = {
					'dist' : width + 'px',
					'prop' : 'margin-left',
					'prev' : { 'margin-left' : '0px' },
					'next' : { 'margin-left' : '-' + width+ 'px' }
				};
			} else {
				this.move = {
					'dist' : height + 'px',
					'prop' : 'margin-top',
					'prev' : { 'margin-top' : '0px' },
					'next' : { 'margin-top' : '-' + height+ 'px' }
				};
			}

			if(op.auto){ this.auto(); }
			
			if(op.action){
				$(op.action.prev).click(function(){
					if(!self.flg) self.scroll(1);
					return false;
				});
				$(op.action.next).click(function(){
					if(!self.flg) self.scroll(0);
					return false;
				});
			}
		},
		auto: function(){
			clearTimeout(this.timer);

			var self = this;
			this.timer = setTimeout(function(){
				self.scroll(0);
				self.auto();
			}, this.options.duration);
		},
		scroll: function(vector){
			clearTimeout(this.timer);

			this.flg = 1;
			this.$items = this.$wrap.find('li');

			var self = this,
				$obj = (vector)
					? this.$items[this.$items.length-1]
					: this.$items[0];

			if(vector){
				this.$wrap.prepend($obj).css(this.move.prop, '-' + this.move.dist)
					.animate(this.move.prev, self.options.speed, function(){
						self.flg = 0;
						if(self.options.auto){ self.auto(); }
					});
			} else {
				this.$wrap
					.animate(this.move.next, self.options.speed, function(){
						self.$wrap.append($obj).css(self.move.prop, '0px');
						self.flg = 0;
						if(self.options.auto){ self.auto(); }
					});
			}
		}
	};

	$.fn[pluginName] = function ( options ) {
		return this.each(function () {
			if (!$.data(this, 'plugin_' + pluginName)) {
				$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
			}
		});
	}

})(jQuery, window, document);


$(function(){

	$('#slider01').slider({
		'duration' : 4500
		,'speed' : 'fast'
		,'auto' : true
		,'action': {
			'prev' : '#slide01Prev',
			'next' : '#slide01Next'
		}
	});

});

