var proName = top.window.location.pathname.split("/")[1],isfullscreen = false, sysUrl = "http://"+window.location.host+"/"+proName , detailWindow , datenow = new Date(),MainPage;
top.WebRtcServerConn = null;
$(function(){
	MainPage = $.MAINPAGE();
	MainPage.loadSystemLogoTitle();
	MainPage.initCurrUserInfo();
	MainPage.initSysMenu();
	MainPage.initTabEvents();
	MainPage.loadMapConfig();
	MainPage.getCustomName();
	perHover();
	window.WebRtcServer && WebRtcServer.connectServer();
});

function perHover(){
	$("li.pull-down" ,"#north").hover(function () {
		   $(this).find(".dropdown-list").show()
		}, function () {
		    $(this).find(".dropdown-list").hide()
		});
}
/*修改密码触发函数*/
function changePassword(){
	var id='win_changePassword';
	if($('#'+id).length==0){
		$('body').append('<div id="'+id+'"><iframe frameborder="no" width="100%" height="100%" style="border:none;"></iframe></div>')
		$('#'+id).dialog({
			title:'修改密码',
			width:300,
			height:220,
			modal:true,
			buttons:[{text:'确定',handler:function(){
				savePassword(id);
			}},
	         {text:'取消',handler:function(){
	        	 $('#'+id).dialog('close');
	         }}
			]
		})
		$('#'+id).children('iframe').attr('src','changePassword.jsp');
	}else{
		var frame=$('#'+id).children('iframe')[0];
		var win=frame.contentWindow||frame.window;
		var jq=win.$;
		jq('.easyui-textbox').each(function(index,item){
			jq(item).textbox('setValue','');
		})		
		$('#'+id).dialog('open');
	}	
}
/*确认修改密码函数*/
function savePassword(id){
	var frame=$('#'+id).children('iframe')[0];
	var win=frame.contentWindow||frame.window;
	var jq=win.$;
	var textboxs=jq('.textbox');
	var oldPassword= textboxs.eq(0).children(0).val();
	  oldPassword=$.trim(oldPassword);
	  if(oldPassword==""||oldPassword==null){
		  $.dlg.alertError("旧密码不可以为空或者空字符串!");
		  return ;
	  }
	  var password = textboxs.eq(1).children(1).val();
	  var passwordconfirm = textboxs.eq(2).children(1).val();
	  password=$.trim(password);
	  passwordconfirm=$.trim(passwordconfirm);	 				  
	  if(password==""||password==null){
		  $.dlg.alertError("新密码不可以为空或者空字符串!");
		  return ;
	  }
	  if(password.length<4){
		  $.dlg.alertError("新密码必须大于等于4位!");
	    return ;
	  }
	  if(password==oldPassword){
		  $.dlg.alertError("新旧密码一致!");
	    return;
	  }
	  if(password!=passwordconfirm){
		  $.dlg.alertError("请保持新密码一致!");
	    return;
	  }
	  $.ajax({
			type:'post',
	        async:false ,
	        data:{oldPasswd:oldPassword,newPasswd:password},
//	        url : 'vpuserAction!updatePassword.do',
	        url : 'loginAction!updateUserPassword.do',
	        success : function(result){
	        	if(result=='success'){
	        		$.dlg.alertInfo("密码更变成功,请重新登录！");
	        		setTimeout(function(){
	        			$('#'+id).dialog('close');
	        			location.href = top.MainPage.proUrl;
	        		},1000)
	        	}
	        	else if(result=='errorMm'){
	 	           $.dlg.alertError("原密码错误!");
	 	        }else if(result=='expiry'){
		    	   $.dlg.alertError("登陆过期!");
			    } else {
	        		$.dlg.alertError(result);
	        	}
	        } 
	  	});
}
//用户登出
function logOut(){
	top.$.dlg.confirm('确认要退出吗？',function(){ 
		$.ajax({
			type:'post',
	        async:false ,
	        url : 'loginAction!logOut.do',
	        success : function(rtn){
	        	console.log(rtn)
	        	if(rtn){
	        		location.href = top.MainPage.proUrl;
	        	}
	        } 
	  	});
		//destroyOCX();
	});
}

function requestFullScreen(element) {
    if (element){
        var docElm = document.documentElement;
        if(element.webkitRequestFullScreen) {
        	if(!isfullscreen){
        		element.webkitRequestFullScreen.call(element);
        		isfullscreen= !0 ;
        		
        	}else{
        		 document.webkitCancelFullScreen();
        		 isfullscreen= !1 ;
        	}
        }
    }
    $("#fullscreen").text(!!isfullscreen ? "退出全屏" : "全屏");
}

function closeNow(){
	var tab=$('#commframe').tabs('getSelected');
	var index=$('#commframe').tabs('getTabIndex',tab);
	var indexNow=$('#mainmenu').data("tabTitle");
    $('#commframe').tabs('close',indexNow);//关闭对应index的tabs  
    if(indexNow>=index){
    	$('#commframe').tabs('select',index);
    }else{
    	$('#commframe').tabs('select',index-1);
    }
}

function closeOthers(){ 
    var tabAll=$('#commframe').tabs('tabs'); 
    var indexNow = $('#mainmenu').data("tabTitle");//获取当前选中tabs的index 
    for(var i=tabAll.length - 1;i>=0;i--){
    	var index = $('#commframe').tabs('getTabIndex',tabAll[i]);
    	if(index != indexNow){
    		$('#commframe').tabs('close',index);
    	}
    }
}

function closeRightAll(){
    var tabAll=$('#commframe').tabs('tabs');
    var indexNow = $('#mainmenu').data("tabTitle");//获取当前选中tabs的index 
    for(var i=tabAll.length - 1;i>=0;i--){
    	var index = $('#commframe').tabs('getTabIndex',tabAll[i]);
    	if(index > indexNow){
    		$('#commframe').tabs('close',index);
    	}
    }
    $('#commframe').tabs('select',indexNow);
}

function closeLeftAll(){
    var tabAll=$('#commframe').tabs('tabs');
    var indexNow = $('#mainmenu').data("tabTitle");//获取当前选中tabs的index 
    for(var i=tabAll.length - 1;i>=0;i--){
    	var index = $('#commframe').tabs('getTabIndex',tabAll[i]);
    	if(index < indexNow){
    		$('#commframe').tabs('close',index);
    	}
    }
    $('#commframe').tabs('select',0);
}