var yjNxmlHttpRequestObj;

var yjNcached = 0;
var yjNvalTable = { 'dom'    : [ '1', 'dom',   'dom|pol|soci|peo'],
                    'c_int'  : [ '1', 'c_int', 'c_int|int|cn|kr'],
                    'bus'    : [ '1', 'bus',   'bus|bus_all|brf|biz|ind'],
                    'c_ent'  : [ '1', 'c_ent', 'c_ent|ent|musi|movi|game|fash'],
                    'c_spo'  : [ '1', 'c_spo', 'c_spo|spo|base|socc|moto|horselgolf|fight'],
                    'c_sci'  : [ '1', 'c_sci', 'c_sci|sci|inet|mobi|secu'],
                    '_order' : 'dom|c_int|bus|c_ent|c_spo|c_sci',
                    '_version' : '1'
               };
var yjNargList = ['toggle','select','suborder'];
var	yjNcloseIcon = "http://i.yimg.jp/images/news/bt/ya_normal.gif";
var	yjNopenIcon = "http://i.yimg.jp/images/news/bt/ya_active.gif";
new Image().src = yjNcloseIcon;
new Image().src = yjNopenIcon;

var yjNcatInfoTable = {
          'dom':     { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=dom'},
          'pol':     { 'name': '政治',         'url': 'http://headlines.yahoo.co.jp/hl?c=pol&t=l'    },
          'soci':    { 'name': '社会',         'url': 'http://headlines.yahoo.co.jp/hl?c=soci&t=l'   },
          'peo':     { 'name': '人',           'url': 'http://headlines.yahoo.co.jp/hl?c=peo&t=l'    },
          'c_int':   { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=c_int'},
          'int':     { 'name': '総合',         'url': 'http://headlines.yahoo.co.jp/hl?c=int&t=l'    },
          'cn':      { 'name': '中国',         'url': 'http://headlines.yahoo.co.jp/hl?c=cn&t=l'     },
          'kr':      { 'name': '韓国',         'url': 'http://headlines.yahoo.co.jp/hl?c=kr&t=l'     },
          'bus':     { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=bus'},
          'bus_all': { 'name': '総合',         'url': 'http://headlines.yahoo.co.jp/hl?c=bus_all&t=l'},
          'brf':     { 'name': '市況',         'url': 'http://headlines.yahoo.co.jp/hl?c=brf&t=l'    },
          'biz':     { 'name': '株式',         'url': 'http://headlines.yahoo.co.jp/hl?c=biz&t=l'    },
          'ind':     { 'name': '産業',         'url': 'http://headlines.yahoo.co.jp/hl?c=ind&t=l'    },
          'c_ent':   { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=c_ent'},
          'ent':     { 'name': '総合',         'url': 'http://headlines.yahoo.co.jp/hl?c=ent&t=l'    },
          'musi':    { 'name': '音楽',         'url': 'http://headlines.yahoo.co.jp/hl?c=musi&t=l'   },
          'movi':    { 'name': '映画',         'url': 'http://headlines.yahoo.co.jp/hl?c=movi&t=l'   },
          'game':    { 'name': 'ゲーム',       'url': 'http://headlines.yahoo.co.jp/hl?c=game&t=l'   },
          'fash':    { 'name': 'ファッション', 'url': 'http://headlines.yahoo.co.jp/hl?c=fash&t=l'   },
          'c_spo':   { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=c_spo'},
          'spo':     { 'name': '総合',         'url': 'http://headlines.yahoo.co.jp/hl?c=spo&t=l'    },
          'base':    { 'name': '野球',         'url': 'http://headlines.yahoo.co.jp/hl?c=base&t=l'   },
          'socc':    { 'name': 'サッカー',     'url': 'http://headlines.yahoo.co.jp/hl?c=socc&t=l'   },
          'moto':    { 'name': 'モーター',     'url': 'http://headlines.yahoo.co.jp/hl?c=moto&t=l'   },
          'horse':   { 'name': '競馬',         'url': 'http://headlines.yahoo.co.jp/hl?c=horse&t=l'  },
          'golf':    { 'name': 'ゴルフ',       'url': 'http://headlines.yahoo.co.jp/hl?c=golf&t=l'   },
          'fight':   { 'name': '格闘技',       'url': 'http://headlines.yahoo.co.jp/hl?c=fight&t=l'  },
          'c_sci':   { 'name': 'トップ',       'url': 'http://headlines.yahoo.co.jp/hl?c=c_sci'},
          'sci':     { 'name': '総合',         'url': 'http://headlines.yahoo.co.jp/hl?c=sci&t=l'    },
          'inet':    { 'name': 'インターネット', 'url': 'http://headlines.yahoo.co.jp/hl?c=inet&t=l' },
          'mobi':    { 'name': 'モバイル',       'url': 'http://headlines.yahoo.co.jp/hl?c=mobi&t=l' },
          'secu':    { 'name': 'セキュリティ',   'url': 'http://headlines.yahoo.co.jp/hl?c=secu&t=l' }
      };

var yjNua = navigator.userAgent;
var yjNsafari    = yjNua.indexOf("Safari")!=-1;
var yjNkonqueror = yjNua.indexOf("Konqueror")!=-1;
var yjNmozes     = ( (yjNa=navigator.userAgent.split("Gecko/")[1] ) ? yjNa.split(" ")[0] : 0 ) >= 20011128 ;
    
function yjNgetElement(id) { 
     var element = null;
     try {
          element = document.getElementById(id);
     }
     catch (e) {
         element = document.all[id];
     }
     return element;
}

function yjNcreateXMLHttpRequest(cbFunc) {
    var XMLhttpObject = null;
    try {
        XMLhttpObject = new XMLHttpRequest();
    } catch (e) {
        try {
            XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                return null;
            }
        }
    }
    if (XMLhttpObject) {
        if (window.opera || yjNsafari || yjNmozes) {
            XMLhttpObject.onload = cbFunc;
        } else {
            XMLhttpObject.onreadystatechange = cbFunc;
        }
    }
    return XMLhttpObject;
}

function yjNprocessRequestChangeInit() {
    if (yjNxmlHttpRequestObj.readyState == 4) {
        if (yjNxmlHttpRequestObj.status == 200) {
            var jsonData = eval( '(' + yjNxmlHttpRequestObj.responseText + ')' );
            yjNvalTable['dom'] = jsonData['dom'];
            yjNvalTable['c_int'] = jsonData['c_int'];
            yjNvalTable['bus'] = jsonData['bus'];
            yjNvalTable['c_ent'] = jsonData['c_ent'];
            yjNvalTable['c_spo'] = jsonData['c_spo'];
            yjNvalTable['c_sci'] = jsonData['c_sci'];
            yjNvalTable['_order'] = jsonData['_order'];
            yjNvalTable['_version'] = jsonData['_version'];
            yjNtoggle_init();
        }
    }
}

function yjNprocessRequestChangeGet() {
    if (yjNxmlHttpRequestObj.readyState == 4) {
        if (yjNxmlHttpRequestObj.status == 200) {
            var jsonData = eval( '(' + yjNxmlHttpRequestObj.responseText + ')' );
            yjNvalTable['dom'] = jsonData['dom'];
            yjNvalTable['c_int'] = jsonData['c_int'];
            yjNvalTable['bus'] = jsonData['bus'];
            yjNvalTable['c_ent'] = jsonData['c_ent'];
            yjNvalTable['c_spo'] = jsonData['c_spo'];
            yjNvalTable['c_sci'] = jsonData['c_sci'];
            yjNvalTable['_order'] = jsonData['_order'];
            yjNvalTable['_version'] = jsonData['_version'];
            yjNcached = 1;
        }
    }
}

function yjNprocessRequestChangeSet() {
    if (yjNxmlHttpRequestObj.readyState == 4) {
        if (yjNxmlHttpRequestObj.status == 200) {
            var response = yjNxmlHttpRequestObj.responseText;
            if ( response.indexOf("success") > 0)  {
                yjNcached = 0;
            }
        }
    }
}

function yjNinit() {
    if (yjNcached == 1) {
        return;
    } else {
        var url= '/lib/ctlCookie_1_0.php?func=get&name=YJNEWSFOLD&v=1';
        yjNxmlHttpRequestObj = yjNcreateXMLHttpRequest(yjNprocessRequestChangeInit);
        if (yjNxmlHttpRequestObj) {
            yjNxmlHttpRequestObj.open("GET", url, true);
            yjNxmlHttpRequestObj.send("");
        } 
    }
    yjNcached = 1;
}

function yjNget() {
    if (yjNcached == 1) {
        return;
    } else {
        var url= '/lib/ctlCookie_1_0.php?func=get&name=YJNEWSFOLD&v=1';
        var d = new Date();
        url += '&d=' + d.getTime();
        yjNxmlHttpRequestObj = yjNcreateXMLHttpRequest(yjNprocessRequestChangeGet);
        if (yjNxmlHttpRequestObj) {
            yjNxmlHttpRequestObj.open("GET", url, false);
            yjNxmlHttpRequestObj.send("");
        } 
    }
}

function yjNset(val) {
    var url = '/lib/ctlCookie_1_0.php?func=set&name=YJNEWSFOLD&v=1';
    var d = new Date();
    url += '&d=' + d.getTime();
    url += val;
	
    yjNxmlHttpRequestObj = yjNcreateXMLHttpRequest(yjNprocessRequestChangeSet);
    if (yjNxmlHttpRequestObj) {
        yjNxmlHttpRequestObj.open("GET", url, false);
        yjNxmlHttpRequestObj.send("");
    }
}

function yjNgetSubValue(subKey) {
    yjNget();

    var ar = new Array();
	if (subKey == '_order' ) {
        ar = yjNvalTable[subKey].split('|');
	} else {
        ar = yjNvalTable[subKey];
	}
    return ar;
}

function yjNsetSubKey(obj) {
    var objList = obj['list'];
    var allValue = '';


    for ( var key in objList) {
        allValue += '&';
        var subKey = objList[key]['keyName'];
        var value = objList[key]['keyValue'];

        if ( subKey == '_order' ) {
            allValue += subKey + '=' + value.join('|');
            yjNvalTable[subKey] = value.join('|');
        } else if ( subKey == '_version' ) {
            yjNvalTable[subKey] = parseInt(value, 10);
        } else {
            allValue += 'cat' + '=' + subKey;
			for (var ind in value ) {
                allValue += '&' + yjNargList[ind] + '=' + value[ind]; 
			}
            yjNvalTable[subKey] = value;
        }
    }
    yjNset(allValue);
}

function yjNtoggle_change(id) {
    var catValArr = yjNgetSubValue(id);

    var obj = yjNgetElement("li_"+id);
    var newValue;
    if (obj) {
        if ( catValArr[0] == "0" ) {
            var catName = catValArr[1];
            obj.style.display = "block";
            var tmpHtml = '<div class="ynbox_bd"><div class="list_ya"><ul class="s130">';
            tmpHtml += yjNcatTable[catName];
            tmpHtml += '</url><p class="yjSt ya"><a href="' + yjNcatInfoTable[catName]['url'] + '">すべての記事を読む</a></p>';
            tmpHtml += '</div>';
            obj.innerHTML = tmpHtml;
            newValue = "1";
        } else {
            obj.style.display = "none";
            newValue = "0";
        }
    }

    var imgobj = yjNgetElement("ib_"+id);
    if (imgobj) {
        imgobj.src = ( catValArr[0] == "0" ) ? yjNopenIcon : yjNcloseIcon;
        imgobj.alt = ( catValArr[0] == "0" ) ? '閉じる' : '開く';
    }

    catValArr[0] = newValue;
    var keyObject = { 'list': [ { 'keyName': id, 'keyValue': catValArr} ] };
    yjNsetSubKey(keyObject);
}

function yjNtoggle_init() {
    var valArr = yjNgetSubValue('_order');
    var arrLength = valArr.length;

    for (var i = 0; i < arrLength; i++) {
        var catId = valArr[i];
        var catValArr = yjNgetSubValue(catId);
        var selCatId = catValArr[1];

        var obj = yjNgetElement("li_"+catId);
        if (obj) {
            if ( catValArr[0] == "0" ) {
                obj.style.display = "none";
            } else {
                var tmpHtml = '<div class="ynbox_bd"><div class="list_ya"><ul class="s130">';
                tmpHtml += yjNcatTable[selCatId];
            	tmpHtml += '</ul><p class="yjSt ya"><a href="' + yjNcatInfoTable[selCatId]['url'] + '">すべての記事を読む</a></p>';
                tmpHtml += '</div></div>';
                obj.innerHTML = tmpHtml;
                obj.style.display = "block";
            }
        }

        var iconObj = yjNgetElement("ia_" + catId);
        if (iconObj) {
            var tmpHtml = '<a href="#" onClick="yjNtoggle_change(' + "'" + catId + "'" + ');return false">' + '<img id="ib_' + catId + '" src="';
            tmpHtml += (catValArr[0] == "0" ) ? yjNcloseIcon : yjNopenIcon;
            tmpHtml += (catValArr[0] == "0" ) ? '" alt="開く"' : '" alt="閉じる"';
            tmpHtml += ' width="19" height="15" border="0" align="absmiddle" style="margin-right:4px;"/></a>';
            iconObj.innerHTML = tmpHtml; 
        }

        var tabObj = yjNgetElement("sub_" + catId);
        if (tabObj) {
            var tabHtml = '';
			var subCatOrder = catValArr[2].split('|');
            var subCategoryLength = subCatOrder.length;
            var onNewsFlg = 0;
            for (var j=0; j < subCategoryLength; j++) {
				var subCatId = subCatOrder[j];
				if (subCatId) {
                    tabHtml += '<li id="' + subCatId + '" class="fsspx subCategory';
                    if (subCatId == selCatId ) {
                        tabHtml += ' on_news"><div>';
                        tabHtml += yjNcatInfoTable[subCatId]['name'] + '</div></li>';
                        onNewsFlg = 1;
                        continue;
                    }
                    if (onNewsFlg == 1) {
                        tabHtml += ' l_space">';
                        onNewsFlg = 0;
                     } else if ( j != 0 ) {
                        tabHtml += ' l_line">';	
                     } else {
                        tabHtml += '">';	
                     }
                     tabHtml += '<a href="' + yjNcatInfoTable[subCatId]['url']	+ '" onClick="yjNshow_list(this);return false">' + yjNcatInfoTable[subCatId]['name'] + '</a>';
                     tabHtml += '</li>';
                 }
            }
            tabObj.innerHTML = tabHtml;
        }
    }
}

function yjNfold_init() {
    var artObj = yjNgetElement("artFold");
    if (artObj) {
        artObj.style.display = "none";
    }
    var linkObj = yjNgetElement("foldLink");
    if (linkObj) {
        var linkHtml = '<a href="#" onClick="yjNfold_change(' + "'op'" + ');return false">[続きを読む]</a>';
        linkObj.innerHTML = linkHtml;
    }
}

function yjNfold_change(state) {
    var artObj = yjNgetElement("artFold");
    if (artObj) {
        if (state == 'op') {
            artObj.style.display = "block";
        } else if (state == 'cl') {
            artObj.style.display = "none";
        }
    }

    var linkObj = yjNgetElement("foldLink");
    if (linkObj) {
        var linkHtml = '';
        if (state == 'op') {
            linkHtml = '<a href="#" onClick="yjNfold_change(' + "'cl'" + ');return false">[記事を折りたたむ]</a>';
        } else if (state == 'cl') {
            linkHtml = '<a href="#" onClick="yjNfold_change(' + "'op'" + ');return false">[続きを読む]</a>';
        }
        linkObj.innerHTML = linkHtml;
    }
}

