$(document).ready(function(){
     
    $("#RightNavigation").mouseout(function(){
	$('#move').stop();
    }).mouseover(function(){
	$('#move').stop();
	moveLeft('jsMenu', 'move' );
    });

    $("#LeftNavigation").mouseout(function(){
	$('#move').stop();
    }).mouseover(function(){
	$('#move').stop();
	moveRight('jsMenu', 'move' );
    });
	
	
getTabs();
getCurMenu();
});
  
  // set width elments menu
	function getWight(ell)
	{
		var widthMenu=0;
		$('#'+ell+' .block').each(function(){
			widthMenu+=$(this).width();
			widthMenu+=1; //  margin-left:5px; 			
		});
		return widthMenu;
	}
	
	// move left
	function moveLeft(ellMain, ellMove)
	{		
		if ( $('#'+ellMain).width()>=getWight(ellMove) && getMoveValueLeft(ellMain, ellMove) != 0)
		{			
			$('#'+ellMove).stop();
		}
		else
		{	
      		$('#'+ellMove).animate({"left": "-="+getMoveValueRight(ellMain, ellMove)+"px"}, 1500);
		}
		
	}
	
	// move right
	function moveRight(ellMain, ellMove)
	{		
		if ( $('#'+ellMain).width()>=getWight(ellMove)  && getMoveValueRight(ellMain, ellMove) != 0)
		{	
			$('#'+ellMove).stop();
		}
		else
		{	
      		$('#'+ellMove).animate({"left": "+="+getMoveValueLeft(ellMain, ellMove)+"px"}, 1500);
		}
		
	}
	
	// get left movie value
	function getMoveValueLeft(ellMain, ellMove)
	{
	  	var mode=$('#'+ellMove).offset().left-$('#'+ellMain).offset().left;
		if (mode<0)
		{
			mode*=-1;			
			return mode;
		}
		else
		{
			return 0;
		}
	}
	
	//get right movie value	
	function getMoveValueRight(ellMain, ellMove)
	{
	  	var mode=$('#'+ellMove).offset().left-$('#'+ellMain).offset().left;
		if (mode<0)
		{
			mode*=-1;
		}
		
		mode=getWight('move')-mode-$('#'+ellMain).width();
		
		if (mode>0)
		{			
			return mode;
		}
		else
		{
			return 0;
		}
	}	
	function getCurMenu()
	{
			 $.ajax({
                 type: "GET",
                 url: urlMenu,
                 dataType: "xml",
                 success: function(xml) {
                     $(xml).find('root').each(function(){
                         var cur_menu = $(this).attr('cur_menu');
						 tabClickFirst(cur_menu);
                     }); 
                 }
             }); 
	}
	 
	function getCurButtom()
	{
		 $.ajax({
                 type: "GET",
                 url: urlMenu,
                 dataType: "xml",
                 success: function(xml) {
                     var cur_button = $(xml).find('root').each(function(){                    
						var cur_button = $(this).attr('cur_button');						
						 tabBoxClick(cur_button);
                     });
					  
                 }
             }); 
	}
	 
	 
	 // get tabs
	function getTabs()
	{ 
			$.ajax({
                 type: "GET",
                 url: urlMenu,
                 dataType: "xml",
                 success: function(xml) {
                     $(xml).find('mn').each(function(i){
                         var tabs = $(this).attr('label');						 
						$('<li></li>').html('<a href="javascript:tabClick(\''+i+'\');" class="rst" id="lnk'+i+'"><img src="/images/zkrTL.png"><b><img src="/images/zkrTR.png"></b>'+tabs+'</a>').appendTo('#tabs');
                     }); 
                 }
             }); 
	}
	
	// get menu Item Intabs
	function getTabMenu(items)
	{
		
	$('#move').stop();
	moveRight('jsMenu', 'move' );	
			$.ajax({
                 type: "GET",
                 url: urlMenu,
                 dataType: "xml",
                 success: function(xml) {				 
					$("#move").html("");
                     $(xml).find('mn').each(function(i){
					 if (i==items)
					 {
					 $(this).find('sub').each(function(i){
							
									var label = $(this).attr('label');
									var url = $(this).attr('url');
									var img = $(this).attr('img');														 
									$('<div class="block" id="block'+i+'"></div>').html('<a href="'+url+'" style="background-image:url('+img+')" title="'+label+'"><span>'+label+'</span></a>').appendTo('#move');						
							
							})
					 }
                     }); 
                 }
             }); 
		
	}
	
	// tabClick
	function tabClick (id)
	{
		getTabMenu(id);
		$(".rst").removeClass('activeTab');
		$("#lnk"+id).addClass('activeTab');	
	}
	
	function tabClickFirst (id)
	{
		getTabMenu(id);
		$(".rst").removeClass('activeTab');
		$("#lnk"+id).addClass('activeTab');		
        getCurButtom();
	}
	
	function tabBoxClick (id)
	{
		$("#block"+id).addClass('activeTabBox');
	}