﻿Type.registerNamespace('TCIT.Web.RSS');
Type.registerNamespace('TCIT.Web.Tools');


GoogleFeed_ItemBuilder.UserFormatedHtmlContentBuilder = function(container, item) {
    var div = document.createElement("Div");
    div.innerHTML = GoogleFeed_ItemBuilder.TextDecode(item.contentSnippet);
    container.appendChild(div);
}
GoogleFeed_ItemBuilder.SimpleBuilder = function(container, item, hasAlt) {
    if (item.title == "") return null;

    var a = document.createElement("A");
    a.setAttribute("target", "_blank");
    a.setAttribute("href", item.link);
    a.appendChild(document.createTextNode(GoogleFeed_ItemBuilder.TextDecode(item.title)));
    if (hasAlt) { a.setAttribute("title", item.contentSnippet); }
    container.appendChild(a);
    return a;
}
GoogleFeed_ItemBuilder.GetBuilder = function(builderType) {
    switch (builderType) {
        case 0:
            return GoogleFeed_ItemBuilder.SimpleBuilder;
            break;
        case 1:
            return GoogleFeed_ItemBuilder.SimpleTitleBuilder;
            break;
        case 2:
            return GoogleFeed_ItemBuilder.AuthorAndTitleBuilder;
            break;
        case 3:
            return GoogleFeed_ItemBuilder.TitleAndContentBuilder;
            break;
        case 4:
            return GoogleFeed_ItemBuilder.TitleAndPictureAndContentBuilder;
            break;
        case 5:
            return GoogleFeed_ItemBuilder.TitleAndHTMLContentBuilder;
            break;
        case 6:
            return GoogleFeed_ItemBuilder.UserFormatedHtmlContentBuilder;
            break;
        default:
            return GoogleFeed_ItemBuilder.SimpleBuilder;
            break;
    }
}

TCIT.Web.Tools.Dictionary = function() {
    var _list = new Array();
    this.Add = function(str, obj) { _list.push([str, obj]); }
    this.IndexOf = function(str) { for (var i = 0; i < _list.length; i++) { if (_list[i][0] == str) { return i; } } return -1; }
    this.Search = function(str) { for (var i = 0; i < _list.length; i++) { if (str.indexOf(_list[i][0]) >= 0) { return _list[i][1]; } } return null; }
    this.Item = function(str) { var index = this.IndexOf(str); return index >= 0 ? _list[index][1] : null; }
    this.Count = function() { return _list.length; }
}

TCIT.Web.RSS.Feed = function(_title, _link, _contentSnippet, _content, _author, _publishedDate, _categories) {
    this.title = _title || null;
    this.link = _link || null;
    this.contentSnippet = _contentSnippet || null;
    this.content = _content || null;
    this.author = _author || null;
    this.publishedDate = _publishedDate || null;
    this.categories = _categories || null;
}

TCIT.Web.RSS.Request = function(rss) {
    var base = this;
    var _service;
    var _url = rss;
    var _container;
    var _count;
    var _buildType;
    var _xmlHttp;
    var _fnOnReceive;
    var _fnOnLoadCompleted = function(rssRequest) {
        var xmlHttp = rssRequest.GetXmlHttpInstance();
        //if (xmlHttp.parseError != null && xmlHttp.parseError.errorCode != 0) {
        //   alert("You have error " + xmlHttp.parseError.reason);
        //   return;
        //}

        //        Using Service
        var result;
        if (window.ActiveXObject != null) {
            var parser = new ActiveXObject("microsoft.XMLDOM");
            parser.loadXML(xmlHttp.responseText);
            result = parser.selectSingleNode("string").childNodes[0].text;
        }
        else {
            var parser = new DOMParser();
            parser = parser.parseFromString(xmlHttp.responseText, "text/xml");
            result = parser.childNodes[0].textContent;
        }
        if (rssRequest.Result.Parent.GetBuildType() == 5) {
            var m_urls, regEx;
            var rss_Ret = result;
            //url地址
            regEx = /href=&quot;\S*\s*&quot;/ig;
            while ((m_urls = regEx.exec(rss_Ret)) != null)// (m_urls = rss_Ret.exec(regEx) != null) {
            {
                var orient_url = m_urls[0]
                var t_url = m_urls[0]
                var m_domain = tcitSSODomain;
                if (m_domain.trim() != "") {
                    m_domain = "/" + m_domain;
                }
                t_url = t_url.replace("[domain]", m_domain);
                if (t_url.indexOf("http") < 0) {

                    t_url = t_url.replace("href=&quot;", "");
                    t_url = t_url.replace("&quot;", "");
                    t_url = "href=&quot;" + tcitHostUrl + t_url + "&quot;";
                }
                result = result.replace(orient_url, t_url);
            }
            //JPG图片
            rss_Ret = result;
            regEximg = /src=&quot;\S*\s*jpg&quot;/ig;
            while ((m_urls = regEximg.exec(rss_Ret)) != null) {
                var orient_url = m_urls[0]
                var t_url = m_urls[0]
                t_url = t_url.replace("src=&quot;", "");
                t_url = t_url.replace("&quot;", "");
                //var httpUrl = window.location.href.substring(0, window.location.href.indexOf(tcitSSODomain));
                t_url = "src=&quot;" + tcitHostUrl + t_url + "&quot;";
                result = result.replace(orient_url, t_url);
            }
            //gif图片
            rss_Ret = result;
            regEximg = /src=&quot;\S*\s*gif&quot;/ig;
            while ((m_urls = regEximg.exec(rss_Ret)) != null) {
                var orient_url = m_urls[0]
                var t_url = m_urls[0]
                t_url = t_url.replace("src=&quot;", "");
                t_url = t_url.replace("&quot;", "");
                // var httpUrl = window.location.href.substring(0, window.location.href.indexOf(tcitSSODomain));
                t_url = "src=&quot;" + tcitHostUrl + t_url + "&quot;";
                result = result.replace(orient_url, t_url);
            }
        }
        eval("rssRequest.Result.NodeList = " + result.replace(/[\r\n]/ig, ""));

        //        var nodeList;
        //        if (window.ActiveXObject != null)
        //        {
        //            var parser = new ActiveXObject("microsoft.XMLDOM");
        //            parser.loadXML(xmlHttp.responseText);
        //            nodeList = parser.selectNodes("/*/channel/item");
        //            if (nodeList.length == 0)
        //            {
        //                nodeList = parser.selectNodes("//*/item");
        //            }
        //        }
        //        else
        //        {
        //            var parser = new DOMParser();
        //            parser = parser.parseFromString(xmlHttp.responseText, "text/xml");

        //            var div = document.createElement("div");
        //            div.innerHTML = parser.childNodes[1].innerHTML;
        //            nodeList = div.getElementsByTagName("item");
        //        }

        //        var arr = new Array();
        //        var emptyNode = {text:""};
        //        for (var i=0; i<nodeList.length; i++)
        //        {
        //            var feed = new TCIT.Web.RSS.Feed();
        //            feed.title = (nodeList[i].getElementsByTagName("title")[0] || emptyNode).text;
        //            feed.link = (nodeList[i].getElementsByTagName("link")[0] || emptyNode).text;
        //            feed.contentSnippet = (nodeList[i].getElementsByTagName("description")[0] || emptyNode).text;
        //            feed.content = feed.contentSnippet;
        //            feed.author = (nodeList[i].getElementsByTagName("author")[0] || emptyNode).text;
        //            feed.publishedDate = (nodeList[i].getElementsByTagName("pubDate")[0] || emptyNode).text;
        //            arr.push(feed);
        //        }
        //        rssRequest.Result.NodeList = arr;

        _fnOnReceive(rssRequest.Result);
    }

    this.Result = { Parent: base };

    this.SetService = function(service) { _service = service; }
    this.SetContainerId = function(listid) { _container = document.getElementById(listid); }
    this.SetNumEntries = function(num) { _count = num; }
    this.SetBuildType = function(btype) { _buildType = btype; }
    this.GetXmlHttpInstance = function() { return _xmlHttp; }
    this.GetContainer = function() { return _container; }
    this.GetNumEntries = function() { return _count; }
    this.GetBuildType = function() { return _buildType; }

    this.Load = function(handler) {
        _fnOnReceive = handler;
        _xmlHttp.open("POST", _service, true);
        _xmlHttp.send(_url);

        //_xmlHttp.open("GET", _url, true);
        //_xmlHttp.send();
    }

    var _init = function() {
        try {
            _xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        }
        catch (e1) {
            try {
                _xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
            }
            catch (e2) {
                try {
                    _xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e3) {
                    try {
                        _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e4) {
                        _xmlHttp = new XMLHttpRequest();
                    }
                }
            }
        }
        _xmlHttp.onreadystatechange = function() {
            switch (_xmlHttp.readyState) {
                case 1: //LOADING
                    break;
                case 2: //LOADED
                    break;
                case 3: //INTERACTIVE
                    break;
                case 4: //COMPLETED
                    if (_xmlHttp.status == 200) {
                        _fnOnLoadCompleted(base);
                    }
                    else {
                        var li = document.createElement("li");
                        li.innerHTML = "Exception: " + _xmlHttp.status + ":" + _xmlHttp.responseText
                        _container.appendChild(li);
                    }
                    break;
            }
        }
    }
    _init();
}

function GoogleFeed_HideEmptyInstance(container) {
    var dragPanel = GetParentByTagName(container, "table");
    if (dragPanel != null && dragPanel.parentNode.tagName.toUpperCase() == "DIV") {
        dragPanel.parentNode.style.display = "none";
    }
}

function TCITFeed_Initialize(listid, rss, num, btype) {
    var tcitFeed = tcitFeed_items.Search(listid);
    if (tcitFeed == null) {
        return;
    }

    var feed = new TCIT.Web.RSS.Request(rss);
    feed.SetService(tcitFeed.Service);
    feed.SetContainerId(listid);
    feed.SetNumEntries(num);
    feed.SetBuildType(btype);
    //feed.setResultFormat(google.feeds.Feed.JSON_FORMAT); //google.feeds.Feed.XML_FORMAT // google.feeds.Feed.MIXED_FORMAT

    var tcit_receive = function(result) {
        //if (!result.error)
        //{
        //    var container = document.getElementById(listid);
        //    if (container == null)
        //    {
        //        return;
        //    }

        //    var fnHandler = GoogleFeed_ItemBuilder.GetBuilder(btype);
        //    for (var i = 0; i < result.feed.entries.length; i++)
        //    {
        //        var data = result.feed.entries[i];
        //        var li = document.createElement("li");
        //        container.appendChild(li);

        //        fnHandler(li, data, true);
        //    }
        //}
        var fnHandler = GoogleFeed_ItemBuilder.GetBuilder(result.Parent.GetBuildType());
        var nodeList = result.NodeList;
        var count = result.Parent.GetNumEntries();
        var container = result.Parent.GetContainer();

        if (nodeList.length == 0) {
            GoogleFeed_HideEmptyInstance(container);
            return;
        }

        for (var i = 0; i < nodeList.length; i++) {
            if (i >= count) break;

            var data = nodeList[i];
            var li = document.createElement("li");
            container.appendChild(li);

            fnHandler(li, data, true);
        }
    }

    feed.Load(tcit_receive);
}

function GoogleFeed_SetOnLoadCallback(listid, rss, num, btype) {
    if (tcitFeed_items.Search(listid) == null) {
        // google.setOnLoadCallback(function(){GoogleFeed_Initialize(listid, rss, num, btype)});
        GoogleFeed_Initialize(listid, rss, num, btype);
    }
    else {
        // google.setOnLoadCallback(function(){TCITFeed_Initialize(listid, rss, num, btype)});
        TCITFeed_Initialize(listid, rss, num, btype)
    }
}

var tcitFeed_items = new TCIT.Web.Tools.Dictionary();
function TCITFeed_SetOnLoadParameter(listid, obj) {
    tcitFeed_items.Add(listid, obj);
}