(function($, window, document, undefined){


var unoTOPMenu = {
	opt : {
		limitNum :0,
		totalNum : 0,
		selectNum : 0,
		movePos : 628,
		firstPos : 0,
		autoTimer : {}
	},
	int : function(){
		var _this = this;
		var _opt  = _this.opt;
		
		var $topMenuList = $('.topMenuList');
		var $switchs = $('.topMenuSwitch');
		
		
		var $topMenuListBox = $('.topMenuListBox');
		var $topMenuListLI  = $topMenuListBox.children('div');
		
		_opt.totalNum = $topMenuListLI.length;
		
		//サークルアイコンの挿入
		for(var i=0; i<_opt.totalNum; i++){
			var anchorClass = (i == 0)? 'selected' : '';
			var switchIcon = $('<li><\/li>').append($('<a><\/a>',{'href':'#'}).addClass(anchorClass));
			$('.topMenuMark').append(switchIcon);
		}
		
		
		$topMenuListBox.css('left', '2140px');
		$topMenuListBox.css('width', (_opt.totalNum*_opt.movePos)*3);
			
		var $cloneList1 = $topMenuListLI.clone();
		var $cloneList2 = $topMenuListLI.clone();
		$topMenuListBox.append($cloneList1).append($cloneList2);
		$topMenuListBox.children('div').each(function(i){
			$(this).addClass('no'+i);
		});
		
		if(_asIE6) DD_belatedPNG.fix('.contKV, .imgPng');
		$('.top_btn_over').hide();
		
		var centerPoint = ($topMenuList.width()/2)-(_opt.movePos/2);
		_opt.firstPos = centerPoint - (_opt.totalNum*_opt.movePos)+4;
		
		$topMenuListBox.show().animate({'left': _opt.firstPos}, 1200, 'easeInOutCubic', function(){
			_this.startMove(_opt.totalNum);
			_this.bindEvent();
			//最初は真ん中の0番目が選ばれている
			_opt.limitNum  = _opt.totalNum*3;
			_opt.selectNum = _opt.totalNum;
			
			//$switchs.fadeIn(400);
			$switchs.children('p').fadeIn(400);
			$switchs.children('ol').fadeIn(400);
		});
	},
	setCircleIcon : function(_num){
		var _this = this;
		var _opt  = _this.opt;
		
		var selectNum = (_num <  _opt.totalNum) ? _num :_num-_opt.totalNum;	
		
		$('.topMenuMark>li>a').removeClass('selected');
		$('.topMenuMark>li').eq(selectNum).children('a').addClass('selected');
	},
	bindEvent : function(){
		var _this = this;
		var _opt  = _this.opt;
		
		$('.topMenuListBox a').hover(function(){
			$(this).find('.top_btn_over').show();
		},function(){
			$(this).find('.top_btn_over').hide();
		});
		
		$('.topMenuMark>li>a').bind('click', function(){
			var myNum = $(this).parent().index();
			
			var setNum = 0;
			
			if(_opt.selectNum < _opt.totalNum){
				setNum = (myNum - _opt.selectNum)*-1;
			}else if(_opt.selectNum >= _opt.totalNum && _opt.selectNum < _opt.totalNum*2){
				setNum = (myNum - (_opt.selectNum - _opt.totalNum))*-1;
			}
			
			_this.moveMenu(setNum, false);
			return false;
		});
		
		$('.topMenuBtnPrev>a').bind('click', function(){
			_this.moveMenu(1, false);
			return false;
		});
		$('.topMenuBtnNext>a').bind('click', function(){
			_this.moveMenu(-1, false);
			return false;
		});
		
		$('.topMenuBtnNet').hover(
			function(){
				$(this).next().addClass('over');
			},
			function(){
				$(this).next().removeClass('over');
			}
		);
	},
	unBindEvent : function(){
		$('.topMenuMark>li>a, .topMenuBtnPrev>a, .topMenuBtnNext>a').unbind('click');
	},
	moveMenu : function(_num, autoFlag){
		var _this = this;
		var _opt  = _this.opt;
		
		_this.unBindEvent();
		if(!autoFlag)_this.stopMove();
		
		var $topMenuListBox = $('.topMenuListBox');
		var nowPos = parseInt($topMenuListBox.css('left'),10);
		var targetPos = nowPos+(_opt.movePos*_num);
		
		
		
		$topMenuListBox.stop().animate({'left':targetPos }, 360, 'easeInOutCubic', function(){
			_this.bindEvent();
			_opt.selectNum -= _num;

			if(_opt.selectNum < Math.ceil(_opt.totalNum/2)){
				_opt.selectNum = Math.ceil((_opt.selectNum*2)+(_opt.totalNum/2));
				var nowPos = parseInt($topMenuListBox.css('left'),10);
				$topMenuListBox.css({'left':nowPos - _opt.movePos*_opt.totalNum});
			}else if(_opt.selectNum >= _opt.totalNum*2){
				_opt.selectNum = _opt.totalNum;
				$topMenuListBox.css({'left':_opt.firstPos});
			}

			_this.setCircleIcon(_opt.selectNum);
			if(!autoFlag)_this.startMove();
		});
	},
	startMove : function(){
		var _this = this;
		var _opt  = _this.opt;
		
		function allInterval(){
			_this.moveMenu(-1, true);
		}
		
		_opt.autoTimer = setInterval(allInterval, 5000);
	},
	stopMove : function(){
		var _this = this;
		var _opt  = _this.opt;
		
		clearInterval(_opt.autoTimer);
	}
};	

function getRand(from, to) {
    return from + Math.floor( Math.random() * (to - from + 1) );
}

function topKV(){
	var num1 = getRand(1, 5);
	var num2 = getRand(1, 2);
	return 'color'+ num1 +'_'+ num2;
}


$(function(){
	$('body').addClass(topKV);
	
	unoTOPMenu.int();
	
	
	var pdTatal = $('.newProductsList>li').length -1;
	var pdNowNum = 0;
	$('.newProductsBtnPrev').hide();
	
	function movePdBox(_num){
		var targtePos = parseInt($('.newProductsList').css('left'), 10) + (302*_num);
		$('.newProductsList').animate({'left':targtePos}, 400, 'easeInOutCubic');
		
		pdNowNum -= _num;
		
		if(pdNowNum == 0){
			$('.newProductsBtnPrev').hide();
			$('.newProductsBtnNext').show();
		}else if(pdNowNum == pdTatal){
			$('.newProductsBtnPrev').show();
			$('.newProductsBtnNext').hide();
		}else{
			$('.newProductsBtnPrev').show();
			$('.newProductsBtnNext').show();
		}
		
	}
	
	
	
	$('.newProductsBtnPrev').bind('click', function(){
		movePdBox(1);
		return false;
	});
	
	$('.newProductsBtnNext').bind('click', function(){
		movePdBox(-1);
		return false;
	});
	
	
	
});
})(jQuery, this, this.document);
