﻿var Header = 
{
	init : function()
	{
		$("#jq-wrapper").append('<div class="header" id="jq-header" style="height:25px;" />');
		$('<div class="family-link" id="jq-family-link" style="border-bottom:none;" />').append('<ul class="channel-list" />').appendTo('#jq-header');
		$('<div id="jq-date" class="date" />').appendTo('#jq-header');
	}
};

function FamilyLinksCallBack(data)
{
	$.each(data.FamilyLinks, function()
	{
	    if (typeof(this.href)=='undefined') return;
	    if (typeof(this.href)=='function')
	    {
	        //進行 url 變造
	        var d_href = {};
	        eval("d_href = " + this.href +"();");
	        $('<li>\r\n').append('<a href="' + d_href.url + '"' + (this.newWindow ? ' target="_blank"' : '') + ' onclick="setLanguage(\'' + d_href.lang + '\')">' + this.value + '</a>').appendTo('ul.channel-list');
	        
	    }
	    else
		    $('<li>\r\n').append('<a href="' + this.href + '"' + (this.newWindow ? ' target="_blank"' : '') + '>' + this.value + '</a>').appendTo('ul.channel-list');
	});

	$('<li>\r\n').append('<a href="http://rss.chinatimes.com/syn/main.htm"><img src="http://www.chinatimes.com/2008Cti/cthead/images/ico-rss.gif" alt="訂閱RSS"></a>').appendTo('ul.channel-list');
	$('ul.channel-list>li:last').addClass('last');
	
	setToday();
}

//轉簡體頁面
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 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

        Header.init();
        $.ajax({url: 'http://www.chinatimes.com/2009Cti/cthead/Scripts-utf8/family-links-out.js', dataType: 'script', scriptCharset: 'utf-8'});
    }
    
    $.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);
