// BLOG TALK Ajax //////////////////////////
// 2009.3 //

var keywords = ["元気","楽しい","負けた","失敗","苦手","がんばる","好き","うれしい","苦しい","カワイイ","笑顔","強い","感謝","幸せ","おめでとう","勝った","当たり","応援","すごい","食べた","買った","ダメ","怒った","わからない","面白い","競馬","おいしい","会いたい","思う","ごめんなさい","困った","怖い","嫌い","悲しい","泣いた","痛い","ハズレ","ひどい","疲れた","忙しい","ショック","眠い"];

var talktype = "-1";

callSwf();
startGetRSS();

function gethorsetype(message) {
    return talktype;
}

function getrefurlRequest(){
	var request = new Object();
	request.host = window.location.host;
	request.pathname = window.location.pathname;
	return "http://" + request.host + request.pathname;
}

// INIT
function startGetRSS() {
    //RSS URL Get/////////
    if(location.host.indexOf("seesaa.net",0) != -1) {
        var rdfurl = "http://"+location.host+"/index.rdf";
    }
    else if(location.host.indexOf("livedoor.jp",0) != -1) {
        var rdfurl = "http://"+location.host+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/index.rdf";
    }
    else if(location.host.indexOf("yahoo.co.jp",0) != -1) {
        var rdfurl = "http://"+location.host+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/rss.xml";
    }
    else if(location.host.indexOf("cocolog-nifty.com",0) != -1) {
        var rdfurl = "http://"+location.host+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/index.rdf";
    }
    else if(location.host.indexOf("ameblo.jp",0) != -1) {
        var rdfurl = "http://rssblog.ameba.jp/"+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/rss.html";
    }
    else if(location.host.indexOf("goo.ne.jp",0) != -1) {
        var rdfurl = "http://"+location.host+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/index.rdf";
    }
    else if(location.host.indexOf("fc2.com",0) != -1) {
        var rdfurl = "http://"+location.host+"/?xml";
    }
    else if(location.host.indexOf("rakuten.co.jp",0) != -1) {
        var rdfurl = "http://api.plaza.rakuten.ne.jp/"+location.host+"/rss/";
    }
    else if(location.host.indexOf("at.webry.info",0) != -1) {
        var rdfurl = "http://"+location.host+"/rss/index.rdf";
    }
    else {
        var rdfurl = "http://"+location.host+location.pathname.slice(0, location.pathname.indexOf("/",1))+"/index.rdf";
    }
    //document.write("<br>"+rdfurl+"<br>");
    requestXML(rdfurl, processXML);
}

// XML PROCESS
function processXML(xmldoc) {
    // New 3 page
    var pagedoc = "";
    var schcnt = 0;
    for(var i = 0; i < 3; i++) {
        var wordloc = xmldoc.indexOf("</content:encoded>",schcnt);
        if(wordloc == -1){
            var wordloc = xmldoc.indexOf("</dc:date>",schcnt);
        }
        if(wordloc == -1){
            var wordloc = xmldoc.indexOf("</pubDate>",schcnt);
        }
        if(wordloc == -1){
            pagedoc = xmldoc;
        }
        else {
            pagedoc += xmldoc.slice(schcnt,wordloc);
            schcnt = wordloc+1;
        }
    }
    // search word
    talktype = "0b";
    for(var i = 0; i < keywords.length; i++) {
        if(pagedoc.indexOf(keywords[i],0) != -1) {
            talktype += (i + 1) + "b";
        }
    }
}

// Make XMLHttpRequest
function createXMLHttp() {
    var obj = null;
    try {  // Mozilla, Opera, Safari, IE7
        obj = new XMLHttpRequest();
    }
    catch(e) {
        try { // IE 6
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try { // IE 5, 5.5
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) { }
        }
    }
    return obj;
}

// Send XMLHttpRequest
function requestXML(url, xmlhandler) {
    var errorbox = document.getElementById("errorbox");
    var xmlhttp = createXMLHttp();
    if(xmlhttp) {
        try {
            // send url
            xmlhttp.onreadystatechange = gotData;
            xmlhttp.open("GET", url, true);
            xmlhttp.send(null);
        }
        catch(e) {
            if(errorbox)
                errorbox.innerHTML = "err";
        }
    }
    else {
        if(errorbox)
            errorbox.innerHTML = "XMLHttpRequest err";
    }
    function gotData() {
        if(xmlhttp.readyState != 4) return;
        if((xmlhttp.status == 200 || xmlhttp.status == 0))
            var xmldoc = xmlhttp.responseText;
        else {
            if(errorbox)
                errorbox.innerHTML = "HTTP ERR：" + xmlhttp.status;
            return;
        }
        if(xmldoc)
            xmlhandler(xmldoc);
        else if(errorbox)
            errorbox.innerHTML = "err xml file";
    }
}

function callSwf() {
    document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" WIDTH="150" HEIGHT="274" id="horsetalk" ALIGN="">');
    document.write('<PARAM NAME=movie VALUE="http://clubkeiba.jp/community/blogparts/jrablog.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=id VALUE=horsetalk> <PARAM NAME=bgcolor VALUE=#FFFFFF> <param name=allowScriptAccess value=always> <EMBED src="http://clubkeiba.jp/community/blogparts/jrablog.swf" quality=high bgcolor=#FFFFFF  WIDTH="150" HEIGHT="274" NAME="horsetalk" allowScriptAccess="always" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>');
}
