/*
 * Gletschereis CMS - Fading-Effekte
 *
 * (C) 2008 Johannes Ott - cms@gletschereis.net
 *
 * $Id: fading.js 1019 2008-03-12 17:33:46Z oetzi $
 *
 */

function cFading(set_width, set_height, set_stepwidth, set_speed)
{
    this.width = set_width;
    this.height = set_height;

    this.rotateInterval = null;
    this.wait = null;
    this.ispaused = false;

    if (set_speed == null)
        this.speed = 100;
    else
        this.speed = set_speed;

    if (set_stepwidth == null)
        this.stepwidth = 1;
    else
        this.stepwidth = set_stepwidth;

    this.objects = new Array();
    this.curOpacs = new Array();
    this.blendIntervals = new Array();

    this.cur = 0;

    this.addObject = function(id, hide)
    {   
        var length = this.objects.length;
        this.objects[length] = id;

        var obj = getObj(id);        

        obj.style.zIndex = 1;
        obj.style.width = this.width + 'px';
        obj.style.height = this.height + 'px';
        obj.style.position = "absolute";
       
		 this.addPauseHandler(obj);

        var usemap = obj.getAttribute("usemap");
		  if (usemap != null && usemap.length > 0 )
        {
            if (usemap.indexOf("#") == 0)
                usemap = usemap.substring(1, usemap.length);

            this.addPauseHandler(getObj(usemap));
        }

        if (hide)
            this.opacity(length, 0);
        else
            this.opacity(length, 100);
    }

    this.rotate = function(wait)
    {
        var curObj = getObj(this.objects[this.cur]);
        curObj.style.display = '';
        curObj.style.zIndex = 1;


        this.nextObject();
        this.wait = wait;
        var myself = this;
        this.rotateInterval = setInterval(function()
                                          {
                                              myself.nextObject();
                                          }, this.rotateTimer(wait));
    }

    this.addPauseHandler = function(obj)
    {
        var myself = this;
        obj.onmouseover = function() { myself.pause();};
        obj.onmouseout = function() { myself.unpause();};   
    }

    this.pause = function()
    {
        if (this.rotateInterval != null)
            clearInterval(this.rotateInterval);
        else
            return;
   
        for (var i = 0; i < this.blendIntervals.length; i++)
        {
            clearInterval(this.blendIntervals[i]);
            this.blendIntervals[i] = null;
        }

        this.setCur(Number(this.cur) - Number(1));
        this.ispaused = true;
    }

    this.unpause = function()
    {
        if (this.ispaused)
        {
            this.ispaused = false;
            this.nextObject();
            var myself = this;
            this.rotateInterval = setInterval(function()
                                              {
                                                  myself.nextObject();
                                              }, this.rotateTimer(this.wait));
        }
    }

    this.setCur = function(to)
    {
        this.cur = this.calc(to);
    }

    this.calc = function(to)
    { 
        var length = this.objects.length - 1;
        if (to > length)
            to = 0
        else if (to < 0)
            to = length;
        
        return to;
    }

    this.getNext = function()
    {
        var ret = Number(this.cur) + Number(1);
        if (ret > this.objects.length - 1)
            return 0;
        else
            return ret;
    }

    this.nextObject = function()
    {

        var cur = this.cur;
        var curID = this.objects[cur];
        var curObj = getObj(curID);

        var next = this.getNext();
        var nextID = this.objects[next];
        var nextObj = getObj(nextID);

        if (this.blendIntervals[this.cur] != null || this.blendIntervals[next] != null)
            return;

        var myself = this;
        this.blendIntervals[this.cur] = setInterval(function()
                                                    {
                                                        myself.fadeOutOpacityTo(cur, 0);
                                                    }, this.speed);
        this.blendIntervals[next] = setInterval(function()
                                                    {
                                                        myself.fadeInOpacityTo(next, 100);
                                                    }, this.speed);
   
        this.setCur(Number(this.cur) + Number(1)); 
    }
    
    this.rotateTimer = function(wait)
    {
        return Number(wait * 1000) + Number(this.speed * Math.round(100/this.stepwidth));
    }

    this.fadeOutOpacityTo = function(nr, to)
    {
        if (to == null || to < 0)
            to = 0;

        if (this.curOpacs[nr] <= to)
        {
            if (this.blendIntervals[nr] != null)
            {
                clearInterval(this.blendIntervals[nr]);
                this.blendIntervals[nr] = null;
            }

            this.opacity(nr, to)
        } 
        else
            this.opacity(nr, Number(this.curOpacs[nr]) - Number(this.stepwidth));
    }

    this.fadeInOpacityTo = function(nr, to)
    {
        var obj = getObj(this.objects[nr]);
        obj.style.zIndex = 2;

        if (to == null || to > 100)
            to = 100;

        if (this.curOpacs[nr] >= to)
        {
            if (this.blendIntervals[nr] != null)
            {
                clearInterval(this.blendIntervals[nr]);
                this.blendIntervals[nr] = null;
            }

            this.opacity(nr, to);
            obj.style.zIndex = 1;
        } 
        else
            this.opacity(nr, Number(this.curOpacs[nr]) + Number(this.stepwidth));
    }

    this.opacity = function(nr, set_opacity)
    {
        var obj = getObj(this.objects[nr]);
        this.curOpacs[nr] = set_opacity;
        opacity(obj, set_opacity);

        if (set_opacity == 0)
            obj.style.display = 'none';
        else
            obj.style.display = '';
    }
}   

