/*
 *  Image Slider ver 0.0.1 - jQuery plugin 
 *  Written by WillCraft info@willcraft.jp 
 *	
 *	 Dual licensed under the MIT
 *	 and GPL licenses.
 * 
 */
 /* 
 
 */
(function($){
	
	$.fn.imageSlider = function(options){
		
		var options = $.extend(
		{
			slides: ".slides",
			prevId:'prevBtn',
			prevText:'Previous',
			nextId:'nextBtn',	
			nextText:'Next',
			controlsShow:true,
			auto:true,//true|false
			speed:800,
			pause:8000,//静止時間の設定
			roop:true,	
			transparency:0.6,
			reflectheight:1/7,
			active:5,
			//動作設定
//			easing:"easeInBack"//少し戻ってから移動
			easing:"easeInOutCubic"//
		}, options);
		
		var timeout;
		
		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){
							$('img',this.image).reflect({
								height: options.reflectheight,
								opacity: 0.6
							});			
//フェード処理をCSSへ変更
//							$(this.image).fadeTo(100, 1);
//							$('span',this.image).fadeTo(1200, 1);
						}else{
							$('img',this.image).unreflect();
//							$(this.image).fadeTo(100, options.transparency);
//							$('span',this.image).hide();
						}
					}
				}
			};
			
			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),
						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),
						position:'absolute'
					});		
				},
				
				imageMove:function(direction, clicked){
					
					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(){
								$("#"+options.nextId).show();
								$("#"+options.prevId).show();
							});
					};
					
					
					if(clicked) clearTimeout(timeout);
					
					if(options.auto && !clicked){;
						timeout = setTimeout(function(){
							parent.imageMove("next",false);
						},options.pause);
					};
					
				}
			}
		})();
		
		return this.each(function(){
				
				var slideobj = $(this);
				
				slideobj.css({
					overflow:"hidden",
					position:"relative",
					height:$('div', $(options.slides, slideobj)).height() + ($('div', $(options.slides, slideobj)).height() * options.reflectheight)
				});			
								
				// 画像オブジェクトセット		
				$('div', $(options.slides, slideobj)).each(function(){
					imageManager.initImage($(this));
				});
								
				if(options.controlsShow){
					var html = "";
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\"></a></span>';
					html += ' <span 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",true);
				});
				
				$("a","#"+options.prevId).click(function(){		
					imageManager.imageMove("prev",true);
				});	
				
				if(options.auto){;
					timeout = setTimeout(function(){
						imageManager.imageMove("next",false);
					},options.pause);
				};		
		});	
		
	}
	
})(jQuery);


