// 设置分类名颜色,edit为编辑时选择的是不可用分类时转为可用
function setOptionColor(edit)
{
	var obj=$i(iwms.classID);
	var keepSelect=false;
	var dispChilds=0;	// 需显示下级数
	if (edit!=null && edit){
		sArr[obj.options[obj.selectedIndex].value][2]=true;
	}
	// 非可添加项变色
	for (var i=obj.options.length-1;i>=0;i--){
		var id = parseInt(obj.options[i].value);
		if (!sArr[id][2]){
			if (sArr[id][0]==0 || dispChilds==0){
				if (document.all){
					obj.options.remove(i);
				}else{
					obj.options[i]=null;
				}
			}else{
				obj.options[i].value="";
				obj.options[i].style.color="gray";
			}
		}else{
			// 深度大于0为子类
			if (sArr[id][3]>0){
				dispChilds+=1;
			}
			if (obj.options[i].selected){
				keepSelect=true;
			}
		}
		if (sArr[id][3]==0){
			dispChilds=0;
		}
	}
	// 选择第一个可添加项
	if (!keepSelect) {
		for (var i=0;i<obj.options.length;i++){
			if ( obj.options[i].value != "") {
				obj.selectedIndex = i;
				break;
			}
		}
	}
}


// 简繁转换
function s2t()
{
	var objs = new Array();
	var o=tinyMCE.get(iwms.body);
	o.setContent(o.getContent().s2t());
	o=tinyMCE.get(iwms.summary);
	o.setContent(o.getContent().s2t());
	objs[0]=$i(iwms.title);
	objs[1]=$i(iwms.author);
	objs[2]=$i(iwms.source);
	objs[3]=$i(iwms.pageTitle);
	for (var i=0; i<objs.length; i++) {
		objs[i].value = objs[i].value.s2t();
	}
}
function t2s()
{
	var objs = new Array();
	var o=tinyMCE.get(iwms.body);
	o.setContent(o.getContent().t2s());
	o=tinyMCE.get(iwms.summary);
	o.setContent(o.getContent().t2s());
	objs[1]=$i(iwms.title);
	objs[2]=$i(iwms.author);
	objs[3]=$i(iwms.source);
	objs[4]=$i(iwms.pageTitle);
	for (var i=1; i<objs.length; i++) {
		objs[i].value = objs[i].value.t2s();
	}
}

//选择调查的窗口
function OpenPoll()
{
	var o=$i(iwms.pollIds);
	var con = "<iframe src='admin_selPoll.aspx?ids="+ o.value +"' height='100%' width='100%' frameborder='0'></iframe>";
	var b=boxys.poll;
	if (b){
		b.toggle();
	}else{
		b=new Boxy(con,{title:'选择调查'});
		boxys.poll=b;
		b.resize(460,330);
	}
}

// 设置调查
function SetPoll(ids)
{
	var o=$i(iwms.pollIds);
	o.value=ids;
}

//选择头条与固顶窗口
function OpenFix(){
	var con = "<iframe src='admin_selFix.aspx' height='100%' width='100%'  frameborder='0'></iframe>";
	var b=boxys.fix;
	if (b){
		b.toggle();
	}else{
		b=new Boxy(con,{title:'设置固顶头条'});
		boxys.fix=b;
		b.resize(360,320);
	}
}

//取当前固顶设置
function GetFixSet(){
	return $i(iwms.fixNews)==null ? "" : $i(iwms.fixNews).value;
}
//设置固顶头条
function SetFix(s){
	var o = $i(iwms.fixNews);
	o.value = s;
}

// ajax检查是否标题重复
function duplicate(ele)
{
	if (ele.value.length==0){
		return;
	}

	iwms.ajSrc=ele;
	$.post('ajax.aspx',{act:'ajax',cmd:'titleDuplicate',title:iwms.ajEncode(ele.value)},function(r){
		if(r.duplicate){
			iwms.popMsg('dup',ele,"你填写的标题已经存在，请检查是否为重复内容！");
			iwms.hideMsg('dup',3000);
			ele.select();
		}
	},'json');
	makeTags(ele);
}

// 用标题生成标签
iwms.tags=[];
function makeTags(ele)
{
	if (ele.value.length==0){
		$i(iwms.key).value="";
		return;
	}
	var n=0, arr=[];
	var v=ele.value.toLowerCase();
	for(var i=0;i<iwms.tags.length;i++){
		if(v.indexOf(iwms.tags[i].toLowerCase())>=0){
			arr.push(iwms.tags[i]);
		}
		if(arr.length==5){
			break;
		}
	}
	$i(iwms.key).value=arr.toString();
}


$(function(){
	// 取tags数组
	$('#'+iwms.title).one('click',function(){
		$.get('json.aspx',{cmd:'tags'},function(r){
			iwms.tags=r;
		});	
	});
});

// 外部链接访问
function LinkOut(url)
{
	if (url==""){
		return;
	}
	window.open(url);
}

//选择专题的函数
function OpenTopic()
{
	var con = "<iframe src='admin_selTopic.aspx' style='height:100%;width:100%'  frameborder='0'></iframe>";
	var b=boxys.topic;
	if (b){
		b.toggle();
	}else{
		b=new Boxy(con,{title:'选择专题'});
		boxys.topic=b;
		b.resize(430,360);
	}
}

// 设置专题
function SetTopic(name, id)
{
	var objTopic=$i(iwms.topic);
	var objTopicID=$i(iwms.topicId);
	objTopic.value=name;
	objTopicID.value=id;
}

//选择关键字的窗口
function OpenKey()
{
	var con = "<iframe src='admin_selKey.aspx' style='height:100%;width:100%' frameborder='0'></iframe>";
	var b=boxys.key;
	if (b){
		b.toggle();
	}else{
		b=new Boxy(con,{title:'选择标签'});
		boxys.key=b;
		b.resize(430,310);
	}
}

// 设置关键字
function SetKey(name)
{
	var oKey=$i(iwms.key);
	if (oKey.value!=""){
		oKey.value += ",";
	}
	oKey.value+=name;
}

// 标签分隔全角逗号转半角
function correctKey(o)
{
	if (o.value.length==0){
		return;
	}
	o.value=o.value.replace(/，/g,",");
}

// 设置标题图片
function SetTitleImg(str)
{
	var obj=$i(iwms.titleImg);
	obj.value=str;
}

// 上传文件,ti为是否设标题图片
function OpenUpload(ti)
{
	var url = "admin_upload.aspx";
	if (ti!=null && ti){
		url += "?ti=1";
		if (GetFixSet().indexOf("|0")>0){
			url += "&hd=1";
		}
	}
	win=window.open(url,"upload","width=900,height=560,left=0,top=0,scrollbars=1,status=1,resizable=1");
	win.focus();
}

// 上传软件,pnum为下载点数
function OpenUploadSoft(pnum)
{
	var url = "admin_upload.aspx?t=soft&n="+pnum;
	win=window.open(url,"upload","width=550,height=250,left=0,top=0,scrollbars=1,status=1,resizable=1");
	win.focus();
}

// 添加上传文件到控件
function AddUpload(str)
{
	var obj=$i(iwms.upload);
	if (obj.value==""){
		obj.value=str;
	}else{
		obj.value+=","+str;
	}
}

// 反选checkbox
function checkAll(layer, bcheck)
{
	$(layer).find(":checkbox").attr("checked",bcheck);
}

// 改变分类时设置是否通过审核
// 添加新闻和软件时用
function sortChanged()
{
	if (sort_pass==undefined){
		return;
	}
	var cid = $i(iwms.classID).value;
	var o_passed = $i(iwms.passed);
	var re = new RegExp("(^|,)"+ cid +"(,|$)","ig");
	if (re.test(sort_pass)){
		if (o_passed.disabled){
			o_passed.disabled = false;
			o_passed.checked = true;
		}
	}else if (!re.test(sort_pass)){
		if (!o_passed.disabled){
			o_passed.disabled = true;
			o_passed.checked = false;
		}
	}
}


// 显示预览标题图片
function DispTitleImg(disp){
	var $o = $('#ylTitleImg').css("z-index",1500);
	var url = $('#'+iwms.titleImg).val();
	if (url.length==0){
		disp=false;
	}
	if (disp){
		if (url.substr(url.length-3).toLowerCase()=="swf"){
			$o.html("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='240' height='180'><param name='movie' value='"+ url +"'/><param name='quality' value='high'/><param name='play' value='-1'><embed src='"+ url +"' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high' width='240' height='180'></embed></object>");
		}else{
			$o.html("<img src='"+ url +"' alt=''/>");
		}
		$o.toggle();
	}else{
		$o.hide();
	}
}

// 过滤字符串中html，保留换行
iwms.clearHtml = function(str){
	var re = new RegExp("<p.*?>|<br\\s*/?>","ig");
	str = str.replace(re, "[br]");
	re = new RegExp("<script.+?</script>","ig");
	str = str.replace(re, "");
	re = new RegExp("<.+?>","ig");
	str = str.replace(re, "");
	str = str.replace(/&nbsp; /ig,"[nbsp]");
	str = str.replace(/&nbsp;&nbsp;/ig,"[nbsp]");
	str = str.replace(/&nbsp;/ig," ");
	re = new RegExp("<.+?$","g");
	str = str.replace(re, "");
	re = new RegExp("&.{0,4}$","g");
	str = str.replace(re, "");
	re = new RegExp("\\[nbsp]","g");
	str = str.replace(re,"　");
	re = new RegExp("\\[br]","g");
	str = str.replace(re,"<br/>");
//	re = new RegExp("\r?\n","g");
//	str = str.replace(re,"<br/>");
	re = new RegExp("<br/>(\\s*<br/>\\s*)+","g");
	str = str.replace(re,"<br/>");
	re = new RegExp("^\\s*<br/>(\\s*<br/>\\s*)*","g");
	str = str.replace(re,"");
	return str;
};

// 从新闻内容生成简介
iwms.setSummary = function(){
	var str = tinyMCE.get(iwms.body).getContent();
	str = iwms.clearHtml(str);
	var idx=str.gbIndexOf("<br/>");
	if (idx>50 && idx<iwms.summaryLength){
		str = str.gbLeft(idx-1);
	}else if (str.gbLen()>iwms.summaryLength){
		str = str.gbLeft(iwms.summaryLength,"...");
	}
//	alert(str);
	var ed = tinymce.EditorManager.get(iwms.summary);
	ed.getBody().innerHTML=str;
};

// 保留用户编辑设置
iwms.editorChg=function (o){
	iwms.cookie("iwmsEditor",o.value,o.value.length>0?30:-1);
};

// 选分类菜单
var icmenu=new Object();
icmenu._inited=false;
icmenu.pop=function(ele,iptId,typ){
	if(typeof(icmenu.items)=='undefined'){
		$.post('ajax.aspx',{act:'ajax',cmd:'selSort',sid:0,type:typ},function(r){
			icmenu.items=r;
			if(icmenu.items.length==0){
				icmenu.items.push({id:0,name:'没有可选分类',add:0,items:[],parent:icmenu.cur});
			}
			icmenu.pop(ele,iptId,typ);
		},'json');
		return;
	}
	if(!icmenu._inited){
		icmenu.$ele=$(ele);
		icmenu.$ipt=$("#"+iptId);
		icmenu.type=typ;
		icmenu._init();
		icmenu._genUl().appendTo(icmenu.$d);
	}
	iwms.popMsg("icmenu",ele,icmenu.$d.get(0),'aj_sel');
	
};

// 选择分类
icmenu.set=function(idx){
	var o=icmenu.items[idx];
	icmenu.$ipt.val(o.id);
	icmenu.$ele.text(o.name);
	iwms.hideMsg('icmenu');
};

// 初始生成菜单
icmenu._init=function(){
	if(typeof(icmenu.$d)=='object'){
		return;
	}
	//alert(typeof(icmenu.$d));
	icmenu.$d=$("<div class='cmenu'></div>");
	
	$("body").append(icmenu.$d);
	icmenu._inited=true;
};

icmenu._genUl=function(){
	var uls = [$("<ul/>")];
	var preDepth = 0;
	
	for(var i=0;i<icmenu.items.length;i++){
		var o=icmenu.items[i];
		if(preDepth<o.depth){
			uls[o.depth] = $("<ul/>");
			uls[o.depth-1].append(uls[o.depth]);
		}
		preDepth=o.depth;
		
		var s = "<li>";
		// 下一个是子类
		if(i+1<icmenu.items.length && o.depth<icmenu.items[i+1].depth){
			s += '<a href="###" onclick="icmenu._open(this)"><img src="pic/b.gif" class="opened"/></a>';
		}else{
			s += '<img src="pic/b.gif" class="space"/>';
		}
		if(o.add){
			s += '<a href="###" onclick="icmenu.set('+i+')">'+ o.name +'</a>';
		}else{
			s += o.name;
		}
		s+="</li>";
		
		uls[o.depth].append(s);
		
	}
	return uls[0];
};

icmenu._open=function(ele){
	$ele=$(ele);
	$ul=$ele.parent().next("ul");
	if($ul.css("display")=="none"){
		$ul.show();
		$ele.children(":eq(0)").attr("class","opened");
	}else{
		$ul.hide();
		$ele.children(":eq(0)").attr("class","closed");
	}
};


// 美化select ===== begin =====
/*换为div显示选取分类，不再有用
iwms.sSelect=function(){};
iwms.sSelect.dArr = [];

iwms.sSelect.add=function(obj){
	if (obj==null){
		return;
	}
	var optLen=obj.options.length;
	var p=iwms.elePos(obj);
	obj.style.visibility="hidden";
	var oA=document.createElement("A");
	var sUl;
	var w=p.w>50 ? p.w : 50;
	oA.id="sel_"+obj.name;
	oA.href="###";
	oA.className="sel";
	oA.style.left=p.x+"px";
	oA.style.top=p.y+"px";
	oA.style.width=w+"px";
	obj.style.width=w+"px";
	document.body.appendChild(oA);
//	obj.insertAdjacentElement("afterEnd",oA);
	var sSpan=document.createElement("span");
	var spanId=optLen>0 ? obj.options[obj.selectedIndex].value : "";
	var spanText=optLen>0 ? obj.options[obj.selectedIndex].text : "　";
	sSpan.id=spanId;
	sSpan.onclick=function(){oA.focus();};	//ie下点span末聚焦到oA
	sSpan.style.lineHeight=p.h+"px";
	sSpan.style.width=w-20+"px";
	sTxt=document.createTextNode(spanText);
	sSpan.appendChild(sTxt);
	oA.appendChild(sSpan);
	
	window.onresize=function(){
		var pp=iwms.elePos(obj);
		oA.style.left=pp.x+"px";
		oA.style.top=pp.y+"px";
		if(sUl){
			sUl.style.left=pp.x+"px";
			sUl.style.top=pp.y+21+"px";
		}
	};
	
	if (optLen<1){
		return;
	}
	
	oA.onkeyup=function(e){
		if (e==null){e=event;}
		var c=e.charCode||e.keyCode;
//alert(c);
		if(c!=13&&c!=38&&c!=40&&c<48||c>57&&c<65||c>90){
			return true;
		}
		var prevIdx;
		var ls=sUl.getElementsByTagName("LI");
		var o = e.target||e.srcElement;
		o=o.childNodes[0];
		
		// 当前选择项索引
		for(var i=0;i<ls.length;i++){
			if (ls[i].className=="chk"){
				prevIdx=i;
				break;
			}
		}
//alert(prevIdx);
		if (c==13){
			// 回车确认
			o.innerHTML=ls[prevIdx].innerHTML;
			obj.value=ls[prevIdx].val;
//alert(ls[prevIdx].val);
			return;
		}else if (c==38 || c==40){
			// 向上或向下
			var n = c==38?-1:1;
			var nIdx=-1;
			for(var i=prevIdx+n;i!=prevIdx;i=i+n){
				i=(ls.length+i)%ls.length;
				if (ls[i].style.fontStyle!="italic"){
					nIdx=i;
					break;
				}
			}
			if(nIdx<0){
				return;
			}
//alert(nIdx);
			ls[prevIdx].className="";
			ls[nIdx].className="chk";
			o.innerHTML=ls[nIdx].innerHTML;
			obj.value=ls[nIdx].val;
			sUl.scrollTop=22*nIdx;
//$i(iwms.topic).value=sUl.scrollHeight+","+22*nIdx;
			return;
		}

		// 取各项头字母数组
		var arr;
		for(var i=0;i<iwms.sSelect.dArr.length;i++){
			if (iwms.sSelect.dArr[i][0]==obj){
				arr = iwms.sSelect.dArr[i][1];
				break;
			}
		}

		c=String.fromCharCode(c).toLowerCase();
		// 从当前项开始找头字母
		for(var i=(prevIdx+1)%arr.length;i!=prevIdx;i=(i+1)%arr.length){
			if (c==arr[i].toLowerCase()){
				ls[prevIdx].className="";
				ls[i].className="chk";
				o.innerHTML=ls[i].innerHTML;
				obj.value=ls[i].val;
				sUl.scrollTop=22*i;
//$i(iwms.topic).value=sUl.scrollHeight+","+22*i;
				break;
			}
		}
		return false;
	};

	oA.onclick=function(){
		if($i("sel_menu_"+obj.name)==null){
			sUl=document.createElement("ul");
			sUl.id="sel_menu_"+obj.name;
			sUl.className="sel";
			sUl.style.left=p.x+"px";
			sUl.style.top=p.y+21+"px";
			sUl.style.width=w+"px";
			document.body.appendChild(sUl);
			
			sUl.onmouseover=function(){
				oA.onblur=null;
			};
			sUl.onmouseout=function(){
				oA.onblur=function(){
					sUl.style.display="none";
				}
			};
			
			var tmp=document.createDocumentFragment();
			var arr = [];
			for(var j=0;j<optLen;j++){
				var opt=obj.options[j];
				var sltGray=obj.options[j].style.color=="gray";
				var sLi=document.createElement("li");
				sLi.val=opt.value;
				sLi.appendChild(document.createTextNode(opt.text));
				// 不可选
				if (opt.style.color=="gray"){
					sLi.style.fontStyle="italic";
					arr.push('');
				}else{
					sLi.onclick=function(){
						sSpan.innerHTML=this.innerHTML;
						obj.value=this.val;
						sUl.style.display="none";
					};
					
					// 转换为拼音
					var str=opt.text;
					if(typeof(PinYin)=="function"){
						str = PinYin.getString(str).toLowerCase();
					}
					// 取第一个字母或数字
					if(str.match(/\w/ig)){
						str=str.match(/\w/ig)[0];
					}else{
						str=str.charAt(0);
					}
					arr.push(str.charAt(0));
				}
				sLi.onmouseover=function(){
					if(this.className==""){
						this.className="over";
					}
				};
				sLi.onmouseout=function(){
					if (this.className=="over"){
						this.className="";
					}
				};
				tmp.appendChild(sLi);
				
				
			}
			sUl.appendChild(tmp);
			iwms.sSelect.dArr.push([obj,arr]);
			if(optLen>10){
				sUl.style.overflowY="scroll";
				sUl.style.height="220px";
			}
		}else{
			sUl=$i("sel_menu_"+obj.name);
			if(sUl.style.display=="none"){
				sUl.style.display="block";
			}else{
				sUl.style.display="none";
			}
		}
		// 选定当前值
		if (sUl.style.display!="none"){
			var v=obj.value;
			var ls=sUl.getElementsByTagName("LI");
			for(var i=0;i<ls.length;i++){
				if(ls[i].val==v){
					ls[i].className="chk";
					sUl.scrollTop=22*i;
				}else{
					ls[i].className="";
				}
			}
		}
	};
	oA.onmouseout=function(){
		oA.onblur=function(){
			sUl.style.display="none";
		};
	};
	
};

// 美化select ===== end =====

// 提交表单
function WBTB_submit()
{
	var WBTB_form=$i(iwms.myForm);
	var os = WBTB_form.tags("input");
	var o;
	for (var i=0;i<os.length;i++)	{
		if(os[i].type=="submit") {
			o=os[i];
		}
	} 
	if (o==null){
		WBTB_form.submit();
	}else{
		o.click();
	}
}
*/

// 新闻标题focus
function focusTitle(){
	$t=$("#"+iwms.title);
	if($t.length==1 && $t.val().length==0){
		$t.focus();
	}
}

setTimeout("focusTitle()",1000);