var first_flg = true;
var end_flg = false;
var wmp_obj = new Object;
var _isMSIE = false;
var chapter_id = 0;
var keywords_id = -1;

var chapter_ary = [0,49.8,105.0,212.5,600];
var keywords_ary = [20,60.3,116,220.0];

/* 説明 ：キーワードを変更する(試作)
*/
function changeKeywords() {
    var active = 'ActiveKeywords';
    var flg = false;
    keywords = $$('#Keywords p');
    scroll_div = $$('#Keywords div')[0];
    i = 0;
    keywords.each( function(word) {
            if(first_flg) {
            first_flg = false;
            Element.show(word);
            } else if(flg) {
            //console.log(word);
            Element.show(word);
            word.addClassName(active);
            //scroll_div.scrollTop = i * getPHeight(word);
            flg = false;
            } else if(word.className == active) {
            if(word != keywords.last()) {
            word.removeClassName(active);
            flg = true;
            }
            } else {
            }
            i++;
            });
    initFunction();

    function getPHeight(p) {
        pheight = p.offsetHeight;
        if(_isMSIE) { // IE の場合
            return pheight - 1;
        } else { // IE以外の場合
            return pheight;
        }
    }
}

/* 説明 ：キーワードを変更する(実働)
*/
function changeKeywordsOnTime(return_flg) {
    var active = 'ActiveKeywords';

    if(wmp_obj.PlayState != 0) {

        now = wmp_obj.CurrentPosition;
        before_keywords_id = keywords_id;
        keywords_id = _selectKeywordCount(now);

        if(keywords_id != before_keywords_id) {
            keywords = $$('#Keywords p');
            i = 0;
            keywords.each( function(word) {
                    if(i > keywords_id) {
                    word.removeClassName(active);
                    Element.hide(word);
                    } else if(i == keywords_id) {
                    word.addClassName(active);
                    Element.show(word);
                    } else {
                    word.removeClassName(active);
                    word.removeClassName(active);
                    Element.show(word);
                    }
                    i++;
                    });
        }

        if(return_flg) {
            new_chapter_id = _selectChapterCount(now);
            changeChapter(new_chapter_id, false);
        }
    }
}


function _selectKeywordCount(now) {
    i = -1;
    keywords_ary.each( function(word_time) {
            if(now < word_time) {
            return i;
            }
            i++;
            });
    return i;
}

function _selectChapterCount(now) {
    var i = -2;
    var flg = true;
    before_time = 0;
    if(now == 0) {
        return 0;
    }
    chapter_ary.each( function(chapter_time) {
            if(flg) {
            i++;
            if(before_time < now && now < chapter_time) {
            //alert(before_time + ' < ' + chapter_time);
            flg = false;
            }
            before_time = chapter_time;
            }
            });
    //alert('now : ' + now + ' | i : ' + i);
    return i;
}

/* 説明 ：タイマーをセット
*/
function initFunction() {
    var _sec = 1;
    tid = setInterval(checkTimer, 1000 * _sec);
}

/* 説明 ：タイマーで動く処理
*/
function checkTimer() {
    changeKeywordsOnTime(true);
}

/* 説明   ：動画を変更する
 * 引数   ：変更先の動画URL
 */
function changeMovie(movie_url) {
    var active = 'ActiveMovie';
    if (movie_url && wmp_obj) {
        if(_isMSIE) {
            wmp_obj.FileName = movie_url;
        } else {
        }

        r = /\/(\w+)\.wmv/i;
        m = movie_url.match(r);
        chapters = $$('#Movie dl');
        chapters.each( function(chapter) {
                if(chapter.id == m[1]) {
                chapter.addClassName(active);
                } else {
                chapter.removeClassName(active);
                }
                });

    }
}

/* 説明 ：チャプター変更
*/
function changeChapter(set_sec_num, change_flg) {
    var active = 'ActiveMovie';
    var set_sec = chapter_ary[set_sec_num];

    before_chapter_id = chapter_id;
    if (wmp_obj) {
        if(change_flg) {
            if(_isMSIE) {
                wmp_obj.CurrentPosition = set_sec;
            } else {
            }
            changeKeywordsOnTime(false);
        }
        chapter_id = set_sec_num;

        if(before_chapter_id != chapter_id) {
            //alert('change!!');
            set_chapter = 'Q' + (set_sec_num + 1);
            chapters = $$('#Movie dl');
            chapters.each( function(chapter) {
                    if(chapter.id == set_chapter) {
                    chapter.addClassName(active);
                    } else {
                    chapter.removeClassName(active);
                    }
                    });
        }
        if(change_flg && wmp_obj.PlayState != 2) {
            wmp_obj.Play();
        }
    }
}

/* 説明 ：再生終了時の処理
*/
function endMovie() {
}

/* 説明 ：動画の再生開始・再生時間変更時の処理
*/
function reloadMovie() {
    //changeKeywordsOnTime(true);
    //alert(wmp_obj.CurrentPosition);
}

/* 説明 ：画面ロード時の処理
*/
function loadFunction() {
    setIsMSIE();

    if(_isMSIE) { // IE の場合
        wmp_obj = MediaPlayer1;
    } else { // IE以外の場合
        wmp_obj = $('MediaPlayer1');
    }
    keywords = $$('#Keywords p');
    keywords.each( function(word) {
            Element.hide(word);
            });
    //console.log(wmp_obj);
    initFunction();

    /* 説明   ：IEとそれ以外を区別する
     * 返り値 ：IE => true
     *          それ以外 => false
     */
    function setIsMSIE() {
        var isMSIE = /*@cc_on!@*/false;
        if(isMSIE) { // IE の場合
            _isMSIE = true;
        } else { // IE以外の場合
            _isMSIE = false;
        }
    }
}

var __t = new Date;

