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'],
                    'c_spo'  : [ '1', 'c_spo', 'c_spo|spo|base|socc|moto|horselgolf|fight'],
                    'c_sci'  : [ '1', 'c_sci', 'c_sci|sci|inet|mobi|secu'],
                    'loc'    : [ '1', 'loc', 'loc|hok|toh|kan|sin|hor|tok|kin|chu|sik|kyu|oki'],
                    '_order' : 'dom|c_int|bus|c_ent|c_spo|c_sci|loc',
                    '_version' : '3'
               };
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 yjNReqURL = "/lib/ctlCookie_1_2.php";
var yjNReqVersion = "3";

var yjNcatInfoTable = {
          'dom':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/dom/more/*http://headlines.yahoo.co.jp/hl?c=dom'},
          'pol':     { 'name': '政治',         'url': 'http://rd.yahoo.co.jp/media/news/top/dom/pol/more/*http://headlines.yahoo.co.jp/hl?c=pol&t=l'    },
          'soci':    { 'name': '社会',         'url': 'http://rd.yahoo.co.jp/media/news/top/dom/soci/more/*http://headlines.yahoo.co.jp/hl?c=soci&t=l'   },
          'peo':     { 'name': '人',           'url': 'http://rd.yahoo.co.jp/media/news/top/dom/peo/more/*http://headlines.yahoo.co.jp/hl?c=peo&t=l'    },
          'c_int':   { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_int/more/*http://headlines.yahoo.co.jp/hl?c=c_int'},
          'int':     { 'name': '総合',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_int/int/more/*http://headlines.yahoo.co.jp/hl?c=int&t=l'    },
          'cn':      { 'name': '中国',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_int/cn/more/*http://headlines.yahoo.co.jp/hl?c=cn&t=l'     },
          'kr':      { 'name': '韓国',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_int/kr/more/*http://headlines.yahoo.co.jp/hl?c=kr&t=l'     },
          'bus':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/bus/more/*http://headlines.yahoo.co.jp/hl?c=bus'},
          'bus_all': { 'name': '総合',         'url': 'http://rd.yahoo.co.jp/media/news/top/bus/bus_all/more/*http://headlines.yahoo.co.jp/hl?c=bus_all&t=l'},
          'brf':     { 'name': '市況',         'url': 'http://rd.yahoo.co.jp/media/news/top/bus/brf/more/*http://headlines.yahoo.co.jp/hl?c=brf&t=l'    },
          'biz':     { 'name': '株式',         'url': 'http://rd.yahoo.co.jp/media/news/top/bus/biz/more/*http://headlines.yahoo.co.jp/hl?c=biz&t=l'    },
          'ind':     { 'name': '産業',         'url': 'http://rd.yahoo.co.jp/media/news/top/bus/ind/more/*http://headlines.yahoo.co.jp/hl?c=ind&t=l'    },
          'c_ent':   { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/more/*http://headlines.yahoo.co.jp/hl?c=c_ent'},
          'ent':     { 'name': '総合',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/ent/more/*http://headlines.yahoo.co.jp/hl?c=ent&t=l'    },
          'musi':    { 'name': '音楽',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/musi/more/*http://headlines.yahoo.co.jp/hl?c=musi&t=l'   },
          'movi':    { 'name': '映画',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/movi/more/*http://headlines.yahoo.co.jp/hl?c=movi&t=l'   },
          'game':    { 'name': 'ゲーム',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/game/more/*http://headlines.yahoo.co.jp/hl?c=game&t=l'   },
          'fash':    { 'name': 'ファッション', 'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/fash/more/*http://headlines.yahoo.co.jp/hl?c=fash&t=l'   },
          'c_spo':   { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/more/*http://headlines.yahoo.co.jp/hl?c=c_spo'},
          'spo':     { 'name': '総合',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/spo/more/*http://headlines.yahoo.co.jp/hl?c=spo&t=l'    },
          'base':    { 'name': '野球',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/base/more/*http://headlines.yahoo.co.jp/hl?c=base&t=l'   },
          'socc':    { 'name': 'サッカー',     'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/socc/more/*http://headlines.yahoo.co.jp/hl?c=socc&t=l'   },
          'moto':    { 'name': 'モーター',     'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/moto/more/*http://headlines.yahoo.co.jp/hl?c=moto&t=l'   },
          'horse':   { 'name': '競馬',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/horse/more/*http://headlines.yahoo.co.jp/hl?c=horse&t=l'  },
          'golf':    { 'name': 'ゴルフ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/golf/more/*http://headlines.yahoo.co.jp/hl?c=golf&t=l'   },
          'fight':   { 'name': '格闘技',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/fight/more/*http://headlines.yahoo.co.jp/hl?c=fight&t=l'  },
          'c_sci':   { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/more/*http://headlines.yahoo.co.jp/hl?c=c_sci'},
          'sci':     { 'name': '総合',         'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/sci/more/*http://headlines.yahoo.co.jp/hl?c=sci&t=l'    },
          'inet':    { 'name': 'インターネット', 'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/inet/more/*http://headlines.yahoo.co.jp/hl?c=inet&t=l' },
          'mobi':    { 'name': 'モバイル',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/mobi/more/*http://headlines.yahoo.co.jp/hl?c=mobi&t=l' },
          'secu':    { 'name': 'セキュリティ',   'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/secu/more/*http://headlines.yahoo.co.jp/hl?c=secu&t=l' },
          'loc':     { 'name': 'トップ',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/more/*http://headlines.yahoo.co.jp/hl?c=loc' },
          'hok':     { 'name': '北海道',       'url': 'http://rd.yahoo.co.jp/media/news/top/loc/hok/more/*http://headlines.yahoo.co.jp/hl?c=hok' },
          'toh':     { 'name': '東北',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/toh/more/*http://headlines.yahoo.co.jp/hl?c=toh' },
          'kan':     { 'name': '関東',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/kan/more/*http://headlines.yahoo.co.jp/hl?c=kan' },
          'sin':     { 'name': '信越',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/sin/more/*http://headlines.yahoo.co.jp/hl?c=sin' },
          'hor':     { 'name': '北陸',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/hor/more/*http://headlines.yahoo.co.jp/hl?c=hor' },
          'tok':     { 'name': '東海',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/tok/more/*http://headlines.yahoo.co.jp/hl?c=tok' },
          'kin':     { 'name': '近畿',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/kin/more/*http://headlines.yahoo.co.jp/hl?c=kin' },
          'chu':     { 'name': '中国',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/chu/more/*http://headlines.yahoo.co.jp/hl?c=chu' },
          'sik':     { 'name': '四国',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/sik/more/*http://headlines.yahoo.co.jp/hl?c=sik' },
          'kyu':     { 'name': '九州',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/kyu/more/*http://headlines.yahoo.co.jp/hl?c=kyu' },
          'oki':     { 'name': '沖縄',         'url': 'http://rd.yahoo.co.jp/media/news/top/loc/oki/more/*http://headlines.yahoo.co.jp/hl?c=oki' }
      };

var yjNcatPhotoInfoTable = {
          'dom':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/dom/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=dom'},
          'c_int':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_int/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=c_int'},
          'bus':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/bus/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=bus'},
          'c_ent':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_ent/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=c_ent'},
          'c_spo':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_spo/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=c_spo'},
          'c_sci':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/c_sci/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=c_sci'},
          'loc':     { 'name': 'トップ',       'url': 'http://rd.yahoo.co.jp/media/news/top/loc/photo/more/*http://headlines.yahoo.co.jp/hl?ty=p&c=loc'}
};

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['loc'] = jsonData['loc'];
            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['loc'] = jsonData['loc'];
            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= yjNReqURL + '?func=get&name=YJNEWSFOLD&v=' + yjNReqVersion;
        yjNxmlHttpRequestObj = yjNcreateXMLHttpRequest(yjNprocessRequestChangeInit);
        if (yjNxmlHttpRequestObj) {
            yjNxmlHttpRequestObj.open("GET", url, true);
            yjNxmlHttpRequestObj.send("");
        } 
    }
    yjNcached = 1;
}

function yjNget() {
    if (yjNcached == 1) {
        return;
    } else {
        var url= yjNReqURL + '?func=get&name=YJNEWSFOLD&v=' + yjNReqVersion;
        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 = yjNReqURL + '?func=set&name=YJNEWSFOLD&v=' + yjNReqVersion;
    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];
            if ( catName == id ) {
                tmpHtml += '</url><p class="yjSt ya"><a href="' + yjNcatInfoTable[catName]['url'] + '" style="margin-right:10px;">すべての記事を読む</a><a href="' + yjNcatPhotoInfoTable[catName]['url'] + '">すべての写真を見る</a></p>';
            } else {
                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);

	if ( catValArr == null ) continue;

        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];
                if ( selCatId == catId ) {
            	    tmpHtml += '</ul><p class="yjSt ya"><a href="' + yjNcatInfoTable[selCatId]['url'] + '" style="margin-right:10px;">すべての記事を読む</a><a href="' + yjNcatPhotoInfoTable[selCatId]['url'] + '">すべての写真を見る</a></p>';
                } else {
            	    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;
    }
}

