﻿var Header = {
    init: function() {
        $("#jq-wrapper").append('<div id="jq-header" class="header" />')
			.append('<div id="banner728x90" />')
			.append('<div id="jq-topnav" class="topnav" />')
			.append('<div id="jq-topnav2" class="topnav2" />');

        $('<h1 id="logo-idx-s" class="h1-logo"><a href="http://news.chinatimes.com/" title="中時電子報">中時電子報</a></h1>').appendTo('#jq-header');
        $('<h1 id="logo-' + sChannel + '" class="h1-logo"></h1>').appendTo('#jq-header');
        $('<div id="jq-family-link" class="family-link" />').append('<ul class="channel-list" />\r\n').appendTo('#jq-header');
        $('<div id="jq-date" class="date" />').appendTo('#jq-header');
        $('<div id="jq-life-info" class="life-info" />').appendTo('#jq-header');
        $('<div id="jq-search" class="search" />').appendTo('#jq-header');

        // 環境輻射即時監測資訊
        //		$('<div class="radiation" style="position: absolute; right: 0; bottom: 4px; *bottom: 3px; z-index: 2; border-left: 1px solid #CCCCCC;"> ' +
        //                                     '<a href="http://www.chinatimes.com/Radiation/index.htm" target="_blank" title="環境輻射監測"><img src="http://www.chinatimes.com/Radiation/images/ico-radiation.gif" style="display: block;" alt="環境輻射監測"></a> ' +
        //                                     '</div> ').appendTo('#jq-header');
        //氣象區塊
        $('<div id="jq-weather" class="weather clear-fix" />').append('<div id="jq-weatherframe" />').appendTo('#jq-header');

        $('<table><tr id="jq-searchtr"><th class="search_th">搜尋：</th><td id="jq-searchtd"></td><td id="jq-seachimg"></td></tr></table>').appendTo('#jq-search');
        $('<ul class="menu clear-fix" />').appendTo('#jq-topnav');
        $('<div class="bgn" />')
			.append('<ul class="menu clear-fix" />')
			.appendTo('#jq-topnav2');
    }
};

function LogoLinksCallBack(data) {
    $.each(data.LogoLinks, function() {
        if (this.channel == sChannel) {
            $('#logo-' + sChannel).append('<a href="' + this.href + '" title="' + this.title + '">' + this.title + '</a>');
            sCategoryChannel = this.catchannel;
        }
    });

    setToday();

    $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/family-links.js', dataType: 'script', scriptCharset: 'utf-8' });
    $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/lifeinfo-list.js', dataType: 'script', scriptCharset: 'utf-8' });
    $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/search-list.js', dataType: 'script', scriptCharset: 'utf-8' });
    $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/channel-link.js', dataType: 'script', scriptCharset: 'utf-8' });
    $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/category-link-' + sChannel + '.js', dataType: 'script', scriptCharset: 'utf-8' });
}

function FamilyLinksCallBack(data) {
    $.each(data.FamilyLinks, function() {
        if (typeof (this.href) == 'undefined') return;
        var $item = $('<li>\r\n');
        if (typeof (this.href) == 'function') {
            //進行 url 變造
            var d_href = {};
            eval("d_href = " + this.href + "();");
            $item.append('<a href="' + d_href.url + '"' + (this.newWindow ? ' target="_blank"' : '') + ' onclick="setLanguage(\'' + d_href.lang + '\')">' + this.value + '</a>')
                 .appendTo('ul.channel-list');
        }
        else {
            $item.append('<a href="' + this.href + '"' + (this.newWindow ? ' target="_blank"' : '') + '>' + this.value + '</a>')
                 .appendTo('ul.channel-list');
            if (this.css.length > 0) $item.addClass(this.css);
        }
    });

    $('<li>\r\n').append('<a href="http://www.chinatimes.com/news-feed/news-feed-idx.htm">Widget</a>').appendTo('ul.channel-list');
    $('ul.channel-list>li:last').addClass('last');

}
//轉簡體頁面
function trans2GbSite() {
    var big5Http = new RegExp("^http[s]*:".concat("//"));
    var gbHttp = new RegExp("^http:".concat("//", "gb.chinatimes.com/", "gate/gb/"));
    if (!gbHttp.test(location.href))
        return { url: location.href.replace(big5Http, "http:".concat("//gb.chinatimes.com/", "gate/", "gb/")), lang: "zh-cn" };
    else
        return { url: location.href, lang: "zh-cn" };
}
//轉回繁體頁面
function trans2BgSite() {
    var gbHttp = new RegExp("^http:".concat("//", "gb.chinatimes.com/", "gate/gb/"));
    return { url: location.href.replace(gbHttp, "http:".concat("//")), lang: "zh-tw" };
}

function LifeInfoCallback(data) {
    $('<select id="selLifeInfo">').appendTo('#jq-life-info').change(function() {
        var item = data.LifeInfoLists[$(this).val()];
        if (item.newWindow)
            window.open("http://www.chinatimes.com/2009Cti/cthead/popupContainer.htm?id=" + $(this).val());
        else
            window.location.href = item.url;
    });

    $.each(data.LifeInfoLists, function(i, item) {
        $('<option />').attr('selected', item.selected).val(i).text(item.text).appendTo("#jq-life-info #selLifeInfo");
    });
}

function SearchListsCallBack(data) {
    $('<form id="formsearch" action="http://search.tol.com.tw/Search.aspx" method="post" name="formsearch" target="_blank" style="display:inline;"></form>')
		.append('<select id="SearchTarget" name="SearchTarget" class="form-txt"></select>&nbsp;')
		.append('<input id="searchkeywd" name="SearchWord" type="text" class="form-txt" value="請輸入搜尋關鍵字" />&nbsp;')
		.append('<input name="送出" type="submit" class="form-btn" value="查詢" />')
		.appendTo('#jq-searchtd');
    $.each(data.SearchLists, function() {
        $('<option value="' + this.value + '">' + this.text + '</option>').appendTo('#SearchTarget');
    });
    $('#SearchTarget>option:first').selected = true;
    $('<img src="http://www.chinatimes.com/2009Cti/cthead/images/logo-yahoo-search.gif" alt="Yahoo" title="Yahoo">').appendTo('#jq-seachimg');

    $('#searchkeywd').focus(function() {
        if (this.value.indexOf('請') != -1)
            if (this.style.backgroundColor = '#ffffe5')
            this.value = '';
    }).blur(function() {
        if (this.value == '')
            if (this.style.backgroundColor = '#FFF')
            this.value = '請輸入搜尋關鍵字';
    });
}

function ChannelLinksCallBack(data) {
    $.each(data.ChannelLinks, function() {
        var li = $('<li>\r\n');
        if (this.Right) {
            li.addClass('rightitem');
        }
        if (this.licss != '') {
            li.addClass(this.licss);
        }

        if (this.channel == sCategoryChannel) {
            li.append('<a href="' + this.href + '" class="on"' + (this.newWindow ? ' target="_blank"' : '') + '><b>' + this.value + '</b></a>').appendTo('#jq-topnav>ul');
        }
        else if (this.css != '') {
            li.append('<a href="' + this.href + '" class="' + this.css + '"' + (this.newWindow ? ' target="_blank"' : '') + '><b>' + this.value + '</b></a>').appendTo('#jq-topnav>ul');
        }
        else {
            li.append('<a href="' + this.href + '"' + (this.newWindow ? ' target="_blank"' : '') + '><b>' + this.value + '</b></a>').appendTo('#jq-topnav>ul');
        }
    });
    $('#jq-topnav>ul>li:first').addClass('rightitem last');
    $('#jq-topnav>ul>li:last').addClass('last');

}

function CategoryLinksCallBack(data) {
    var ul = $('#jq-topnav2 div.bgn > ul:last')[0];
    var i = 1;
    var isdefined = false;

    $.each(data.CategoryLinks, function() {
        if (typeof (FocusCategory) != 'undefined' && i == FocusCategory) {
            $('<li>\r\n').attr('id', this.id).addClass(this.css).append('<a href="' + this.href + '" class="on">' + this.value + '<span class="pointer"/></a>').appendTo(ul);
        }
        else if (typeof (FocusCategory) != 'undefined' && this.id == FocusCategory) {
            $('<li>\r\n').attr('id', this.id).addClass(this.css).append('<a href="' + this.href + '" class="on">' + this.value + '<span class="pointer"/></a>').appendTo(ul);
        }
        else {
            var li = $('<li>\r\n');
            if (this.id != '') {
                li.attr('id', this.id).addClass(this.css).append('<a href="' + this.href + '">' + this.value + '</a>');
            }
            else {
                li.addClass(this.css).append('<a href="' + this.href + '">' + this.value + '</a>');
            }
            if (i == data.CategoryLinks.length) {
                li.addClass('last');
            }
            li.appendTo(ul);
        }
        i++;
    });
}

function setToday() {
    var mydate = new Date();
    var YY = mydate.getFullYear();
    var MM = mydate.getMonth() + 1;
    var DD = mydate.getDate();
    var WW = new Array("日", "一", "二", "三", "四", "五", "六");
    if (MM.length == 1) MM = "0" + MM;
    $("#jq-date").html(YY + "-" + (MM >= 10 ? MM : "0" + MM) + "-" + (DD >= 10 ? DD : "0" + DD) + "&nbsp;星期" + WW[mydate.getDay()]);
}

/*  Onload Functions    //---------------------------*/
(function($) {
    function loadHeader() {
        //語系條件判斷 start
        try { if (_isDirectUrl) return; } catch (e) { }
        //語系條件判斷 end

        if (!sChannel || sChannel == undefined || sChannel == null || sChannel == '') {
            sChannel = 'news';
            sCategoryChannel = 'ct';
        }
        Header.init();

        $.ajax({ url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/logo-link.js', dataType: 'script', scriptCharset: 'utf-8' });
        // 氣象區塊內容
        $('#jq-weatherframe').append('<iframe id="weather-frame" name="weather-frame" ' +
                                             'src="http://www.chinatimes.com/weather/weather-widget.htm" ' +
                                             'marginwidth="0" ' +
                                             'marginheight="0" ' +
                                             'frameborder="0" ' +
                                             'scrolling="no" ' +
                                             'allowtransparency="true" ' +
                                             'class="weather-frame"></iframe>');

    }

    $.ajaxSettings.cache = true;
    //檢查 jquery.cookie 套件
    if (typeof ($.cookie) == "undefined") {
        //載入 jquery.cookie 套件
        $.getScript('http://www.chinatimes.com/2009Cti/cthead/jquery.cookie.js', function() {
            //檢查 頁面語系條件 函式是否載入
            if (typeof (directPage) == "undefined")
                $.getScript('http://www.chinatimes.com/2009Cti/cthead/directCurrentPage.js', loadHeader);   //載入頁面語系條件 函式
            else
                loadHeader();
        });
    }
    else if (typeof (directPage) == "undefined")
        $.getScript('http://www.chinatimes.com/2009Cti/cthead/directCurrentPage.js', loadHeader);   //載入頁面語系條件 函式
    else
        loadHeader();

})(jQuery);


//google +1 button
//document.write('<script type="text/javascript" src="https://apis.google.com/js/plusone.js">');
//document.write('  {lang: \'zh-TW\'}');
//document.write('</script>');

