/**
 * cycSlidesShow.js ver1.1.0 , Wed August 04 2010
 * prototype & script.aculo.us ベースの 簡単・単純 連続スライドショー
 * 予め以下のモジュールをロードしておく必要があります。
 * prototype.js  effects.js
 *    Copyright (c) 2010 (有) サイバー・シビル  (http://cybercivil.net)
 *
 * cycSlidesShow.js is freely distributable under the terms of an MIT-style license.
 */



cycSlidesShow = Class.create({
    initialize :function (id, slides, options){
        this.ancestor = $(id); //親div
        this.Infs = slides;
        this.showEle = null;
        this.hideEle = null;
        this.imgEle = new Array();
        var innerStr = "";
        var tag;

        for(var i = 0; i < this.Infs.length; i++){
            tag = id + "-inImg" + i.toPaddedString(3);

            innerStr += '<img id="' + tag + '" src="'+ this.Infs[i].img + '" style="position:absolute; ';

            if(this.Infs[i].anchor != ""){
                innerStr +='cursor:pointer; ' ;
            }
            if(i == 0){
                innerStr += '" >' + "\n";
            }
            else{
                innerStr += ' display:none;" >' + "\n";
            }
            this.imgEle.push(tag);
        }
        this.nowDispCnt = 0;
        this.maxDispCnt = this.Infs.length;
        this.ancestorWidth = this.ancestor.getWidth();
        this.ancestorHeight = this.ancestor.getHeight();
        this.ancestor.update(innerStr);
//      this.ancestor.relativize();
//      IEではele.relativize();がエラーとなるためsetStyleで
        this.ancestor.setStyle({
            overflow:'hidden',
            position: 'relative',
            top: '0px',
            left: '0px'
        });

        this.options = $H({
            interval : 5,
            duration : 0.5,
            limmit : 0,
            parallel : true

        });
        this.options.update(options);
        this.options.set('interval', parseInt(this.options.get('interval') * 1000));
        this.loopCnt = 0;
        this.loopLimmit = this.options.get('limmit');
        if (this.Infs[this.nowDispCnt].scrool == 'H' || this.Infs[this.nowDispCnt].scrool == 'V') {
            this.showEle = $(this.imgEle[this.nowDispCnt]);
            this.setbeforPos();
            this.slideSclool();
        }

        for(var i = 0; i < this.maxDispCnt; i++){
            if(this.Infs[i].anchor != ""){
                Event.observe(this.imgEle[i], 'click', this.slideClick.bindAsEventListener(this));
            }
        }

        this.timerID = setInterval(this.changeSlide.bind(this), this.options.get('interval'));

    },
    slideClick : function(){
        location.href = this.Infs[this.nowDispCnt].anchor;
    },
    changeSlide : function(){
        this.hideEle = $(this.imgEle[this.nowDispCnt]);
        this.nowDispCnt += 1;
        if(this.nowDispCnt >= this.maxDispCnt){
            this.nowDispCnt = 0;
            if(this.loopLimmit > 0){
                this.loopCnt += 1;
                if(this.loopCnt >= this.loopLimmit){
                    clearInterval(this.timerID);
                }
            }
        }
        this.showEle = $(this.imgEle[this.nowDispCnt]);
        if (this.options.get('parallel')) {
            var effectFactor = [];

            effectFactor.push(Effect.Fade(this.hideEle, {
                sync: true
            }));


            effectFactor.push(Effect.Appear(this.showEle, {
                sync: true,
                beforeStart: this.setbeforPos.bind(this),
                afterFinish: this.slideSclool.bind(this)
            }));

            new Effect.Parallel(effectFactor, {
                duration: this.options.get('duration')
            });
        }
        else{
            Effect.Fade(this.hideEle, {duration: this.options.get('duration')});
            Effect.Appear(this.showEle, {duration: this.options.get('duration'),
                                         beforeStart: this.setbeforPos.bind(this),
                                         afterFinish: this.slideSclool.bind(this)});
        }
    },
    setbeforPos : function(){
        switch(this.Infs[this.nowDispCnt].scrool){
            case 'H':
                var w = this.showEle.getWidth();
                if(w > this.ancestorWidth){
                    w -= this.ancestorWidth;
                }
                else
                    return;
                if(this.Infs[this.nowDispCnt].direction == 0){
                    this.showEle.setStyle('left:0px;');
                }
                else{
                    w *= -1;
                    this.showEle.setStyle('left:'+w+'px;');
                }
                break;
            case 'V':
                var h = this.showEle.getHeight();
                if(h > this.ancestorHeight){
                    h -= this.ancestorHeight;
                }
                else
                    return;
                if(this.Infs[this.nowDispCnt].direction == 0){
                    this.showEle.setStyle('top:0px;');
                }
                else{
                    h *= -1;
                    this.showEle.setStyle('top:'+h+'px;');
                }
                break;
        }
    },
    slideSclool: function(){
        switch (this.Infs[this.nowDispCnt].scrool) {
            case 'H':
                var w = this.showEle.getWidth();
                if (w > this.ancestorWidth) {
                    w -= this.ancestorWidth;
                }
                else
                    return;
                if (this.Infs[this.nowDispCnt].direction == 0) {
                    w *= -1;
                }
                new Effect.Move(this.showEle, {
                    x: w,
                    y: 0,
                    duration: this.Infs[this.nowDispCnt].sDuration
                });
                break;
            case 'V':
                var h = this.showEle.getHeight();
                if (h > this.ancestorHeight) {
                    h -= this.ancestorHeight;
                }
                else
                    return;
                if (this.Infs[this.nowDispCnt].direction == 0) {
                    h *= -1;
                }
                new Effect.Move(this.showEle, {
                    x: 0,
                    y: h,
                    duration: this.Infs[this.nowDispCnt].sDuration
                });
                break;
        }
    }
});

