/****************************************************
 * jQuery RSS Plugin by fieeeld
 * version: 0.04 (2008/12/01)
 * @requires jQuery v1.2.6 or later
 *
 * Demo at: http://tpfields.xrea.jp/demo/js/sitefeeds/
 *
 ****************************************************/
$(function(){
	$.ajax({
		url: "blog/owner/?feed=rss2", //RSSファイル名
		async: true,
		cache: false,
		dataType:"xml",
		success: successXml,
		error: errXml
    	});
});

// xml取得成功
function successXml(xml)
{
    var heightLeft = 0;
    $('#news-l dl.clearFix').each(function(){
        heightLeft += $(this).height();
    });
    var heightRight = 0;
    $('#news-r dl.clearFix').each(function(){
        heightRight += $(this).height();
    });
    
    var imgData = {
            "蟹江展示場"   : "icon_ex01.gif",
            "一宮展示場"   : "icon_ex02.gif",
            "春日井展示場" : "icon_ex03.gif",
            "北展示場"     : "icon_ex04.gif",
            "神宮東展示場" : "icon_ex05.gif",
            "長久手展示場" : "icon_ex06.gif",
            "日進展示場"   : "icon_ex07.gif",
            "大府展示場"   : "icon_ex08.gif",
            "半田展示場"   : "icon_ex09.gif",
            "刈谷展示場"   : "icon_ex10.gif",
            "安城展示場"   : "icon_ex11.gif",
            "豊田展示場"   : "icon_ex12.gif",
            "丸山展示場"   : "icon_ex13.gif",
            "西尾展示場"   : "icon_ex14.gif",
            "岡崎西展示場" : "icon_ex15.gif",
            "岡崎展示場"   : "icon_ex16.gif",
            "豊橋展示場"   : "icon_ex17.gif",
            "豊橋南展示場" : "icon_ex18.gif"
    };
    
    var content = "";
    $(xml).find('item').each(function(i){
        /* 初期設定で3件出力します。件数を変更は"i > 2"の部分を修正してください。
        数値は"出力したい件数 - 1"を入力して下さい。*/
        if ( i > 4 ) {
            return true;
        }
        var title = $(this).find('title').text();
        var url = $(this).find('link').text();
        var categoryName = '';
        var categories = $(this).find('category').each(function(j){
            if (j > 0) {
                return true;
            } else {
                if (imgData[$(this).text()]) {
                    categoryName = '<img src="images/top/' + imgData[$(this).text()] + '" width="69" height="17" alt="' + $(this).text() + '" />';
                } else {
                    categoryName = $(this).text();
                }
            }
        });

        //日付を整形
        var date = dateParse($(this).find('pubDate').text());
        
        content += '<dl class="clearFix">' + "\n";
        content += '<dt>' + categoryName + '</dt>' + "\n";
        content += '<dd class="newsdate03">' + date[0] + '</dd>' + "\n";
        content += '<dd><a href="' + url + '" target="_blank">' + title + '</a></dd>' + "\n";
        content += '</dl>' + "\n";
    });
    
    $(".newsboxBlog").append(content);
    
    if ($("#idBlogShow").val() == '1') {
        $(".newsbox").height(115);
        $("#idBlogR").remove();
    } else if ($("#idBlogShow").val() == '2') {
        $("#idBlogL").remove();
        $(".newsboxR").height(115);
    } else {
        if (heightLeft < heightRight) {
            $(".newsbox").height(115);
            $("#idBlogR").remove();
        } else {
            $("#idBlogL").remove();
            $(".newsboxR").height(115);
        }
    }
}

// xml取得失敗
function errXml(req, status, err)
{
    $("#idBlogR").remove();
    $("#idBlogL").remove();
}

//dateParse: "10月14日(土)" 形式
function dateParse(str){
    var arrWeek = new Array("日", "月", "火", "水", "木", "金", "土");
    var objDate = new Date(str);
    var nowDate = new Date();
    //現在の日付との差を計算
    myDay = Math.floor((nowDate.getTime()-objDate.getTime()) / (1000*60*60*24)) + 1;
    //もし2週間以内なら"new!"マーク
    if (myDay < 15 ){
        var newMsg = '&nbsp;&nbsp;<span style="color:#ff6666; font-weight:bold;">new!</span>';
    } else {
        var newMsg = '';
    }
    var year = objDate.getFullYear();
    var month = objDate.getMonth() + 1;
    var date = objDate.getDate();
    var wDay = objDate.getDay();
    //if ( month < 10 ) { month = "0" + month; }
    //if ( date < 10 ) { date = "0" + date; }
    str = month + '月' + date + '日(' + arrWeek[wDay] + ')';
    rtnValue = new Array(2);
    rtnValue[0] = str;
    rtnValue[1] = newMsg;
    return rtnValue;
}


