/*
 *  Image Slider ver 0.0.1 - jQuery plugin 
 *  Written by WillCraft info@willcraft.jp 
 *	
 *	 Dual licensed under the MIT
 *	 and GPL licenses.
 * 
 */
/*
null
linear, swing, jswing
easeInQuad, easeOutQuad, easeInOutQuad
easeInCubic, easeOutCubic, easeInOutCubic
easeInQuart, easeOutQuart, easeInOutQuart
easeInSine, easeOutSine, easeInOutSine
easeInExpo, easeOutExpo, easeInOutExpo
easeInCirc, easeOutCirc, easeInOutCirc
easeInElastic, easeOutElastic, easeInOutElastic
easeInBack, easeOutBack, easeInOutBack
easeInBounce, easeOutBounce, easeInOutBounce
*/
 /* 
 
 */
(function($){
	
	$.fn.imageSlider = function(options){
		
		var options = $.extend(
		{
			slides: ".slides",
			prevId:'prevBtn',
			prevText:'Previous',
			nextId:'nextBtn',	
			nextText:'Next',
			controlsShow:true,
			speed:1000,//10000,
			pause:3000,
			roop:true,	
			transparency:1,
			active:5,
			easing:null//"linear"
		}, options);
		
		var timeout;
		var imageHover = false;
		
		var imageManager = (function(){
			
			var image = function(){
				return {
					
					// イメージ移動
					move:function(position, active, callback){
						
						var obj = this;
						
						images[i].image.animate(
							{ left:position }, 
							options.speed,
							options.easing,
							function(){
								obj.deco(active);
								if(callback){
									callback();	
								}
							}
						);				
					},
					
					// イメージデコレーション
					deco:function(active){
						if(active)
							 $(this.image).fadeTo(100, 1);
						else $(this.image).fadeTo(100, options.transparency);
					}
				}
			};
			
			var images = [];
			
			return {
				
				initImage:function(obj){
					
					img = new image();
					
					images[images.length] = img;
					
					img.image  = obj;
					img.width  = obj.width();
					img.height = obj.height();
					img.deco(images.length == options.active);
				},
				
				setPosition:function(){
					
					window_width = $(window).width();
	
					for(i = 0; i < images.length; i++){					
						images[i].image.css({
							position:"absolute",
							left:images[i].width * i - (images[i].width * (options.active - 1) - ((window_width - images[i].width) / 2)),
							display:"block"
						});
						
						$('span',images[i].image).css({
							top:images[i].height - $('span',images[i].image).height(),
                            left:0
						});
					}
					
					$("#"+options.nextId).css({
						top:images[0].height / 2 - $("#"+options.nextId).height() / 2,
						left:((window_width / 2) + images[0].width / 2) - ($("#"+options.nextId).width() / 2) + 110,
						position:'absolute'
					});
					
					$("#"+options.prevId).css({
						top:images[0].height / 2 - $("#"+options.prevId).height() / 2,
						left:((window_width / 2) - images[0].width / 2) - ($("#"+options.prevId).width() / 2) - 110,
						position:'absolute'
					});		
				},
				
				imageMove:function(direction){
					
					var parent = this;
					
					if(direction == "next") images.push((function(){
						obj = images.shift();
						obj.image.css("left", images[images.length - 1].image.css("left"));
						return obj;
					})());
					if(direction == "prev") images.unshift((function(){
						obj = images.pop();
						obj.image.css("left", images[0].image.css("left"));
						return obj;
					})());
					
					$("#"+options.nextId).hide();
					$("#"+options.prevId).hide();
					
					window_width = $(window).width();
					
					for(i = 0; i < images.length; i++){
						images[i].move(images[i].width * i - (images[i].width * (options.active - 1) - 
							((window_width - images[i].width) / 2)), i == options.active - 1, 
							function(){
								if(imageHover){
									$("#"+options.nextId).show();
									$("#"+options.prevId).show();
								}
							});
					};
					
					if(!imageHover){
						timeout = setTimeout(function(){
							parent.imageMove("next");
						},options.pause);
					}
				}
			}
		})();
		
		return this.each(function(){
				
				var slideobj = $(this);
				
				slideobj.css({
					overflow:"hidden",
					position:"relative",
					height:$('div', $(options.slides, slideobj)).height()
				});			
								
				slideobj.hover(
					function(){
						imageHover = true;
						clearTimeout(timeout);
						$("#"+options.nextId).show();
						$("#"+options.prevId).show();
					}, 
					function(){
						imageHover = false;
						$("#"+options.nextId).hide();
						$("#"+options.prevId).hide();
						timeout = setTimeout(function(){
							imageManager.imageMove("next");
						},options.pause);
					}
				);				
								
				// 画像オブジェクトセット		
				$('div', $(options.slides, slideobj)).each(function(){
					imageManager.initImage($(this));
				});
								
				if(options.controlsShow){
					var html = "";
					html += ' <span style="display : none;" id="'+ options.prevId +'"><a href=\"javascript:void(0);\"></a></span>';
					html += ' <span style="display : none;" id="'+ options.nextId +'"><a href=\"javascript:void(0);\"></a></span>';
					$(options.slides, slideobj).after(html);	
				};
				
				// 画像配置
				imageManager.setPosition();
				
				// リサイズイベントセット
				$(window).resize(function(){
					imageManager.setPosition()
				});

				$("a","#"+options.nextId).click(function(){		
					imageManager.imageMove("next");
				});
				
				$("a","#"+options.prevId).click(function(){		
					imageManager.imageMove("prev");
				});	
					
				timeout = setTimeout(
					function(){
						imageManager.imageMove("next");
					},options.pause
				);
		});	
		
	}
	
})(jQuery);


