// 入力値保持用
var vd_Keywords;

// インデックス保持用
var vd_Index;

// ID保持用
var vd_IDs;

// Title保持用
var vd_Titles;

// メイン表示用divエレメント保持用
var VideoDiv;

// リスト表示用divエレメント保持用
var ListDiv;

// リスト表示状態
var ListClose;
var ListCloseStr;
var ListOpenStr;

// 初期処理
function vd_init() {
    vd_Keywords  = "";
    vd_Index     = 0;
    vd_IDs       = new Array();
    vd_Titles    = new Array();
    VideoDiv     = document.getElementById('videobox');
    ListDiv      = document.getElementById('videolist');
    ListCloseStr = '<p style="text-align : center;"><strong><a href="javascript:vd_List();">動画リストを見る</a></strong></p>';
    ListOpenStr  = '<p style="text-align : center;"><strong><a href="javascript:vd_List();">動画リストを隠す</a></strong></p>';
    ListClose    = true;
}

// 終了時処理
function vd_conclude(e) {
    for(objname in httpObj) {
        abortXMLHttpRequest(objname);
    }
}

// 描画処理
function vd_Disp() {
    buf = "";
    if(vd_IDs.length>vd_Index) {
        buf       = '<p style="text-align : center;"><object width="425" height="335"><param name="movie" value="http://www.youtube.com/v/' + vd_IDs[vd_Index] + '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + vd_IDs[vd_Index] + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="335"></embed></object></p>';
        buf      += '<p><strong>' + vd_Titles[vd_Index] + '</strong></p>';
        if(vd_IDs.length==1) {
            buf  += '<p style="text-align : right;">|&lt; &lt;&lt; &lt;Prev [ ' + (vd_Index+1) + ' / ' + vd_IDs.length + ' ] Next&gt;&gt;&gt;&gt;|</p>';
        }
        else
        if(vd_Index==0) {
            buf  += '<p style="text-align : right;">|&lt; &lt;&lt; &lt;Prev [ ' + (vd_Index+1) + ' / ' + vd_IDs.length + ' ] <a href="javascript:vd_Paging(\'NEXT\');">Next&gt;</a> <a href="javascript:vd_Paging(\'NEXT10\');">&gt;&gt;</a> <a href="javascript:vd_Paging(\'LAST\');">&gt;|</a></p>';
        }
        else
        if(vd_Index==vd_IDs.length-1) {
            buf  += '<p style="text-align : right;"><a href="javascript:vd_Paging(\'TOP\');">|&lt;</a> <a href="javascript:vd_Paging(\'PREV10\');">&lt;&lt;</a> <a href="javascript:vd_Paging(\'PREV\');">&lt;Prev</a> [ ' + (vd_Index+1) + ' / ' + vd_IDs.length + ' ] Next&gt; &gt;&gt; &gt;|</p>';
        }
        else {
            buf  += '<p style="text-align : right;"><a href="javascript:vd_Paging(\'TOP\');">|&lt;</a> <a href="javascript:vd_Paging(\'PREV10\');">&lt;&lt;</a> <a href="javascript:vd_Paging(\'PREV\');">&lt;Prev</a> [ ' + (vd_Index+1) + ' / ' + vd_IDs.length + ' ] <a href="javascript:vd_Paging(\'NEXT\');">Next&gt;</a> <a href="javascript:vd_Paging(\'NEXT10\');">&gt;&gt;</a> <a href="javascript:vd_Paging(\'LAST\');">&gt;|</a></p>';
        }

        if(ListDiv!=null) {
            ListClose = !ListClose;
            vd_List();
        }
    }
    else {
        buf       = '<p>見つかりませんでした…。</p>';
    }
    VideoDiv.innerHTML = buf;
}

// ページング
function vd_Paging(ope) {
    if(ope=="TOP") {
        vd_Index = 0;
    }
    else
    if(ope=="PREV10") {
        if(0<vd_Index-10) {
            vd_Index -= 10;
        }
        else {
            vd_Index = 0;
        }
    }
    else
    if(ope=="PREV") {
        vd_Index--;
    }
    else
    if(ope=="NEXT") {
        vd_Index++;
    }
    else
    if(ope=="NEXT10") {
        if(vd_IDs.length>vd_Index+10) {
            vd_Index += 10;
        }
        else {
            vd_Index = vd_IDs.length - 1;
        }
    }
    else
    if(ope=="LAST") {
        vd_Index = vd_IDs.length - 1;
    }
    vd_Disp();
}

// リスト表示
function vd_List() {
    var buf;

    if(ListClose) {
        buf  = ListOpenStr;
        buf += '<ol style="list-style-type: decimal">';
        for(i=0;i<vd_IDs.length;i++) {
            if(i==vd_Index) {
                buf += '<li style="font-weight: bold; color: #FF0000;">';
                buf += '<a style="font-weight: bold; color: #FF0000;" href="javascript:vd_Index=' + i + ';vd_Disp();">' + vd_Titles[i] + '</a>';
            }
            else {
                buf += '<li>';
                buf += '<a href="javascript:vd_Index=' + i + ';vd_Disp();">' + vd_Titles[i] + '</a>';
            }
            buf += '</li>';
        }
        buf += '</ol>';
        ListClose = false;
    }
    else {
        buf  = ListCloseStr;
        ListClose = true;
    }
    ListDiv.innerHTML = buf;
}

// 検索結果表示・共通処理
function vd_displayresults(xml) {
    VideoDiv.innerHTML = "";
    items = xml.getElementsByTagName('video');
    //CountNode = xml.getElementsByTagName('total');
    //Count     = CountNode[0].childNodes[0].nodeValue;
    for(i=0;i<items.length;i++) {
        IDNode       = items[i].getElementsByTagName('id');
        ID           = IDNode[0].childNodes[0].nodeValue;
        TitleNode    = items[i].getElementsByTagName('title');
        Title        = TitleNode[0].childNodes[0].nodeValue;
        vd_IDs[i]    = ID;
        vd_Titles[i] = Title;
    }
    vd_Index = 0;
    vd_Disp();
}

// 検索処理
function vd_Search() {
    // HTTP通信を開始し、完了したら結果表示関数を実行させる
    httpRequest("http://www.sirmiles.com/plaza/xml_tool_youtube.php","tag='" + encodeURIComponent(vd_Keywords) + "'",function(xml){vd_displayresults(xml);});
}

// アンロードされた際に、vd_conclude()関数を実行させる
addListener(window, 'unload', vd_conclude, false);

