$(document).ready(function(){
	
	//------------------------------------------------------------------------------------------------------------------ sub main menu
	// one sub inner Width= 180 padding-left = 60  height = 55 pdb = 10
	var subCountLoop = 1;
	var subBoxWidth = 240;
	var subBoxHeight = 50;
	var subCountRow = 3; //set row
	var subCountChild =  0;
	var totalCol = 1;
	var totalRow = 1;
	var subColPerPage = 4; //set col
	
	var subPositionL = Array('start');
	var subPositionT = Array('start');
	
	var checkFirstChild = Array();
	var totalColWidth = 0;

	var allNameID = Array('subAboutUs', 'subServices', 'subAnnounce', 'subNewsRoom', 'subExchangeRate', 'subInterestRate', 'subJobs', 'subMForm');
	var checkNextBack = Array();
	var countCheckNextBack = 0;
	var getHeightBox = Array();
	
	var oldView = '';
	var newView = '';
	
	var checkDelayMenu = true;

	
	function showSubMenu(tabNameID, NameID){
		subCountChild = $('#'+NameID+' .content-sub-menu-rel').children().length;
		//alert(subCountChild);
		
		totalCol = Math.ceil (subCountChild/subCountRow);
		totalRow = Math.ceil(subCountChild/subColPerPage);
		
		if(totalRow > subCountRow)
		totalRow = subCountRow;
		
		//alert(totalCol);
		
		if(totalCol > subColPerPage){
			checkNextBack[NameID] = true;
			countCheckNextBack++;
		}else{
			checkNextBack[NameID] = false;
			countCheckNextBack++;
		}
		
		//set height
		//alert( NameID + '        ' + $('#'+NameID).css('height') +'   '+subBoxHeight +'   '+ totalRow +'   =   '+ ((totalRow * subBoxHeight) + (10 * totalRow) + 5));
		getHeightBox[NameID] =  ((totalRow * subBoxHeight) + (10 * totalRow) + 5) + 'px';

		
		subPositionL = Array('start');
		subPositionT = Array('start');
		
		subCountLoop = 1;
		
		if(totalCol > subColPerPage){
			while(subCountLoop <= subCountChild){
				subPositionL[subCountLoop] = ((Math.ceil (subCountLoop/subCountRow) -1) * subBoxWidth);
	
				if(subCountLoop%subCountRow == 1){
					subPositionT[subCountLoop] = (subBoxHeight * 0) + 20;
					addPosition(NameID, subCountLoop);  // add position
				}
				else if(subCountLoop%subCountRow == 2){
					subPositionT[subCountLoop] = (subBoxHeight * 1)  + 20;
					addPosition(NameID, subCountLoop);  // add position			
				}
				else {
					subPositionT[subCountLoop] = (subBoxHeight * 2) + 20;
					addPosition(NameID, subCountLoop);  // add position
				}
				
				subCountLoop++;
			}
			subCountLoop = 1;
		}else {
			while(subCountLoop <= subCountChild){
				
				subPositionT[subCountLoop] = ((Math.ceil (subCountLoop/subColPerPage) -1) * subBoxHeight) + 20;
				
				if(subCountLoop%subColPerPage == 1){
					subPositionL[subCountLoop] = subBoxWidth * 0;
					addPosition(NameID, subCountLoop);  // add position
				}
				else if (subCountLoop%subColPerPage == 2){
					subPositionL[subCountLoop] = subBoxWidth * 1;
					addPosition(NameID, subCountLoop);  // add position
				}
				else if (subCountLoop%subColPerPage == 3){
					subPositionL[subCountLoop] = subBoxWidth * 2;
					addPosition(NameID, subCountLoop);  // add position
				}
				else {
					subPositionL[subCountLoop] = subBoxWidth * 3;
					addPosition(NameID, subCountLoop);  // add position
				}
								
				subCountLoop++;
			}
			
			subCountLoop = 1;
						
		}
		
		//------------------ add event
		
		$('#'+tabNameID+'').mouseenter(function(){
			
			/*
			$('#'+NameID).css({'height': getHeightBox[NameID]});
			$('#'+NameID+' .content-sub-menu-rel').css({'height': getHeightBox[NameID]});
			
			$('#'+NameID+'').slideDown('fast', function(){
				
				if(checkNextBack[NameID]){
					checkFirstChild = $('#'+NameID+' .content-sub-menu-rel a:first-child').css('left').split('px');
			
					if(checkFirstChild[0] < 0){
						$('#'+NameID+' .btn-sub-next').fadeOut('fast');
						$('#'+NameID+' .btn-sub-back').fadeIn('fast');
					}else {
						$('#'+NameID+' .btn-sub-next').fadeIn('fast');
						$('#'+NameID+' .btn-sub-back').fadeOut('fast');
					}
					
				}

				$('#'+NameID+' .one-sub').fadeIn('fast');
			});
			*/
			
			if(checkNextBack[NameID]){
				checkFirstChild = $('#'+NameID+' .content-sub-menu-rel a:first-child').css('left').split('px');
		
				if(checkFirstChild[0] < 0){
					$('#'+NameID+' .btn-sub-next').fadeOut('fast');
					$('#'+NameID+' .btn-sub-back').fadeIn('fast');
				}else {
					$('#'+NameID+' .btn-sub-next').fadeIn('fast');
					$('#'+NameID+' .btn-sub-back').fadeOut('fast');
				}
			}

			$('#'+NameID).css({'height': getHeightBox[NameID]});
			$('#'+NameID+' .content-sub-menu-rel').css({'height': getHeightBox[NameID]});
			$('#'+NameID+'').css({'display': 'block'});
			//$('#'+NameID+' .one-sub').css({'display': 'block'});
			$('#'+NameID+' .one-sub').fadeIn('fast');
			
			
		}).mouseleave(function(){
			/*
			if(checkNextBack[NameID]){
				$('#'+NameID+' .btn-sub-back').fadeOut('fast');
				$('#'+NameID+' .btn-sub-next').fadeOut('fast');
			}

			$('#'+NameID+' .one-sub').fadeOut('fast', function(){
				$('#'+NameID+'').slideUp('fast');
			});
			*/
			
			if(checkNextBack[NameID]){
				$('#'+NameID+' .btn-sub-back').fadeOut('fast');
				$('#'+NameID+' .btn-sub-next').fadeOut('fast');
			}

			$('#'+NameID+' .one-sub').css({'display': 'none'});
			$('#'+NameID+'').css({'display': 'none'});
		});

		
		//------------------ add back next
		
		$('#'+NameID+' .btn-sub-back').click(function(){
			if(checkDelayMenu){
				checkDelayMenu = false;
				checkFirstChild = $('#'+NameID+' .content-sub-menu-rel a:first-child').css('left').split('px');
				
				if(checkFirstChild[0] < 0)
				$('#'+NameID+' .one-sub').animate({'left' : '+='+(subBoxWidth * subColPerPage)}, {duration: 1000, easing: 'easeInOutExpo'});
	
				checkShowBTN((parseInt(checkFirstChild[0]) + (subBoxWidth * subColPerPage)), NameID);
			}
		});
		
		$('#'+NameID+' .btn-sub-next').click(function(){
			if(checkDelayMenu){
				checkDelayMenu = false;
				subCountChild = $('#'+NameID+' .content-sub-menu-rel').children().length;
				totalCol = Math.ceil (subCountChild/subCountRow);
				checkFirstChild = $('#'+NameID+' .content-sub-menu-rel a:first-child').css('left').split('px');
				
				totalColWidth = checkFirstChild[0] * totalCol;
				
				if(checkFirstChild[0] >= (-totalColWidth) && checkFirstChild[0] <= 0 && totalCol> subColPerPage)
				$('#'+NameID+' .one-sub').animate({'left' : '-='+(subBoxWidth * subColPerPage)}, {duration: 1000, easing: 'easeInOutExpo'});
				
				checkShowBTN((parseInt(checkFirstChild[0]) - (subBoxWidth * subColPerPage)), NameID);
			}
		});
			
	}
	
	function addPosition(focusNameID, childIndex){
		$('#'+focusNameID+' .content-sub-menu-rel a:nth-child('+childIndex+')').css({'left' : subPositionL[childIndex], 'top' : subPositionT[childIndex]});
	}
	
	function checkShowBTN(XChild, NameID){
		setTimeout(function(){
			//alert(XChild +' / '+NameID);
			if(XChild < 0){
				$('#'+NameID+' .btn-sub-next').fadeOut('fast');
				$('#'+NameID+' .btn-sub-back').fadeIn('fast');
			}else {
				$('#'+NameID+' .btn-sub-next').fadeIn('fast');
				$('#'+NameID+' .btn-sub-back').fadeOut('fast');
			}
			checkDelayMenu = true;
		}, 1000);
	}
		
	//------------------------------------------------ set function ------- showSubMenu(id tab, id content tab)
	
	
	showSubMenu('AboutUs', 'subAboutUs');
	showSubMenu('Services', 'subServices');
	showSubMenu('Announce', 'subAnnounce');
	showSubMenu('NewsRoom', 'subNewsRoom');
	showSubMenu('ExchangeRate', 'subExchangeRate');
	showSubMenu('InterestRate', 'subInterestRate');	
	showSubMenu('Jobs', 'subJobs');
	showSubMenu('MForm', 'subMForm');
		
});
