var debug = false;
jQuery.extend({
    // 弹出层 *** *** webeminer *** ***
     poping: function(opts,pops,titleinfo,maininfo,timedelay,callbackfunc){//opts：$.extend($.blockui.defaults.pageMessageCSS,_opts); 最外层div的css属性
	 //pops 为div.title和div.main设置css属性，包含两个元素title和main
 //    var path  = $("script[src*=jquery.eminer.js]").attr("src").replace("/js/jquery.js","");//利用此方法获得url的path
      // var screen=e=winSize();
		var success;
		var menuObjposition='',objwidth=500;
		
		
		/* fixed在iframe里老是出现问题
		if(BROWSER.ie && BROWSER.ie < 7) {
			menuObjposition = 'absolute';
			emtop = (emclientHeight - objheight) / 2 + emscrollTop;
		} else {
			emtop = (emclientHeight - objheight) / 2 ;
			menuObjposition = 'fixed';
		}*/
		menuObjposition = 'absolute';
		var _opts = {position:menuObjposition,width:objwidth+"px",'margin-left':'0px','margin-top':'0px',textAlign:'left','border-style':'solid', 'border-width':'2px', 'border-color':'#999','height':'auto'};
        //居中时可设置margin-left为width的一半
		var _pops = {title:{},main:{}};

		if(opts.overlayCSS){
			 $.extend($.blockui.defaults.overlayCSS,opts.overlayCSS);
			 delete opts.overlayCSS;
		}
        $.extend(_opts,opts);//$.extend(target, prop1, propN)
        $.extend(_pops,pops);
        $.extend($.blockui.defaults.overlayCSS,{backgroundColor:'#000',opacity:'0.6',cursor:'default'});
        $.extend($.blockui.defaults.pageMessageCSS,_opts);
		_titleinfo=titleinfo||'Loading...';
		_maininfo=maininfo||('<img class="os" src="images/loading.gif" />正在操作中...');
		var restormaxbutt ='';
		if(_opts.restormaxbutt)
		restormaxbutt = '<a href="javascript:void(0);" class="close"><img class="wicon" onclick="emwchange(this)" src="images/emwmax.png"></a>;'
		
		$.blockui('<div id="blockuititle" class="title"><span id="poptitleinfo">'+_titleinfo+'</span><a href="javascript:void(0);" onclick="$.unblockui();"  class="close">×</a>'+restormaxbutt+'</div><div id="popmaininfo" class="main">'+_maininfo+'</div>',{cursor:'default'});
        $("div.blockMsg .title").css(_pops.title);
        $("div.blockMsg .main").css(_pops.main);
       
		if(timedelay)setTimeout( function() {
			if(callbackfunc)callbackfunc();
			$.unblockui();return false;							
		},  timedelay);

		var drag=true;
		if(drag) {
			var menuObj = $2("poping");
			var titleObj = $2("blockuititle");
			titleObj.style.cursor = 'move';
			titleObj.onmousedown = function(event) {
			dragMenu(menuObj, event, 1);
			};
		}
        /*
        $("div[class=blockui],div#poping .close").click(function(){
            $.unblockui();return false;
        });
        */
    },
    // 使用post方法提交，并更新弹出层 *** *** webeminer *** ***
    posts:function(options,params){//options如为'object'，则params没用，如是'string',则为url，params为要发送的数据
        if (typeof options == 'object'){
            var _options = options||{};
        } else {
            var _params = params||{};
            var _options = {url:options,params:_params,opts:{},pops:{}};//params为要发送的数据  即：待发送的 Key/value 参数  ////opts:{},pops:{}为弹出层设置css属性  ，opts：$.extend($.blockui.defaults.pageMessageCSS,_opts);      pops 为div.title和div.main设置css属性
        }
        $.poping(_options.opts,_options.pops);
        $.post(_options.url,_options.params,function(data){
            if (debug){alert(data);}
            $.updatepop(data);
        });
    },
    // 使用get方法提交，并更新弹出层 *** *** webeminer *** ***
    loading: function(options,params){
        if (typeof options == 'object'){
            var _options = options||{};
        } else {
            var _params = params||{};
            var _options = {url:options,params:_params,opts:{},pops:{}};
        }
        $.poping(_options.opts,_options.pops);
        $.get(_options.url,_options.params,function(data){
            if (debug){alert(data);};
            $.updatepop(data);
        });
    },
    // 更新弹出层 *** *** webeminer *** ***
    updatepop: function(data){
        if ((typeof data) == 'string'){
            data = $.parseJSON(data);
        }
        $("div#poping .title span").html(data.title);
        $("div#poping .main").html(data.main);
        $("div#poping .close").click(function(){
            $.unblockui();return false;
        });
		if(data.url){
			setTimeout( function() {location.href=data.url},  3000);
		}
		if(!data.notclose){
			setTimeout( function() {$.unblockui();},  3000);
		}
    },
	submitajax:function(options,params,opts,pops,titleinfo,maininfo,suc_callbackfunc){//options如为'object'，则params没用，如是'string',则为url，params为要发送的数据
	 	if (typeof options == 'object'){
            var _options = options||{};
            
        } else {
            var _params = params||{};
            var _options = {url:options,params:_params,opts:{},pops:{}};
        }
        var _opts = opts||{};
		var _pops = pops||{};
		if(_options.opts) $.extend(_opts,_options.opts);
		if(_options.pops) $.extend(_pops,_options.pops);
		var _titleinfo = titleinfo||'';
		_titleinfo=_titleinfo||_options.titleinfo;
		_titleinfo=_titleinfo||'提交中...';
		var _maininfo = maininfo||'';
		_maininfo=_maininfo||_options.maininfo;
		$.poping(_opts,_pops,_titleinfo,_maininfo);
		$.ajax({
			type: "POST",
			url: _options.url,
			timeout: 9000000,//刘改 单位 毫秒
			cache: false,
			timedelay: _options.timedelay?_options.timedelay:2500,
			data: typeof(_options.params)=="undefined"?"":_options.params,
			dataType: "json" ,
			success: function(data){			
				if(data.status=='error'){
					$("div#poping .title span#poptitleinfo").html(data.title||'操作失败');
					$("div#poping .main").html(data.info||'操作失败，请关闭本窗口返回检查原因！');
					if(data.info && data.info.indexOf('|')>0){
						var infos=data.info.split('|');
						if(infos){
							$("div#popmaininfo").html(infos[0]);
							$('#'+infos[1]+'_tipBox div').hide();
							$('#'+infos[1]+'_tipBox div.errorTip').show().html(infos[0]);
							$("input[name='"+infos[1]+"']").focus();
						}
					}
				}else
				if(data.status=='success'){
					var success=true,$errinfo='';					
					if(!success){
						$errinfo='但未能同步本地数据更新，请返回检查原因！';
						$("div#poping .title span#poptitleinfo").html(data.title||'操作故障');
						$("div#poping .main").html(data.info||$errinfo);
					}else{
						if (suc_callbackfunc) success=suc_callbackfunc(data);
						$("div#poping .title span#poptitleinfo").html(data.title||'操作成功');
						$("div#poping .main").html(data.info||'操作成功，请关闭本窗口！');
						
					}
					
				}
				setTimeout( function() {
							 $.unblockui();
							 if(data.data && data.data=='reload') document.location.reload();
							 if(data.url) location.href=data.url;
							else if(data.data && typeof data.data=='string'&& data.data.indexOf('.php')>0) location.href=data.data;
						},  data.timedelay?data.timedelay:this.timedelay);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$("span#poptitleinfo").html("出现故障");
				$("div#poping .main").html("服务器或网络故障，请返回检查原因再试！"+textStatus);
				setTimeout( function() {
					 $.unblockui();
				}, this.timedelay);
			}
		});	
    }
});

$(function(){
    //loadImage(path()+'/system/images/os/dir0.gif',path()+'/system/images/os/dir1.gif',path()+'/system/images/loading.gif');
    // td变色
    $('.main tr,.lz_table tr').hover(function(){
        $(this).addClass('selected');
    },function(){
        $(this).removeClass('selected');
    });
   
    // 在每个li上面绑定haver事件，鼠标触发下拉菜单
    $(".diymenu li,.logo").each(function(i){
        $(this).hover(function(){
            $('ul',this).show();
        },function(){
            $('ul',this).hide();
        });
    });
   
   
});

//异步的jsonp
var monor_jsonp= function (){
	this.timedelay=5000;//单位：毫秒
	this.trycount=1000;//刘：临时，应改成10 10*200=2000毫秒
	this.default_suctitle='操作成功';
	this.default_errtitle='操作失败';
	this.default_errmaininfo='已成功远程操作，但未能通过本地验证，请返回检查原因！';
	this.default_sucmaininfo='操作成功，请关闭本窗口！';


	this.localloginfinish=false;
	this.sh=false;//setinterval触发变量
	this.senddata={};
	this.url='';
	this.num=0;
	this.now = (new Date()).getTime();
	this.do_locallogin=function(remotedata) {
		$.getJSON(this.url,this.senddata,this.callbackfunc);
		var owner=this;
		this.sh=setInterval(function(){
			owner.lasthandle(owner);},200);
		
	};

	this.lasthandle=function(owner) {
		owner.num++;
		if(typeof(this.localloginfinish) != "undefined" && this.localloginfinish==true) {
			clearInterval(owner.sh);
		}else{
			if(owner.num>owner.trycount){
				clearInterval(owner.sh);
				owner.err_handle();
			}
		}
	};
	
	
	this.err_handle=function(data) {
		if(!data)data={};
		$("div#poping .title span#poptitleinfo").html(data.title||this.default_errtitle);
		if(!data.info ) $("div#poping .main").html(this.default_errmaininfo);
		else{
			var infos=data.info.split('|');
			$("div#poping .main").html(infos[0]);
			if(infos[1]){	
				//$("label[for='"+infos[1]+"']").html(infos[0]);
				//$("input[name='"+infos[1]+"']").focus();
				$('#'+infos[1]+'_tipBox div').hide();
				$('#'+infos[1]+'_tipBox div.errorTip').show().html(infos[0]);
				$("input[name='"+infos[1]+"']").focus();
			}
		}
		setTimeout( function() {
			 $.unblockui();
		},  this.timedelay);
	};	
}
function BUIe1434c60dCallback(status,info,data,url){
	var newdata={};
	if(data='alert')alert(info);
	else{
		newdata['info'] = info;
		newdata['status'] = status;
		newdata['title'] = status=='success'?'操作成功':'操作失败';
		newdata['data'] = data;
		//if(data && $("input.nid").length>0)$("input.nid").val(data);
		newdata['url'] = url;
		$.updatepop(newdata);
	}
}
//要求三个选项卡对应的区域的id分别为'#handlearea0,#handlearea1,#handlearea2'
function showTab(index){ 
    $("ul.menu li").attr("class",""); 
	$("ul.menu li:eq("+index+")").attr("class","selected");
	$('#handlearea0,#handlearea1,#handlearea2').hide();
	$('#handlearea'+index).show();
	var i=0;
	$("ul.menu a").each(function(){
		$(this).attr("href","javascript:showTab("+i+");");
		i++;
	});
	$("ul.menu li:eq("+index+") a").attr("href","javascript:void(0);");
}

function addconode()
{
	
	var maininfo=$("div#addnodequery").html();
	$.poping({height:'auto',width:'580px'},{},'添加采集节点',maininfo);
}

function IsNum(input,type)
{        
	if(!type)type=0;
	var num= /^[0-9]+.?[0-9]*$/;  //判断正整数 /^[1-9]+[0-9]*]*$/   

    if((type && input.value==='') || !num.test(input.value))
    {
       alert("请在该输入框中输入整数数字");
       $(input).focus();
       return false;
    }
    return true;
}
