var _UA = navigator.userAgent;
var _asIE6 = (_UA.indexOf('MSIE 6') > -1)?true:false;

(function($, window, document, undefined){
				
var topMenu = {
	opt : {
		limitNum :0,
		totalNum : 0,
		selectNum : 0,
		movePos : 900,
		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('img, .imgPng');
		//$('.top_btn_over').hide();
		
		var centerPoint = ($topMenuList.width()/2)-(_opt.movePos/2);
		_opt.firstPos = centerPoint - (_opt.totalNum*_opt.movePos);
		
		
		$topMenuListBox.css('left', _opt.firstPos);
		$topMenuListBox.css('opacity', 0);
		//$topMenuListBox.show().animate({'left': _opt.firstPos}, 2000, 'easeInOutCubic', function(){
		$topMenuListBox.show().animate({'opacity': 1}, 1000, 'easeInOutCubic', function(){
			_this.startMove(_opt.totalNum);
			_this.bindEvent();
			_opt.limitNum  = _opt.totalNum*3;
			_opt.selectNum = _opt.totalNum;
			
			$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 }, 2500, 'easeInOutQuint', 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, 10000);
	},
	stopMove : function(){
		var _this = this;
		var _opt  = _this.opt;
		
		clearInterval(_opt.autoTimer);
	}
};	




$(function(){
	
	
	topMenu.int();
	
	
	
});
})(jQuery, this, this.document);
