﻿function addJavascript(jsname) {
    var th = document.getElementsByTagName('head')[0];
    var s = document.createElement('script');
    s.setAttribute('type','text/javascript');
    s.setAttribute('src','js/'+jsname+'.js');
    th.appendChild(s);
}
var qsid=0;
var title='待解决问题';
function asktab(){
    if(!islogin){
        Ext.MessageBox.alert('提示', "请先登录，然后才能发表问题！");
        return;
    }
    var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('tab_ask');
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
        askpanel=new AskPanel({isNormal:isnormal});
        centerPanel.add(askpanel).show();
     }
}
function qlist(qs1,title1){
     qsid=qs1;
     title=title1;
     if(qsid<1000){skey="";}
     var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('tab_'+qsid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
        if(typeof(queslistrender)=='function'){
            new queslistrender(qsid,title,skey,isnormal);
        }
        else{
            addJavascript('queslist');
        }
     }
}
preViewer = {};
Ext.onReady(function(){
    Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createInterceptor(function() {
			this.form.items.each(function(item) {
				if (item.el.getValue() == item.emptyText) {
					item.el.dom.value = '';
				}
			});
		});
		Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createSequence(function() {
			this.form.items.each(function(item) {
				if (item.el.getValue() == '' && item.emptyText) {
					item.el.dom.value = item.emptyText;
				}
			});
		});
    var tpl = Ext.Template.from('preview-tpl', {
        compiled:true,
        getBody : function(v, all){
            return Ext.util.Format.stripScripts(v || all.description);
        },
        formatDate:function(sDate){var exp = sDate.toString().replace(new RegExp('\\/Date\\((-?[0-9]+)\\)\\/', 'g'), "new Date($1)");
		    var a=eval(exp);
		    return a.getFullYear()+"-"+(a.getMonth()+1)+"-"+a.getDate()+" "+a.getHours()+":"+a.getMinutes();
		}
    });
    preViewer.getTemplate = function(){
        return tpl;
    } 
    Ext.BLANK_IMAGE_URL="ext2/resources/images/default/s.gif";
    Ext.QuickTips.init();
    Ext.form.Field.prototype.stateful = false; 
    Ext.form.Field.prototype.msgTarget = 'side'; 
    var northPanel=new Ext.BoxComponent({ 
                    region:'north',
                    el: 'north',
                    height:170
                });
   var southPanel=new Ext.BoxComponent({
                    region:'south',
                    el:'south',
					id:"south-panel",
                    height: 62
                });
   var eastPanel=new Ext.Panel({
                    region:'east',
                    title: 'East Side',
                    collapsible: true,
                    split:true,
                    width: 225,
                    minSize: 175,
                    maxSize: 400,
                    layout:'fit',
                    margins:'0 5 0 0'
                 }); 
                 
    var knowledgemenu = new Ext.Panel({
    	            title: '翰维知道',
    	            contentEl:'knowledgemenu',
    	            border:false,
                    iconCls:'nav'
                });
    var libmenu = new Ext.Panel({
    	            title: '专题',
    	            contentEl:'libmenu',
    	            border:false,
                    iconCls:'nav'
                });
    /*var dictmenu = new Ext.Panel({
    	            title: '词典',
    	            contentEl:'dict',
    	            border:false,
                    iconCls:'nav'
                });*/
    var westPanel=new Ext.Panel({
                    region:'west',
                    el:'west',
                    title:'功能菜单',
                    collapsible: true,
                    //collapseMode:'mini',
                    split:true,
                    width: 175,
                    minSize: 175,
                    maxSize: 175,
                    margins:{left: 10, top: 0, right: 0, bottom: 0},
                    layout:'accordion',
                    layoutConfig:{
                        animate:true
                       // fill :false
                    },
                    items:[knowledgemenu, libmenu]
    }); 
    
    var centerPanel = new Ext.TabPanel({
           region:'center',
           id:"center-Panel",
           deferredRender:false,
           enableTabScroll:true,
           margins:{left: 0, top: 0, right: 10, bottom: 0},
           activeTab:0,
           items:[{
                title: '起始页',
                id :'StartPage',
			    contentEl:'centerABC',
                autoScroll:true
           }]
        });
     var viewport = new Ext.Viewport({
           layout:'border',
           items:[northPanel,southPanel,westPanel,centerPanel]
     });
     
     Ext.getBody().addClass('bodybackground');
     if(aqid>0&&t!=""){
        openquestion(aqid,t,s);
     }
     if(arid>0&&arti!=""){
        openArticle(arid,arti,11);
     }
      if(searchkey!=""&&searchmethod!=""){
         searchAll(searchkey,searchmethod);
      }
	  Ext.get('loading').remove();
        Ext.get('loading-mask').fadeOut({remove:true});
});
function openquestion(qid,qt,st){
        var tab;
        var centerpanel=Ext.getCmp('center-Panel');
        if(!(tab = centerpanel.getItem('_ALL_'+qid))){
            tab = new questionrender({
                id: '_ALL_'+qid,
                qid:qid,
                st: st,
                cls:'preview single-preview',
                title: qt,
                tabTip: qt,
                closable:true,
                autoScroll:true,
                border:true
            });
            centerpanel.add(tab);
        }
        centerpanel.setActiveTab(tab);
}
var searchcounter=0;
var skey="";
function search(){
    skey=document.getElementById("searchkey").value;
    if(skey!=""){
        qlist(1000+searchcounter,"搜索符合条件“"+skey+"”的结果");
        searchcounter++; 
   } 
}

function keysearch(e){
    var evt= e || window.event;
    var element=evt.srcElement || evt.target;
    var key= e.keyCode|| e.which
    if(key==13){
        search();
    }    
}

function searchAll(seak,seam){
    if(seak!=""){
        skey=seak;
        switch(seam){
            case '0':
                qlist(1000+searchcounter,"搜索符合条件“"+skey+"”的结果");
                searchcounter++;
                break;
            case '1':
                openMemberList(seak);
                break;
            case '2':
                openArticleList(seak);
                break;
            case '3':
                openBookSearch(seak);
                break;
            case '4':
                break;
            default:
                qlist(1000+searchcounter,"搜索符合条件“"+skey+"”的结果");
                searchcounter++;
                break;     
        } 
    } 
}

function login(){
    var loginw=new LoginWindow({
        title:'登录',
        id:'loginwin'
    });
    loginw.show(); 
}

function logout(){
    var conn=new Ext.data.Connection();
    conn.request({
        url:"loginjs.aspx", 
		params:{act:"LOGOUT"},
		method: 'post',
		scope: this,
		callback:function(options,success, response){ 
		    if(success){   
		      var Data=Ext.util.JSON.decode(response.responseText);
		      if(Data.success){
		            Ext.get("UserHeader1_logindiv").update(Data.info);
		            islogin=false;
		            location.reload();
		      }
		   }    
		   else{
		      Ext.MessageBox.alert("提示","退出时发生错误，请重试");
		      return;
		   }     
		}
    }); 
}

function openknown(){
     var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('knowledge_tab');
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
        if(typeof(knowledgeRender)=='function'){
            new knowledgeRender (isnormal);
        }
        else{
            addJavascript('knowledge');
        }
     }
}

function openMemberList(searchkey){
    var centerPanel=Ext.getCmp('center-Panel');
     if(!searchkey){
        searchkey="";
     } 
     var tabid='specialist'+(searchkey==""?"":searchcounter);
     var tab = centerPanel.getItem(tabid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new SpecialistPanel ({
                id:tabid,
                title:'专家库'+(searchkey==""?"":" - 搜索："+searchkey),
                tabTip:  searchkey==""?"":"搜索："+searchkey,
                searchkey:searchkey  
            });
            centerPanel.add(tab).show();
            tab.store.load({params:{start:0, limit:10}});
            searchcounter++;
     }
}

function openArticleList(searchkey){
    var centerPanel=Ext.getCmp('center-Panel');
     if(!searchkey){
        searchkey="";
     } 
     var tabid='articlelist'+(searchkey==""?"":searchcounter);
     var tab = centerPanel.getItem(tabid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new ArticleListPanel ({
                id:tabid,
                title:'知识库'+(searchkey==""?"":" - 搜索："+searchkey),
                tabTip:  searchkey==""?"":"搜索："+searchkey,
                caid:23,
                searchkey:searchkey,
                author:'',
                closable:true
            });
            centerPanel.add(tab).show();
            tab.store.load({params:{start:0, limit:25}});
            searchcounter++;
     }
}

function openArList(caid){
    var centerPanel=Ext.getCmp('center-Panel');
     if(!caid){
        caid=23;
     } 
     var tabid='arlist'+caid;
     var tab = centerPanel.getItem(tabid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new ArticleListPanel ({
                id:tabid,
                title: caid==23?'文章库':'软件库',
                caid:caid,
                searchkey:'',
                author:'',
                closable:true
            });
            centerPanel.add(tab).show();
            tab.store.load({params:{start:0, limit:25}});
            searchcounter++;
     }
}

function openBookSearch(searchkey){
    var centerPanel=Ext.getCmp('center-Panel');
     if(!searchkey){
        searchkey="";
     } 
     var tabid='booklist'+(searchkey==""?"":searchcounter);
     var tab = centerPanel.getItem(tabid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new ArticleListPanel ({
                id:tabid,
                title:'在线阅读'+(searchkey==""?"":" - 搜索："+searchkey),
                tabTip:  searchkey==""?"":"搜索："+searchkey,
                caid:11,
                searchkey:searchkey,
                author:'',
                closable:true
            });
            centerPanel.add(tab).show();
            tab.store.load({params:{start:0, limit:25}});
            searchcounter++;
     }
}

function openBookList(){
    var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('BookPanel');
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new BookPanel ();
            centerPanel.add(tab).show();
            tab.init();
     }
}

function openArticle_old(arid,title,caid){
    var centerPanel=Ext.getCmp('center-Panel');
    if(!caid){caid=23;}
     var tab = centerPanel.getItem('Ar_'+arid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            tab= new ArticleDetailsPanel ({
                id:'Ar_'+arid,
                title:title,
                tabTip:title, 
                arid:arid,
                closable:true
            });
            centerPanel.add(tab).show();
            tab.init(caid);
     }
}

function openArticle(arid,title,caid){
    if(!caid){caid=23;}
    var tab = new fullWindow({
        url:"/articles/"+caid+"/"+arid+".html",
        title:title
    });
    tab.show();
    tab.init();
    Ext.Ajax.request({
        url: 'ArticleCounter.aspx',
        params: { arid: arid }
    });
}

function openArticle(arid,title,caid){
    if(!caid){caid=23;}
    var tab = new fullWindowOld({
        url:"/articles/"+caid+"/"+arid+".html",
        title:title
    });
    tab.show();
    tab.init();
    Ext.Ajax.request({
        url: 'ArticleCounter.aspx',
        params: { arid: arid }
    });
}

function openArticle1(arid,title,url){
     var tab = new fullWindow({
        url:url,
        title:title
    });
    tab.show();
    tab.init();
    Ext.Ajax.request({
        url: 'ArticleCounter.aspx',
        params: { arid: arid }
    });
}

function openMember(arid,title){
    var centerPanel=Ext.getCmp('center-Panel');
    var caid=10000; 
     var tab = centerPanel.getItem('Sm_'+arid);
     if(tab){
        centerPanel.setActiveTab(tab);
     }
     else{
            //tab= new ArticleDetailsPanel ({
            tab= new SpecialistinfoPanel ({
                id:'Sm_'+arid,
                title:'专家：'+title,
                tabTip:title, 
                arid:arid,
                aname:title
            });
            centerPanel.add(tab).show();
            //tab.init(caid);
            tab.init();
     }
}

function taglist(tag){
    var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('tag_List');
     var title;
     if(tag){
        title="标签："+tag;
     }
     else{
        title="热门标签";
     }
     if(tab){
        centerPanel.setActiveTab(tab);
        tab.tag=tag;
        tab.init(false);
     }
     else{
            tab= new TagPanel ({
                id:'tag_List',
                title:title,
                tabTip:title,
                isNormal:isnormal,
                tag:tag 
            });
            centerPanel.add(tab).show();
            tab.init(true);
     }
}

function openDict(link){
    var centerPanel=Ext.getCmp('center-Panel');
     var tab = centerPanel.getItem('dict_tab');
     /*var link;
     if(s==0){
        link='http://www.sinoserviceone.com:8080/ITSMDict/';
     }
     else{
        link='http://www.sinoserviceone.com:8080/ITSMDict/term_index.jsp';
     }*/
     if(tab){
        centerPanel.setActiveTab(tab);
        tab.setSrc(link);
     }
     else{
            tab= new Ext.ux.ManagedIframePanel({
               id: 'dict_tab',
               title:'ITSM词典',
               closable:true,
               loadMask:{msg:'正在载入...'},
               defaultSrc :  link
            });
            centerPanel.add(tab).show();
     }
}
