/**
 *
 *  Javascript Animator
 *  Author: Rony Liemmukda
 *  Date:   21/11/2009
 *  Version: 1.1
 *
 *  Description:
 *
 *  Will provide an object that can do an animation on a <div>-element.
 *  It's also possible to have a second <div> which gets manipulated as well.
 *
 *  @modify:    03/03/10    added alphablending
 *  @modify:    03/14/10    fixed initial start up
 */



function Animator( maxAnimations, animatorDiv, teaserDiv, root ) {
    this.frames = new Array();
    this.currentAnimation = -1;
    this.maxAnimations = maxAnimations;
    this.root   = root;
    this.layer = animatorDiv;
    this.teaserDiv = teaserDiv;
    this.addAnimation = animatorAddAnimation;
    this.nextAnimation = animatorNextAnimation;
    this.refresh    = animatorRefresh;
    this.play = animatorPlayAnimation;
    this.decrementOpacity = animatorDecrementOpacity;
    this.incrementOpacity = animatorIncrementOpacity;
    this.lastElement = null;
    this.start = animatorStart;
    this.stop = animatorStop;
    this.resume = animatorResume;
    this.timer = null;
    this.name = "";   
    this.currentOpacity = 1;
    this.timeout = 0;
}

function animatorAddAnimation( bigImage, element, teaserText, title, url ) {
    var framedata = new Object();
    framedata.image = bigImage;
    framedata.element = null;
    framedata.teaser = null;
    framedata.url = "#";
    framedata.title = "No title";

    if ( element ) {
        framedata.element = element;
    }
    if ( teaserText ) {
        framedata.teaser = teaserText;
    }
    if ( title ) {
        framedata.title = title;
    }           
    if ( url ) {
        framedata.url = url;
    }            
    this.frames.push( framedata );
}

function animatorDecrementOpacity() {
    this.currentOpacity -= 0.05;
    this.layer.style.opacity = this.currentOpacity;
    this.layer.style.filter = "Alpha(opacity = " + (this.currentOpacity * 100 ) +")";
    
    if ( this.currentOpacity <= 0 ) {
        this.currentOpacity = 0;
    } else {
        if ( 0 != this.name.length ) {
            setTimeout( this.name + ".decrementOpacity()", 50 );
        }
    }
}


function animatorIncrementOpacity() {
    this.currentOpacity += 0.05;
    this.layer.style.opacity = this.currentOpacity;
    this.layer.style.filter = "Alpha(opacity = " + (this.currentOpacity * 100 ) +")";

    if ( this.currentOpacity >= 1 ) {
        this.currentOpacity = 1;
    } else {
        if ( 0 != this.name.length ) {
            setTimeout( this.name + ".incrementOpacity()", 50 );
        } 
    }
}


function animatorNextAnimation( ) {

    if ( 0 == this.timeout ) {
        this.timeout = 4000;
    }

    this.currentAnimation =  ( this.currentAnimation + 1 ) % this.maxAnimations;
    this.refresh();

    if ( 1 == this.currentOpacity ) {
        this.decrementOpacity();
    } else {
        this.incrementOpacity();
    }    
}

function animatorRefresh() {
    try {
        var framedata = this.frames[ this.currentAnimation ];

        if ( 1 == this.currentOpacity ) {
            this.root.style.backgroundImage  = "url('"+ framedata.image +"')";
            this.root.setAttribute( "onclick", "javascript: window.location = '"+ framedata.url +"'" );
        } else {
            this.layer.style.backgroundImage = "url('"+ framedata.image +"')";
            this.layer.setAttribute( "onclick", "javascript: window.location = '"+ framedata.url +"'" );
        }

        if ( this.lastElement ) {
            this.lastElement.className = "animatoridle";
            this.lastElement = null;
        }

        if ( framedata.element ) {                
            framedata.element.className = "animatoractive";
            this.lastElement = framedata.element;
        }

        if ( this.teaserDiv ) {
            this.teaserDiv.innerHTML = framedata.url + "<br/>\n";
            this.teaserDiv.innerHTML += framedata.teaser;
        }

        if ( this.timer ) {
            this.resume();
        } 

    } catch ( err ) {
    	alert( err )
    }
}

function animatorPlayAnimation( animationId ) {
    this.currentAnimation = ( animationId % this.maxAnimations );
    this.refresh();
}
function animatorResume() {
    this.start( this.name );
}

function animatorStart( variable ) {
    this.name = variable;
    if ( 1 < this.maxAnimations )  {    
        this.timer = setTimeout( variable + ".nextAnimation()", this.timeout );
    } else {
        this.nextAnimation();
    }
}

function animatorStop() {
    if ( this.timer ) {
        clearTimeout( this.timer );
        this.timer = null;
    }
    this.currentOpacity = 1;
    this.layer.style.opacity = 0;
    this.layer.style.filter = "Alpha(opacity = 0)";
    
}


