define(function(require){
	var $ = require('jquery');
	var IFA = require('util'); 
	var contextPath = $('#contextPath').val();
	require('uriTemplate')($);
	require('validation')($);
	require('dialog')($);
	require('cookie')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/validation.css');
	$(function(){
		
		$("#loginOut").bind("click",loginOut);
		login();
		//顶部按钮显示
	function login(){
		  var url =contextPath+"/topMenu.json";
		  $.ajax({
				type:"get",
				url : url,
				success:function(response) {
			      var topmenu =response.topMenu; 
			  	   if(topmenu){
				    $.each( topmenu,function(i,authority){
					    	var a = $('<a href="#"></a>');
					    	a.attr("id",authority.code);
					    	a.attr("data",authority.alias);
					    	a.html(authority.name);
					    	a.bind("click",{id:authority.code},show);
			                $('#topmenu').append(a);
					});
				    initFirstLeftMenu();
			  	   }else{
			  		   var url = $("#loginUrl").val();
			  		   window.location.href = url;
			  	   }
				}
			});
	}	

			

	//topMenu的切换
			function show(event)
			{    
				$('#topmenu a').each(function(i,sub)
						{
					       $(sub).removeClass('topfocus');
				});
				var id = event.data.id;
				var param = {"topId":id};
				$('#'+id).addClass('topfocus');
			  	var url =contextPath+"/sideMenu.json";
				  $.ajax({
						type:"get",
						url : url,
						data:param,
						success:function(response) {
					    $('#leftMenu').empty();
						$.each(response.sideMenu,function(i,node){
							  if(node.url !='#'&& node.url !='N'){
							         var li = $('<li class ="level1" ></li>');
							         var a = $('<a href='+contextPath+node.url+' id="'+node.name+'"></a>');
							         a.html(node.name);
//							         a.bind('click',function(){
//							        	 location.replace(contextPath+node.url);
//								         });
							         a.appendTo(li);
							         $('#leftMenu').append(li);
							  }
					  		});  
				  }
				  });
				  showLeftMenu();
			  	}
			
			function showLeftMenu(){
				var sideBar	= $("#sideBarCon");
				if(sideBar.css("display")=="none"){
					var tbodyCon = $("#tbodyCon");
					var switchImage = $("#switchImage");
					sideBar.show();
					tbodyCon.css("padding-left","200px");
					switchImage.attr("src",seajs.contextPath+"/images/btn_arrow01.gif");
					document.cookie="hidebar=false; path=/";
				}
			}
			
		
		
			/**
			 * 初始化左侧菜单
			 */
			function initFirstLeftMenu(){
				//选中topMenu加上样式
				//$('#topmenu a:first-child').addClass('topfocus');
				
				var topMenuId = $("#topMenuId").val();
				if(topMenuId!=""){
					$('#topmenu a[data='+topMenuId+']').addClass('topfocus');
			    //获取第一个topmenu的id
				 	topMenu= $('#topmenu a[data='+topMenuId+']').attr("id");
				}else{
					$('#topmenu a:first-child').addClass('topfocus');	
					topMenu = $('#topmenu a:first-child').attr("id");
				}
				
			    var topMenuId = topMenu;
			 	var url =contextPath+"/sideMenu.json";
			 	var param = {"topId":topMenuId};
			  	//左侧按钮显示
				  $.ajax({
						type:"get",
						url : url,
						data:param,
						success:function(response) {
					    $('#leftMenu').empty();
						$.each(response.sideMenu,function(i,node){
						
							         var li = $('<li class ="level1" ></li>');
							         var a = $('<a href='+contextPath+node.url+' id="'+node.name+'"></a>');
							         a.html(node.name);
//							         a.bind('click',function(){
//							        	 location.href = contextPath+node.url;
//								         });
							         a.appendTo(li);
							         $('#leftMenu').append(li);
							         var menuId = $("#leftMenuId").val();
								  	 if(node.alias==menuId){
									  a.attr("class","cur");
								  	 }
					  		}); 
				  }
				  });
			}
		
		
	});
	
		
		function loginOut(){
			var url=contextPath+"/loginOut.do";
			
			window.location.href=url;
			
		}	
});