function killerror()
{
	return true;	
}
window.onerror = killerror;
jQuery.extend({
	sendForm: function(form,callback,type)
	{
		var type = type || 'json';
		IsPost(form.id,true);
		var data = $(form).serialize() + '&ajax=1';
		$(":submit").attr('disabled',true);
		$.ajax({
			type: "POST",
			url: form.action,
			data: data,
			async: true,
			dataType : type,
			success: function(r){
				if(r.run)
				{
					MsgBox(r.title,r.message);
					return false;
				}
				if(callback) callback(r);
			},
			complete: function()
			{
				$(":submit").attr('disabled',false);
				IsPost(form.id,false);
			},
			error : function()
			{
				messageBox2('未知错误',5000);
			}
		});
	},
	loadbox : function(o)
	{
		if(!document.body) return false;
		var zIndex = o.zIndex || 1;
		var bgId = o.bgId || 'bgId';
		var boxId = o.boxid || 'boxId';
		try{
			document.body.removeChild(document.getElementById(bgId));
			document.body.removeChild(document.getElementById(boxId));
			//document.body.onselectstart = function(){return true;}
			document.body.oncontextmenu = function(){return true;}
		}catch(e){}
		
		var bg = document.createElement('div');
		bg.id = bgId;
		$(bg).attr("class","pop_bg");
		$(bg).css('width',$('body').width()+'px');
		$(bg).css('height',$(document).height()+'px');
		$(bg).css('zIndex',zIndex++);
		$('body').append(bg);
		
		var AlertFrame = document.createElement('div');
		AlertFrame.id = boxId;
		$(AlertFrame).css('position','absolute');
		
		
		$(AlertFrame).css('width','168px');
		$(AlertFrame).css('height','55px');
		
		
		$(AlertFrame).css('zIndex',++zIndex);
		$('body').append(AlertFrame);
		$(AlertFrame).html('<div class="pop_whtbg fl"><div class="wht_bord"><img align="absmiddle" src="/ui/css/home_img/loading.gif"> 数据加载中, 请稍候...</div></div>');
		$(AlertFrame).css('left',($('body').width()-160)/2 + 'px');
		$(AlertFrame).css('top',($(window).height()-55)/2 + $(window).scrollTop() + 'px');

		if(true === o.iframe)
		{
			var iframe = document.createElement('iframe');
			$(iframe).attr('class','iframe');
			$(iframe).attr('frameborder',0);
			$(iframe).attr('scrolling','no');
			$(iframe).attr('allowtransparency',true);
			$(iframe).attr('src',o.url);
			$(AlertFrame).html('');
			$(AlertFrame).css('width',o.width);
			$(AlertFrame).css('height',o.height);
			$(AlertFrame).css('left',($('body').width()-parseInt(o.width))/2 + 'px');
			$(AlertFrame).css('top',($(window).height()-parseInt(o.height))/2 + $(window).scrollTop() + 'px');
			$(AlertFrame).append(iframe);
		}else if(o.html){
			$(AlertFrame).html('');
			$(AlertFrame).css('width',o.width);
			$(AlertFrame).css('height',o.height);
			$(AlertFrame).css('left',($('body').width()-parseInt(o.width))/2 + 'px');
			$(AlertFrame).css('top',($(window).height()-parseInt(o.height))/2 + $(window).scrollTop() + 'px');
			$(AlertFrame).html(o.html);
		}else{
			
			$.ajax({
				type: "get",
				url: o.url,
				data: {'ajax':1},
				async: true,
				dataType : 'text',
				success: function(serverData){
					try{
						var r  = eval('('+serverData+')');
						if(r.message.trim())
						{
							MsgBox(r.title,r.message);
							$.unload();
							return false;
						}
					}catch(e){
						$(AlertFrame).css('width',o.width);
						$(AlertFrame).css('height',o.height);
						$(AlertFrame).html('');
						$(AlertFrame).css('left',($('body').width()-parseInt(o.width))/2 + 'px');
						$(AlertFrame).css('top',($(window).height()-parseInt(o.height))/2 + $(window).scrollTop() + 'px');
						$(AlertFrame).html(serverData);
					}
				}
			});
		}
		hide();
		
		$(bg).click(function(){
				$('#'+boxId+' .pop_title').css('background','#D3DBE8');
				setTimeout(function(){$('#'+boxId + ' .pop_title').css('background','#E0EFF3');},500);
		});
		
		$(window).resize(function(){
  			$(bg).css('width',$('body').width()+'px');
			$(bg).css('height',$(document).height()+'px');
		});
		//document.body.onselectstart = function(){return false;}
		document.body.oncontextmenu = function(){return false;}
	},
  unload : function(sh,bgId,boxId)
  {
		var bgId = bgId || 'bgId';
		var boxId = boxId || 'boxId';
		sh = sh == false ? sh : true;
		document.body.removeChild(document.getElementById(bgId));
		document.body.removeChild(document.getElementById(boxId));
		//document.body.onselectstart = function(){return true;}
		document.body.oncontextmenu = function(){return true;}
		if(sh){show();};
		$(window).unbind( "resize" );
  }
})

function getMouseX(e)
{
	return e.clientX + getScrollX() - document.documentElement.clientLeft - document.body.clientLeft || e.pageX || 0;
}
function getMouseY(e)
{
	return e.clientY + getScrollY() - document.documentElement.clientTop - document.body.clientTop || e.pageY || 0;
}
var dBody = null;
function getBody()
{
if (!dBody)
	dBody = (document.compatMode && document.compatMode.indexOf('CSS') > -1) ? document.documentElement : document.body;
	return dBody;
}
function getScrollX()
{
	return window.pageXOffset || window.scrollX || getBody().scrollLeft || 0;
}
function getScrollY()
{
	return window.pageYOffset || window.scrollY || getBody().scrollTop || 0;
}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
};
   
String.prototype.width = function()          
{                 
	return this.replace(/[^\x00-\xff]/g, "**").length;  
};

String.prototype.subStrWidth = function(n, sp)
{
	if (n >= this.width())
	{
		return this;
	}
	if (sp)
	{
		n -= sp.width();
	}
	var str = this.substr(0, n);
	var temp = n;
	while (str.width() > n)
	{
		temp --;
		str = str.substr(0, temp);
	}
	str += (sp) ? sp : "";
	return str;
};

function Login(form)
{
	if(userIsLogin) return false;
	$.loadbox({'url':'/public/main/login','width':'352px','height':'250px'});
	return false;
}
var messageBoxState = '';
function messageBox(msg, time, cname)
{
	if(!messageBoxState)
	{
		messageBoxState = document.createElement('div');
		$(messageBoxState).addClass('set_pw4 pop');
	}
	time = time ? time : 2000;
	var tt = time / 1000;
	cname = cname ? cname : 'icon_ok';

	var html = '<div class="blue_boxbg"><div class="blue_box_c"><div class="pad10"><p class="tip_fcon"><input type="button" onfocus="this.blur()" class="icon_prompt '+cname+' fl" /><i>'+msg+'</i><span class="icon_btnx"><a href="javascript:void(0);" onclick="$(messageBoxState).slideUp(\'fast\');clearTimeout(t);show();return false;"></a></span></p><p align="right">本提示 '+tt+'秒 后自动关闭</p></div></div></div>';
	
	$(messageBoxState).html(html);
	hide();
	$('body').append(messageBoxState);
	$(messageBoxState).css('left',($('body').width()-310)/2 + 'px');
	$(messageBoxState).css('top',($(window).height()-100)/2 + $(window).scrollTop() + 'px');
	$(messageBoxState).show('fast',function(){t = setTimeout("$(messageBoxState).slideUp('slow');show();",time)})
}

var messageBox2State = '';
var messageBox2StateShow = false;
function messageBox2(msg, time, cname, show)
{
	if(!messageBox2State)
	{
		messageBox2State = document.createElement('div');	
		$(messageBox2State).addClass('set_pw4 pop');
	}
	cname = cname ? cname : 'icon_x';

	messageBox2StateShow = !show ? false : true;
	time = time ? time : 2000;
	var tt = time / 1000;
	var html = '<div class="yellow_boxbg"><div class="yellow_box_c"><div class="pad10"><p class="tip_fcon"><input type="button" onfocus="this.blur()" class="icon_prompt '+cname+'" /><i class="red_f">'+msg+'</i><span class="icon_btnx"><a href="javascript:void(0);" onclick="$(messageBox2State).slideUp(\'fast\');clearTimeout(t);if(!messageBox2StateShow){show()};return false;"></a></span></p><p align="right">本提示 '+tt+'秒 后自动关闭</p></div></div></div>';
	
	$(messageBox2State).html(html);
	hide();
	$('body').append(messageBox2State);
	$(messageBox2State).css('left',($('body').width()-310)/2 + 'px');
	$(messageBox2State).css('top',($(window).height()-100)/2 + $(window).scrollTop() + 'px');
	$(messageBox2State).show('fast',function(){t = setTimeout("$(messageBox2State).slideUp('slow');if(!messageBox2StateShow){show();}",time)})
}

var msg;
function MsgBox(title,message,icon)
{
	if(!msg)
	{
		msg = document.createElement('div');
		$(msg).addClass('pop');
		$(msg).css('position','absolute');
		$(msg).css('display','none');
	}
	hide();
	var html = '<div class="set_pw3 pop_boxbg"><div class="pop_bord"><div class="pop_title"><input type="button" onfocus="this.blur()" class="icon_prompt icon_i" />系统提示<span class="icon_btnx"><a href="javascript:void(0)" onclick="$.unload(false,\'MsgBoxBgId\',\'MsgBoxId\');return false;"></a></span></div><div class="pad20"><div class="pad_b20">'+message+'</div><p align="center" class="pad_b10"><input type="button" value=" 知道了 " onclick="return $.unload(false,\'MsgBoxBgId\',\'MsgBoxId\')" class="btn_2" /></p></div></div></div>';
	
	$.loadbox({'html':html,'width':'305px','height':'140px','bgId':'MsgBoxBgId','boxid':'MsgBoxId','zIndex':9999});
}
function hide(){if($.browser.msie && $.browser.version == '6.0')$('select').css('visibility','hidden');}
function show(){if($.browser.msie && $.browser.version == '6.0')$('select').css('visibility','visible');}


COOKIE = {
"get" : function(name)
{
	var rs = new RegExp("(^|)" + name + "=([^;]*)(;|$)", "gi").exec(document.cookie), tmp;
	if(tmp = rs)
	{
		return unescape(tmp[2]);
	}
	return false;
},

"set" : function(name, value)
{
	var expdate = new Date();
	var argv = arguments;
	var argc = arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : "/";
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if (null != expires)
	{
		expdate.setTime(expdate.getTime() + (expires * 1000));
	}
	document.cookie = name + "=" + escape(value)
	+ ((expires == null) ? "" : (";expires=" + expdate.toGMTString()))
	+ ((path == null) ? "" : (";path=" + path))
	+ ((domain == null) ? "" : (";domain=" + domain))
	+ ((secure == false) ? "" : ";secure");
},
"delete" : function(name)
{
	document.cookie = name + "=";
}
};
function doLogin(form,t)
{
	var email = form.email.value;
	var password = form.password.value;
	var regexp = /^[a-z0-9_\-\.]{1,32}@[a-z0-9_\-\.]{1,32}\.[a-z]{2,5}$/i;
	
	var id = 'login_msg';
	if(t) id = id + '_pop';

	if(!email.match(regexp))
	{
		$('#'+id).css('display','block');
		$('#'+id+' em').html('账号不正确，请填写注册时的邮箱地址');
		return false;
	}
	if(!password.match(/\S{1,32}/))
	{
		$('#'+id).css('display','block');
		$('#'+id+' em').html('密码不符合规则');
		return false;
	}
	$('#'+id).css('display','none');
	$.sendForm(form,function(r){
			if(r.state == 'ok')
			{ 
				if(r.url){ 
						location.href = r.url;
				}else{
						location.href = location.href;
				}
			}else if(r.state == 'noactive'){
				window.location.href = '/register/?step=2';
				return false;
			}else{
				$('#'+id).css('display','block');
				$('#'+id+' em').html(r.message);
				return false;	
			}
		}
	)
	return false;	
}

function initInput(){
	$("input[type=text],textarea").each(function(){
		
		if(!$(this).attr('intro')){
			return true;	
		}
		if($(this).val() == ''|| $(this).val() == $(this).attr('intro'))
		{
			$(this).val($(this).attr('intro'));
			$(this).css('color','#999');
		}else{
			$(this).css('color','#333');
		}
		$(this).unbind('focus');
		$(this).focus(function(){
			$(this).css('color','#333');
			if($(this).val() == $(this).attr('intro'))
			{
				$(this).val('');
			}
			$(this).select();
		})
		$(this).unbind('blur');
		$(this).blur(function(){
			if($(this).val() == '')
			{
				$(this).val($(this).attr('intro'));
				$(this).css('color','#999');
			}else{
				$(this).css('color','#333');
			}
		})
	});
}

var mytitle = null;
function initMytitle()
{
	if(!mytitle)
	{
		mytitle = document.createElement('div');
		mytitle.id = 'myTitle';
		mytitle.className = 't_msg';
		$(mytitle).html('<p class="t_list"></p>');
		document.body.appendChild(mytitle);
	}
	$('a,image,input[type=text]').each(function(){
			var info = $(this).attr('mytitle');
			if(!info){
				return true;
			}

			$(this).bind("mouseover", function(e){
			 	if($(this).attr('title'))  $(this).attr('title','');
				var left = getMouseX(e);
				var top = getMouseY(e) + 20 + getScrollY();
				$(mytitle).css('left',left);
				$(mytitle).css('top',top);
				$('#myTitle > p').html(info);
				$(mytitle).css('display','block');
			});
			$(this).bind("mousemove",function(e){
				var left = e.clientX;
				if($.browser.msie && $.browser.version == '6.0')
				{
					left = left-2;
				}
				var top = e.clientY + 20 + getScrollY();
				$('#myTitle > p').css('float','left');
				
				if($('body').width() <= left + 300){
					//left = left - $('#myTitle p').width() - 11;	
					left = left - 290;
					$('#myTitle > p').css('float','right');
				}
				
				$(mytitle).css('left',left);
				$(mytitle).css('top',top);		
				
			});
			$(this).mouseout(function(){
					$('#myTitle > p').html('');
					$(mytitle).css('display','none');
				}
			);
		}		
	);
}

function IsPost(id,state)
{
	$('#'+id+' input[type=text],textarea').each(
		function(){
				if($(this).attr('intro') && $(this).val() == $(this).attr('intro'))
				{
					$(this).attr('disabled',state);	
				}
			}											
	);
}
function number_format (number, decimals, dec_point, thousands_sep)
{
    var n = number, prec = decimals;
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);        return (Math.round(n*k)/k).toString();
    };
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
    if (abs >= 1000) {
        _ = abs.split(/\D/);        i = _[0].length % 3 || 3;
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);    } else {
        s = s.replace('.', dec);
    }
    var decPos = s.indexOf(dec);    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';    }
    return s;
}
function getFormatSize(size)
{
	if(size >= 1073741824)
	{
		return number_format(size/1073741824,2)+' GB';
	}
	if(size >= 1048576){
		return number_format(size/1048576,2)+' MB';
	}
	return number_format(size/1024,2)+' KB';	
}
function getSerialNo(n)
{
	var info = Array;
	info[1]  = '一';
	info[2]  = '二';
	info[3]  = '三';
	info[4]  = '四';
	info[5]  = '五';
	info[6]  = '六';
	info[7]  = '七';
	info[8]  = '八';
	info[9]  = '九';
	info[10]  = '十';
	info[11]  = '十一';
	info[12]  = '十二';
	info[13]  = '十三';
	info[14]  = '十四';
	info[15]  = '十五';
	info[16]  = '十六';
	info[17]  = '十七';
	info[18]  = '十八';
	info[19]  = '十九';
	info[20]  = '二十';
	
	return info[n];	
}
function AddFavorite(url,title)
{
    try
    {
        window.external.AddFavorite(url, title);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(title, url, "");
        }
        catch (e)
        {
            alert("加入收藏失败，请使用Ctrl+D添加");
        }
    }
	return false;
}
function copyToClipboard(txt) {

	if(window.clipboardData) {
		window.clipboardData.clearData();       
		window.clipboardData.setData("Text", txt);  
	} else if(navigator.userAgent.indexOf("Opera") != -1) 
	{       
		window.location = txt;   
	} else if (window.netscape) {       
		try {       
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");       
		} catch (e) {       
			alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");       
		}       
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);       
		if (!clip)       
		   return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);       
		if (!trans)       
		   return;       
		trans.addDataFlavor('text/unicode');       
		var str = new Object();       
		var len = new Object();       
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);       
		var copytext = txt;       
		str.data = copytext;       
		trans.setTransferData("text/unicode",str,copytext.length*2);       
		var clipid = Components.interfaces.nsIClipboard;       
		if (!clip)       
		   return false;       
		clip.setData(trans,null,clipid.kGlobalClipboard);       
	}  
	return true;
}

function hover(selector,classname){
	$(selector).hover(
	  function () {
		$(this).addClass(classname);
	  },
	  function () {
		$(this).removeClass(classname);
	  }
	);	
}

function pop_Upload(directory,type)
{
	if(!userIsLogin) return Login();
	var action = 'flashupload';
	if(type) action = 'generalupload';
	$.loadbox({'url':'/public/main/'+action+'?directory='+directory,'width':'682px','height':'583px'});
	return false;
}


