var keyword = '';
function showTags( pageno, word)
{
	if (!pageno) pageno = 1;
	keyword = word || '';
	var link = "system.php?sys=keyword&action=manage&channelid="+channelid+"&pageno="+pageno+"&keyword="+encodeURIComponent(keyword);
	AJAX.request( link,
	{
		method		: 'get',
		onComplete	: function(resp)
		{
			var json = evaljson(resp);
			if ( json.status == 200){
				clearTable();
				var len = json.message.length;
				for( var i=0; i<len; i++){
					var _tr = $('mytable').insertRow(i+1);
						_tr.id      = 'item-'+json.message[i].kid;
						_tr.className = "item";
					var a   = _tr.insertCell(0);
						a.height = '24px';
						a.className = 'tablerowl';
						a.innerHTML = "<input type='checkbox'  id='ids_"+json.message[i].tid+"' value='"+json.message[i].tid+"' onclick=\"try{event.cancelBubble=true;}catch(e){event.stopPropagation();}\" />";
					var b   = _tr.insertCell(1);
						b.align = 'center';
						b.innerHTML = json.message[i].id;
					var b   = _tr.insertCell(2);
						b.align = 'center';
						b.innerHTML = json.message[i].name;						
					var e   = _tr.insertCell(3);
						e.innerHTML = (json.message[i].subject == null) ? " 该内容可能在审核中或者回收站里 "  : (json.message[i].subject.toString().substr(0,24) + '<font color="blue">['+json.message[i].id +']</font>');
						e.style.paddingLeft = '15px';
					var c   = _tr.insertCell(4);
						c.align     = 'center';
						c.innerHTML = json.message[i].rank;
					var d   = _tr.insertCell(5);
						d.align     = 'center';
						d.innerHTML = "<span onclick='modifyTags("+json.message[i].kid+");' style='cursor:pointer;'>修改关键词</span> | <span onclick='delTags("+json.message[i].kid+");' style='cursor:pointer;'>删除关联</span>";
				}
			
				pg.Total    = json.total;
				pg.PageSize = json.pagesize;
				pg.PageButtonNumber = 7;
				$('pg_pager').innerHTML = pg.ShowPagerBar();
				gridTableEvent();
			}
			else
			{
				clearTable();
				var _tr = $('mytable').insertRow(1);
				_tr.className = "item";
				var a   = _tr.insertCell(0);
				a.height = '24px';
				a.className = 'tablerow';
				a.setAttribute('colSpan', 6);
				a.innerHTML = "<p style='padding:25px;text-align:center'>还没有相关数据</p>";
			}
		}
	});
}
function delTags( kid)
{
	if(makeSure() == false) return false;
	var link = "system.php?sys=keyword&action=update&channelid="+channelid;
	AJAX.request( link,
	{
		method		: 'post',
		parameters	: "type=1&kid="+kid,
		onComplete	: function( json)
		{
			showTags( pg.CurrentPage, keyword);
		}
	});
}
function addTags()
{
	var name = $('name').value;
	var rank = $('rank').value;
	var kid  = $('kid').value;
	if ( !name || !rank)
	{
		alert('请添加完整数据');
		return false;
	}
	$('name').value = '';
	$('rank').value = '';
	var type = $('addTag').value == '添加关键词' ? 2 : 3;
	var link = "system.php?sys=keyword&action=update&channelid="+channelid;
	AJAX.request( link,{
		method		: 'post',
		parameters	: "type="+type+"&name="+name+"&rank="+rank+"&kid="+kid,
		onComplete	: function( json){
			var json = evaljson(json);
			$('addTag').value = '添加关键词';
			if( json.status == 200){
				if( type == 2 ){
					alert("关键词汇添加成功") ;
				}else{
					clearTable();
					showTags( pg.CurrentPage);
				}
			}else if ( json.status == 403){
				clearTable();
				showTags( pg.CurrentPage);
			}else{
				alert( lang[json.message]);
			}
		}
	});
}

function editTags( kid){
	var obj  = $('kid_'+kid);
	$('name').value = obj.cells[2].innerText;
	$('rank').value = obj.cells[4].innerText; 
	$('kid').value  = kid; 
	$('addTag').value = '编辑关键词汇';
	$('rank').focus(); 
}

function clearTable(){
	for( var i=0; i< $('mytable').rows.length; i++){
		var id  = $('mytable').rows[i].id.toString();
		if( id.substr(0,4) == 'item'){
			$('mytable').deleteRow(i);
			i--;
		}
	}
};

function modifyTags(kid){
	qmeCms.dialogView.get("修改关键词", "system.php?sys=keyword&action=modify&channelid="+config.channelid+"&kid="+kid, {width:450});
};

function searchTag(){
	qmeCms.dialogView.form("搜索关键词 - 两种方式选一", "<p class='padding'>关键词：<input type='text' class='f-text' id='tag-search-text' /></p><p class='padding'>文档ID：<input type='text' class='f-text' id='tag-search-id' /></p>", {width:450});
	qmeCms.dialogView.setSubmit(true, "搜索", function(){
		var v = $('tag-search-text').value.trim();
		var id = $('tag-search-id').value.trim();
		id = isNaN(parseInt(id)) ? 0 : parseInt(id);
		if(!v && !id) return false;
		$('dialog-submit').disabled = true;
		showTags(1, id > 0 ? id : v);
		qmeCms.dialogView.close(500);
	});
	return false;
}

Pager.prototype.goPrePage = function()
{
	this.CurrentPage--;
	showTags( this.CurrentPage, keyword);
	$('pg_pager').innerHTML = this.ShowPagerBar();
}
Pager.prototype.goLastPage = function()
{
	this.CurrentPage = this.PageCount;
	showTags( this.CurrentPage, keyword);
	$('pg_pager').innerHTML = this.ShowPagerBar();
}
Pager.prototype.goPage = function(n)
{
	this.CurrentPage = n;
	showTags( n, keyword);
	$('pg_pager').innerHTML = this.ShowPagerBar();
}
Pager.prototype.goNextPage = function()
{
	this.CurrentPage++;
	showTags( this.CurrentPage, keyword);
	$('pg_pager').innerHTML = this.ShowPagerBar();
}