psfm = {
    e:
        function(id) {
            return document.getElementById(id);
        }
    ,
    DISABLE:
        0
    ,
    ENABLE:
        1
    ,
    LOAD:
        "load"
    ,
    CLICK:
        "click"
    ,
    KEYDOWN:
        "keydown"
    ,
    KEYUP:
        "keyup"
    ,
    attachEvent:
        function(obj, type, func) {
            if (obj.addEventListener) {
                obj.addEventListener(type, func, false);
            } else if (obj.attachEvent) {
                obj.attachEvent('on' + type, func);
            } else {
                obj['on' + type] = func;
            }
        }
}


var XMLToJSON = function(ajax) {
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(ajax.responseText);
    } else if (window.DOMParser) {
        var xmlDoc = new DOMParser().parseFromString(ajax.responseText, "application/xml");
    } else {
        return;
    }
    var loopParse = function(obj) {
        var res = {}, cacheTag = {};
        var ob = {}, att = obj.attributes;
        if (att != null && att.length != 0) {
            for (var a = 0, lenA = att.length; a < lenA; a++) {
                ob[att[a].nodeName.toLowerCase()] = att[a].nodeValue;
            }
            res._attr = ob;
        }
        if (obj.childNodes.length > 0) {
            for (var i = 0, len = obj.childNodes.length; i < len; i++) {
                var ch = obj.childNodes[i];
                if (ch.nodeType == 3) {
                    if (ch.nodeValue.replace(/[\s|\t|\n]/g, "") == "" || ch.nodeValue == null) {
	                    continue;
	                } else {
	                    return ch.nodeValue;
	                }
	            } else if (ch.nodeType == 1) {
                    if (ch.tagName == "category") {
						var chVal = (ch.firstChild == null) ? ch.nodeValue : ch.firstChild.nodeValue;
		                if (ch.tagName in cacheTag){
	                        cacheTag[ch.tagName].push(chVal);
	                    } else {
	                        cacheTag[ch.tagName] = [chVal];
	                    }
                    } else {
		                if (ch.tagName in cacheTag){
	                        cacheTag[ch.tagName].push(arguments.callee(ch));
	                    } else {
	                        cacheTag[ch.tagName] = [arguments.callee(ch)];
	                    }
					}
	            }
	        }
	    } else {
	        return "";
	    }
	    for (var p in cacheTag) {
	        (cacheTag[p].constructor == Array && cacheTag[p].length == 1) ? res[p] = cacheTag[p][0] : res[p] = cacheTag[p];
	    }
	    return res;
    }
    return loopParse(xmlDoc);
}

psfm.xmlHTTP = {
    FUNCTION:
        null
    ,
    SRC:
        0
    ,
	load:
        function (uri, func) {
            psfm.xmlHTTP.FUNCTION = func;
			psfm.xmlHTTP.SRC = psfm.xmlHTTP.createRequest(psfm.xmlHTTP.execute);
            var src = psfm.xmlHTTP.SRC;
			if (src){
				src.open("GET", uri, true);
				src.send(null);
			}
		}
    ,
	createRequest:
        function (func) {
			var src = null;
			try{
				src = new XMLHttpRequest();
			}catch(e){
				try{
					src = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						src = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e){
						return null;
					}
				}
			}
			if (src) {
                src.onreadystatechange = func;
            }
			return src;
		}
    ,
    execute:
        function() {
            var src = psfm.xmlHTTP.SRC;
			if ((src.readyState == 4) && (src.status == 200)){
                 psfm.xmlHTTP.FUNCTION(src);
			} else {
                 return;
			}
        }
}
/***************************************************************************************/
psfm.tamayura = {
    newContents:
        null
    ,
    INSERT_ELEMENT:
        "newsContainer"
    ,
    item:
        function(title, link, category, date, isArray) {
            this.title = title;
            this.link = link;
            if (isArray) {
	            if (category[0].indexOf("ぶろぐ") > -1 || category[1].indexOf("ぶろぐ") > -1) {
	                this.category = "blog";
	            } else {
	                this.category = "news";
				}
                this.news = '<strong style="color:#f00;">New!</strong>&nbsp;';
            } else {
	            if (category.indexOf("ぶろぐ") > -1) {
	                this.category = "blog";
	            } else {
	                this.category = "news";
				}
                this.news = "";
            }
            var formatDate = psfm.tamayura.dateObj(date);
            if (psfm.tamayura.newContents == null || psfm.tamayura.newContents == formatDate) {
                this.date = '<strong>' + formatDate + '</strong>';
                psfm.tamayura.newContents = formatDate;
            } else {
                this.date = formatDate;
            }
        }
    ,
    dateObj:
        function(dateStr) {
            var monthHash = {Jan:'01',Feb:'02',Mar:'03',Apr:'04',May:'05',Jun:'06',Jul:'07',Aug:'08',Sep:'09',Oct:'10',Nov:'11',Dec:'12'};
            var dateObj = dateStr.split(" ");
            return dateObj[3] + "." + eval("monthHash." + dateObj[2]) + "." + dateObj[1];
        }
    ,
    RSS:
        './ajax/rss.php'
    ,
    getContent:
        function(cat) {
            var func = function(src) {
                var json = XMLToJSON(src);
                var insertCount = 0;
                var oneContent = false;
		        var title = "";
		        var link = "";
                var category = "";
                var date = "";
                for(var item in json['rss']['channel']['item']) {
                    if (isNaN(item)) {
                        oneContent = true;
	                    if (item == 'title') {
	                        title = json['rss']['channel']['item'][item];
	                    }
	                    if (item == 'link') {
	                        link = json['rss']['channel']['item'][item];
	                    }
	                    if (item == 'category') {
	                        category = json['rss']['channel']['item'][item];
	                    }
	                    if (item == 'pubDate') {
	                        date = json['rss']['channel']['item'][item];
	                    }
                        continue;
                    }
		            title = "";
		            link = "";
                    if (insertCount == 14) { break; }
                        isArray = false;
	                for(var getCode in json['rss']['channel']['item'][item]) {
	                    if (getCode == 'title') {
	                        title = json['rss']['channel']['item'][item][getCode];
	                    }
	                    if (getCode == 'link') {
	                        link = json['rss']['channel']['item'][item][getCode];
	                    }
	                    if (getCode == 'category') {
                            if(json['rss']['channel']['item'][item][getCode].length == 2 && json['rss']['channel']['item'][item][getCode][0].length > 1) {
	                            category = new Array();
                                for (var i = 0; i < json['rss']['channel']['item'][item][getCode].length; i++) {
									category.push(json['rss']['channel']['item'][item][getCode][i]);
                                }
                                isArray = true;
                            } else {
	                            category = json['rss']['channel']['item'][item][getCode];
                            }
	                    }
	                    if (getCode == 'pubDate') {
	                        date = json['rss']['channel']['item'][item][getCode];
	                    }
	                }
                    insertCount++;
	                var obj = new psfm.tamayura.item(title, link, category, date, isArray);
	                psfm.tamayura.addHTML(obj);
                }
                if (oneContent) {
	                var obj = new psfm.tamayura.item(title, link, category, date, isArray);
	                psfm.tamayura.addHTML(obj);
                }
            }
            var path = psfm.tamayura.RSS;
            psfm.xmlHTTP.load(path, func);
        }
    ,
    addHTML:
        function(item) {
            psfm.e(psfm.tamayura.INSERT_ELEMENT).innerHTML += '<li class="clearfix"><span class="date">' + item.date + '</span><span class="news_info ' + item.category + '">' + item.news + '<a href="' + item.link + '">' + item.title + '</a></span></li>';
        }
}

