﻿var _Top = 0;
var _WindowID = 0;
var _MenuLeft = -128;
var _TwitAlpha = 0;
var _TwitOpen = false;
var _BackAlpha = 0;
var _BackOpen = false;

var _Ajax = new rqAjax();
var _CallTwit = null;
var _CallBlog = null;
var _CallNico = null;
var _CallBack = null;
var _LoadingBack = true;

// 背景
var _BGFeed;
var _BGID;
var _BGWidth = 800;
var _BGHeight = 600;
var _BGWaitCount;

window.onresize = changeWindowSize;

// オンロード
function start()
{
    document.getElementById( "output" ).innerHTML = "";
    document.getElementById( "cm" ).innerHTML = "<iframe src='delay.html' width=40 height=15 frameborder=0 scrolling=no></iframe>";
    document.getElementById( "counter" ).innerHTML = '<font size =1><img src="http://counter1.fc2.com/counter_img.php?id=1692954">peoples<br>since 2000/9/22</font>';
    document.getElementById( "hand" ).innerHTML = '<a href="http://clap.fc2.com/post/rrrq/?url=http%3A%2F%2Fhp.vector.co.jp%2Fauthors%2FVA018980%2F&title=RRRQ%27s+Homepage" target="_blank" title="web拍手 by FC2"><img src="http://clap.fc2.com/images/button/black/rrrq?url=http%3A%2F%2Fhp.vector.co.jp%2Fauthors%2FVA018980%2F" alt="web拍手" style="border:none;" /></a>';
    document.getElementById( "amazon" ).innerHTML = '<iframe src="http://rcm-jp.amazon.co.jp/e/cm?t=nomotivation-22&o=9&p=6&l=ez&f=ifr&f=ifr" width="120" height="150" scrolling="no" marginwidth="0" marginheight="0" border="0" frameborder="0" style="border:none;"></iframe>';

    // メニュー作成
    createMenu();

    // 最新つぶやき取得
    getLatestTwit();

    // ウィンドウサイズ変更
    changeWindowSize();

    // 背景読み込み
    _CallBack = _Ajax.addScript( "http://pipes.yahoo.com/pipes/pipe.run?_id=90a9dd99380253ddb7442d2157285072&_render=json&_callback=getBackground" + "&cacheCancel=" + (new Date()).toString() );

    // アニメon
    setTimer();
}

// メニュー作成
function createMenu()
{
    var windows = new Array();
    var titles = new Array();

    windows[1] = "blog";
    windows[2] = "software";
    windows[3] = "notice";
    windows[4] = "links";

    titles[1] = "blog";
    titles[2] = "software";
    titles[3] = "notice";
    titles[4] = "links";

    var inner = "";
    for( var i = 1; i < windows.length; i++ )
    {
        inner += "<div class='forclick' id='" + windows[i] + "' onClick=\"openWindow('" + windows[i] + "')\" onMouseOver=\"menuHover('" + windows[i] + "')\" onMouseOut=\"menuOut('" + windows[i] + "')\">・ <u>" + titles[i] + "</u></div>"
    }

    document.getElementById( "menu" ).innerHTML = inner;
}

// 背景読み込み
function getBackground( f )
{
    _BGFeed = f;
    var count = _BGFeed.count;
    if( count > 5 ) count = 5;
    if( count > 0 )
    {
        var id = Math.floor(Math.random() * count);

        _Ajax.removeScript( _CallBack );
        _CallBack = null;

        // 表示
        showBackground( id );
        
        // ランダムセレクト    
        setTimeout( selectRandomBG, 1000 );
    }
}

// id版の背景を読み込み
function showBackground( id )
{
    _BGID = id;

    var item = _BGFeed.value.items[id];
    var back = item.url;
    
    var img = "<img src='" + back + "' onload='loadedImage()'>";
    document.getElementById( "preload" ).innerHTML = img;
    
    //　ロード中
    _LoadingBack = true;
    setTimer();
    
    // やじるし非表示
    arrowOff();

    // loading
    document.body.background = "img/loading.gif";
}

// 背景読みこみ完了
function loadedImage()
{
    // 有効化
    _BackOpen = true;
    _BGWaitCount = 0;
    _LoadingBack = false;

    var item = _BGFeed.value.items[_BGID];
    var back = item.url;
    back = back.replace( "_original", "" );
    
    var idx = item.date.indexOf( "T" );
    var dateonly = item.date.substr( 0, idx );

    var img = "";
    img += "<img src='" + back + "' onload='startFadein()' id='backimg'>";
    img += "<div id='imginfo'> <img src='img/larrow.gif' class='larrow' id='larrow' onClick='onLArrow()'><img src='img/rarrow.gif' class='rarrow' id='rarrow' onClick='onRArrow()'>";

    img += "<a href='" + item.link +"' target='_blank'>" + item.title + "</a>  " + dateonly + "</div>";
    document.getElementById( "background" ).innerHTML = img;

    document.getElementById( "background" ).style.display = "none";
    document.getElementById( "imginfo" ).style.display = "none";
}

// フェードイン開始
function startFadein()
{
    document.body.background = "";
    document.getElementById( "background" ).style.display = "block";

    _BackAlpha = 0;
    setAlpha( document.getElementById( "background" ), 0 );

    _BGWidth  = document.getElementById( "background" ).offsetWidth;
    _BGHeight = document.getElementById( "background" ).offsetHeight;

    if( _BGWidth > _BGHeight )
    {
        if( _BGWidth > 800 )
        {
            var rate = 800 / _BGWidth;
            _BGWidth = 800;
            _BGHeight *= rate;
            
            option = "width=" + _BGWidth;
        }
    }
    else
    {
        if( _BGHeight > 600 )
        {
            var rate = 600 / _BGHeight;
            _BGHeight = 600;
            _BGWidth *= rate;

            option = "height=" + _BGHeight;
        }
    }

    document.getElementById( "backimg" ).width = _BGWidth;
    document.getElementById( "backimg" ).height = _BGHeight;

    document.getElementById( "imginfo" ).style.display = "block";

    // ウィンドウサイズ変更
    changeWindowSize();

    // タイマーセット
    setTimer();
}

// やじるし表示
function arrowOn()
{
    if( _WindowID == "" && _LoadingBack == false )
    {
        document.getElementById( "larrow" ).style.display = "block";
        document.getElementById( "rarrow" ).style.display = "block";
    }
}
// やじるし非表示
function arrowOff()
{
    if( document.getElementById( "larrow" ) && document.getElementById( "rarrow" ) )
    {
        document.getElementById( "larrow" ).style.display = "none";
        document.getElementById( "rarrow" ).style.display = "none";
    }
}

// 背景ランダム変更
function selectRandomBG()
{
    _BGWaitCount++;
    if( _BGWaitCount == 30 && _LoadingBack == false )
    {
        var count = _BGFeed.count;
        var id = Math.floor(Math.random() * count);
        while( id == _BGID )
        {
            id = Math.floor(Math.random() * count);
        }

        // 表示
        showBackground( id );
    }

    // ポーリング
    setTimeout( selectRandomBG, 1000 );
}

// やじるし押された
function onLArrow()
{
    _BGID++;
    if( _BGID >= _BGFeed.count ) _BGID = 0;

    // 表示
    showBackground( _BGID );
}
function onRArrow()
{
    _BGID--;
    if( _BGID < 0 ) _BGID = _BGFeed.count - 1;

    // 表示
    showBackground( _BGID );
}

// ウィンドウサイズ変更
function changeWindowSize()
{
    var ofsx = (document.body.clientWidth - _BGWidth) / 2;
    var ofsy = (document.body.clientHeight - _BGHeight) / 2;

    if( document.getElementById( "background" ) )
    {
        document.getElementById( "background" ).style.left = ofsx + "px";
        document.getElementById( "background" ).style.top = ofsy + "px";
    }
}

// メニュー色変更(マウスホバー状態)
function menuHover( id )
{
    document.getElementById( id ).style.color = "#FFFFFF";
}

// メニュー色元に戻す
function menuOut( id )
{
    document.getElementById( id ).style.color = "#CCCCCC";
}

// ウィンドウ閉じる
function closeWindow()
{
    _WindowID = "";

    document.getElementById( "window" ).style.display = "none";
    document.getElementById( "loading" ).style.display = "none";
    
    setTimer();
}

// ウィンドウを開く
function openWindow( id )
{
    if( _WindowID != id )
    {
        _WindowID = id;

        document.getElementById( "window" ).style.display = "none";
        document.getElementById( "loading" ).style.display = "block";

        if( id == "blog" )
        {
            if( _CallBlog == null )
            {
                // ブログ読み込み
                _CallBlog = _Ajax.addScript( "http://rrrqs.blogspot.com/feeds/posts/default?alt=json-in-script&callback=readBlog" + "&cacheCancel=" + (new Date()).toString() );
            }
        }
        else
        {
            _Ajax.requestGET( id + ".inc", getInc );
        }
    }
    else
    {
        // 同じメニューが押されたらクローズ
        closeWindow();
    }
}

// インクルード取得
function getInc( inc )
{
    showWindow( inc.responseText );
}

// ウィンドウ可視化
function showWindow( txt )
{
    _Top = 88;
    setTimer();

    document.getElementById( "window" ).innerHTML = txt;
    document.getElementById( "window" ).style.top = _Top + "px";
    document.getElementById( "window" ).style.display = "block";
    setAlpha( document.getElementById( "window" ), 0 );
    document.getElementById( "loading" ).style.display = "none";
}

// ニコニコ動画フリップ
function flipNico()
{
    if( document.getElementById( "nico" ).style.display != "block" )
    {
        if( _CallNico == null )
        {
            _CallNico = _Ajax.addScript( "http://pipes.yahoo.com/pipes/pipe.run?_id=bfbd9ed3979bf71ed12f3f36bc291a6c&_render=json&_callback=getNicoFeed" + "&cacheCancel=" + (new Date()).getDate() );
        }
    }
    else
    {
        document.getElementById( "nico" ).style.display = "none";
    }
}

// ニコニコフィード取得
function getNicoFeed( f )
{

    document.getElementById( "nico" ).style.display = "block";


    var player = f.value.items[0].link;
    player = player.replace( "http://www.nicovideo.jp/watch/", "http://ext.nicovideo.jp/thumb_watch/" );

    var target = document.getElementById( "ifnico" );
    var head;
    if( target.contentDocument )
    {
        head = target.contentDocument.getElementsByTagName("head").item(0);
    }
    else
    {
        head = parent.frames["ifnico"].document.getElementsByTagName("head").item(0);
    }

    var obj = document.createElement("script");

    obj.setAttribute("type", "text/javascript");
    obj.setAttribute("src", player );

    head.appendChild( obj );

   
    // 開放
    _Ajax.removeScript( _CallNico );
    _CallNico = null;
}

// タイマー作動
var _enableTimer = false;
function setTimer()
{
    if( _enableTimer == false )
    {
        setTimeout( "onTimer()", 16 );
        _enableTimer = true;
    }
}

// 各アニメーション処理
function onTimer()
{
    var finished = 1;
    var goal = 72;

    if( _Top > goal )
    {
        var addition = (goal - _Top) / 4;
        if( addition > -1 ) addition = -1;
        _Top += addition;
        
        if( _Top > goal )
        {
            finished = 0;
        }
        else
        {
            _Top = goal;
        }

        document.getElementById( "window" ).style.top = _Top + "px";
        
        var alpha = 1 - ((_Top - 72) / (88 - 72));
        setAlpha( document.getElementById( "window" ), alpha );
    }

    goal = 8;
    if( _MenuLeft < goal )
    {
        var addition = (goal - _MenuLeft) / 4;
        if( addition < 1 ) addition = 1;
        _MenuLeft += addition;
        
        if( _MenuLeft < goal )
        {
            finished = 0;
        }
        else
        {
            _MenuLeft = goal;
        }

        document.getElementById( "menu-line" ).style.left = _MenuLeft + "px";
    }
    
    if( _TwitOpen )
    {
        goal = 100;
        if( _TwitAlpha < goal )
        {
            var addition = (goal - _TwitAlpha) / 8;
            if( addition < 1 ) addition = 1;
            _TwitAlpha += addition;
            
            if( _TwitAlpha < goal )
            {
                finished = 0;
            }
            else
            {
                _TwitAlpha = goal;
            }

            setAlpha( document.getElementById( "twit" ), _TwitAlpha / 100 * 0.90 );
        }
    }
    
    if( _BackOpen )
    {
        if( _WindowID == 0 && _LoadingBack == false )
        {
            goal = 100;
        }
        else
        {
            goal = 40;
        }
        if( _BackAlpha != goal )
        {
            var dif = (goal - _BackAlpha);
            var addition = dif / 8;
            if( dif > 0 )
            {
                if( addition < 1 ) addition = 1;
            }
            else
            {
                if( addition > -1 ) addition = -1;
            }
            _BackAlpha += addition;
            
            if( dif > 0 )
            {
                if( _BackAlpha < goal )
                {
                    finished = 0;
                }
                else
                {
                    _BackAlpha = goal;
                }
            }
            else
            {
                if( _BackAlpha > goal )
                {
                    finished = 0;
                }
                else
                {
                    _BackAlpha = goal;
                }
            }
            setAlpha( document.getElementById( "background" ), _BackAlpha / 100 * 1.00 );
        }
    }

    if( finished == 0 )
    {
        setTimeout( "onTimer()", 16 );
    }
    else
    {
        _enableTimer = false;
    }
}

// twitterリクエスト
function getLatestTwit()
{
    if( _CallTwit == null )
    {
        _CallTwit = _Ajax.addScript( "http://twitter.com/statuses/user_timeline/RRRQ.json?callback=twit&count=10" + "&cacheCancel=" +  + (new Date()).toString() );
    }

    setTimeout( "getLatestTwit()", 1000 * 30 );
}

var _PrevTwit = "";

// 右下表示
function twit( a )
{
    var str = "";
    for( var i = 0; data = a[i]; i++ )
    {
        var text = data.text;
        
        if( text.indexOf( "@" ) == -1 )
        {
            str = text;
            break;
        }
    }
    
    if( str == "" )
    {
        str = "・・・。";
    }

    str = str.replace( /(https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/g, "<a href='$1' target='_blank'>$1</a>" );

    //レスポンスを取得
    if( _PrevTwit != str )
    {
        _PrevTwit = str;
        document.getElementById("disp").innerHTML = str;
        document.getElementById("twit").style.display = "block";
        if( document.getElementById("disp").offsetWidth > 300 )
        {
            document.getElementById("disp").style.width = "300px";
        }
        setAlpha( document.getElementById("twit"), 0 );

        _TwitOpen = true;
        _TwitAlpha = 0;
        setTimer();
    }

    // 開放
    _Ajax.removeScript( _CallTwit ); 
    _CallTwit = null;
}

// ブログ読み込み
function readBlog( data )
{
    if( _WindowID == "blog" )
    {
        var txt = "";
        txt += "<div class='w-title'>blog<div class='close' onClick='closeWindow();'></div></div><div class='inner' id='inner'>";
        txt += "<div class='rss'><a href='http://rrrqs.blogspot.com/feeds/posts/default'><img src='img/rss.gif' border=0></a></div>";

        var entry = data.feed.entry;
        var prevdate = "";
        for( var i = 0; d = entry[i]; i++ )
        {
            if( i >= 5 ) break;
            var update = d.published["$t"];
            var idx = update.indexOf( "T" );
            var date = update.substr( 0, idx );
            date = date.replace( /-/g, "/" );
            
            var time = update.substr( idx+1 );
            idx = time.indexOf( "." );
            time = time.substr( 0, idx-3 );
            
            if( date != prevdate )
            {
                txt += "<font size=2><b>" + date + "</b></font>";
                prevdate = date;
            }

            txt += "<div class='blog'><div class='subject'>" + "<font size=2 color=666666>[" + time + "]</font>　";
            var url = "";
            for( var j = 0; l = d.link[j]; j++ )
            {
                if( l.rel == "alternate" )
                {
                    url = l.href;
                    break;
                }
            }
            txt += "<a href='" + url +"' target='_blank'>"
            txt += "<font size=4><b>" + d.title["$t"] + "</b></font></a></div>";
            txt += "<div class='blog-content'>" + d.content["$t"];;
            txt += "</div></div>";
        }

        txt +="</div>";
        showWindow( txt );
    }
    
    // 開放
    _Ajax.removeScript( _CallBlog );
    _CallBlog = null;
}

// クロスプラットフォームアルファ設定
function setAlpha( elem, alpha )
{
    elem.style.filter = "alpha(opacity=" + (alpha * 90)+ ")";
    elem.style.mozopacity = alpha * 0.9;
    elem.style.opacity = alpha * 0.9;
}

