﻿
function setMenuBar(obj, path, ext) {
    var over = path + "_over" + ext;
    var out  = path + ext;
    var on   = path + "_on" + ext;

    addEvent(obj, 'mouseover', rolloverImage);
    addEvent(obj, 'mouseout', rolloverImage);
    addEvent(obj, 'click', switchImage);

    obj.imgOn = new Image();
    obj.imgOn.src = on;

    obj.imgOut = new Image();
    obj.imgOut.src = out;

    obj.imgOver = new Image();
    obj.imgOver.src = over;

}

objPast = null;

function rolloverImage(event) {
    var obj = event.target || event.srcElement;
    if (objPast == obj) {

        if (event.type == "mouseover") {

            obj.src = obj.imgOver.src;
        } else {
            obj.src = obj.imgOn.src;
        }
    } else {
        
        if (event.type == "mouseover") {
            obj.src = obj.imgOver.src;
        } else {
            obj.src = obj.imgOut.src;
        }
    }
    
}

function switchImage(event) {
    var obj = event.target || event.srcElement;
    if (objPast != obj) {
        if (objPast != null) { objPast.src = objPast.imgOut.src; }
        obj.src = obj.imgOn.src;
        objPast = obj;
    }
}

function addEvent(obj, type, func) {
    if (obj.addEventListener) {
        obj.addEventListener(type, func, false);
    }
    else if (obj.attachEvent) {
        obj.attachEvent('on' + type, func);
    }
}

function link_up(url) {
    window.scroll(0, 0);
    requestFile('', 'GET', url, true, reload_contents_area);
}

function link(url) {
    requestFile('', 'GET', url, true, reload_contents_area);
}

//XMLHttpRequestオブジェクト生成
function createHttpRequest() {

    var xhr = null;
    if (XMLHttpRequest) {
        // 組み込みオブジェクトとして定義されていればそれを利用
        xhr = new XMLHttpRequest();
    } else {
        // さもなくばActiveXオブジェクトを利用
        try {
            xhr = new ActiveXObject('MSXML2.XMLHTTP.6.0');
        } catch (e) {
            try {
                xhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            } catch (e) {
                try {
                    xhr = new ActiveXObject('MSXML2.XMLHTTP');
                } catch (e) {
                    alert("ActiveXを有効にしてください");
                }
            }
        }
    }
    return xhr;
}

httpoj = createHttpRequest();

//ファイルにアクセスし受信内容を確認します
function requestFile(data, method, fileName, async, callback) {

    httpoj.abort();

    var ht = createHttpRequest();

    //受信時に起動するイベント
    ht.onreadystatechange = function () {
        if (ht.readyState == 4 && ht.status == 200) {
            //コールバック
            callback(ht);
        }
    }
        
    var aNode = document.getElementById("contents_area");
    
    for (var i = aNode.childNodes.length - 1; i >= 0; i--) {
        aNode.removeChild(aNode.childNodes[i]);
    }
    var image = document.createElement("img");
    image.src = "./images/now_loading.gif";
    aNode.appendChild(image);

    //open メソッド
    ht.open(method, fileName, async);
    //send メソッド
    ht.send(data);

    httpoj = ht;
}

function reload_contents_area(oj) {
    //レスポンスを取得
    var res = oj.responseText
    //DIV へ出力
    document.getElementById('contents_area').innerHTML = res
}

function SearchSubmit() {
    var target = "artist";
    if (document.getElementById('search_target_content').checked) {
        target = "content";
    }

    var parameter =
        "word=" + encodeURIComponent(document.getElementById('search_word').value)
        + "&" +
        "target=" + target;

    

    requestFile("", 'GET', 'list_search.php?' + parameter, true, reload_contents_area);
}
