﻿    var ROW_CHAR_LENGTH = 24;    //版面設定字型的大小，每行約莫23全形字

    function importJs(src, look_for, onload)
    {//debugger;
        var scp = document.createElement("script");
        scp.setAttribute("src", src);
        scp.setAttribute("type", "text/javascript");
        if (onload) wait_for_script_load(look_for, onload);
        if (eval("typeof " + look_for) == "undefined")
        {
            var head = document.getElementsByTagName("head")[0];
            if (head) head.appendChild(scp);
            else document.body.appendChild(scp);
        }
    }
    
    function wait_for_script_load(look_for, callback)
    {
        var interval = setInterval(function()
        {
            if (eval("typeof " + look_for) != "undefined")
            {
                clearInterval(interval);
                callback();
            }
        }, 50);
    }
    
    function footBuilding(data)
    {
        //處理 系統項目 部分(ex: 報紙廣告, 網路廣告, 旺旺集團)的連結列表
        var gloalSiteLink = $("\r\n<ul class='global-site-link' />\r\n");                
        $.each(data.sysItems, function(i, item)
        {
            var listItem = $("<li />\r\n")
                            .append((item.url==""?item.text:"<a href='" + item.url + "'>" + item.text + "</a>"))
                            .appendTo(gloalSiteLink);
            if (i == data.sysItems.length - 1) listItem.attr("class", "last");
        });
        //處理分類(頻道)列表
        var siteMap = $("<div class='site-map clear-fix' />");    //.appendTo(container);
        $.each(data.posData, function(i, cates)
        {
            var sec = $("<div class='sec-0" + (i+1).toString() + "' />").appendTo(siteMap); //目前有3區: sec-01, sec-02, sec-03
            $.each(cates, function(i, cate)
            {
                if (typeof(cate) == "undefined") return;
                $("<h2>" + cate.cateName + "</h2>").appendTo(sec);  //分類標題(名稱)
                var list = $("\r\n<ul class='site-map-link' />\r\n").appendTo(sec);
                var sumChars = 0;
                var preItem = null;
                $.each(cate.cateData, function(i, item)
                {
                    sumChars += item.text.length + 1;
                    if (sumChars > ROW_CHAR_LENGTH) //字數超過要換行(就是追加 <ul>)
                    {
                        list = $("\r\n<ul class='site-map-link' />\r\n").appendTo(sec);
                        sumChars = item.text.length + 1;
                        preItem.attr("class", "last");  //前一個 <li> 加上 class="last"
                    }
                    if (item.isLast)
                        $("<li class='last' />\r\n").append("<a href='" + item.url + "'" + (item.isOpenNewWin ? " target='_blank'" : "") + ">" + item.text + "</a>").appendTo(list);
                    else
                        preItem = $("<li />\r\n").append("<a href='" + item.url + "'" + (item.isOpenNewWin ? " target='_blank'" : "") + ">" + item.text + "</a>").appendTo(list);
                });
            });
        });
                    
        $("<div class='container' />")
            .appendTo($("<div class='footer' />").appendTo("#jq-foot"))
            .append(siteMap)
            .append($("<div class='logo-wantwant-group'>旺旺集團</div>"))
            .append($("<div class='logo-chinatimes-group'>中時集團</div>"))
            .append(gloalSiteLink)
            .append($("<div class='copyright' />").html("&copy; 1995 - " + new Date().getFullYear() + " China Times Inc. 請尊重智慧財產權勿任意轉載違者依法必究。"))
            .append($("<div style='clear: both;' />"));
    }
    
    importJs("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js", "jQuery", function()
    {
        (function($)
        {
            $.ajaxSettings.cache = true;            
            $.ajax({
                url: 'http://www.chinatimes.com/2009Cti/cthead/foot-data.js', 
                dataType: 'script', 
                scriptCharset: 'utf-8'
            });
        })(jQuery);    
    });
