var qmeCms = window.qmeCms || {};
qmeCms.Calendar = function (obj)
{
	var o = this;
	var timer;
	var nowIs = new Date();
	var yy = nowYear = nowIs.getFullYear();
	var mm = nowMonth = nowIs.getMonth() + 1;
	var dd = nowDate = nowIs.getDate();
	o.Element = $(obj);
	if(o.Element == null) return false;
	o.Element.oldOnfocus = typeof o.Element.onfocus == 'function' ? o.Element.onfocus : function(){};
	o.Element.oldOnblur = o.Element.onblur;
	if (!o.Element.format) o.Element.format = "yy-mm-dd";

	o.Element.onfocus = function ()
	{
		var y = nowYear, m = nowMonth, d = nowDate, tt;
		var yyreg = new RegExp(this.format.replace("yy","(\\d+)").replace("mm","\\d+").replace("dd","\\d+"));
		var mmreg = new RegExp(this.format.replace("yy","\\d+").replace("mm","(\\d+)").replace("dd","\\d+"));
		var ddreg = new RegExp(this.format.replace("yy","\\d+").replace("mm","\\d+").replace("dd","(\\d+)"));
		if (tt=this.value.match(yyreg)) y = tt[1];
		if (tt=this.value.match(mmreg)) m = tt[1];
		if (tt=this.value.match(ddreg)) d = tt[1];
		o.showDayLists(y,m,d);
		var e = fetchOffset(this);
		var x = e.left;
		var y=e.top;
		o.DivObject.style.left = x + this.offsetWidth+ "px";
		o.DivObject.style.top = y + "px";
		o.DivObject.style.display = "block";
	}
	o.getDaysCount = function (yea,mon){return (new Date(yea,mon,0)).getDate();}
	o.getFirstDay = function (yea,mon){return (new Date(yea,mon-1,1)).getDay();}
	o.innerHTMLToTd = function (n,str)
	{
		var tds = o.DivObject.getElementsByTagName("td");
		tds[n+8].innerHTML = str;
		tds[n+8].onclick = (str=="&nbsp;")?null:function ()
		{
			dd = this.innerHTML.replace(/[^\d]/g,"");
			o.Element.value = o.Element.format.replace(/yy/g ,yy).replace(/mm/g ,mm).replace(/dd/g ,dd);
			o.DivObject.style.display = "none";
		};
	}
	o.showDayLists = function (yea,mon,day)
	{
		var ms = o.getDaysCount(yea,mon);	
		var fd = o.getFirstDay(yea,mon);
		mon =(""+mon).replace(/^0+/g,"");
		day = (""+day).replace(/^0+/g,"");

		var sels = o.DivObject.getElementsByTagName("select"); //年月下拉
		sels[0].value = yea;
		sels[1].value = mon;
		for(var i=0;i<42;i++) o.innerHTMLToTd(i,"&nbsp;");
		for(var i=1;i<=ms;i++){
			var TdValue = i
			if (sels[0].value==yea&&sels[1].value==mon&&day==i) TdValue = ("<b><font color=\"green\">"+i+"</font></b>")
			if (nowYear==yea&&nowMonth==mon&&nowDate==i) TdValue = ("<b><font color=\"red\">"+i+"</font></b>")
			o.innerHTMLToTd(i+fd-1,TdValue);
		}
		sels[0].onchange = function (){o.showDayLists(yy=this.value,mm);}
		sels[1].onchange = function (){o.showDayLists(yy,mm=this.value);}
	}
	o.init = function ()
	{
		var s = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"200\" height=\"161\" bgcolor=\"#ECF2FC\" style=\"padding: 2px;border: 1px solid #333333;\" >\
			<tr height=\"20\" bgcolor=\"#0A246A\">\
				<td colspan=\"7\">\
					<select style=\"width:70px;\" _DronFw_Calendar=\"true\">";
		for(var i=nowYear-40;i<=nowYear+10;i++)
		s = s.concat("<option" + (i==nowYear?" selected=\"selected\"":"") + " value=\"" +i+ "\">" +i+ "年</option>");
		s = s.concat("</select><select style=\"width:50px;\" _DronFw_Calendar=\"true\">");
		for(var i=1;i<=12;i++)
		s = s.concat("<option" +(i==nowMonth?" selected=\"selected\"":"")+ " value=\"" +i+ "\">" +i+ "月</option>");
		s = s.concat("</select>&nbsp;&nbsp;<a onclick=\"this.parentNode.parentNode.parentNode.parentNode.parentNode.style.display=\'none\'\" style=\"color:#FFFFFF;cursor: pointer;\">关闭日历</a>\
				</td>\
			</tr>\
			<tr height=\"20\" align=\"center\">");
		s = s.concat("<td bgcolor=\"#F5F2D8\" width=\"20\">日</td>");
		for(var i=1;i<7;i++)
		s = s.concat("<td width=\"20\" bgcolor=\"#ECF2FC\" >" +"日一二三四五六".charAt(i)+ "</td>");
		s = s.concat("</tr>");
		for(var i=0;i<6;i++)
		{
			s = s.concat("<tr height=\"20\" bgcolor=\"#ECF2FC\" align=\"center\" style=\"cursor: pointer;\"><td width=\"20\" bgcolor=\"#F5F2D8\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td>");
			for(var j=0;j<5;j++)
			s = s.concat("<td width=\"20\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td>");
			s = s.concat("<td width=\"20\" bgcolor=\"#ECF2FC\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td></tr>");
		}
		s = s.concat("</table>");
		o.DivObject = document.createElement("div");
		o.DivObject.style.position = "absolute";
		o.DivObject.style.display = "none";
		o.DivObject.style.zIndex = 100000;
		o.DivObject.innerHTML = s;
		document.body.insertBefore(o.DivObject, document.body.firstChild);

		$$E.addEvent(document.body, 'click', function(){
			o.DivObject.style.display = 'none';
		});
		$$E.addEvent(o.Element, 'click', function(e){
			var e = e || window.event;
			e.stopPropagation();
		});
		$$E.addEvent(o.DivObject, 'click', function(e){
			var e = e || window.event;
			e.stopPropagation();
		});
	};
	o.check = function(){
		setTimeout(function(){
			if($(obj) == null || $(obj).offsetWidth == 0){
				o.DivObject.style.display = 'none';
			}else{
				o.check();
			}
		}, 1000);
	};
	o.init();
	o.check();
}

function SelectBrowse(url, what, width, height)
{
	var link = url+"&_what="+what;
	var w = 660;
	qmeCms.dialogView.iframe('', link, {width:w,type:'abs'});
}
var imgEditorCallback = {
	param:{},
	def:function(resp){
		if(parseInt(resp.type)!=1) return;
		AJAX.request("system.php?sys=module&action=ajax&action_do=imgeditor&channelid="+this.param['channelid']+'&id='+this.param['id'],{
			method:'post',
			parameters:'thumb='+encodeURIComponent(config.url + resp.img.substr(3)),
			onComplete: function(json){
				
			}
		});		
	},
	fun:function(){},
	complete:function(resp){
		this.fun(resp);
		qmeCms.dialogView.close();
	}
};
function picEdit( picurl, w_p, cid, call, param)
{	
	imgEditorCallback.fun = call || function(){};
	imgEditorCallback.param = param || {};
	var cid = cid || 0;
	imgEditorCallback.param.channelid = cid;
	var url = "system.php?sys=image_editor&channelid="+cid+"&action=manage&imageName="+picurl;
	qmeCms.dialogView.iframe('图片修改', url, {width:800,type:'abs'});
}

var tID=0;
function ShowTabs(ID){
	if(ID!=tID){
		if($("TabTitle"+tID)) $("TabTitle"+tID).className ='title5';
		if($("TabTitle"+ID)) $("TabTitle"+ID).className  ='title6';
		if($("Tabs"+tID)) $("Tabs"+tID).style.display ='none';
		if($("Tabs"+ID)) $("Tabs"+ID).style.display  ='';
		tID=ID;
	}
}

function selectChecked( id, val)
{
	if($(id) == null) return false;
	if(val == undefined) return;
		switch( $(id).type.toLowerCase()){
			case 'select-one':
				var obj = $(id);
				for ( var i=0; i<obj.options.length; i++){
					if ( obj.options[i].value == val){
						obj.options[i].selected = true ;
						return ;
					}
				}
				return;
			break;

			case 'select-multiple':
				var obj = $(id);
				for ( var i=0; i<obj.length; i++){
					obj.options[i].selected = false ;
				}
				for (var j=0; j<val.length; j++){
					for ( var i=0; i<obj.length; i++){
						if ( obj.options[i].value == val[j]){
							obj.options[i].selected = true ;
							break;
						}
					}
				}
				return ;
			break;

			case 'radio':
				var name = $(id).getAttribute('name');
				var obj = $(id).form.getElementsByTagName('input');
				var type = (typeof val).toLowerCase() == 'number' ? true : false;
				for ( var i=0; i<obj.length; i++){
					if (obj[i].type.toLowerCase() == 'radio' && obj[i].getAttribute('name') == name){
						var v = type ? parseInt(obj[i].value) : obj[i].value;
						if(v == val){
							obj[i].checked = true ;
							break;
						}
					}
				}
				return ;
			break;

			case 'checkbox':
				if ( typeof(val) == 'object'){
					var name = $(id).getAttribute('name');
					var temp = $(id).form.getElementsByTagName('input');	
					var obj = [];
					for ( var i=0; i<temp.length; i++){
						if (temp[i].type.toLowerCase() == 'checkbox' && temp[i].getAttribute('name') == name){
							obj.push(temp[i]);
						}
					}
					for ( var i=0; i<obj.length; i++){
						obj[i].checked = false ;
					}
					for (var j=0; j<val.length; j++){
						for ( var i=0; i<obj.length; i++){
							if ( obj[i].value == val[j]){
								obj[i].checked = true ;
								break;
							}
						}
					}
				}else{
					var obj = $(id);
					if ( obj.value == val){
						obj.checked = true ;
					}else{
						obj.checked = false ;
					}
				}
				return ;
			break;
	
			case 'text':
				var obj = $(id);
				obj.value = val;
				return;
			break;		
		}
}

var swfSave = {
	getObj:function(){
		return parent.document.getElementById('top').contentWindow.swfSave;
	},
	set:function(key, dat, call) {
		if(top == window) return;
		var call = call || function(){};
		this.getObj().set(key, dat, call);
	},
	del:function(key, call){
		if(top == window) return;
		var call = call || function(){};
		this.getObj().del(key, call);
	},
	get:function(key, call){
		if(top == window) return;
		var call = call || function(){};
		this.getObj().get(key, call);
	}
};

function delTag( tid,kid){
	var obj = $('tag_'+tid);
	obj.disabled = true;
	var link = "system.php?sys=module&mod="+mod+"&action=tag&channelid="+channelid;
	AJAX.request( link,{
		method		: 'post',
		parameters  	: '&id='+id+'&tid='+tid+'&kid='+kid+'&ajax=1&tag=del',
		onComplete	: function(json){
			var json = evaljson(json);
			if( json.status == 1){
				obj.parentNode.removeChild(obj);
			}else{
				obj.disabled = false;
			}
		}
	});
	return false;
}
function addTag(){
	var name= $('name').value;
	if( !name) return false;
	$('add_tag').disabled = true;
	$('add_tag').value = '数据加载中';
	var link = "system.php?sys=module&mod="+mod+"&action=tag&channelid="+channelid;
	AJAX.request( link,{
		method		: 'post',
		parameters  : 'name='+name+'&id='+id+'&ajax=1&tag=add',
		onComplete	: function(json){
			var json = evaljson(json);
			$('name').value = '' ;
			var ul = $('keyword_start').getElementsByTagName('ul')[0];
			for( var i=0; i<json.length; i++){
				if( json[i].status == 1){
					var li = document.createElement('li');
					li.setAttribute('id', 'tag_' + json[i].data.tid);
					li.innerHTML = "<a onclick=\"return delTag("+json[i].data.tid+", "+json[i].data.kid+");\" href=\"###\" title=\"删除\" class=\"keyword\">"+json[i].data.name+"</a>";
					ul.appendChild(li);
				}else{
					alert( lang[json[i].message]);
				}
			}
			$('add_tag').disabled = false;
			$('add_tag').value = '添加关键词';
		}
	});
}
var colorSet = function(color, _what){
	$( _what).value= color ;
	$( _what).style.backgroundColor = color;
}
function getDir(url, id){
	var len = (config.url+'templates/').length;
	$(id).value = url.substr(len);
	qmeCms.dialogView.close();
}
function getColor(obj, _what){
	qmeCms.color.show(obj, _what);
}

qmeCms.rightMenu = function (obj){
	if (window.RightMenu_Status) return ;
	window.RightMenu_Status = true;
	this.MenuContent = [];
	this.id = 0;
	this.timer;
	this.div = document.createElement("div");
	var self = this;
	this.AddItem = function (nam,url){
		this.MenuContent[this.MenuContent.length] = [nam,url];
	}
	this.AddLine = function (){
		this.MenuContent[this.MenuContent.length] = "line";
	}
	this.Close = function(){
		self.div.style.visibility = "hidden";
		self.div.style.left = 0;
		self.div.style.top = 0;
		self.resetColor();
		self.display(true);
	}
	this.display = function(status){
		if(!$$B.ie6) return;
		Each(document.getElementsByTagName('select'), function(){
			this.style.visibility = status ? 'visible' : 'hidden';	
		});
	}
	this.resetColor = function(){
		if(self.id > 0 && $('item-' + self.id)){
			var input = $('item-' + self.id).getElementsByTagName('input');
			removeClassName($('item-' + self.id), 'item-menu');
			self.id = 0;
		}
	}
	this.Setup = function (){
		self.div.className = 'rightmenu';
		document.body.appendChild(self.div);
		var s = "";
		for (var i=0; i<this.MenuContent.length; i++)
		if (this.MenuContent[i]=="line") s += "<div class=\"line\"></div>"
		else s += "<div oncontextmenu=\"return false;\" class=\"item\" onclick=\"" + this.MenuContent[i][1] + "\" onmouseover=\"addClassName(this, 'hover');\" onmouseout=\"removeClassName(this, 'hover');\">&nbsp;" + this.MenuContent[i][0] + "</div>";
		self.div.oncontextmenu = function(){return false;};
		self.div.innerHTML = s;
		self.div.oW = self.div.offsetWidth;
		self.div.oH = self.div.offsetHeight;
		self.div.style.visibility = "hidden";
		self.div.style.left = 0;
		self.div.style.top = 0;
		self.div.onmouseover = function(){
			clearTimeout(self.timer);
		}
		self.div.onmouseout = function(){
			self.timer = setTimeout(function(){
				self.Close();
			}, 500);
		}
		clearTimeout(self.timer);
		$(obj).oncontextmenu = function (e){
			var oid = 0;
			var e = e || window.event;
			var o = e.target || e.srcElement;
			while(o.tagName.toLowerCase() != 'body'){
				if(o.tagName.toLowerCase() == 'tr' && o.className.toLowerCase().indexOf('item') > -1){
					var oo = o.getElementsByTagName('input');
					if(oo.length > 0){
						oid = oo[0].value;
					}
					break;
				}
				o = o.parentNode;
			}
			var item = $('item-'+oid);
			if(oid == 0 || item == null){
				self.Close();
				return false;
			}
			self.resetColor();
			self.id = oid;
			addClassName(item, 'item-menu');
			getWin();
			var nx = win.left + e.clientX
			var ny = win.top + e.clientY;
			var nh = Math.min(win.height, document.documentElement.clientHeight);
			if (e.clientX + self.div.oW > win.width) nx = nx - self.div.oW;
			if (e.clientY + self.div.oH > nh && ny - self.div.oH > 0 ) ny = ny - self.div.oH;
			self.div.style.left = nx + 2 + 'px';
			self.div.style.top = ny + 'px';
			self.div.style.visibility = "visible";
			self.display(false);
			clearTimeout(self.timer);
			self.timer = setTimeout(function(){
				self.Close();
			}, 2000);
			this.onclick = function ( e){
				var e = e||window.event;
				if( e.button == 2 || e.button == 3) return ;
				setTimeout(function (){
					self.Close();
				},100);
				this.onclick = null;
			}
			return false;
		}
	}
}
qmeCms.dialogView = {
	stop:false,
	IE6:$$B.ie6,
	listenTimer:0,
	closeTimer:0,
	iframeIndex:0,
	docReady:0,
	iframeReadyTimer:0,
	configDefault:{width:500,left:0,top:40,type:'abs'},
	config:{width:500,left:0,top:40,type:'abs'},
	form:function(title, message, option){
		clearTimeout(this.closeTimer);
		var html = "<div class='b-g'><div class='hd' id='dialog-title'><h4>"+title+"</h4></div><div class='bd' id='dialog-content'>"+message+"</div><div class='ft' id='dialog-ft'><input type='button' id='dialog-submit' class='f-button hidden' value='&#30830;&#23450;'/><input type='button' id='dialog-cancel' onclick='return qmeCms.dialogView.close()' class='f-button' value='关闭'/><a class='dialog-close' href='#' onclick='return qmeCms.dialogView.close()'></a></div><div class='underlay' id='dialog-underlay'><p></p></div></div>";
		var obj = $('dialog-advanced');
		if(obj != null) this.close();
		var option = option || {};
		$$.extend(this.config, option);
		if($('shield') == null){
			var obj = document.createElement("DIV");
			obj.id = "shield";
			document.body.insertBefore(obj, document.body.firstChild);
		}else{
			var obj = $('shield');
		}
		obj.style.height = Math.max(document.body.offsetHeight, document.documentElement.clientHeight) + 'px';
		if(this.IE6 && obj.getElementsByTagName('iframe').length == 0) obj.innerHTML = '<iframe src="about:blank" frameBorder=0 scrolling=no style="width:100%;height:100%;filter:alpha(opacity=0);opacity:0"></iframe>';
		obj.style.display = 'block';
		var obj = document.createElement('DIV');
		obj.className = 'dialog-advanced';
		obj.id = 'dialog-advanced';
		obj.innerHTML = html;
		document.body.insertBefore(obj, document.body.firstChild);
		if(!message) this.loading('数据加载中，请等待....');
		this.setPostion(obj);
		this.stop = false;
		this.listen();
	},
	get:function(title, url, option){
		var _o = this;
		this.form(title, "<p class='loading'>数据加载中，请等待....</p>", option);
		setTimeout(function(){
			AJAX.request(url,{method:'get',onComplete:function(resp){
				_o.setMessage(resp);
			}});
		},100);
	},
	iframe:function(title, url, option){
		if(!this.docReady){
			var o = this;
			o.alert('提示信息','', option);
			return this.iframeReadyTimer = setTimeout(function(){
				o.iframe(title, url, option);
			},500);
		}
		this.iframeIndex++;
		var title = title || '数据';
		this.form(title, "", option);
		var iframe = document.createElement("iframe");
		iframe.id = 'ajaxIframe_'+this.iframeIndex;
		iframe.setAttribute('frameBorder', '0');
		iframe.style.width = '100%';
		iframe.style.height = '50px';
		iframe.style.border = '0';
		iframe.setAttribute('scrolling', 'no');
		iframe.style.overflow = 'hidden';
		iframe.src = url;
		$('dialog-content').appendChild(iframe);
		iframe.onload = function(){
			qmeCms.dialogView.iframeOnload();
		}
		$('ajaxIframe_'+this.iframeIndex).name = 'ajaxIframe_'+this.iframeIndex;
	},
	iframeOnload:function(){
		if($('dialog-loading') != null) $('dialog-content').removeChild($('dialog-loading'));
		qmeCms.dialogView.reHeight();
	},
	reHeight:function(h){
		if($('ajaxIframe_'+this.iframeIndex) == null) return;
		if(!h){
			var o = $('ajaxIframe_'+this.iframeIndex);
			var oh = parseInt(o.style.height);
			var doc = o.contentWindow.document;
			var h = Math.max(doc.body.offsetHeight, doc.documentElement.clientHeight);
		}
		$('ajaxIframe_'+this.iframeIndex).style.height = h + 'px';
	},
	ajaxGet:function(url, callBack){
		AJAX.request(url,{method:'get',onComplete:function(resp){
			callBack(resp);
		}});
	},
	listen:function(){
		if($('dialog-advanced') == null) return false;
		if(!this.IE6) return false;
		var o = this;
		var underlayHeight = 0;
		var change = function(){
			if(o.stop == true) return false;
			var advancedHeight = $('dialog-advanced').offsetHeight;
			if(underlayHeight == advancedHeight) return false;
			$('dialog-underlay').style.height = advancedHeight.toString() + 'px';
			underlayHeight = advancedHeight;
		};
		setInterval(change, 50);
		change();
	},
	setPostion:function(obj){
		var setScroll = function(){
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			obj.style.top = o.config.top + scrollTop + 'px';
		};
		var o = this;
		if(this.config.type == 'fixed'){
			if(this.IE6){
				obj.style.position = 'absolute';
				$$E.addEvent(window, 'scroll', setScroll);
				setScroll();
			}else{
				obj.style.position = 'fixed';
				obj.style.top = this.config.top + 'px';
			}
		}else{
			obj.style.position = 'absolute';
			setScroll();
		}
		obj.style.width = this.config.width + 'px';
		if(this.config.left != 0){
			obj.style.left = this.config.left + 'px';
		}else{
			obj.style.left =  '50%';
			obj.style.marginLeft = (0-obj.offsetWidth)/2 + 'px';
		}
	},
	close:function(){
		clearTimeout(this.closeTimer);
		clearTimeout(this.iframeReadyTimer);
		if(arguments.length > 0) return this.closeTimer = setTimeout(function(){
			qmeCms.dialogView.close()
		}, arguments[0]);
		clearInterval(this.listenTimer);
		this.stop = true;
		if($('dialog-advanced') == null) return false;
		var obj = $('dialog-advanced');
		while(obj.getElementsByTagName('iframe').length > 0){
			var p = obj.getElementsByTagName('iframe')[0];
			p.parentNode.removeChild(p);
		}
		obj.parentNode.removeChild(obj);
		$('shield').parentNode.removeChild($('shield'));
		this.config.width = this.configDefault.width;
		this.config.left = this.configDefault.left;
		this.config.top = this.configDefault.top;
		this.config.type = this.configDefault.type;
		return false;
	},
	setSubmit:function(type,value, call){
		if($('dialog-submit') == null) return false;
		var obj = $('dialog-submit');
		if(type == true){
			removeClassName(obj, "hidden");
		}else{
			addClassName(obj, "hidden");
		};
		if(value) $('dialog-submit').value = value;
		if(typeof call == 'function') $('dialog-submit').onclick = call;
	},
	setCancel:function(value, call){
		if($('dialog-cancel') == null) return false;
		var obj = $('dialog-cancel');
		if(value) $('dialog-cancel').value = value;
		if(call) $('dialog-cancel').onclick = call;
	},
	setMessage:function(message){
		if($('dialog-content') != null){
			$('dialog-content').innerHTML = message;
		}
		evalscript(message);
	},
	loading:function(message){
		this.setMessage("<p class='loading' id='dialog-loading'>"+message+"</p>");
	},
	alert:function(title, message){
		this.form(title, message, {type:'abs'});
		this.setSubmit(false);
	}
};
domReady(function(){qmeCms.dialogView.docReady=1;});
qmeCms.extend = {
	needItem:[],
	add:function(type, field, need, name, filter, filter_error){
		need = isNaN(parseInt(need)) ? 0 : 1;
		this.needItem.push([type,field, need, name, filter, filter_error]);
	},
	checkItem:function(){
		var index = this.getTab();
		for(var i=0;i<this.needItem.length;i++){
			if(this.needItem[i][2] > 0){
				var v = this.getValue(this.needItem[i][0], this.needItem[i][1]);
				if(v.length == 0){
					if(index != null) ShowTabs(index);
					alert(this.needItem[i][3] + " 是必填/必选项，请选择");
					return false;
				}else if(this.needItem[i][4].length > 0){
					try{
						var pattern = new RegExp(this.needItem[i][4], "ig");
						var matcher = pattern.exec(v);
						if(matcher == null){
							if(index != null) ShowTabs(index);
							alert(this.needItem[i][3] + " 的输入不符合规则 \n----------------------------\n" + this.needItem[i][5]);
							return false;
						}
					}catch(e){
						if(index != null) ShowTabs(index);
						alert(this.needItem[i][3] + " 的验证规则不正确，无法使用");
						return false;
					}
				}
			}
		}
		return true;
	},
	getValue:function(type, field){
		var o = $('ext_'+field+'_0')
		switch(type.toString().toLowerCase()){
			case 'input':
			case 'select':
			case 'area':
				return o.value.trim();
			break;

			case 'radio':
				var data = '';
				Each(this.getFamily(o), function(){
					if(this.checked == true) data = this.value.trim();
				});
				return data;
			break;

			case 'checkbox':
				var data = [];
				Each(this.getFamily(o), function(){
					if(this.checked == true){
						data.push(this.value.trim());
					}
				});
				return data.join(',');
			break;

			case 'html':
				if(typeof FCKeditorAPI == 'undefined') return '';
				var oEditor = FCKeditorAPI.GetInstance('ext_'+field+'_0');
				return oEditor.GetXHTML(true).stripTags().trim();
			break;

			default:
				alert('extend type error');
				return '';
			break;
		}
	},
	getFamily:function(o){
		var name = o.getAttribute('name');
		var type = o.getAttribute("type").toLowerCase();
		var tag = o.tagName;
		var data = [];
		Each(o.form.getElementsByTagName(o.tagName), function(){
			if(this.getAttribute("type").toLowerCase() == type && this.getAttribute("name") == name){
				data.push(this);
			}
		});
		return data;
	},
	getTab:function(){
		var obj = $('menus');
		if(obj == null) return null;
		var o = obj.getElementsByTagName('td');
		var self = null;
		for(var i=0; i<o.length;i++){
			if(o[i].innerHTML == '扩展选项'){
				var i = o[i].getAttribute('id').toString().substr(8,1);
				i = parseInt(i);
				if(isNaN(i)) return null;
				return i;
			}
		}
		return null;
	}
};
qmeCms.related = {
	channelid:0,
	cid:0,
	page:1,
	mod:'',
	oid:0,
	sp:null,
	relatedid:0,
	id:0,
	map:{},
	init:function(oid, mod, channelid, cid){
		if(this.sp == null) this.sp = new Pager('qmeCms.related.sp');
		this.id = 0;
		this.oid = oid;
		this.mod = mod;
		this.channelid = channelid;
		this.cid = cid;
		qmeCms.dialogView.form("相关内容","",{type:'abs', width:700});
		var html = [];
		var filter = '<option value="0">过滤条件</option>';
		for(var i=0;i<filterDataJson.length;i++){
			filter += "<option value='"+(i+1)+"'>"+ filterDataJson[i] +"</option>";
		}
		html.push("<table class='tableborder' width='100%' border='0' cellpadding='1' cellspacing='1'><tbody>");
		html.push("<tr class='item'>");
		html.push("<td class='tablerowl' style='height:30px' colspan='3'><form action='###' method='get' onsubmit='return qmeCms.related.categoryChange();'>"+this.getChannel()+" <select id=\"related_cid\"><option value='0'>栏目加载中</option></select> <select id='related-filter'>"+filter+"</select> <select id='related-type'><option selected value='id'>按文档ID</option><option value='subject'>按文档标题</option></select> <input type='text' value='' size='20' namlength='64' class='f-text' id='related-id' /><input onclick=\"qmeCms.related.categoryChange();\" type='button' value=' 获取 ' class='f-button' /></form></td>"); 
		html.push("</tr>"); 
		html.push("</tbody></table><div id='related-dom'></div>"); 
		qmeCms.dialogView.setMessage(html.join(''));
		this.sp.CurrentPage = 1;
		this.channelChange(this.channelid);
		this.get(1);
	},
	loading:function(){
		$('related-dom').innerHTML = "<p class='loading'>数据加载中...</p>";
	},
	get:function(page){
		var o = this;
		this.loading();
		var url = "system.php?sys=module&mod="+this.mod+"&channelid="+this.channelid+"&action=manage&cid="+this.cid+"&filter="+$('related-filter').value+"&pageno="+page+"&pagesize=10";
		if(this.id){
			url += "&f=1&keyword="+encodeURIComponent(this.id)+"&type="+$('related-type').value;
		}
		AJAX.request(url,{method:'get',onComplete:function(resp){
			var json = evaljson(resp);
			o.create(json);
		}});
		return false;
	},
	getChannel:function(){
		var html = '<select id="related_channelid" onchange="qmeCms.related.channelChange(this.value);"><option value="0">选择频道</option>';
		for(var i in channel){
			if(channel[i]['link'] > 0 || typeof channel[i]['channelid'] == 'undefined') continue;
			html += "<option value='"+channel[i]['channelid']+"'"+(this.channelid == channel[i]['channelid'] ? " selected" : "" )+">"+channel[i]['channelname']+"</option>";
		}
		return html + "</select>";
	},
	channelChange:function(channelid){
		if(channelid < 1) return false;
		qmeCms.dialogView.ajaxGet("system.php?sys=getcontent&action=category&channelid="+channelid, function(resp){
			var json = evaljson(resp);
			if(typeof json == 'undefined' || json == null) return alert(resp);
			if($('related_cid') == null) return false;
			var obj = $('related_cid');
			obj.options.length = 0;
			obj.options[0] = new Option("选择栏目", 0);
			for(var i=0;i<json.length;i++){
				var len = obj.options.length;
				json[i]['title'] = json[i]['title'].replace(/&nbsp;/, "");
				obj.options[len] = new Option(json[i]['title'], json[i]['cid'] +"-"+ json[i]['child']);
				obj.options[len].style.paddingLeft = json[i]['vlayer'] + 'em';
			}
		});
		return false;
	},
	categoryChange:function(){
		var c = $('related_channelid').value;
		if(c == 0) return;
		var cid = $('related_cid').value;
		this.id = $('related-id').value;
		this.cid = isNaN(parseInt(cid)) ? 0 : parseInt(cid);
		this.channelid = $('related_channelid').value;
		this.sp.CurrentPage = 1;
		this.get(1);
		return false;
	},
	create:function(data){
		var html = [];
		this.sp.Total = data.total;
		this.sp.PageSize = 10;
		html.push("<table class='tableborder' width='100%' id='mytable' border='0' cellpadding='0' cellspacing='0'><tbody>");
		if(data['data'].length > 0){
			for(var i=0;i<data['data'].length;i++){
				html.push("<tr id='item-"+data['data'][i]['channelid']+"-"+data['data'][i]['id']+"' class='item'>"); 
				html.push("<td class='tablerowl' height='24' width='30'><input onclick=\"try{event.cancelBubble=true;}catch(e){event.stopPropagation();}\" type='checkbox' value='"+data['data'][i]['channelid']+"-"+data['data'][i]['id']+"' id='ids_"+data['data'][i]['id']+"' /></td><td  width='80' align='center'>"+data['data'][i]['id']+"</td><td style=\"padding-left:15px;\" width=\"460\"><a href='"+data['data'][i]['ym']+"' target='_blank'>"+data['data'][i]['subject']+"</a></td>"); 
				html.push("</tr>"); 
			}
		}else{
				html.push("<tr class='item'>"); 
				html.push("<td class='tablerowl' style='height:40px' colspan='3'>没有找到相关数据</td>"); 
				html.push("</tr>"); 
		}
		html.push("<tr><td class='tablerowl' height='24' width='30'><input type='checkbox' value='checkbox' onclick=\"checkall()\" id=\"chkall\" name=\"chkall\"></td><td colspan='2' class='tablerow' height='24'>"+this.sp.ShowPagerBar()+"</td></tr>"); 
		html.push("</tbody></table>"); 
		$('related-dom').innerHTML = html.join('');
		this.setSubmit();
		gridTableEvent();
	},
	setSubmit:function(){
		qmeCms.dialogView.setSubmit(true, " 关联 ", function(){
			qmeCms.related.build();
		});
	},
	modify:function(id, cid, json){
		var o = this;
		if(!json){
			qmeCms.dialogView.ajaxGet("system.php?sys=getcontent&action=get&cid="+cid+"&channelid="+cid+"&id="+id, function(resp){
				var json = evaljson(resp);
				if(typeof json == 'undefined' || json == null) return alert(resp);
				if(json.length > 0){
					for(var i=0;i<json.length;i++){
						o.oid = o.template(json[i]['aid']);
						$('related_'+o.oid+'_subject').value = json[i]['title'];
						$('related_'+o.oid+'_ordering').value = json[i]['ordering'];
						o.buildItem(json[i]['data']);
					}
				}else{
					o.template();
				}
				o.end();
			});
		}else{
			if(json.length > 0){
				for(var i=0;i<json.length;i++){
					o.oid = o.template(json[i]['aid']);
					$('related_'+o.oid+'_subject').value = json[i]['title'];
					$('related_'+o.oid+'_ordering').value = json[i]['ordering'];
					o.buildItem(json[i]['data']);
				}
			}else{
				o.template();
			}
			o.end();
		}
	},
	end:function(){
		var div = document.createElement('div');
		div.style.style = 'none';
		div.innerHTML = "<input type='hidden' name='related_complete' value='1' />";
		$('related').appendChild(div);
	},
	build:function(){
		var ids = getInputIds('mytable');
		if(ids.length == 0) return alert("没有选中任何内容");
		qmeCms.dialogView.setSubmit(true, " 关联中... ", function(){});
		var o = this;
		qmeCms.dialogView.ajaxGet("system.php?sys=getcontent&action=content&channelid="+this.channelid+"&id="+ids.join(','), function(resp){
			var json = evaljson(resp);
			if(typeof json == 'undefined' || json == null) return alert(resp);
			o.buildItem(json);
			o.setSubmit();
		});
	},
	buildItem:function(json){
		var obj = $('related_'+this.oid+'_data');
		for(var i=0;i<json.length;i++){
			var id = 'related_'+this.oid+'-data-'+json[i]['channelid']+'-'+json[i]['id'];
			if($(id) != null) continue;
			var tr = obj.insertRow(obj.rows.length);
			tr.onmouseover = function(){addClassName(this, 'item-hover');};
			tr.onmouseout = function(){removeClassName(this, 'item-hover');};
			tr.setAttribute("class", 'item');
			tr.className = 'item';
			tr.setAttribute("id", id);
			td = tr.insertCell(0);
			td.height = '24';
			td.setAttribute("class", 'tablerowl');
			td.className = 'tablerowl';
			td.innerHTML = json[i]['channelname'] + "<input type='hidden' name='related[id]["+this.oid+"][]' value='"+json[i]['channelid']+'-'+json[i]['id']+"' />";
			td = tr.insertCell(1);
			td.setAttribute("class", 'tablerowl');
			td.className = 'tablerowl';
			td.innerHTML = json[i]['id'];
			td = tr.insertCell(2);
			td.innerHTML = "<a href='"+json[i]['url']+"' target='_blank'>"+json[i]['subject']+"</a>";
			td.style.paddingLeft = '10px';
			td = tr.insertCell(3);
			td.setAttribute("class", 'tablerow');
			td.className = 'tablerow order';
			td.align = 'center';
			td.innerHTML = "<a class='del' href='javascript:void(0)' onclick=\"qmeCms.related.del('"+id+"');\">删除</a><a class='up' href='javascript:void(0)' onclick=\"qmeCms.related.move(this.parentNode.parentNode, 0);\">向上</a><a class='down' href='javascript:void(0)' onclick=\"qmeCms.related.move(this.parentNode.parentNode,1);\">向下</a>";
		}
	},
	del:function(id){
		if(makeSure()){
			var o = $(id).rowIndex;
			$(id).parentNode.deleteRow(o);
		}
	},
	move:function(tr, type, num){
		var num = num || 1;
		var newIndex = 0;
		if(type == 0){
			newIndex = tr.rowIndex - num;
			if(newIndex <= 0) return false;
		}else{
			newIndex = tr.rowIndex + num;
			if(newIndex >= tr.parentNode.rows.length) return false;
		}
		if(document.all){
			tr.parentNode.moveRow(tr.rowIndex, newIndex);
		}else{
			tr.parentNode.insertBefore(tr, tr.parentNode.rows[type == 0 ? newIndex : newIndex+1]);
		}
		removeClassName(tr, 'item-hover');
	},
	swap:function(ele, ele2){
		var num = ele2.rowIndex - ele.rowIndex;
		if(num == 0) return true;
		this.move(ele, num > 0 ? 1 : 0, Math.abs(num) );
	},
	drag:function(ele){
		var self = this;
		this.status = false;
		this.dragEle = null;
		this.dragTimer = 0;
		this.dragTip = null;
		this.getEle = function(e){
			var o = e.target || e.srcElement;
			var i = 0;
			while(i <= 3 && o.tagName.toLowerCase() != 'tr'){
				o = o.parentNode;
				i++;
			}
			return (o.tagName.toLowerCase() == 'tr' && o.className.indexOf('item') > -1) ?  o : null;
		};
		this.dispose = function(){
			ele.onmousedown = null;
			$$E.removeEvent(ele, 'mousemove', self.mousemove);
			$$E.removeEvent(ele, 'mouseover', self.mouseover);
			$$E.removeEvent(ele, 'mouseout', self.mouseout);
			$$E.removeEvent(ele, 'mouseup', self.mouseup);
			self.dragTip.parentNode.removeChild(self.dragTip);
			self.dragTip = null;
			self.dragEle = null;
		};
		this.mousemove = function(e){
			if(self.status == false) return true;
			var e = e || window.event; 
			if(self.dragEle != null && self.dragEle.className.indexOf('move') == -1){
				addClassName(self.dragEle, 'move');
				self.dragTip.style.display = 'block';
			}
			(function(o){
				o.left = $$E.fixEvent(e).pageX + 10 + 'px';
				o.top = $$E.fixEvent(e).pageY + 'px';
			})(self.dragTip.style);
		};
		this.mouseover = function(e){
			clearTimeout(self.dragTimer);
		};
		this.mouseout = function(e){
			if(self.status == false) return true;
			self.dragTimer = setTimeout(function(){
				if(self.dragEle != null){
					removeClassName(self.dragEle, 'move');
					self.dragTip.style.display = 'none';
					self.status = false;
					self.dragEle = null;
				}
			},200);
		};
		this.mouseup = function(e){
			if(self.status == false) return true;
			self.status = false;
			var e = e || window.event;
			var o = self.getEle(e);
			removeClassName(self.dragEle, 'move');
			self.dragTip.style.display = 'none';
			if(o == null) return true;
			if(self.dragEle.parentNode == o.parentNode && self.dragEle.rowIndex != o.rowIndex){
				qmeCms.related.swap(self.dragEle, o);
				var obj = o.parentNode.rows[self.dragEle.rowIndex].cells[2];
				var tp = obj.style.cssText;
				new colorFocus(obj, cs, ce, 'backgroundColor', function(){
					obj.removeAttribute('style');
					obj.style.cssText = tp;
				});
			}
			return true;
		};
		ele.onselectstart = function(){
			return false
		};
		ele.onmousedown = function(e){
			var e = e || window.event; 
			var o = self.getEle(e);
			if(o != null){
				self.status = true;
				self.dragEle = o;
				if(self.dragTip == null){
					self.dragTip = document.createElement('div');
					self.dragTip.className = 'drag-tip';
					document.body.appendChild(self.dragTip);
				}
				self.dragTip.innerHTML = '<p>您正在拖动：'+self.dragEle.cells[2].innerHTML.stripTags()+'</p>';
				return false;
			}else{
				var o = e.target || e.srcElement;
				if(o.tagName.toLowerCase() == 'tbody') return false;
				return true;
			}
		};
		$$E.addEvent(ele, 'mousemove', self.mousemove);
		$$E.addEvent(ele, 'mouseover', self.mouseover);
		$$E.addEvent(ele, 'mouseout', self.mouseout);
		$$E.addEvent(ele, 'mouseup', self.mouseup);
	},
	template:function(){
		var html = $('template-related').value.replace("", "");
		var pattern = new RegExp("#id#", "g");
		html = html.replace(pattern, this.relatedid);
		var aid = arguments.length == 1 ? arguments[0] : 0;
		var pattern = new RegExp("#aid#", "g");
		html = html.replace(pattern, aid);
		var div = document.createElement('div');
		div.innerHTML = html;
		div.className = 'row';
		$('related').appendChild(div);
		this.map['r-' + this.relatedid] = new qmeCms.related.drag($('related_'+this.relatedid+'_data'));
		this.relatedid++;
		this.resetRow();
		if(this.template.caller == null) this.end();	
		return this.relatedid - 1
	},
	removeTemplate:function(id){
		if($('related_'+id.toString()) == null){
			alert("出了点问题，请刷新浏览器重试");
			return false;
		}
		if(typeof this.map['r-' + id] != 'undefined'){
			this.map['r-' + id].dispose();
			delete this.map['r-' + id];
		}
		var obj = $('related_'+id.toString()).parentNode;
		var o = this;
		colorFocus(obj, cs, ce, 'backgroundColor', function(){
			obj.parentNode.removeChild(obj);
			o.resetRow();
		})
	},
	resetRow:function(){
		var o = getElementsByClassName($('related'), 'row');
		Each(o, function(i){
			removeClassName(this, 'hover-n');
			removeClassName(this, 'hover');
			if(i % 2 == 0){
				addClassName(this, 'hover');
			}else{
				addClassName(this, 'hover-n');
			}
		});
	}
}
function checkall(){
	if($('chkall') ==  null) return true;
	var c = $('chkall').checked;
	var o = arguments.length == 0 ? $('mytable').getElementsByTagName('input') : arguments[0].getElementsByTagName('input');
	for(var i=0;i<o.length;i++){
		if(o[i].type.toLowerCase() != 'checkbox' || o[i].disabled == true || o[i].id.toString().substr(0,4) != 'ids_') continue;
		o[i].checked = c;
		var op = o[i].parentNode.parentNode;
		if(c == true){
			addClassName(op, 'item-click');
		}else{
			removeClassName(op,  'item-hover');
			removeClassName(op,  'item-click');
		}
	}
}
function gridTableEvent(oid){
	var oid = arguments.length == 0 ? 'mytable' : arguments[0];
	if($(oid) == null) return;
	var ep = $(oid);
	var ele = getElementsByClassName($(oid), 'item');
	var move = false;
	var clickStatus = false;
	var ctrlKey = false;
	var gridTableEventIndex = 0;
	var mousePoint = {
		x:0,
		y:0,
		w:0,
		h:0,
		end:{x:0,y:0}
	};
	var moveTimer = 0;
	if(!window._gridDiv){
		window._gridDiv = document.createElement('div');
		window._gridDiv.className = 'grid-event hidden';
		document.body.insertBefore(window._gridDiv, $('shield'));
	}
	var div = window._gridDiv;
	function checkElement(obj){
		var o = obj.getElementsByTagName('input');
		if(o.length == 0) return false;
		if(o[0].id.toString().substr(0,4) != 'ids_' || o[0].type.toLowerCase() != 'checkbox' || o[0].disabled == true) return false;
		return o[0];
	};
	function timerEvent(){
		moveTimer = setInterval(function(){
			Each(ele, function(i){
				if(this.checkbox == false || (ctrlKey == true && this.checkbox.checked == true)) return;
				var offset = fetchOffset(this);
				var top = offset.top + this.offsetHeight;
				var min = Math.min(mousePoint.y, mousePoint.end.y);
				var max = Math.max(mousePoint.y, mousePoint.end.y);
				var min = Math.min(mousePoint.y, mousePoint.end.y);
				var max = Math.max(mousePoint.y, mousePoint.end.y);
				if((top >= min && top <= max) || (offset.top >= min && offset.top <= max) || (offset.top <= min && top >= max)){
					if(this.checkbox.checked == false){
						this.checkbox.checked = true;
						addClassName(this, 'item-click');
					}
				}else{
					if(this.checkbox.checked == true){
						this.checkbox.checked = false;
						removeClassName(this, 'item-click');
					}
				}
			});
		},200);
	};
	function upEvent(e){
		if(move == false) return;
		move = false;
		clickStatus = true;
		clearTimeout(moveTimer);
		addClassName(div, 'hidden');
		mousePoint.x = 0;
		mousePoint.y = 0;
		div.style.width = '0px';
		div.style.height = '0px';
		mousePoint.w = mousePoint.h = 0;
		Each(ele, function(i){
			this.status = false;
		});
	};
	function moveEvent(e){
		var e = e || window.event;
		ctrlKey = e.ctrlKey ? true : false;
		var x = $$E.fixEvent(e).pageX;
		var y = $$E.fixEvent(e).pageY;
		if(x >= mousePoint.x){
			div.style.width = Math.max(0, x - mousePoint.x - 2) + 'px';
			mousePoint.w = x - mousePoint.x;
		}else{
			div.style.left = x + 2 + 'px';
			div.style.width = (mousePoint.x - x) + 'px';
			mousePoint.w = mousePoint.x - x;
		}
		if(y >= mousePoint.y){
			div.style.height = y - mousePoint.y + 'px';
			mousePoint.h = y - mousePoint.y;
		}else{
			div.style.top = y + 'px';
			div.style.height = (mousePoint.y - y) + 'px';
			mousePoint.h = mousePoint.y - y;
		}
		mousePoint.end.x = x;
		mousePoint.end.y = y;
	};
	Each(ele, function(i){
		var tr = this;
		this.status = false;
		this.move = false;
		this.checkbox = checkElement(this);
		this.onselectstart = function(){
			return false
		};
		this.onmousedown = function(e){
			var e = e || window.event;
			var element = e.target || e.srcElement;
			if(element.tagName.toLowerCase() == 'input' || element.tagName.toLowerCase() == 'a' || !this.getAttribute('id')) return true;
			move = true;
			clickStatus = false;
			mousePoint.x = $$E.fixEvent(e).pageX;
			mousePoint.y = $$E.fixEvent(e).pageY;
			mousePoint.w = mousePoint.h = 0;
			div.style.left = mousePoint.x + 'px';
			div.style.top = mousePoint.y + 'px';
			return false;
		};
		$$E.addEvent(this, 'click', function(e){
			var e = e || window.event;
			var element = e.target || e.srcElement;
			if(this.checkbox == false) return true;
			if(e.ctrlKey == true){
				if(this.checkbox.checked == true){
					this.checkbox.checked = false;
					removeClassName(this,  'item-click');
				}else{
					this.checkbox.checked = true;
					addClassName(this,  'item-click');
				}
				gridTableEventIndex = i;
			}else if(e.shiftKey == true){
				var start = Math.min(i,gridTableEventIndex);
				var end = Math.max(i,gridTableEventIndex);
				Each(ele, function(k){
					if(this.checkbox == false) return;	
					if(k >= start && k<= end){
						if(this.checkbox.checked == false){
							this.checkbox.checked = true;
							addClassName(this,  'item-click');
						}
					}else{
						if(this.checkbox.checked == true){
							this.checkbox.checked = false;
							removeClassName(this,  'item-hover');
							removeClassName(this,  'item-click');
						}
					}
				});
			}else{
				Each(ele, function(k){
					if(i == k){
						if(this.checkbox.checked == true){
							this.checkbox.checked = false;
							removeClassName(this,  'item-hover');
							removeClassName(this,  'item-click');
						}else{
							this.checkbox.checked = true;
							addClassName(this,  'item-click');
						}
					}else{
						if(this.checkbox == false) return;	
						this.checkbox.checked = false;
						removeClassName(this,  'item-hover');
						removeClassName(this,  'item-click');
					}
				});
				gridTableEventIndex = i;
			}
			return true;
		});
		$$E.addEvent(this, 'mouseover', function(){
			if(move == true) this.status = true;
			addClassName(this,  'item-hover');
		});
		$$E.addEvent(this, 'mouseout', function(){
			removeClassName(this,  'item-hover');
			if(this.checkbox && this.checkbox.checked == true){
				addClassName(this,  'item-click');
			}else{
				removeClassName(this,  'item-click');
			}
		});
		$$E.addEvent(this, 'mousemove', function(){
			if(move == true) this.status = true;
		});
	});

	$$E.addEvent(ep, 'mousemove', function(e){
		if(move == false) return true;
		if(div.className.indexOf('hidden') > -1){
			removeClassName(div, 'hidden');
			timerEvent();
		}
		var e = e || window.event;
		moveEvent(e);
	});
	$$E.addEvent(div, 'mousemove', function(e){
		var e = e || window.event;
		moveEvent(e);
	});
	$$E.addEvent(ep, 'mouseup', upEvent);
	$$E.addEvent(div, 'mouseup', upEvent);
}
domReady(gridTableEvent);
function getInputIds(id){
	var id = id || 'mytable';
	var obj = $(id).getElementsByTagName('INPUT');
	var data = [];
	for(var i=0; i<obj.length; i++){
		if ((obj[i].type == 'checkbox' || obj[i].type == 'radio') &&  obj[i].name != 'chkall' && obj[i].disabled != true && obj[i].checked == true){
			data.push(obj[i].value);
		}
	}
	return data;
}
function selectAllChecked(obj){
	var o = obj.options;
	for(var i=0;i<o.length;i++){
		o[i].selected = true;
	}
}
function selectAllUnChecked(obj){
	var o = obj.options;
	for(var i=0;i<o.length;i++){
		o[i].selected = false;
	}
}
function setFilter(channelid, id){
	qmeCms.dialogView.get("快速修改", "system.php?sys=module&channelid="+channelid+"&action=filter&id="+id, {width:720});
}
function setLocation(key, val){
	var url = window.location.href;
	var v = getParam(key);
	if(v == null){
		url += (url.match(/\?/) ? '&' : '?') + key+"="+val;
	}else{
		url = url.replace(key +"="+v.toString(), key+"="+val);
	}
	window.location.href = url;
}
function exportDataToExecl(channelid){
	var ids = getInputIds();
	if(ids.length == 0){
		alert("请选择需要导出的内容");
		return;
	}
	var html = ["<form name='ajax_export' id='ajax_export' action='system.php?sys=module&action=export&mod="+mod+"&channelid="+config['channelid']+"' method='post'><p class='padding modify-cate'><input  checked  type='checkbox' value='editor' id='ex_editor' /><label for='ex_editor'>编辑</label>",
		"<input type='checkbox' name='edit_time'  checked  id='ex_edit_time' value='1' /><label for='ex_edit_time'>最后修改时间</label>",
		"<input type='checkbox' name='dateline'  checked  id='ex_dateline' value='1'  /><label for='ex_dateline'>添加时间</label>",
		"<input type='checkbox' name='title' id='ex_title' checked value='1'  /><label for='ex_title'>所属分类</label>",
		"<input type='checkbox' name='filter' id='ex_filter' value='1'  /><label for='ex_filter'>过滤属性</label></p>",
		"<p class='padding modify-cate' style='padding-top:0px' value='1' ><input type='checkbox' name='dview' id='ex_dview' value='1' /><label for='ex_dview'>日浏览数</label>",
		"<input type='checkbox' name='wview' id='ex_wview' value='1'  /><label for='ex_wview'>周浏览数</label>",
		"<input type='checkbox' name='mview' id='ex_mview' value='1'  /><label for='ex_mview'>月浏览数</label>",
		"<input type='checkbox' name='views' id='ex_views' value='1'  /><label for='ex_views'>总浏览数</label></p>",
		"<p class='padding modify-cate' style='padding-top:0px'>系统默认含有文档ID，文档标题<p><input type='hidden' name='ids' value='"+ids.join(',')+"' /></form>",
	].join('');
	qmeCms.dialogView.form("内容导出字段", html, {width:500});
	qmeCms.dialogView.setSubmit(true, "导出", function(){
		qmeCms.dialogView.setSubmit(true, "提交中...");
		$('ajax_export').submit();
		qmeCms.dialogView.close(5000);
	});
}
function createChannelIndex(channelid){
	if(parseInt(channel[channelid]['is_html']) == 0) return alert("出错了：频道首页没有设置成生成静态页");
	var url = config.url + "index.php?mod="+channelid+"&ac=index";
	var url = config.url + "admin/url.php?url=" + encodeURIComponent(url);
	AJAX.get(url, function(resp){
		if(resp.substr(0,4) == 'http'){
			AJAX.get(resp, function(){
				alert("首页生成完毕");
			});
		}else{
			alert(resp);
		}
	});
}
function addPrivateExtend(){
	var _t = (new Date()).getTime();
	var obj = $('extdata');
	var tr = obj.insertRow(obj.rows.length);
	var td = tr.insertCell(0);
	td.width = '180';
	td.style.verticalAlign = 'top';
	td.innerHTML = '<label class="f-label" style="width:40px">Key：</label><input name="extdata[key][]" type="text" value="" class="f-text" size="15" />';
	var td = tr.insertCell(1);
	td.width = '410';
	td.style.verticalAlign = 'top';
	td.innerHTML = '<label class="f-label" style="width:60px">Value：</label><textarea id="extdata_'+_t.toString()+'" name="extdata[value][]" class="f-text" style="width:325px;height:50px;"></textarea>';
	var td = tr.insertCell(2);
	td.style.verticalAlign = 'top';
	td.innerHTML = '<input type="button" value="上传" onclick="qmeCms.dialogView.iframe(\'上传\',\'system.php?sys=upload&id=extdata_'+_t.toString()+'&channelid='+config.channelid+'&file_type=all\',{type:\'abs\'});" /> <input type="button" value="删除" onclick="$(\'extdata\').deleteRow(this.parentNode.parentNode.rowIndex)" />';
};
qmeCms.areaIndex = function(name){
	var o = this;
	if(arguments.length == 2){
		var obj = $(arguments[0]);
		var index = $(arguments[1]);
	}else{
		var obj = $(name + "-data");
		var index = $(name + "-index");
	}
	var len = 0;
	obj.oncontextmenu = function(){
		return false;
	};
	$$E.addEvent(obj, 'keyup', function(){
		o.line();
	});
	$$E.addEvent(obj, 'scroll', function(){
		o.autoScroll();
	});
	this.line = function(){
		var n = obj.value.replace(/\r/gi,"").split("\n").length;
		if(n == len) return;
		var str = '';
		for(var i=1;i<=n;i++){
			str += i.toString() + "\n";
		}
		index.value = str;
		len = n;
	};
	this.autoScroll = function(){
		index.scrollTop = obj.scrollTop;
		setTimeout(function(){
			o.	autoScroll();
		},100);
	};
	this.line();
};
qmeCms.category = {
	show:function(){
		qmeCms.dialogView.get('选择栏目', 'system.php?sys=module&action=category&channelid='+config.channelid, {width:640});
	}
};
qmeCms.color = {
	color:'000000,993300,333300,003300,003366,000080,333399,333333,800000,FF6600,808000,808080,008080,0000FF,666699,808080,FF0000,FF9900,99CC00,339966,33CCCC,3366FF,800080,999999,FF00FF,FFCC00,FFFF00,00FF00,00FFFF,00CCFF,993366,C0C0C0,FF99CC,FFCC99,FFFF99,CCFFCC,CCFFFF,99CCFF,CC99FF,FFFFFF'.split(','),
	id:'',
	timer:0,
	show:function(obj, id){
		clearTimeout(this.timer);
		var pos = fetchOffset(obj);
		var o = $('color-select');
		o.style.left = pos.left - o.offsetWidth + obj.offsetWidth + 'px';
		o.style.top = pos.top + obj.offsetHeight + 'px';
		o.style.visibility = 'visible';
		this.id = id;
		var o = this;
		this.timer = setTimeout(function(){o.close();},3000)
	},
	set:function(c){
		if($(this.id) != null){
			if(c){
				$(this.id).value = "#"+c;
			}else{
				$(this.id).value = '';
			}
			$(this.id).onchange();
		}
		this.close();
	},
	close:function(){
		clearTimeout(this.timer);
		$('color-select').style.visibility = 'hidden';
	},
	build:function(){
		var html = "<div class='color'><ul><li class='default' onclick=\"qmeCms.color.set('');\">默认颜色</li>";
		for(var i=0;i<this.color.length;i++){
			html += "<li style=\"background:#"+this.color[i]+"\" onclick=\"qmeCms.color.set('"+this.color[i]+"');\">"+this.color[i]+"</li>";
		}
		html += "<div class='clear'></div><ul></div>";
		$('color-select').innerHTML = html;
		var o = this;
		$$E.addEvent($('color-select'), 'mouseout', function(){
			clearTimeout(o.timer);
			o.timer = setTimeout(function(){
				o.close();
			},500)
		});
		$$E.addEvent($('color-select'), 'mouseover', function(){
			clearTimeout(o.timer);
		});
	}
};

var cs = '#ffffff';
var ce = '#fbfb04';

function changeOrdering(id, obj){
	obj.value = obj.value.trim().replace(/[^\d]/g,'');
	clearTimeout(obj.timer);
	obj.timer = setTimeout(function(){
		var v = obj.value.trim();
		if(v.length == 0 || isNaN(parseInt(v))) return false;
		if(parseInt(v) == parseInt(obj.getAttribute('rel').trim())) return false;
		obj.style.background = "#fff url(images/loading.gif) no-repeat center center";
		Ajax.get("system.php?sys=module&action=ajax&action_do=ordering&module="+mod+"&channelid="+config.channelid+"&id="+id+"&val="+obj.value.trim(), function(resp){
			obj.style.backgroundImage = "none";
			obj.setAttribute('rel', v);
		});
	},500);
};

function formSubmit(form){
	document.forms[form].submit();
}

function getMapMark(id){
	window.open("images/map.html");
}