var blogId = {
    'イベログ' : "evelog",
    'コープのギフトブログ' : "gift",
    'Deli－Boo－Log' : "deliboo",
    'かかし日記' : "kakashi",
    'LPAおさるランド' : "lpa",
    'おいしいブログ' : "oisi",
    '魚屋の市場日記' : "suisan",
    '29Log' : "b29log",
    'みんなのぶろぐ' : "minalog",
    '［シーズドラッグ］Wellness Life' : "wellnesslife",
    'だ～いすきトドック' : "todok",
    'ななの採用活動日記' : "saiyou",
    'クマスター日記' : "kuma",
    'ECOLOG' : "ecolog",
    'コープさっぽろ　ひんかんひろば' : "hinkan",
    'たすけ愛ランド' : "tasukeiland",
    'しゃべろぐ' : "grocery"
};

var myAjax = new Request ({
    method: 'post',
    data: '',
    url: 'http://blog.todock.com/mt/mt-static/themes/style-funsite/php/fetch_rss.php',
    onFailure: function(){
        alert('Access Failed');
    },
    onComplete: function(e){
        if (e) {
            var myJSON = JSON.decode(e);
            myJSON.each(function(blog) {
                alert(blog.name);
                alert(blog.title);
                alert(blog.url);
                alert(blog.date);
            });
        }
    }
});

var myXml = function() {
    var url = [
        '../evelog/rss.xml',
        '../gift/rss.xml',
        '../deliboo/rss.xml',
        '../kakashi/rss.xml',
        '../lpa/rss.xml',
        '../oisi/rss.xml',
        '../suisan/rss.xml',
        '../29log/rss.xml',
        '../minalog/rss.xml',
        '../wellnesslife/rss.xml',
        '../todok/rss.xml',
        '../saiyou/rss.xml',
        '../kuma/rss.xml',
        '../ecolog/rss.xml',
        '../hinkan/rss.xml',
        '../tasukeiland/rss.xml',
        '../grocery/rss.xml'
    ];
    for (var i=0; i<url.length; i++) {
        var http = new JKL.ParseXML(url[i]);
        http.setOutputArrayElements("item");
        var data = http.parse();
        if (data["rss"]["channel"]["item"]) {
            var name = data["rss"]["channel"]["title"];
            var title = data["rss"]["channel"]["item"][0]["title"];
            if (title.length > 10) {
                title = title.substring(0,10);
                title += '...';
            }
            var link = data["rss"]["channel"]["item"][0]["link"];
            var update = data["rss"]["channel"]["item"][0]["pubDate"];
            date = new Date(update);
            month = date.getMonth() + 1;
            //alert(date.getFullYear()+'年'+month+'月'+date.getDate()+'日 '+date.getHours()+':'+date.getMinutes());
            var lastupd = date.getFullYear()+'年'+month+'月'+date.getDate()+'日 '+date.getHours()+':'+date.getMinutes();
            var par = $$('#blogIndex dl#'+blogId[name]);
            var bTit = par.getElement('dt');
            var bUpd = par.getElement('dd');
            var bLink = par.getParent();
            bTit.set('text', title);
            bUpd.set('text', lastupd);
            bLink.set('href', link);
        }
    }
}

var btnRollFunction = function () {
    var thisImg = this.src;
    thisImg = thisImg.replace("http://blog.todock.com/funsite/","");
    var thisPath = new Array();
    thisPath = thisImg.split("/");
    var thisFile = thisPath.pop();
    var tmpImg = thisFile.split(".");
    var baseName = tmpImg[0];
    baseName += '_on';
    var hoverName = baseName + '.' + tmpImg[1];
    var path = thisPath.join("/") + '/' + hoverName;
    this.src = path;
}

var btnOutFunction = function () {
    var thisImg = this.src;
    thisImg = thisImg.replace("http://blog.todock.com/funsite/","");
    var thisPath = new Array();
    thisPath = thisImg.split("/");
    var thisFile = thisPath.pop();
    var tmpImg = thisFile.split(".");
    var baseName = tmpImg[0];
    baseName = baseName.replace("_on","");
    var hoverName = baseName + '.' + tmpImg[1];
    var path = thisPath.join("/") + '/' + hoverName;
    this.src = path;
}

var fontMFunction = function () {
    $('MSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeM_on.gif";
    $('LSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeL.gif";
    $('fontSize').href = "http://blog.todock.com/mt/mt-static/themes/style-funsite/css/fontSizeM.css";
    var myCookie  = Cookie.write('size', 'Middle', {duration: 30});
}

var fontLFunction = function () {
    $('LSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeL_on.gif";
    $('MSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeM.gif";
    $('fontSize').href = "http://blog.todock.com/mt/mt-static/themes/style-funsite/css/fontSizeL.css";
    var myCookie  = Cookie.write('size', 'Large', {duration: 30});
}

window.addEvent('domready', function() {
    //Smooth Scroll
    new SmoothScroll({
        duration:900,
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false
    }, window);

    //font-size
    if (Cookie.read("size")) {
        var myCookie = Cookie.read("size");
        if (myCookie == 'Middle') {
            fontMFunction();
        } else if (myCookie == 'Large') {
            fontLFunction();
        }
    } else {
        $('MSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeM_on.gif";
        $('LSize').getElement('img').src = "http://blog.todock.com/mt/mt-static/themes/style-funsite/img/bt_fontSizeL.gif";
    }
    if($('MSize')) { $('MSize').addEvent('click', fontMFunction); }
    if($('LSize')) { $('LSize').addEvent('click', fontLFunction); }

    //External Link
    if($('blogIndex')) {
        var anchors = $('blogIndex').getElements('a');
        anchors.each(function(element,index) {
            if (element.rel == 'external') {
                element.target = '_blank';
            }
        });
    }

    //Rollover
    if($$('.btn')) { $$('.btn').addEvent('mouseover', btnRollFunction); }
    if($$('.btn')) { $$('.btn').addEvent('mouseleave', btnOutFunction); }

    //Fetch RSS
    if($('blogIndex')) {
       myXml();
    }

    //ToolTips
    if ($$('.tips1')) {
        $$('.tips1').each(function(element,index) {
            var content = element.get('title').split('::');
            element.store('tip:title', content[0]);
            element.store('tip:text', content[1]);
        });
        var myTips = new Tips($$('.tips1'),{
            showDelay: 50,
            hideDelay: 70,
            className: 'tool-tip',
            onShow: function(tip) {
               tip.fade('0.8');
            },
            onHide: function(tip) {
               tip.fade('out');
            }
        });
    }

    //Accordion
    if ($$('.archiveDate')) {
        $$('.archiveDate').each(function(element,index) {
            var mySlide = new Fx.Slide(element);
            mySlide.hide();
            element.setStyle('visibility','visible');
        });
        //entry highlight
        if ($('individual') || $('yearly')) {
			var this_month = $('column').getElement('.date').getElement('span').get('text');
			hl = $("m"+this_month);
			var myHl = new Fx.Slide(hl);
			myHl.show();
        }

        $$('.archiveBtn li a.show').each(function(element,index) {
            element.addEvent('click', function(e) {
                e.stop();
                var arcElm = element.getParent().getParent().getParent().getParent().getElement('.archiveDate');
                var slideElm = new Fx.Slide(arcElm);
                slideElm.slideIn();
            });
        });

        $$('.archiveBtn li a.hide').each(function(element,index) {
            element.addEvent('click', function(e) {
                e.stop();
                var arcElm = element.getParent().getParent().getParent().getParent().getElement('.archiveDate');
                var slideElm = new Fx.Slide(arcElm);
                slideElm.slideOut();
            });
        });
    }
});


// ajax comment
var ajaxloader = 'http://blog.todock.com/mt/mt-static/themes/style-funsite/img/ajax-loader.gif';
var ajaxcomment_divid = '';
var ajaxcomment_cgi = ['http://blog.todock.com/mt/mt-comments.cgi'].join('');

/** ローダーイメージの表示／非表示 **/
function show_ajaxloader(elem, enable) {
	if (enable) {
		elem.setStyle('display', 'block');
		var myScroll = new Fx.Scroll(window, {
			duration:900
		}).toElement(elem);
		elem.set('html', '');
		elem.setStyle('height', '100px');
		elem.setStyle('background', 'transparent url('+ajaxloader+') no-repeat center center');
	} else {
		elem.setStyle('display', 'block');
		var myScroll = new Fx.Scroll(window, {
			duration:900
		}).toElement(elem);
		elem.setStyle('height', 'auto');
		elem.setStyle('background', 'none');
	}
}

/** コメント投稿 **/
function ajaxcomment_post(divid) {
	ajaxcomment_divid = divid;
	show_ajaxloader($(ajaxcomment_divid), true);

	var form = $('commentsForm');
	if (form.bakecookie.checked) rememberMe(form);
	var pars = form.toQueryString()+'&post=post';
	var myRequest = new Request({
		url: ajaxcomment_cgi,
		method: 'post',
		data: pars,
		onComplete: function (req) {
			var divid = $(ajaxcomment_divid);
			show_ajaxloader(divid, false);
			divid.set({
				'html': req
			});
		},
		onFailure: function (req) {
			var divid = $(ajaxcomment_divid);
			show_ajaxloader(divid, false);
			divid.set({
				'html': req
			});
		}
	});
	myRequest.send();
	return false;
}
/** コメントプレビュー **/
function ajaxcomment_preview(divid) {
	ajaxcomment_divid = divid;
	show_ajaxloader($(ajaxcomment_divid), true);
	var form = $('commentsForm');
	var pars = form.toQueryString()+'&preview=preview';
	var myRequest = new Request({
		url: ajaxcomment_cgi,
		method: 'post',
		data: pars,
		onComplete: function (req) {
			var elem = $(ajaxcomment_divid);
			show_ajaxloader(elem, false);
			elem.set({
				'html': req
			});
		},
		onFailure: function (req) {
			var divid = $(ajaxcomment_divid);
			show_ajaxloader(divid, false);
			divid.set({
				'html': req
			});
		}
	});
	myRequest.send();
	return false;
}
