﻿// JScript File

document.observe("dom:loaded",function() {
    var marquee_data = new Array();
    var marquee_index = -1;
    var marquee_time = 5;
    var marquee_step = 2;
    var marquee_slower_pixel = 4;
    var marquee_slower_step = 1;
    new Ajax.Request("/mod_marquee/marquee.php",{
        method:'post',
        onSuccess : function(response) {
            response.responseJSON.each(function(i) {
                marquee_data.push(i.marqueeText);
            });
            new PeriodicalExecuter(setMarquee, 0); 
            //$('ticker').update(marquee_data.length);
        }
    });
    
    function setMarquee(pe) {
        pe.stop();
        marquee_index++;
        if (marquee_index > marquee_data.length-1)
            marquee_index = 0;
        smoothOut(marquee_data[marquee_index]);
    }
    
    function smoothIn(text) {
        var ticker = $('tickerInner')
        var pos = ticker.ancestors()[0].getHeight()-15;
        var targetpos = parseInt(ticker.getStyle('top'));
        var height = 0;
        var maxheight = ticker.getHeight();
        ticker.update(marquee_data[marquee_index]);
        ticker.setStyle({'top':pos+'px','height':'0px'});
        new PeriodicalExecuter(function(pe) {
            if (pos < (targetpos + marquee_slower_pixel))
                pos-=marquee_slower_step;
            else
                pos-=marquee_step;
            height += marquee_step;
            if (height > maxheight)
                height = maxheight;
            if (pos < targetpos)
                pos = targetpos;
            ticker.setStyle({'top':pos+'px','height':height+'px'});
            if (pos <= targetpos) {
                new PeriodicalExecuter(setMarquee, marquee_time);
                new PeriodicalExecuter(sponsorRotate, 0);
                pe.stop();
            }
        }, 0.05); 
    }

    function smoothOut(text) {
        var ticker = $('tickerInner');
        var pos = parseInt(ticker.getStyle('top'));
        var orig = pos;
        var targetpos = -ticker.getHeight();
        new PeriodicalExecuter(function(pe) {
            pos-=marquee_step;
            ticker.setStyle({'top':pos+'px'});
            if (pos <= targetpos) {
                ticker.setStyle({'top':orig+'px'});
                smoothIn(text);
                pe.stop();
            }
        }, 0.05); 
    }
    
});
