﻿/**
 * slider
 *
 * @version      1.0
 * @author       ajike
 * @copyright    ajike.co.jp
 * @license      The MIT License
 *
 *�?�?�?��?�ｼ�?�ｳ�?�ｺ�?�?�?�ｹ�??
 *�?�?$('#slider-content').slider();
 *  
 *  �?�css (slider.css)
 *  #slider-content
 *  .slider-box
 *  .slider-boxinner
 *  .slider-large
 *  .slider-small
 *  .slider-btn
 *
 */

(function($) {
	
	// ***********************************************
	//
	// slider Plugin
	// 
	// ***********************************************
	$.fn.slider = function(options) {
		var config = $.extend({
			intervalWidth : 680, //�?�ｻ�?�?�?�ｮ�?�?�??ｮ�?�?�?�?�?�?�???�?�?�?�?�?�ｻ�?�?�?�?�?�ｺ�?�ｹ�?�?
			offsetX:490, //�?�ｭ蠢?�?�ｹX�?�?�?�?�?�?offset
			offsetY:185, //�?�ｭ蠢?�?�ｹY�?�?�?�?�?�?offset
			smallWidth:784, //�?�ｮ�???�?�?�?�ｮ�?ｪ�?�?
			smallHeight:344, //�?�ｮ�???�?�?�?�ｮ�?�ｦ�?�?
			largeWidth:980, //�?�｡�??ｧ�?�?�?�ｮ�?ｪ�?�?
			largeHeight:430, //�?�｡�??ｧ�?�?�?�ｮ�?�ｦ�?�?
			idx:0, //�?�?�?�?index �?�ｻ�?�ｾ�?�ｨ0�?�ｮ�?�ｿ�?�?�???�?�ｮ�?�､�?�ｯ�?�ｪ�?�?�?�?
			duration:1000, //move�?�ｫ�?�?�?�?�?�?�?�?�?�?
			timerDelay:3500 //�?�ｪ�?�?�?�ｧ�?�?�?�?�?�?�?�?�?�?�?�?
		}, options);
		
		var $content = this;
		var $contentInner = $(".slider-contentinner",$content);
		var $btnLeft = $(".slider-btnleft",$content);
		var $btnRight = $(".slider-btnright",$content);
		var factory = new OmkBoxFactory();
		var _boxs = [];
		var _deleteBoxs = [];
		var _idx = config.idx;
		var _oldidx = config.idx; //�?�?�?�?idx�?�､�?�ｮ�??ｻ�?�?�?�?�?�｡�???�?�?�?�?�?�?�?�?�?�ｫold�?�､�?�?�?�?�?�｣�?�ｦ�?�?�?�?
		var _timerId;  //setInterval�?�ｮID
		var beforeclickTime = 0;
		
		
		//�?�?�?�?�?�?�?�?�?�ｫ�?�ｼ�?�ｳ�?�ｺ�?�?�?�?�?�ｦ�?�?�?�ｪ�?�?�?�?�?�ｰ�?�ｨ�?�ｩ�?�ｼ
		if($content.length == 0){
			throw new Error(" $('#slider-content').slider(); �?�ｨ�?�ｼ�?�ｳ�?�ｺ�?�?�?�ｦ�?�?�?��?�?�?�?�?�? ");
		}
		
		//$content�?�? id �?�ｧ�?�ｪ�?�? class�?�ｧ�?�?�?�?�?�?�?�?�?�ｦ�?�?�?�?�?�ｰ�?�ｨ�?�ｩ�?�ｼ
		if($content.attr("id") == null || $content.attr("id") == ""){
			throw new Error(" class�?�ｧ�?�ｪ�?�?id�?�ｧ�?�?�?�?�?�?�?�ｦ�?�?�?��?�?�?�?�?�? ");
		}
		
		function _init(){
			//�?�?�?�?�?�?�?�?�?�ｮbox�?�?�?�ｻ�?�?�?�?
			$(".slider-box",$content).each(function(i){
				factory.add($(this));
			})
			
			for (var i = -1; i <= 1; i++){
				var boxClass = factory.create(_idx + i);
				boxClass.position(i);
				$contentInner.append(boxClass.$box);
				_boxs.push(boxClass);
			};
			
			addHoverTimerStop($btnLeft);
			addHoverTimerStop($btnRight);
			_addTimer();  //�?�ｿ�?�､�?�?�?�ｼ�?�ｹ�?�ｿ�?�ｼ�?�?
		}
		_init();
		
		
		// ***********************************************
		//
		// Event Handler
		// 
		// ***********************************************
		$btnLeft.click(function(){
			var time = _getTimer();
			if((time - this.beforeclickTime) < config.duration-100){
				return;
			}
			this.beforeclickTime = time;
			
			_idx = --_idx;
			removeBoxRight();
			addBoxLeft();
			_move();
		});
		
		$btnRight.click(function(){
			var time = _getTimer();
			if((time - this.beforeclickTime) < config.duration-100){
				return;
			}
			this.beforeclickTime = time;
			
			_idx = ++_idx;
			removeBoxLeft();
			addBoxRight();
			_move();
		});
		
		function _timerHandler(){
			var time = _getTimer();
			if((time - this.beforeclickTime) < config.duration-100){
				return;
			}
			this.beforeclickTime = time;
			
			_idx = ++_idx;
			removeBoxLeft();
			addBoxRight();
			_move();
		}
		
		// ***********************************************
		//
		// private function
		// 
		// ***********************************************
		function _move(){
			if(_oldidx == _idx) return; //�?�?�?�?�?�?�??ｮ�?�ｧ�?�ｮ�?�ｦ�?�?�?�?�?�?�?�ｿ
			
			for (var i = 0; i < _boxs.length; i++){
				var box = _boxs[i];
				var toIdx = box.idx - _idx;
				box.anime(toIdx);
			};
			
			_oldidx = _idx;
		}
		
		/*
		 *  �?�ｿ�?�､�?�?�?�ｼ�?�ｦ�?�?�??ｽ�?�
		 */
		function _addTimer(){
			//�?�ｿ�?�､�?�?�?�ｼ�?�ｦ�?�?
			_timerId = setInterval(_timerHandler,config.timerDelay);
		}
		
		/*
		 *  �?�ｿ�?�､�?�?�?�ｼ�?�ｦ�?�?�?�?�?�､
		 */
		function _removeTimer(){
			//�?�ｿ�?�､�?�?�?�ｼ�?�ｦ�?�?
			clearInterval(_timerId);
		}
		
		/*
		 *  �?�?�?�ｦ�?�ｹ�?�ｪ�?�ｼ�?�?�?�ｼ�?�ｭ�?�ｯ�?�ｿ�?�､�?�?�?�ｼ�?�?�?�?�?�､
		 */
		function addHoverTimerStop($obj){
			$obj.hover(
				function(){_removeTimer();}
				,function(){_addTimer();}
			);
		}
		
		/*
		 *  �?�ｦ�?�ｯ�?�ｫbox�?�?�??ｽ�?��?�?�??ｻ�?�?�?�?�?�ｫ�?�ｼ�?�ｰ�?�?�?�?�?�?
		 */
		function addBoxLeft(){
			var boxClass = factory.create(_idx-1);
			boxClass.position(-2);
			$contentInner.append(boxClass.$box);
			_boxs.unshift(boxClass);
		}
		
		/*
		 *  �?�ｳ�?�ｯ�?�ｫbox�?�?�??ｽ�?��?�?�??ｻ�?�?�?�?�?�ｫ�?�ｼ�?�ｰ�?�?�?�?�?�?
		 */
		function addBoxRight(){
			var boxClass = factory.create(_idx+1);
			boxClass.position(+2);
			$contentInner.append(boxClass.$box);
			_boxs.push(boxClass);
		}
		
		/*
		 *  �?�ｦ�?�ｯ�?�ｮbox�?�?�?�?�?�､�?�?�??ｻ�?�?�?�?�?�ｫ�?�ｼ�?�ｰ�?�?�?�?�?�?
		 */
		function removeBoxLeft(){
			var box = _boxs[_deleteBoxs.length];
			box.removeflg = true;
			box.removeTarget = "left";
			_deleteBoxs.push(box);
		}
		
		/*
		 *  �?�ｳ�?�ｯ�?�ｮbox�?�?�?�?�?�､�?�?�??ｻ�?�?�?�?�?�ｫ�?�ｼ�?�ｰ�?�?�?�?�?�?
		 */
		function removeBoxRight(){
			var box = _boxs[_boxs.length-1 -_deleteBoxs.length];
			box.removeflg = true;
			box.removeTarget = "right";
			_deleteBoxs.push(box);
		}
		
		function _getTimer(){
			var now = new Date();
			return now.getTime();
		}
		
		// ***********************************************
		//
		// Class (new�?�?�?�ｦ�?�ｿ�?�?�?�?�?�?)
		// 
		// ***********************************************
		/*
		 *  new OmkBoxFactory()
		 *  new OmkBox�?�?�?�?�?�?�?�?�?�?�?�ｯ�?�ｩ�?�ｹ
		 */
		function OmkBoxFactory(){
			this.node = {};
			this.count = 0;
			this.add = function($box){
				this.node[this.count] = $box;
				this.count++;
				$box.remove(); 
			}
			this.create = function(idx){
				//-2�?�?-1,2,3,5�?�ｫ�??ｾ蠢?
				var boxNum = this.getTargetBoxNum(idx);
				return new OmkBox(idx ,boxNum , this.node[boxNum].clone() );
			}
			this.getTargetBoxNum = function(idx){
				return (this.count + (idx % this.count))%this.count;
			}
		}
		
		/*
		 *  new OmkBox()
		 *  .slider-box�?�ｫ�??ｾ蠢?�?�?�?�?�?�ｯ�?�ｩ�?�ｹ
		 */
		function OmkBox(idx,boxNum,$box){
			this.idx = idx;
			this.boxNum = boxNum;
			this.$box = $box;
			this.$img = new OmkImg( $(".slider-img",$box) );
			this.$btn = new OmkBtn( $(".slider-btn",$box) );
			this.removeflg = false;
			this.removeTarget;
			 
			this.anime = function(toIdx){
				var removeflg = this.removeflg;
				var disposeFunc = this.dispose;
				var scope=this;
				var zindex;
				if(this.idx == _idx){
					//�?�｡�??ｧ
					this.$img.animeLarge();
					this.$btn.animeShow();
					zindex = 100;
				} else {
					//�?�ｮ�???
					this.$img.animeSmall();
					this.$btn.animeHide();
					zindex = 1;
				}
				//box�?�?�??ｻ�?�?
				this.$box.animate(
					{
						"left":toIdx * config.intervalWidth + config.offsetX - this.$img.smallW/2
						,"top":config.offsetY - this.$img.smallH/2
					}
					,config.duration
					,"easeOutCirc"
					,function(){
						if(_deleteBoxs.length > 0) disposeFunc.apply(scope);
					}
					);
				//豺ｱ�?ｦ�?�?�???�?�ｴ
				this.$box.css({
					"z-index": zindex
				});
			}
			
			this.dispose = function(){
				var n = _deleteBoxs.length;
				for(i = 0 ; i < n ; i++){
					var box = _deleteBoxs.shift();
					if(box.removeTarget == "left"){
						_boxs.shift();
					} else {
						_boxs.pop();
					}
					box.$box.remove();
				}
			}
			
			this.position = function(positionIdx){
				this.$box.css({
					"left":config.offsetX - this.$img.smallW/2 + config.intervalWidth*positionIdx +"px"
					,"top":config.offsetY - this.$img.smallH/2 +"px"
				});
			}
			
			//init
			this._init = function(){
				if(this.idx == config.idx){
					this.$img.animeLarge(true);
					this.$btn.animeShow(true);
					this.$box.css({"z-index": 100});
				} else {
					this.$img.animeSmall(true);
					this.$btn.animeHide(true);
					this.$box.css({"z-index": 1});
				}
				
				addHoverTimerStop(this.$box);
				//this.$box.remove() //init�?�?�?�ｯ�?�?�?�､
				//console.log(this.$box);
			}
			this._init();
		}
		
		/*
		 *  new OmkImg()
		 *  .slider-img�?�ｫ�??ｾ蠢?�?�?�?�?�?�ｯ�?�ｩ�?�ｹ
		 */
		function OmkImg($img){
			this.$img = $img;
			this.smallW = config.smallWidth;
			this.smallH = config.smallHeight;
			this.largeW = config.largeWidth;
			this.largeH = config.largeHeight;
			
			this.animeLarge = function(initBool){
				
				
				//IE�?�ｮ�?�ｴ�?�?�?�?PNG�?�ｫopacity�?�?谺�?�?�?�?�?�ｨ�??�?�?�?�ｪ�?�?�?�?�?�?�?�｡�???
				var ua = $.browser;
				if(!ua.msie){
					this.$img.animate({
						"width":this.largeW
						,"height":this.largeH
						,"left":-(this.largeW - this.smallW)/2
						,"top":-(this.largeH - this.smallH)/2
						,"opacity":1
					}
					,initBool ? 0 :config.duration,"easeInQuad" );
				} else {
					this.$img.animate({
						"width":this.largeW
						,"height":this.largeH
						,"left":-(this.largeW - this.smallW)/2
						,"top":-(this.largeH - this.smallH)/2
					}
					,initBool ? 0 :config.duration,"easeInQuad" );
				}
			}
			this.animeSmall = function(initBool){
				
				
				//IE�?�ｮ�?�ｴ�?�?�?�?PNG�?�ｫopacity�?�?谺�?�?�?�?�?�ｨ�??�?�?�?�ｪ�?�?�?�?�?�?�?�｡�???
				var ua = $.browser;
				if(!ua.msie){
					this.$img.animate({
						"width":this.smallW
						,"height":this.smallH
						,"left":0
						,"top":0
						,"opacity":0.4
						}
					,initBool ? 0 :config.duration,"easeOutQuad");
				} else {
					this.$img.animate({
						"width":this.smallW
						,"height":this.smallH
						,"left":0
						,"top":0
						}
					,initBool ? 0 :config.duration,"easeOutQuad");
				}
			}
			
			this._init = function(){
				this.$img.css({"width":this.smallW});
				this.$img.css({"height":this.smallH});
			}
			
			this._init();
		}
		
		/*
		 *  new OmkBtn()
		 *  .slider-btn�?�ｫ�??ｾ蠢?�?�?�?�?�?�ｯ�?�ｩ�?�ｹ
		 */
		function OmkBtn($btn){
			this.$btn = $btn;
			
			this.animeShow = function(initBool){
				//IE�?�ｮ�?�ｴ�?�?�?�?PNG�?�ｫopacity�?�?谺�?�?�?�?�?�ｨ�??�?�?�?�ｪ�?�?�?�?�?�?�?�｡�???
				var ua = $.browser;
				if(!ua.msie){
					this.$btn.animate({
						"opacity":1
						}
					,initBool ? 0 :config.duration,"easeInExpo" );
				} else {
					this.$btn.show();
				}
				
							}
			this.animeHide = function(initBool){
				//IE�?�ｮ�?�ｴ�?�?�?�?PNG�?�ｫopacity�?�?谺�?�?�?�?�?�ｨ�??�?�?�?�ｪ�?�?�?�?�?�?�?�｡�???
				var ua = $.browser;
				if(!ua.msie){
					this.$btn.animate({
						"opacity":0
						}
					,initBool ? 0 :config.duration,"easeOutExpo" );
				} else {
					this.$btn.hide();
				}
			}
			
			
			this._init = function(){
				$("a",this.$btn).simpleRollOver();
				var w = $btn.width() !== 0 ? $btn.width() : $("img",$btn).attr("width") !== 0 ? $("img",$btn).attr("width") : 230;
				var h = $btn.height() !== 0 ? $btn.height() : $("img",$btn).attr("height") !== 0 ? $("img",$btn).attr("height") : 40;
				this.$btn.animate({
					"left":config.largeWidth - w -102
					,"top":config.largeHeight - h -55
				},0);
				
			}
			this._init();
		}
		
		return this;		
	}

})(jQuery);

$(document).ready(function(){
	//slider-content�?�?�?�?�?�?
	$('#slider-content').slider();
});
	
	
// JavaScript Document
(function($){
	
	$.fn.simpleRollOver = function( i_options ){
	
		var defaults = {
			postfix:"_over"
		};
		var options = $.extend( true, defaults, i_options );
		
		$(this).each(function(){
			var $a = $(this);
			var $i = $a.children('img');
			if(!$i.length && $a.attr("src")) $i = $a;
			if(!$i.length) return;
			
			var up = $i.attr("src");
			var over = up.replace(/\.([a-zA-Z0-9]+)$/,options.postfix+".$1");
			
			$("<img />").attr("src",over);
			$a.hover(
				function(){ $i.attr("src",over); },
				function(){ $i.attr("src",up); }
			);
		});
		
		return this;
	};
	
})(jQuery);
