// JavaScript Document
// -- Coded by Alex 2007.10.8
// -- Updated by Alex 2009.9.24







// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}


// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
   {
	  if( !xNode ) { xNode = this; } 
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 )
      {
         return xItems[0];
      }
      else
      {
         return null;
      }
   }
   
   // prototying the Element
   Element.prototype.selectSingleNode = function(cXPathString)
   {    
      if(this.ownerDocument.selectSingleNode)
      {
         return this.ownerDocument.selectSingleNode(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }

}

function InterviewHelper(instanceName) {
    //全局函数

    this.Instance = instanceName;
    
    this.CurrentNum; //当前显示的页
    this.TotalFrames;
    this.ActiveStyle = "normal"; //导航按钮激活时的样式
    this.IsIE;
    this.xml = "interview.xml";
    this.xmlDoc;
    this.loaded = false;
    this.ListArray = {};  //用于保存多个ListHelper实例



    this.LoadXML = function() {
        // IE 浏览器
        if (window.ActiveXObject) {
            IsIE = true;
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.load(xml);
            return (xmlDoc.parseError.errorCode == 0);

        } // Mozilla, Firefox, Opera浏览器
        else if (document.implementation && document.implementation.createDocument) {
            IsIE = false;
            //xmlDoc = document.implementation.createDocument("","doc",null);  
            xmlDoc = document.implementation.createDocument("", "", null);
            xmlDoc.async = false;
            return xmlDoc.load(xml);
        }
        else {
            IsIE = false;
            document.write("你的浏览器不支持Javascript,无法加载数据<br>");
        }
        return false;
    };



    //显示专家列表,rowsperframe表示每页显示数量
    this.iShow = function(rowsperframe, website) {
        var RealRows = 0;
        var FrameStr = "";
        var GuideStr = "";

        if (this.loaded == false) {
            exShowEmpty();
            return;
        }
        else {
            xmlDoc.setProperty('SelectionLanguage', 'XPath');
            var EX = xmlDoc.documentElement;
            var current = EX.attributes.getNamedItem("current").value;
            var xpath;
            if (website.toLowerCase() == 'all')
                xpath = 'issue';
            else
                xpath = 'issue[@website="all"]|issue[contains(@website,"' + website.toLowerCase() + '")]';

            var data = EX.selectNodes(xpath);

            if (data == null) { exShowEmpty(); return; }
            if (IsIE)
                RealRows = data.length;
            else
                RealRows = Math.floor(data.length / 2);

            var totalrows = RealRows;
            if (rowsperframe > RealRows) rowsperframe = RealRows;

            TotalFrames = Math.floor(((totalrows - 1) / rowsperframe) + 1);
            for (j = 0; j < TotalFrames; j++) {
                var style = "style='display:none;'";
                FrameStr += "<div class='expert_review ' id='frame" + (j + 1) + "' " + style + ">";
                //alert("建立第"+j+"个frame");
                for (i = 1; i <= rowsperframe; i++) {
                    var index = j * rowsperframe + i - 1;
                    if (index >= totalrows) break;
                    var name, type, title, company, article, imgLarge, imgSmall, synopsis, issue;
                    if (IsIE) {
                        var m = data[index];
                        issue = m.attributes.getNamedItem("no").value;
                        name = m.selectSingleNode("name").text;
                        type = m.selectSingleNode('type').text;
                        title = m.selectSingleNode("title").text;
                        company = m.selectSingleNode("company").text;
                        article = m.selectSingleNode("article").text;
                        articleLink = m.selectSingleNode("link").text;
                        imgLarge = m.selectSingleNode("imgLarge").text;
                        imgSmall = m.selectSingleNode("imgSmall").text;
                        synopsis = m.selectSingleNode("synopsis").childNodes[0].text;
                    }
                    else {
                        var m = data[index];
                        issue = m.attributes.getNamedItem("no").value;
                        name = m.selectSingleNode("name").childNodes[0].nodeValue;
                        type = m.selectSingleNode('type').childNodes[0].nodeValue;
                        title = m.selectSingleNode("title").childNodes[0].nodeValue;
                        company = m.selectSingleNode("company").childNodes[0].nodeValue;
                        article = m.selectSingleNode("article").childNodes[0].nodeValue;
                        articleLink = m.selectSingleNode("link").childNodes[0].nodeValue;
                        imgLarge = m.selectSingleNode("imgLarge").childNodes[0].nodeValue;
                        imgSmall = m.selectSingleNode("imgSmall").childNodes[0].nodeValue;
                        synopsis = m.selectSingleNode("synopsis").childNodes[0].childNodes[0].nodeValue;
                    }
                    //if(synopsis.length>70)synopsis=synopsis.substr(0,82)+"...";
                    FrameStr += "<div class='expert_outline b m_r l' id='issue" + issue + "'>";
                    FrameStr += '<div class="expert_review_photo l"><img src="' + imgSmall + '" width="115" height="150" alt="' + title + '"/></div>';
                    FrameStr += '<div class="expert_review_content l">';
                    FrameStr += '<div class="expert_review_name"><span class="expert_review_name2">' + name + '</span> - ' + title + '</div>';
                    //FrameStr += '<div >'+article+'</div>';
                    FrameStr += '<div class="expert_review_major_text"><a href="' + articleLink + '" target="_blank">' + synopsis + '</a> </div>';
                    FrameStr += '</div></div>';
                }

                FrameStr += '</div>';
            }

            GuideStr += '<div class="pageNum">';
            GuideStr += '<div class="num">';
            GuideStr += '<A  title="上一页" href="javascript:' + this.Instance + '.iShowPreviousFrame()">上一页</A>';
            if (TotalFrames > 1) {
                for (k = 1; k <= TotalFrames; k++) {
                    var style = "";
                    if (k > 10) style = ' style="display:none" ';
                    GuideStr += '<A id="page' + k + '" title=第' + k + '页 href="javascript:' + this.Instance + '.iShowFrame(' + k + ')" ' + style + '>' + k + '</A>';
                }
            }
            GuideStr += '<A  title="下一页" href="javascript:' + this.Instance + '.iShowNextFrame()">下一页</A>';
            GuideStr += "</div></div>";

            document.write(GuideStr);
            document.write(FrameStr);
            this.iShowFrame(1);


        }
    };





    //显示推荐人物
    //website:current,cs,ls.....
    this.iShowCurrent = function(website) {
        var FrameStr = "";
        if (this.loaded == false) {
            exShowEmpty();
            return;
        }
        else {
            var EX = xmlDoc.documentElement;
            var currents = EX.attributes.getNamedItem(website).value.split(",");

            if (currents == null) { exShowEmpty(); return; }
            var name, type, title, company, article, imgLarge, imgSmall, synopsis, articleLink;
            for (i = 0; i < currents.length; i++) {
                var current = currents[i];
                if (IsIE) {
                    var m = EX.selectSingleNode("issue[@no='" + current + "']");
                    name = m.selectSingleNode("name").text;
                    type = m.selectSingleNode('type').text;
                    title = m.selectSingleNode("title").text;
                    company = m.selectSingleNode("company").text;
                    article = m.selectSingleNode("article").text;
                    articleLink = m.selectSingleNode("link").text;
                    imgLarge = m.selectSingleNode("imgLarge").text;
                    imgSmall = m.selectSingleNode("imgSmall").text;
                    synopsis = m.selectSingleNode("synopsis").childNodes[0].text;
                }
                else {
                    var m = EX.selectSingleNode("issue[@no='" + current + "']");
                    name = m.selectSingleNode("name").childNodes[0].nodeValue;
                    type = m.selectSingleNode('type').childNodes[0].nodeValue;
                    title = m.selectSingleNode("title").childNodes[0].nodeValue;
                    company = m.selectSingleNode("company").childNodes[0].nodeValue;
                    article = m.selectSingleNode("article").childNodes[0].nodeValue;
                    articleLink = m.selectSingleNode("link").childNodes[0].nodeValue;
                    imgLarge = m.selectSingleNode("imgLarge").childNodes[0].nodeValue;
                    imgSmall = m.selectSingleNode("imgSmall").childNodes[0].nodeValue;
                    synopsis = m.selectSingleNode("synopsis").childNodes[0].childNodes[0].nodeValue;
                }
                //if(synopsis.length>70)synopsis=synopsis.substr(0,82)+"...";
                FrameStr += "<div class='new_expert'>";
                FrameStr += '<div class="new_expert_photo l"><a href="' + articleLink + '" target="_blank">';
                FrameStr += '<img src="' + imgLarge + '" width="200" height="250" border="0" alt="' + article + '" /></a></div>';
                FrameStr += '<div class="new_expert_con l">';
                FrameStr += '<div class="new_expert_name">';
                FrameStr += '<div class="qianjia_talk_t l"><img src="bkimages/qianjia_talk_t.gif" width="99" height="23" /></div>';
                FrameStr += '<div class="new_expert_name_con l"><span class="new_expert_name1">' + name + '</span></div>';
                FrameStr += '</div>';
                FrameStr += '<div class="new_expert_title_con">--' + title + '</div>';
                FrameStr += '<div class="expert_talk_title "><a href="' + articleLink + '" target="_blank">' + article + '</a></div>';
                FrameStr += '<div class="expert_talk_major">' + synopsis + '</div>';
                FrameStr += '</div></div>';

            }
            document.write(FrameStr);
        }
    };


    //建立空白页
    this.exShowEmpty = function() {
    };

    //切换到指定页
    this.iShowFrame = function(num) {
        if (num < 1) num = 1;
        if (num > TotalFrames) num = TotalFrames;
        if (num == CurrentNum) return;
        var LastNum;
        LastNum = CurrentNum;
        CurrentNum = num;

        //处理显示页
        var activeFrame = document.getElementById("frame" + CurrentNum);
        if (LastNum != null) var lastFrame = document.getElementById("frame" + LastNum);

        activeFrame.style.display = "";
        if (LastNum != null) lastFrame.style.display = "none";

        //处理分页按钮
        var activePage = document.getElementById("page" + CurrentNum);
        if (LastNum != null) var lastPage = document.getElementById("page" + LastNum);

        var newActivePage = document.createElement('<span id="page' + CurrentNum + '" title=第' + CurrentNum + '页 class="normal"></span>');
        if (LastNum != null) var newLastPage = document.createElement('<A id="page' + LastNum + '" title=第' + LastNum + '页 href="javascript:' + this.Instance + '.iShowFrame(' + LastNum + ')"></A>');

        newActivePage.innerHTML = CurrentNum;
        if (LastNum != null) newLastPage.innerHTML = LastNum;

        if (activePage != null) {
            var pageParent = activePage.parentElement;
            pageParent.replaceChild(newActivePage, activePage);
        }
        if (LastNum != null) pageParent.replaceChild(newLastPage, lastPage);

        if (typeof (pageParent) == 'undefined') return;
        var count = pageParent.childNodes.length;

        if (num < 6) {
            for (var i = 1; i < num; i++) {
                pageParent.childNodes[i].style.display = 'block';
            }

            for (var i = num + 1; i < count - 1; i++) {
                if (i <= 10) {
                    pageParent.childNodes[i].style.display = 'block';
                }
                else {
                    pageParent.childNodes[i].style.display = 'none';
                }
            }

        } else if (num > count - 7) {
            for (var i = 1; i < num; i++) {
                if (i >= count - 12) {
                    pageParent.childNodes[i].style.display = 'block';
                }
                else {
                    pageParent.childNodes[i].style.display = 'none';
                }
            }

            for (var i = num + 1; i < count - 1; i++) {
                pageParent.childNodes[i].style.display = 'block';
            }
        }
        else {
            for (var i = 1; i < count - 1; i++) {

                var d = num - 1 - i;
                if (d >= 5 || d < -6) {
                    pageParent.childNodes[i].style.display = 'none';
                }
                else {
                    pageParent.childNodes[i].style.display = 'block';
                }
            }
        }


    };

    this.iShowNextFrame = function() {
        this.iShowFrame(CurrentNum + 1);
    };

    this.iShowPreviousFrame = function() {
        this.iShowFrame(CurrentNum - 1);
    };

    this.iShowList = function(type, website, len, rowsperframe) {
        var ilist;
        eval("ilist = this.ListArray." + type);
        if ((typeof (ilist) == "undefined")) {
            eval("this.ListArray." + type + " = new this.ListHelper(type, website, len, rowsperframe)");
            eval("ilist = this.ListArray." + type);
        }

        ilist.show();

    };







    this.ListHelper = function(type, website, len, rowsperframe) {
        this.listCurrentNum; //当前显示的页
        this.listTotalFrames;
        this.name = type;
        this.type = type; //type-类别:专家,高层,媒体
        this.website = website; //website-所调用的频道
        this.len = len; //len-显示宽度
        this.rowsperframe = rowsperframe; //rowsperframe-每页条数

        this.test = function() {
            alert(this.name);
        };

        //显示指定类别的人物列表
        this.show = function() {
            var RealRows = 0;
            if (loaded == false) {
                this.exShowEmpty();
                return;
            }
            else {
                var type = this.type;
                var website = this.website;
                var len = this.len;
                var rowsperframe = this.rowsperframe;
                var listTotalFrames;

                xmlDoc.setProperty('SelectionLanguage', 'XPath');
                var EX = xmlDoc.documentElement;
                var current = EX.attributes.getNamedItem("current").value;
                var xpath;
                if (website.toLowerCase() == 'all') {
                    if (type.toLowerCase() == 'all')
                        xpath = 'issue';
                    else
                        xpath = 'issue[type="' + type + '"]';
                }
                else {
                    if (type.toLowerCase() == 'all')
                        xpath = 'issue[@website="all"]|issue[contains(@website,"' + website.toLowerCase() + '")]';
                    else
                        xpath = 'issue[type="' + type + '" and @website="all"]|issue[type="' + type + '" and contains(@website,"' + website.toLowerCase() + '")]';
                }

                var data = EX.selectNodes(xpath);

                if (data == null) { this.exShowEmpty(); return; }
                if (IsIE)
                    RealRows = data.length;
                else {
                    var temp = data.length / 2;
                    RealRows = Math.floor(temp);
                }
                var totalrows = RealRows;
                if (rowsperframe > RealRows) rowsperframe = RealRows;
                if (rowsperframe == 0)
                    TotalFrames = 1;
                else
                    TotalFrames = Math.floor(((totalrows - 1) / rowsperframe) + 1);
                this.listTotalFrames = TotalFrames;


                var FrameStr = "";
                for (j = 0; j < TotalFrames; j++) {
                    var style = "style='display:none;'";
                    FrameStr += "<div class='expert_list' id='" + this.name + "listframe" + (j + 1) + "' " + style + ">";
                    //alert("建立第"+j+"个frame");

                    for (i = 1; i <= rowsperframe; i++) {
                        var index = j * rowsperframe + i - 1;
                        if (index >= totalrows) { FrameStr += "<br/>"; continue; }

                        var name, type, title, company, article, imgLarge, imgSmall, synopsis, issue;
                        issue = "";
                        if (IsIE) {
                            var m = data[index];
                            issue = m.attributes.getNamedItem("no").value;
                            name = m.selectSingleNode("name").text;
                            type = m.selectSingleNode('type').text;
                            title = m.selectSingleNode("title").text;
                            company = m.selectSingleNode("company").text;
                            article = m.selectSingleNode("article").text;
                            articleLink = m.selectSingleNode("link").text;
                            imgLarge = m.selectSingleNode("imgLarge").text;
                            imgSmall = m.selectSingleNode("imgSmall").text;
                            synopsis = m.selectSingleNode("synopsis").childNodes[0].text;
                        }
                        else {
                            var m = data[index];
                            issue = m.attributes.getNamedItem("no").value;
                            name = m.selectSingleNode("name").childNodes[0].nodeValue;
                            type = m.selectSingleNode('type').childNodes[0].nodeValue;
                            title = m.selectSingleNode("title").childNodes[0].nodeValue;
                            company = m.selectSingleNode("company").childNodes[0].nodeValue;
                            article = m.selectSingleNode("article").childNodes[0].nodeValue;
                            articleLink = m.selectSingleNode("link").childNodes[0].nodeValue;
                            imgLarge = m.selectSingleNode("imgLarge").childNodes[0].nodeValue;
                            imgSmall = m.selectSingleNode("imgSmall").childNodes[0].nodeValue;
                            synopsis = m.selectSingleNode("synopsis").childNodes[0].childNodes[0].nodeValue;
                        }
                        var content, clen;
                        switch (type) {
                            case "专家":
                                content = "<font color='orange'>" + name + "</font> " + title;
                                clen = len + 30;
                                break;
                            case "高层":
                                content = "<font color='red'>" + name + "</font> " + title;
                                clen = len + 27;
                                break;
                            case "传媒":
                                content = "<font color='green'>" + name + "</font> " + title;
                                clen = len + 29;
                                break;
                            default:
                                content = "<font color='blue'>" + name + "</font> " + title;
                                clen = len + 28;
                        }
                        if (content.length > clen) content = content.substr(0, clen - 1) + "...";
                        FrameStr += "第" + issue + "期: <a href='" + articleLink + "' title='" + title + "' target='_blank' onmouseover='" + Instance + '.ListArray.' + this.name + ".onMouseOver(\"" + issue + "\",event)' onmouseleave='" + Instance + '.ListArray.' + this.name + ".onMouseLeave()'>" + content + "</a><br/> ";

                    } //for i
                    FrameStr += "</div>";
                } //for j



                document.write(FrameStr);
                this.iShowGuide();
                this.iShowListFrame(1);
            }
        }

        this.iShowGuide = function iShowGuide() {
            var GuideStr = "";
            GuideStr += '<div class="pageListNum">';
            GuideStr += '<div class="listNum">';
            if (this.listTotalFrames > 1) {
                GuideStr += '<A  title="上一页" href="javascript:' + Instance + '.ListArray.' + this.name + '.iShowListPreviousFrame()"><<</A>';
                for (k = 1; k <= this.listTotalFrames; k++) {
                    GuideStr += '<A id="' + this.name + 'page' + k + '" title=第' + k + '页 href="javascript:' + Instance + '.ListArray.' + this.name + '.iShowListFrame(' + k + ')">' + k + '</A>';
                }
                GuideStr += '<A  title="下一页" href="javascript:' + Instance + '.ListArray.' + this.name + '.iShowListNextFrame()">>></A>';
            }
            GuideStr += "</div></div>";
            document.write(GuideStr);
        }

        this.iShowBox = function iShowBox(msg, x, y) {
            var box = document.getElementById(this.name + "box");
            if (box == null) {
                box = document.createElement("<div id='" + this.name + "box'></div>");
                box.className = "expert_outline b";
                box.style.position = "absolute";
                box.style.backgroundColor = "white";
                box.innerHTML = msg;
                document.body.appendChild(box);
            }
            else {
                box.innerHTML = msg;
                box.style.display = "block";
            }
            box.style.pixelTop = y;
            box.style.pixelLeft = x;
        }

        this.iHideBox = function iHideBox() {
            var box = document.getElementById(this.name + "box");
            if (box != null) box.style.display = "none";
        }


        //切换到指定列表页
        this.iShowListFrame = function iShowListFrame(num) {
            if (num < 1) num = 1;
            if (num > this.listTotalFrames) num = this.listTotalFrames;

            if (num == this.listCurrentNum) return;
            var LastNum = this.listCurrentNum;
            this.listCurrentNum = num;

            //处理显示页
            var activeFrame = document.getElementById(this.name + "listframe" + this.listCurrentNum);
            if (LastNum != null) var lastFrame = document.getElementById(this.name + "listframe" + LastNum);

            activeFrame.style.display = "";
            if (LastNum != null) lastFrame.style.display = "none";

            //处理分页按钮
            var activePage = document.getElementById(this.name + "page" + this.listCurrentNum);
            if (LastNum != null) var lastPage = document.getElementById(this.name + "page" + LastNum);

            var newActivePage = document.createElement('<span id="' + this.name + 'page' + this.listCurrentNum + '" title=第' + this.listCurrentNum + '页 class="normal"></span>');
            if (LastNum != null) var newLastPage = document.createElement('<A id="' + this.name + 'page' + LastNum + '" title=第' + LastNum + '页 href="javascript:' + Instance + '.ListArray.' + this.name + '.iShowListFrame(' + LastNum + ')"></A>');

            newActivePage.innerHTML = this.listCurrentNum;
            if (LastNum != null) newLastPage.innerHTML = LastNum;

            if (activePage != null) {
                var pageParent = activePage.parentElement;
                pageParent.replaceChild(newActivePage, activePage);
            }
            if (LastNum != null) pageParent.replaceChild(newLastPage, lastPage);
        }

        this.iShowListNextFrame = function iShowListNextFrame() {
            this.iShowListFrame(this.listCurrentNum + 1);
        }

        this.iShowListPreviousFrame = function iShowListPreviousFrame() {
            this.iShowListFrame(this.listCurrentNum - 1);
        }

        this.onMouseOver = function onMouseOver(issue, e) {
            var content = document.getElementById("issue" + issue);
            if (content == null) return;
            var x, y;
            x = e.x;
            y = e.y + scrollTop();
            this.iShowBox(content.innerHTML, x, y);
        }

        this.onMouseLeave = function onMouseLeave() {
            this.iHideBox();
        }
        this.exShowEmpty = function() {
        };
    }


    //load xml
    var Instance = this.Instance;
    var CurrentNum = this.CurrentNum; //当前显示的页
    var TotalFrames = this.TotalFrames;
    var ActiveStyle = this.ActiveStyle; //导航按钮激活时的样式
    var IsIE = this.IsIE;
    var xml = this.xml;
    var xmlDoc = this.xmlDoc;
    this.loaded = this.LoadXML();
    var loaded = this.loaded;

    //if (!this.Loaded) document.write("无法加载数据");


 
}









//-----------------------------------------------list




function pageWidth()
{
    return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}

function pageHeight()
{
    return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

function scrollLeft()
{
    return window.pageXOffset != null? window.pageXOffset: document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft:document.body != null? document.body.scrollLeft:null;
}

function scrollTop()
{
    return window.pageYOffset != null? window.pageYOffset: document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body != null? document.body.scrollTop:null;
}






