$(function(){
	//圖片輪撥
	var indexPlist1RT = 5000;
	var indexPlist2RT = 5000;
	var indexPlist3RT = 5000;
	var indexPlist4RT = 5000;
	if($('.roundCount1 img').length>1){
		setTimeout(function(){
			var $hiddenObj = $('.roundCount1 img.hidden');
			var $showObj = $('.roundCount1 img:not(.hidden)');
			$showObj.fadeOut(500,function(){$(this).addClass('hidden');	});
			$hiddenObj.fadeIn(1000,function(){$(this).removeClass('hidden');	});
			setTimeout(arguments.callee, indexPlist1RT);
		},indexPlist1RT);
	}
	if($('.roundCount2 img').length>1){
		setTimeout(function(){
			var $hiddenObj = $('.roundCount2 img.hidden');
			var $showObj = $('.roundCount2 img:not(.hidden)');
			$showObj.fadeOut(500,function(){$(this).addClass('hidden');	});
			$hiddenObj.fadeIn(1000,function(){$(this).removeClass('hidden');	});
			setTimeout(arguments.callee, indexPlist2RT);
		},indexPlist2RT);
	}
	if($('.roundCount3 img').length>1){
		setTimeout(function(){
			var $hiddenObj = $('.roundCount3 img.hidden');
			var $showObj = $('.roundCount3 img:not(.hidden)');
			$showObj.fadeOut(500,function(){$(this).addClass('hidden');	});
			$hiddenObj.fadeIn(1000,function(){$(this).removeClass('hidden');	});
			setTimeout(arguments.callee, indexPlist3RT);
		},indexPlist3RT);
	}
	if($('.roundCount4 img').length>1){
		setTimeout(function(){
			var $hiddenObj = $('.roundCount4 img.hidden');
			var $showObj = $('.roundCount4 img:not(.hidden)');
			$showObj.fadeOut(500,function(){$(this).addClass('hidden');	});
			$hiddenObj.fadeIn(1000,function(){$(this).removeClass('hidden');	});
			setTimeout(arguments.callee, indexPlist4RT);
		},indexPlist4RT);
	}
	
	
	
	var moveIndex = new Array(0,0,0,0);
	var moveing = new Array(false,false,false,false);
	var autoMoveTime = new Array(5000,5000,5000,5000);
	var stopReAuto = new Array(5000,5000,5000,5000); 
	var autoRunSet = new Array(true,true,true,true);
	
	//追加無限輪撥用 3格時，要多2個
	$('.ipccBottomContentsInMove').each(function(index){
		
		var toailLen = $('.ipccBottomContentsInMove').eq(index).find('ul li').length;
		var $carouselMove = $('.ipccBottomContentsInMove').eq(index);
		if(toailLen>3){
			$carouselMove.find('ul li:eq(0)').clone().appendTo($carouselMove.find('ul'));
			$carouselMove.find('ul li:eq(1)').clone().appendTo($carouselMove.find('ul'));
			$carouselMove.find('ul li:eq('+(toailLen-1)+')').clone().prependTo($carouselMove.find('ul'));
			$carouselMove.find('ul li:eq('+(toailLen-1)+')').clone().prependTo($carouselMove.find('ul'));
			$carouselMove.css('margin-left','-180px');
			moveIndex[index]=2;
			
			setTimeout(function(){
				if(autoMoveTime[index]<=0){
					autoMoveTime[index]=5000;
					nextIndex = moveIndex[index]+1;
					nextpic(nextIndex,'next',index);
				}
				autoMoveTime[index]-=1000;
				setTimeout(arguments.callee, 1000);
				
			},1000);
		}
		
	});
	//調整移動區域大小 
	$('.ipccBottomContentsInMove').each(function(){
		$(this).width($(this).find('img').length*102);
	});
	

	$('.iccbaBefore').click(function(index){
			var index = $('.iccbaBefore').index(this);
			//一般撥放
			//var backIndex = (moveIndex[index]-1) < 0 ? $('.ipccBottomContentsInMove').eq(index).find('ul li').length-3 : moveIndex[index]-1;
			backIndex = moveIndex[index]-1;
			nextpic(backIndex,'back',index);
			return false;
	});
	$('.iccbaAfter').click(function(){
			var index = $('.iccbaAfter').index(this);
			//一般撥放
			//var nextIndex = (moveIndex[index]+1) > $('.ipccBottomContentsInMove').eq(index).find('ul li').length-3 ? 0: moveIndex[index]+1;
			nextIndex = moveIndex[index]+1;
			nextpic(nextIndex,'next',index);
			return false;
	});
		
		function nextpic(mindex,type,index){
			var picLength = $('.ipccBottomContentsInMove').eq(index).find('ul li').length;
			var mtpostion = parseInt($('.ipccBottomContentsInMove').eq(index).css('margin-left'));
			//if(moveIndex[index]!=mindex && !moveing[index]&& ((mtpostion!=0&&type=='back') || (mtpostion!=-((picLength-3)*90)&&type=='next'  ))){
			if(moveIndex[index]!=mindex && !moveing[index] && picLength>3){
				if( picLength > 0 ){
					var postion = -(mindex*90) + 'px';
					moveing[index] = true;
					moveIndex[index] = mindex;	
					$('.ipccBottomContentsInMove').eq(index).animate({'margin-left':postion},500,function(){
			
						//巡環撥放
						var len = $('.ipccBottomContentsInMove').eq(index).find('ul li').length;
						if( moveIndex[index]==(len-2)){
							moveIndex[index] = 2;
							var resetPos = -(2*90) + 'px';
							$('.ipccBottomContentsInMove').eq(index).css('margin-left',resetPos);
						}else if( moveIndex[index]==1){
							var resetPos = -((len-3)*90) + 'px';
							moveIndex[index] = (len-3);
							$('.ipccBottomContentsInMove').eq(index).css('margin-left',resetPos);
						}
								moveing[index]=false;
								autoMoveTime[index] = stopReAuto[index];
					});
				}
			}
		}
		
		
		
		
		var moveWidth = new Array(126,126,126,126);
		//移動最大範圍
		var moveMaxX = new Array(60,60,60,60);
		//var temp = -(($('.ipccBottomContentsInMove ul li').length-3) * moveWidth)-63;
		var moveMinX = new Array(0,0,0,0);
		var moveing =  new Array(false,false,false,false);
		
		//建立索引位置
		var moveArray = new Array(new Array(),new Array(),new Array(),new Array());	
		$('.ipccBottomContentsInMove').each(function(index){
			for (var i=0;i<$(this).find('ul li').length-2;i++){
				var temp = -(($(this).find('ul li').length-2) * moveWidth)-63;
				moveMinX[index] = temp;
				moveArray[index][i] = -(i * moveWidth[index]);
			}
		});

		
		var movestratX = new Array(0,0,0,0);
		var movetempX = new Array(0,0,0,0);
		
		var movestratY = new Array(0,0,0,0);
		var movetempY = new Array(0,0,0,0);
		
		
		function touchStart(e) {
			e.preventDefault();
			touchIndex = $('.ipccBottomContentsInMove').index(this);
			autoRun = false;
			var touches = e.changedTouches,
				i = 0, l = touches.length, touch;
			for (; i < l; i++) {
				touch = touches[i];
				movestratX[touchIndex] = touch.pageX;
				movetempX[touchIndex] = movestratX[touchIndex];
				
				movestratY[touchIndex] = touch.pageY;
				movetempY[touchIndex] = movestratY[touchIndex];
			}
			//return false;
		}
		
		function touchMove(e) {
			e.preventDefault();
			var touches = e.targetTouches,
				i = 0, l = touches.length, touch;
			for (; i < l; i++) {
				if(!moveing[touchIndex]){
					touch = touches[i];
					var nowML = parseInt($('.ipccBottomContentsInMove').eq(touchIndex).css('margin-left'));
					var moveP = nowML + (touch.pageX-movetempX[touchIndex]);
					//超出範圍時，移動位置等於最大位置
					if(touch.pageX>movestratX[touchIndex]+60){
						autoRunSet[touchIndex]=false;
						$('.iccbaBefore').eq(touchIndex).trigger('click');
						movestratX[touchIndex] = touch.pageX;
						autoMoveTime[touchIndex] = stopReAuto[touchIndex];
						//moveP = moveMaxX[touchIndex];
						return;
					}else if(touch.pageX<movestratX[touchIndex]-60){
						autoRunSet[touchIndex]=false;
						$('.iccbaAfter').eq(touchIndex).trigger('click');
						movestratX[touchIndex] = touch.pageX;
						
						//moveP = moveMinX[touchIndex];
						return;
					}
					
					autoMoveTime[touchIndex] = stopReAuto[touchIndex];
					//移動開始
					//$('.ipccBottomContentsInMove').eq(touchIndex).css('margin-left',moveP);
	
					//備存移動後位置
					movetempX[touchIndex] = touch.pageX;
	
				}
			}
			return false;
		}
		
		function touchEnd(e) {
			var touches = e.changedTouches,
			i = 0, l = touches.length, touch;
			for (;i < l; i++) {
				if(!moveing[touchIndex]){
					moveing[touchIndex] = true;
					touch = touches[i];
					var nowML = parseInt($('.ipccBottomContentsInMove').eq(touchIndex).css('margin-left'));
					var moveP = nowML + (touch.pageX-movetempX[touchIndex]);
					if(moveP>moveArray[touchIndex][0]){
						moveP = moveArray[touchIndex][0];
						alert('a');
					}else if(moveP<moveArray[touchIndex][moveArray[touchIndex].length-1]){
						moveP = moveArray[touchIndex][moveArray[touchIndex].length-1];
						alert('b');
					}else if((touch.pageX < movestratX[touchIndex] - 63 )){
						moveIndex[touchIndex]++;
						moveIndex[touchIndex] = (moveIndex[touchIndex]>=moveArray[touchIndex].length-2) ? moveArray[touchIndex].length-2 : moveIndex[touchIndex];
						moveP = moveArray[touchIndex][moveIndex];
						alert(moveIndex[touchIndex]);
					}else if((touch.pageX > movestratX[touchIndex] + 63 )){
						moveIndex[touchIndex]--;
						moveIndex[touchIndex] = (moveIndex[touchIndex]<=0) ? 0 : moveIndex[touchIndex];
						moveP = moveArray[touchIndex][moveIndex];
						alert('d');
					}else if( (touch.pageX < movestratX[touchIndex] )|| (touch.pageX > movestratX[touchIndex] ) ){
						moveP = moveArray[touchIndex][moveIndex];
						alert('e');
					}
					$('.ipccBottomContentsInMove').eq(touchIndex).animate({marginLeft:moveP},200,function(){
						moveing[touchIndex]=false;
					});
				}
			}
		}
		$(".ipccBottomContentsInMove").each(function(index){
			var topObj = $(".ipccBottomContentsInMove").get(index);
			if (topObj.addEventListener){
			 topObj.addEventListener("touchstart", touchStart, false);
			 topObj.addEventListener("touchmove", touchMove, false);
			// topObj.addEventListener("touchend", touchEnd, false);
			} else if (topObj.attachEvent){
			 topObj.attachEvent("touchstart", touchStart);
			 topObj.attachEvent("touchmove", touchMove);
		//	 topObj.attachEvent("touchend", touchEnd);
			}
		});

	$.ajax({
		type: "POST",
		url: "http://messenger.services.live.com/users/80d59604ba78d4b9@apps.messenger.live.com/presence/?cb=?",
		dataType: "jsonp",
		success: function(data){
			$('#msn img').attr('src','/images/index/indexIcon4'+data.status+'.jpg');
		}
	});
		
});
