/*
 * mopSlider 1.1
 * By Hiroki Miura (http://www.mopstudio.jp)
 * Copyright (c) 2008 mopStudio
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * August 12, 2008
*/

var mopSliderFunc;

jQuery.fn.extend({

	mopSlider:function(setting){
	
	/*path to image*/
	var btnLeft=new Image();
	/*paper*/
	btnLeft.src="/inc/js/top/sliderBtnLeftW.png";
	/*monochrome
	btnLeft.src="mopSlider/sliderBtnLeft.png";
	*/
	var btnLeftF=new Image();
	/*paper*/
	btnLeftF.src="/inc/js/top/sliderBtnLeftW_f.png";
	/*monochrome
	btnLeftF.src="mopSlider/sliderBtnLeft_f.png";
	*/
	
	var btnRight=new Image();
	/*paper*/
	btnRight.src="/inc/js/top/sliderBtnRightW.png";
	/*monochrome
	btnRight.src="mopSlider/sliderBtnRight.png";
	*/
	var btnRightF=new Image();
	/*paper*/
	btnRightF.src="/inc/js/top/sliderBtnRightW_f.png";
	/*monochrome
	btnRightF.src="mopSlider/sliderBtnRight_f.png";
	*/
	
	var btnChange=new Image();
	/*paper*/
	btnChange.src="/inc/js/top/sliderBackRightRightW.png";
	/*monochrome
	btnChange.src="mopSlider/sliderBackRightRight.png";
	*/
	var btnChangeF=new Image();
	/*paper*/
	btnChangeF.src="/inc/js/top/sliderBackRightRightW_f.png";
	/*monochrome
	btnChangeF.src="mopSlider/sliderBackRightRight_f.png";
	*/
	var mopSliderW=setting.w;
	var mopSliderH=setting.h;
	var sldW=setting.sldW;
	var btnW=setting.btnW;
	var auto=setting.auto;
	var move=setting.move;
	var interval=setting.interval;
	var itemMgn=setting.itemMgn;
	var shuffle=setting.shuffle;
	var anim,turn="go",preTurn=false;
	var posi,posiBack;
	var scrollMax,scrollNum,caseW,itemNum,sliderNum,checkNum,movNum;
	var nextMov=[],nextMovBack=[];
	var nextNum=0,nextNumBack=0;
	var scrollW=0,scrollWback=0;
	var mopSliderMotion;
	var ua=navigator.userAgent,browser,os;
	var px="px"
	var btnPosi,boxPosi,btnPast,timeCheck;
	
	if(setting.auto==null){auto=0};
	if(setting.move==null){move=1000};
	if(setting.interval==null){interval=2000};
	if(setting.itemMgn==null){itemMgn=20};
	if(setting.indi==null){indi=1};
	if(setting.shuffle==null){shuffle=1};
	
	/*shuffle*/
	var arr=jQuery.makeArray($(this).children());
	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;
	};
	if(shuffle==1){
		arr.shuffle();
		$(arr).appendTo(this);
	};
	
	if(ua.indexOf("Mac",0)>=0){
		os="mac";
	}else if(ua.indexOf("Win",0)>=0){
		os="win";
	}
	/*ie6*/
	if((jQuery.browser.msie==true)&&(jQuery.browser.version.charAt(0)==6)){
		browser="ie6";
	}
	/*start auto or manual*/
	if(auto==1){anim=true;}else{anim=false;}
	/*setting*/
	$(this).css({position:"absolute",overflow: "hidden",left: "0px",display: "block"});
	/*items number*/
	itemNum=$(this).children().length;
	
	var allW=0;
	/*items count*/
	var num=0;
	/*setting items*/
	for (i=1; i<(itemNum+1); i++){
		var itemW=eval($(this).children().eq(num).css("width").split("px")[0]);
		nextMov.push(itemW);
		var itemH=eval($(this).children().eq(num).css("height").split("px")[0]);
		var mgn=(mopSliderH-itemH)/2;
		$(this).children().eq(num).css({marginTop:mgn+px});
		num+=1;
		allW+=itemW;
	};
	/*for back*/
	var lengthNum=nextMov.length;
	for (i=1; i<(nextMov.length); i++){
		/*change to 0start*/
		var pushW=nextMov[lengthNum-1];
		nextMovBack.push(pushW);
		lengthNum-=1;
	};
	/*put mopSlider*/
	$(this).wrap("<div id='mopSlider01'><div class='holder'></div></div>");
	$(this).parent().after(
	'<div class="sliderCase">'+
		'<div class="sliderCaseLeft"></div>'+
		'<div class="sliderCaseRight"></div>'+
		'<div class="slider">'+
			'<div class="sldLeft"></div>'+
			'<div class="sldCenter"></div>'+
			'<div class="sldRight"></div>'+
			'<div class="sliderBtn">'+
				'<div class="sldBtnLeft"></div>'+
				'<div class="sldBtnCenter"><div class="indi"></div></div>'+
				'<div class="sldBtnRight"></div>'+
			'</div>'+
	'</div>'+
	'</div>'+
	'<div class="leftTop"></div>'+
	'<div class="rightTop"></div>'+
	'<div class="leftBottom"></div>'+
	'<div class="rightBottom"></div>'+
	'<div class="logo"></div>'
	);
	$("#mopSlider01").pngFix();
		
		mopSliderFunc={
			init:function(){
				
				/*setting*/
				/*setting mopSlider*/
				$("#mopSlider01").css({width:mopSliderW+px,height:mopSliderH+28+px});
				/*setting box*/
				$("#mopSlider01 .box").css({width:mopSliderW+px,height:mopSliderH+px});
				/*indication "manual,auto"*/
				if(setting.indi==0){$("#mopSlider01 .indi").hide()};
				
				/*setting items*/
				$("#mopSlider01 .holder").children().children().css({marginLeft:itemMgn+"px",float:"left",position:"relative"});

				/*all items margin*/
				var itemMgnAll=itemMgn*itemNum;
				/*box width*/
				var boxW=allW+itemMgnAll+itemMgn;
				/*case width*/
				$("#mopSlider01 .slider").css({width:sldW+px});
				$("#mopSlider01 .sliderBtn").css({width:btnW+px});
				/*box scroll saize*/
				scrollMax=boxW-mopSliderW;
				/*0 to100% numbers(box)*/
				scrollNum=scrollMax*0.01;
				/*0 to100% numbers(slider)*/
				sliderNum=(sldW-btnW)*0.01;
				/*setting slider*/
				$("#mopSlider01 .sliderCase").css({width:sldW+50+px});
				var sldCaseW=eval($("#mopSlider01 .sliderCase").css("width").split("px")[0]);
				var sliderLeftMgn=(mopSliderW-sldCaseW)/2;
				$("#mopSlider01 .sliderCase").css({left:sliderLeftMgn+px});
				/*setting holder*/
				var holderWidth=(mopSliderW+scrollMax*2);
				$("#mopSlider01 .holder").css({
					width:holderWidth+"px",height:mopSliderH+px,position:"relative",left:-(scrollMax)+px});
				$("#mopSlider01 .holder").children().css({width:boxW+px,left:scrollMax+px});
				
				/*setting inside of slider*/
				$("#mopSlider01 .sldLeft").css({left:"0px"});
				$("#mopSlider01 .sldRight").css({right:"0px"});
				$("#mopSlider01 .sldCenter").css({left:"20px",width:sldW-40+px});
				$("#mopSlider01 .sldBtnLeft").css({left:"0px"});
				$("#mopSlider01 .sldBtnRight").css({right:"0px"});
				$("#mopSlider01 .sldBtnCenter").css({left:"20px",width:btnW-40+px});
				/*setting final move*/
				var lastLength,lastLengthBack;
				/*final number of go*/
				for (i=1; i<(nextMov.length); i++){
					var lenchCheck=i-1;
					scrollW+=nextMov[lenchCheck]+itemMgn;
					if(scrollW>=scrollMax){
					lastLength=lenchCheck;
					break;
					}
				}
				/*final number of back*/
				for (i=1; i<(nextMovBack.length); i++){
					var lenchCheckBack=i-1;
					scrollWback+=nextMovBack[lenchCheckBack]+itemMgn;
					if(scrollWback>=scrollMax){
					lastLengthBack=lenchCheckBack;
					break;
					}
				}
				/*final move go*/
				var lastMov=eval(nextMov[lastLength]+itemMgn);
				checkNum=scrollMax-lastMov;
				
				/*final move back*/
				var lastMovBack=eval(nextMovBack[lastLengthBack]+itemMgn);
				checkNumBack=lastMovBack;
				
				/*final move go fix*/
				var amari=scrollMax-scrollW;
				nextMov[lastLength]+=amari;
				
				/*final move back fix*/
				var amariBack=scrollMax-scrollWback;
				nextMovBack[lastLengthBack]+=amariBack;
				
				/*draggable*/
				$("#mopSlider01 .sliderBtn").draggable({
					axis:"x",
					containment:"parent",
					start:function(){
						$("#mopSlider01 .sliderBtn").stop();
						clearInterval(timeCheck);
						timeCheck=setInterval("mopSliderFunc.getPosi1()",80);
					},
					drag:function(){
						mopSliderFunc.check();
					},
					stop:function(){
						mopSliderFunc.finalMove();
					}
				});
				$("#mopSlider01 .holder").children().draggable({
					axis:"x",
					containment:"parent",
					start:function(){
						$("#mopSlider01 .sliderBtn").stop();
						clearInterval(timeCheck);
						timeCheck=setInterval("mopSliderFunc.getPosi1()",80);
					},
					drag:function(){
						mopSliderFunc.check();
					},
					stop:function(){
						mopSliderFunc.finalMove();
					}
				});
				/*click change manual,auto*/
				$("#mopSlider01 .sliderCaseRight").click(
					function(){
						if(anim==true){
							mopSliderFunc.manualAct();
						}else{
							clearInterval(mopSliderMotion);
							mopSliderFunc.autoAct();
						}
					}
				);
				/*box click*/
				$("#mopSlider01 .holder").children().click(
					function(){
						if(anim==true){
							mopSliderFunc.manualAct();
						}
					}
				);
				/*auto or manual*/
				if(anim==true){
					$("#mopSlider01 .sliderBtn").draggable("disable");
					$("#mopSlider01 .holder").children().draggable("disable");
					mopSliderFunc.auto();
					mopSliderMotion=setInterval("mopSliderFunc.move()",interval);
				}else{
					mopSliderFunc.manualAct();
				}
				/*foe mac*/
				if(os=="mac"){$("#mopSlider01 .indi").css({letterSpacing:"0.1em"})}
				
				mopSliderFunc.goInit();
				mopSliderFunc.backInit();
			},
			
			check:function(){
				btnPosi=eval($("#mopSlider01 .sliderBtn").css("left").split("px")[0]);
				var sliderNum100=btnPosi/sliderNum;
				boxPosi=eval($("#mopSlider01 .holder").children().css("left").split("px")[0]);
				/*box count 0to100*/
				var boxPosi0=-(boxPosi-scrollMax);
				var boxNum=(boxPosi0/scrollNum)*-1;
				/*go or back*/
				if(anim==true){
					if((boxPosi0)>=checkNum){preTurn=true;}
					else if((boxPosi0)<(checkNumBack+2)){preTurn=false;}
				}
				/*auto or manual*/
				if(anim==true){
					$("#mopSlider01 .sliderBtn").css({left:-(boxNum*sliderNum)+px});
				}else if(anim==false){
					$("#mopSlider01 .holder").children().css({left:-(sliderNum100*scrollNum)+scrollMax+px});
					$("#mopSlider01 .sliderBtn").css({left:-(boxNum*sliderNum)+px});
				}
			},
		
			/*box auto animation*/
			move:function(){
					if(anim==true){
						if(turn=="go"){
							$("#mopSlider01 .holder").children().animate({left:-posi+scrollMax+px},
								{duration:move,easing:'swing',
									step:function(){mopSliderFunc.check();},
									complete:function(){
										nextNum+=1;
										posi+=((nextMov[nextNum])+itemMgn);
									}
								}	
							);
						}else if(turn=="back"){
							$("#mopSlider01 .holder").children().animate({left:posiBack+px},
								{duration:move,easing:'swing',
									step:function(){mopSliderFunc.check();
									},
									complete:function(){
										nextNumBack+=1;
										posiBack+=((nextMovBack[nextNumBack])+itemMgn);
									}
								}
							);
						}
					mopSliderFunc.turnCheck();
				}
			},
			turnCheck:function(){
				if(preTurn==true){turn="back";mopSliderFunc.goInit();}
				else if(preTurn==false){turn="go";mopSliderFunc.backInit();}
			},
			goInit:function(){
				nextNum=0;
				posi=nextMov[nextNum]+itemMgn;
			},
			backInit:function(){
				nextNumBack=0;
				posiBack=nextMovBack[nextNumBack]+itemMgn;
			},
			manualAct:function(){
				anim=false;
				clearInterval(mopSliderMotion);
				$("#mopSlider01 .holder").children().stop();
				$("#mopSlider01 .holder").children().css({cursor:"move"});
				$("#mopSlider01 .holder").children().draggable("enable");
				$("#mopSlider01 .sliderBtn").draggable("enable");
				mopSliderFunc.manual();
			},
			autoAct:function(){
				anim=true;
				/*初期化*/
				clearInterval(mopSliderMotion);
				turn="go"
				$("#mopSlider01 .holder").children().css({cursor:""});
				mopSliderFunc.goInit();
				mopSliderFunc.backInit();
				/*stop draggable*/
				$("#mopSlider01 .sliderBtn").draggable("disable");
				$("#mopSlider01 .holder").children().draggable("disable");
				mopSliderFunc.auto();
				/*back to start*/
				$("#mopSlider01 .holder").children().animate({left:scrollMax+px},
					{duration:move,easing:'swing',
						step:function(){
							mopSliderFunc.check();
						},
						complete:function(){
							mopSliderMotion=setInterval("mopSliderFunc.move()",interval);
						}
					}
				);
				
			},
			manual:function(){
				$("#mopSlider01 .indi").html("Manual");
				if(browser!="ie6"){
					$("#mopSlider01 .sldBtnLeft").css({backgroundImage:"url("+btnLeft.src+")"});
					$("#mopSlider01 .sldBtnRight").css({backgroundImage:"url("+btnRight.src+")"});
					$("#mopSlider01 .sliderCaseRight").css({backgroundImage:"url("+btnChangeF.src+")"});
				}
			},
			auto:function(){
				$("#mopSlider01 .indi").html("Auto");
				if(browser!="ie6"){
					$("#mopSlider01 .sldBtnLeft").css({backgroundImage:"url("+btnLeftF.src+")"});
					$("#mopSlider01 .sldBtnRight").css({backgroundImage:"url("+btnRightF.src+")"});
					$("#mopSlider01 .sliderCaseRight").css({backgroundImage:"url("+btnChange.src+")"});
				}
			},
			getPosi1:function(){
				btnPast=btnPosi;
			},
			finalMove:function(){
				var btnMoveNum=btnPosi-btnPast;
				/*last slip move*/
				if(btnMoveNum>10){
					btnMoveNum=10;
				}else if(btnMoveNum<-10){
					btnMoveNum=-10;
				}
				var btnMoveTo=btnMoveNum+btnPosi;
				
				if(btnMoveTo>(sldW-btnW)){
					btnMoveTo=sldW-btnW;
				}else if(btnMoveTo<0){
					btnMoveTo=0;
				}
				if((btnMoveNum>5)||(btnMoveNum<-5)){
					$("#mopSlider01 .sliderBtn").animate({left:btnMoveTo+px},
						{duration:500,easing:'linear',
							step:function(){mopSliderFunc.check()},
							complete:function(){
								clearInterval(timeCheck);
							}
						}
					);
				}
			}
		}
		mopSliderFunc.init();
	}
});
