	/*
* mopSlider 5.01
 * By Hiroki Miura (http://www.mopstudio.jp)
 * Copyright (c) 2011 mopStudio
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * July 12, 2011
 */
		



jQuery.fn.extend({
	mopSlider:function(stt){
		Array.prototype.shuffle = function() {
			var i = this.length;
			while(i){
				var j = Math.floor(Math.random()*i);
				var t = this[--i];
				this[i] = this[j];
				this[j] = t;
			}
			return this;
		};
		/*setting*/
		var btnMgn=stt.btnMgn;
		var shuffle=stt.shuffle;
		var interval=stt.interval;
		var autoMotion=false;
		if(stt.auto==="y"){autoMotion=true;}
		var speed=[];
		if(stt.speed){speed=stt.speed;}else{speed=[500,100,25];}
		var bounce=[];
		if(stt.bounce){bounce=stt.bounce;}else{bounce=[35,10]}
		var showing=0;
		var clickedBtn=0;
		var next=0;
		var delay=0;
		var delayTime=stt.delay;
		var size;
		var motionNo;
		var motion=false;
		var stopAuto=false;
		var ulSize=$(this).children("ul").size();
		var width=$(this).width();
		var w=$(this).children("#navi").children(".btn").width();
		var ua=navigator.userAgent;
		var browser;
		/*recording left for each item*/
		var left;
		$(this).children("ul").each(function(){
			$(this).children("li").each(function() {
                left=$(this).css("left");
				jQuery.data(this,"posiL",left);
            });
		});
		/*set mopSlider parts*/
		$(this).append(
			'<div id="cornerLT" class="upper"></div>'+
			'<div id="cornerRT" class="upper"></div>'+
			'<div id="cornerLB" class="upper"></div>'+
			'<div id="cornerRB" class="upper"></div>'
		);
		$(this).wrap("<div id='mopSlider'></div>");
		/*fix for ie6*/
		if(ua.indexOf("MSIE 6")>-1){browser="ie6";}
		var btn1=$(this).children("#navi").children(".btn").eq(0);
		if(browser!=="ie6"){
			btn1.css({"background-position":w+"px 0px"});
		}else{
			$(" #mopSlider #navi .btn").removeClass("btnGeneral");
			btn1.addClass("btnForIe6-2");
			btn1.siblings().addClass("btnForIe6");
			var blank='<img src="mopSlider/blank.gif" width="10" height="10" />';
			if($("#mopLogo")){
				$("#mopLogo").append(blank);
			}
			$("#mopSlider #cornerLT,#mopSlider #cornerRT,#mopSlider #cornerLB,#mopSlider #cornerRB").append(blank);
			
		};
		/*set data to each button*/
		$(this).children("#navi").children(".btn").each(function(who){jQuery.data(this,"num",who);});
		/*set data to each ul*/
		$("#mopSlider").children().children("ul").each(function(){jQuery.data(this,"side","obverse");});
		var reverseIt=function(which){
			var arr=jQuery.makeArray($("#mopSlider").children().children("ul").eq(showing).children("li"));
			arr.reverse();
			$(arr).appendTo($("#mopSlider").children().children("ul").eq(showing));
			jQuery.data($("#mopSlider").children().children("ul").eq(showing).get(0),"side",which);
		};
		var reverseThem=function(who,which){
			var that=who;
			var arr=jQuery.makeArray($(that).children("li"));
			arr.reverse();
			$(arr).appendTo($(that));
			jQuery.data(that,"side",which);
		};
		var preposition=function(who,which){
			var side;
			var that=who;
			var posi;
			if(which==="goRight"){posi=width;}else if(which==="goLeft"){posi=-width;};
			$(that).children("li").each(function() {
				$(this).css({"left":((jQuery.data(this,"posiL").split("px")[0]*1)+posi)+"px"});
			});
			side=jQuery.data(that,"side");
			if(which==="goRight"){
				if(side==="reverse"){reverseThem(that,"obverse");};
			}else if(which==="goLeft"){
				if(side!=="reverse"){reverseThem(that,"reverse");};
			};
		};
		var motionFunc=function(which){
			motionNo=1;
			var side,posi1,set;
			motion=true; 
			if(which==="goLeft"){side="obverse"; posi1=-width; set="reverse";}
			else if(which==="goRight"){side="reverse"; posi1=width; set="obverse";}
			if(autoMotion===false){next=clickedBtn;}
			if(jQuery.data($("#mopSlider").children().children("ul").eq(showing).get(0),"side")===set){reverseIt(side);}
			$("#mopSlider").children().children("ul").eq(showing).children("li").each(function(who){
				$(this).delay(delay).animate({
					"left":(jQuery.data(this,"posiL").split("px")[0]*1+posi1)+"px"},speed[0],"easeOutCubic",
					function(){
						if(who===size-1){
							if(showing<(ulSize-1)){
								showing+=1;
							}else if(showing===(ulSize-1)){
								showing=0;	
							}
							motionFunc2(which);
						}
					}
				);
				delay+=delayTime;
			});
			reverseIt(set);
		};
		var motionFunc2=function(which){
			motionNo=2;
			var posi2,posi3;
			motion=true;
			if(which==="goLeft"){posi2=-bounce[0]; posi3=bounce[1];}
			else if(which==="goRight"){posi2=bounce[0]; posi3=-bounce[0];}
			if(autoMotion===false){next=clickedBtn;}
			delay=0;
			size=$("#mopSlider").children().children("ul").eq(next).children("li").size();
			$("#mopSlider").children().children("ul").eq(next).children("li").each(function(who2){
				$(this).delay(delay).animate({"left":(jQuery.data(this,"posiL").split("px")[0]*1+posi2)+"px"},
					speed[0],"easeInQuad",
					function(){
						if((autoMotion===true)&&(who2===0)){btnChange(btn.eq(next));};
						$(this).animate({"left":(jQuery.data(this,"posiL").split("px")[0]*1+posi3)+"px"},
							speed[1],
							"swing",
							function(){
								$(this).animate({"left":jQuery.data(this,"posiL")},
									speed[2],
									"swing",
									function(){
										if(who2===size-1){motion=false;showing=next;}
									}
								);
							}
						);
					}
				);
				delay+=delayTime; 
			});
		};
		var stopAutoFunc=function(){
			var sameBtn=false;
			if(showing===next){}
			if(showing===clickedBtn){sameBtn=true}
			clearInterval(animation);
			autoMotion=false;
			size=$("#mopSlider").children().children("ul").eq(clickedBtn).children("li").size();
			if(motion==true){
				
				if((motionNo===1)&&(showing!==clickedBtn-1)){
					$("#mopSlider").children().children("ul").eq(showing).children("li").each(function(){
						$(this).stop();
					});
					$("#mopSlider").children().children("ul").eq(next).children("li").each(function(){
						$(this).stop();
					});
				}else if((motionNo===2)&&(showing!==clickedBtn)){
					$("#mopSlider").children().children("ul").eq(showing).children("li").each(function(){
						$(this).stop();
					});
				};
				$("#mopSlider").children().children("ul").each(function(who){
					if(who>clickedBtn){preposition(this,"goRight");
					}else if(who<clickedBtn){preposition(this,"goLeft")}
				});
				if(motionNo===1){
					if(showing===clickedBtn){motionFunc2("goRight");}
					else{motionFunc2("goLeft");}
				}else if(motionNo===2){
					if(showing!==clickedBtn){motionFunc2("goLeft");}
				};
			};
		};
		var afterStop=function(){};
		var btnChange=function(who){
			var that=who;
			if(browser==="ie6"){
				if($(that).hasClass("btnForIe6")){
					$(that).removeClass("btnForIe6");
					$(that).addClass("btnForIe6-2");
					$(that).siblings().removeClass("btnForIe6-2");
					$(that).siblings().addClass("btnForIe6");
				}
			}
			var posi=$(that).css("background-position");
			if(!posi){
				var posiX=$(that).css('backgroundPositionX');
				var posiY=$(that).css('backgroundPositionY');
				posi=posiX+" "+posiY;
			}
			if((posi==="0px 0px")||(posi==="0% 0%")){
				$(that).css({"background-position":w+"px 0px"});
				$(that).siblings().css({"background-position":"0px 0px"});
			}
		};
		$(this).children("#navi").children(".btn").click(function(){
			clickedBtn=jQuery.data(this,"num");
			btnChange(this);
			if(autoMotion===true){stopAutoFunc();}
			stopAuto=true;
			if((motion===false)&&(autoMotion===false)){
				delay=0;
				size=$("#mopSlider").children().children("ul").eq(showing).children("li").size();
				/*set to preposition, left or right*/
				$("#mopSlider").children().children("ul").each(function(who){
					if(who>showing){preposition(this,"goRight");
					}else if(who<showing){preposition(this,"goLeft")};
				});
				/*clicked bigger number*/
				if(showing<clickedBtn){motionFunc("goLeft");};
				/*clicked smaller number*/
				if(showing>clickedBtn){motionFunc("goRight");};
			};
		});
		
		/*touch events*/
		var isTouch = ('ontouchstart' in window);
		$("#slider").bind({
			'touchstart mousedown':function(e) {
				e.preventDefault();
			 	this.startX = (isTouch ? event.changedTouches[0].pageX : e.pageX);
			 	this.startY = (isTouch ? event.changedTouches[0].pageY : e.pageY);
				this.touched = true;
			},
			'touchmove mousemove': function(e) {
				if (!this.touched) {
					return;
				}
				e.preventDefault();
				this.endX = (isTouch ? event.changedTouches[0].pageX : e.pageX);
				this.endY = (isTouch ? event.changedTouches[0].pageY : e.pageY);
			},
			'touchend mouseup': function(e) {
				if (!this.touched) {
					return;
				}
				this.touched = false;
				if(!this.endX){
					this.endX=this.startX
				}
				this.which=this.endX-this.startX;
				if(this.which<-10){
					/*alert("goLeft");*/
				}else if(this.which>10){
					/*alert("goRight");*/
				}else{
				}
				this.endX="";
			}
		});	
		
		
		/*setting position of buttons*/
		var btn=$(this).children("#navi").children(".btn");
		var stops=btn.size();
		var fastBtnMgn=(width-((btn.width()*stops)+(btnMgn*(stops-1))))/2;
		$(this).children("#navi").children(".btn").each(function(who){
			if(who!==0){
				$(this).css({"margin-left":btnMgn+"px"});
			}else{
				$(this).css({"margin-left":fastBtnMgn+"px"})
			}
			if(browser==="ie6"){
				$(this).css({"display":"inline"})
			}
		});
		/*shuffle*/
		if(shuffle==="y"){
			var arr=jQuery.makeArray($(this).children("ul"));
			arr.shuffle();
			$(arr).appendTo($(this));
		};
		/*setting position of items*/
		$(this).children("ul").children("li").each(function(){
			$(this).css({"left":(width+(jQuery.data(this,"posiL").split("px")[0]*1))+"px"});
		});
		/*setting position of fast items*/
		$(this).children("ul").eq(0).children("li").each(function() {
            $(this).css({"left":jQuery.data(this,"posiL")})
        });
		/*start*/
		$(this).children("ul").delay(100).fadeIn("slow");
		/*numbers start from zero*/
		/*showing=0;*/
		var animation=setInterval(
			function(){
				if(showing<(ulSize-1)){
					next+=1;
					motion=true;
					delay=0;
					size=$("#mopSlider").children().children("ul").eq(showing).children("li").size();
					motionFunc("goLeft");
				}else if(showing===(ulSize-1)){
					next=0;
					$("#mopSlider").children().children("ul").each(function(who){
						if(who!==showing){preposition(this,"goRight")}
					});
					motion=true;
					delay=0;
					size=$("#mopSlider").children().children("ul").eq(showing).children("li").size();
					motionFunc("goLeft");
				}
			},interval
		);
	}	
});
