﻿//XMLHttpRequestではクロスドメインアクセスができない為、JSONPで実装する

/********************************************************************************/
var siteWindow = null;
function openSite(siteUrl){
    if(null==siteWindow || siteWindow.closed){
        siteWindow=window.open(siteUrl,null,'left=300, top=100, height=600, width= 260, status=no, resizable= no, scrollbars= no, toolbar= no,location= no, menubar= no');    
    }else{
        siteWindow.location = siteUrl;
    }
    //Windowを前面に移動する。
    siteWindow.focus();
};
/************WEBパーツ処理（以下）************************************************/
//getsitedata.aspxを先にコールしておくことによりサイトデータがsiteDatas変数に格納されます。


var imgWidth = 140;
var imgHeight=215;
var rng = 7;
var interval=20;
var mainInterval=13000;
//var host='http://localhost:2296/MobadWebApp/';
var host='http://www.mysite-is.jp/';
SlideTxtlist = new Array();

/*文字をスライドする処理*/
function TxtSlider(){
    this._iniFlg = false;
    this._HIGH_RANGE = 12;
    this._LOW_RANGE = 3;
    this._txtPos = "";
    this._tstid="";
    this._curTxtIndex=0;
};
TxtSlider.prototype ={
    slideText : function(){
        var txtElm = document.getElementById('txtArea');
        if(!this._iniFlg){
            this._iniFlg = true;
            this._txtPos = imgWidth;
            var txtNode = txtElm.firstChild;
            if(null==txtNode){
                txtNode = document.createTextNode(SlideTxtlist[this._curTxtIndex]);
                txtElm.appendChild(txtNode);
            }else{
                txtNode.nodeValue = SlideTxtlist[this._curTxtIndex];
            }
            this.resetTimeerId();
            this._tstid = setInterval("TextSlider.slideText()",interval);
        }else{
            this._txtPos -= this._HIGH_RANGE;
            if(this._txtPos<5){
                this._txtPos = 5;
                this.resetTimeerId();
                this._curTxtIndex++;
                if(this._curTxtIndex<SlideTxtlist.length){
                    //次の文字をスライドさせる
                    setTimeout("TextSlider.slideText()",1000);
                }else{
                    this._curTxtIndex=0;
                    //配列の最後の文字はそのまま流す
                    setTimeout("TextSlider.marqueeText()",1000);
                }
                this._iniFlg=false;
            }
        }
        txtElm.style.left=this._txtPos + 'px';
    },
    marqueeText:function(){
        var txtElm = document.getElementById('txtArea');
        this._txtPos -= this._LOW_RANGE;
        txtElm.style.left=this._txtPos + 'px';
        if(this._tstid==""){
            this._tstid = setInterval("TextSlider.marqueeText()",interval);
        }
    },
    resetTimeerId:function(){
        if(this._tstid!=""){
            clearInterval(this._tstid);
            this._tstid="";
        }
    },
    ResetTextSlider:function(){
        this.resetTimeerId();
        this._iniFlg=false;//念の為ここでも初期化フラグを立てる
    }
};
TextSlider = new TxtSlider();//テキスト情報を表示するクラスのインスタンス

/********************************************
*右から左にスライドさせるSlider
*********************************************/
function L2RSlider(){
    this._pos = imgWidth;
    this._tid = "";
};
L2RSlider.prototype ={
    slideShow : function(){
        var imgElm = document.getElementById('thumbnailImg');
        if(this._tid==""){
            this._pos = imgWidth;
            this._setImgStyle();
            this._tid = setInterval("Sliders[0].slideShow()",interval);
        }else{
            this._pos -= rng;
            if(this._pos<0){
                this._pos = 0;
                clearInterval(this._tid);
                this._tid="";
                //テキスト情報をスライドする。
                TextSlider.slideText();
            }
        }
        imgElm.style.left= this._pos+'pt';
    },
    _setImgStyle : function(){
        var imgElm = document.getElementById('thumbnailImg');
        imgElm.style.position='absolute';
    }
};

function WipeSlider(){
    this._tid = "";
    this._width="";
};
WipeSlider.prototype={
    slideShow : function(){
        var imgElm = document.getElementById('thumbnailImg');
        
        if(this._tid==""){
            this._width=rng;
            this._tid = setInterval("Sliders[1].slideShow()",interval);
        }else{
            this._width = imgElm.getAttribute("width");
            this._width +=rng;
            if(this._width>imgWidth){
                this._width = imgWidth;
                clearInterval(this._tid);
                this._tid="";
                //テキスト情報をスライドする。
                TextSlider.slideText();
            }
        }
        imgElm.setAttribute("width",this._width);
    }
};
//スライダーを設定
var Sliders = new Array();
Sliders[0] = new L2RSlider();
Sliders[1] = new WipeSlider();

//サーバサイドで出力するJavaScript変数（この変数名を利用）
var GetData = {};
//コールバック処理（サーバ側でデータ取得後に以下の処理を実行する旨のメソッドを出力する）
GetData.onload = function(data){
    Watcher.onCallback(data);
};
/*表示データ取得処理（非同期通信で取得する）*/
function SiteWatcher(){
    this.REQUEST_URL = host + "parts/getsitedata.aspx";//データ取得サイトのurl
    this._initFunc = null;//既に他のスクリプト等で設定されていたonload時処理
    this._index=0;//現在表示しているデータ
    this.siteDatas = null;//サーバより取得したサイトデータ
    this._iniFlg = false;
    this._imgList = null;
    this._retry = 0;//処理エラー時のリトライ回数
};

SiteWatcher.prototype={
    //データ取得後にコールされるメソッド
    onCallback : function(data){
        if(data!=null){
            //サーバから取得したJSONデータを設定する
            Watcher.siteDatas = data;
            //Imageキャッシュデータを作成しておく
            Watcher.createImgCash();
            //プレビュー処理を開始する
            Watcher._previewData();
        }
    },
    //初期処理
    //データ取得用aspx読み込みスクリプトをHeaderタグに追加する
    initialize : function(){
        //データ取得用aspx読み込みスクリプトをHeaderタグに追加しデータ取得処理を実行する
        var scriptElm = document.createElement("script");
        scriptElm.setAttribute("type", "text/javascript");
        scriptElm.setAttribute("charset", "utf-8");
        scriptElm.setAttribute("src", this.REQUEST_URL);
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(scriptElm);
    
        if(window.onload!=null && window.onload!=''){
            this._initFunc = window.onload;//他のスクリプト等でload処理が設定されている場合はそれを退避しておく
        }
        window.onload=Watcher._execute;
    },
    //イメージキャッシュデータを作成
    createImgCash : function(){
        if(this.siteDatas==null)return;
        
        if(this._imgList==null){
            this._imgList = new Array();
            for(var i=0;i<this.siteDatas.length;i++){
                this._imgList[i] = new Image();
                this._imgList[i].src = this.siteDatas[i].ThumbnailUrl;
            }
        }
    },
    //SiteWatcher起動処理（メイン処理）
    _execute : function(){
        //他のスクリプト等によりonload処理が設定されていればそれらを起動する。
        if(Watcher._initFunc!=null){
            Watcher._initFunc();
        }
    },
    /*サイトデータのプレビュー処理*/
    _previewData : function(){
        //背景領域を取得
        var backElm = document.getElementById('backArea');
        //戻るボタンで遷移した場合、上記エリアがIEでは取得できないので一定時間処理を待つ
        if(backElm==null && document.readyState!=undefined && document.readyState!='complete'){
            if(Watcher._retry<3){
                setTimeout("Watcher._previewData()",500);
                Watcher._retry++;
            }
            return;
        }
        
        if(Watcher.siteDatas==null)return;
        if(Watcher._index>=Watcher.siteDatas.length)Watcher._index=0;
        
        //テキストスライダーを初期化（前回起動処理の後処理）しておく
        TextSlider.ResetTextSlider();
        
        //スライドするテキスト情報を設定する          
        SlideTxtlist[0]="アクセス順位：" + Watcher.siteDatas[Watcher._index].Rank+"位";
        SlideTxtlist[1]="総アクセス数：" + Watcher.siteDatas[Watcher._index].TotalAccessCnt;
        SlideTxtlist[2]="本日のアクセス数：" + Watcher.siteDatas[Watcher._index].TodayAccessCnt;
        SlideTxtlist[3]="サイト紹介："+ Watcher.siteDatas[Watcher._index].Introduction;

        
        if(this._iniFlg){
            if(backElm.style.backgroundImage==""){
                backElm.style.backgroundImage="url('" + host + "parts/blogpartsini.gif')";
            }else{
                var preIndex = Watcher._index-1;
                if(Watcher._index==0)preIndex = Watcher.siteDatas.length-1;
                if(this._imgList[preIndex].src!= undefined){
                    //キャッシュした画像を表示する（こちらしか動かないハズ）
                    backElm.style.backgroundImage="url('"+this._imgList[preIndex].src +"')";
                }else{
                    backElm.style.backgroundImage="url('"+Watcher.siteDatas[preIndex].ThumbnailUrl+"')";
                }
                
            }
        }else{
            this._iniFlg=true;
        }
        var imgElm = document.getElementById('thumbnailImg');
        if(null!=imgElm){
            //起動時はImageタグは非表示にしているのでそれを解除する。
            imgElm.style.display="";
            
            //リンクを設定
            var lnkElm = document.getElementById('imglink');
            if(null!=lnkElm){
                lnkElm.href="JavaScript:openSite('"+Watcher.siteDatas[Watcher._index].SiteUrl+"')";
            }
            if(this._imgList[Watcher._index].src!= undefined){
                //キャッシュ画像を設定（こちらしか処理されないはず）
                imgElm.src = this._imgList[Watcher._index].src;
            }else{
                imgElm.setAttribute("src",Watcher.siteDatas[Watcher._index].ThumbnailUrl);
            }
            //画像スライド処理を設定
            Sliders[0].slideShow();
            Watcher._index++;
            //本処理を予約
            setTimeout("Watcher._previewData()",mainInterval);
        }
    }
    
};
var Watcher = new (SiteWatcher);
Watcher.initialize();

