/**
 *  Slideshow script
 * This file is part of the slideshow script.
 *
 * @author        Florian Haßmann<hassmann@flottix.com>
 * @version        0.0.1
 * @file        slideshow.js
 * @release        12.01.07
 * @id            2589275DCB0E79E96EE0F6868823AA67
 * @status        tested
 */
include_js('./dojo/dojo.js');
////////////////////////////////////////////////////////////////
/** STDLib
 * frequently used procedures
 */
function $(s){return document.getElementById(s);}
function delChilds(o){
    while(o.hasChildNodes())
        o.removeChild(o.childNodes[0]);
    return true;
}

////////////////////////////////////////////////////////////////
function $POff(o,s)
{
    var v=[o.offsetLeft,o.offsetTop];
    if(o.offsetParent)
    {
        var t=$POff(o.offsetParent);
        v[0]+=t[0];
        v[1]+=t[1];
    }
    return v;
}

////////////////////////////////////////////////////////////////
//Container Engine
var g_aoContainer    =    [];
function $add(o){var i=g_aoContainer.length;g_aoContainer[i]=o;return i;}
function $get(i){return g_aoContainer[i];}
////////////////////////////////////////////////////////////////
/**
 * internally used constants
 * *** var used instead of const because of compatibility ***
 */
var SLIDESHOW_STATUS_STOP    =    0;
var SLIDESHOW_STATUS_MOVE    =    1;

////////////////////////////////////////////////////////////////
/**
 * Contructor for the slideshow script.
 */
function slideshow(aConf){
    //__________________________________________________________________________
    this.m_aConf        =    aConf;                        //configuration array
    this.m_aImageSizes    =    new Array();                //array for the image sizes
    this.m_bRenderAll    =    true;                        //if this is set the complex will be rendered completly
    //__________________________________________________________________________
    this.m_iUid            =    $add(this);                    //unique object id
    this.m_iLastTS        =    new Date().getTime();        //the last timestamp of move
    this.m_iFRImage        =    0;                            //The actual first rendered image
    this.m_iPos            =    0;                            //the actual movement position
    this.preload();
}

////////////////////////////////////////////////////////////////
/** 
 * Rendering process for the slideshow script.
 */
slideshow.prototype.render    =    function(){
    if(this.m_bRenderAll){
        delChilds(this.m_aConf["node"]);
        this.m_aConf["node"].style.width    =    "1000000px";
        var actwidth    =    0;
        var actpos        =    0;
        while(true)
        {
            var id    =    (actpos+this.m_iFRImage)%this.m_aConf["images"].length;
            var a    =    document.createElement("a");
            var img    =    document.createElement("img");
            a.href    =    this.m_aConf["images"][id][1];
            img.setAttribute("src",this.m_aConf["images"][id][0]);
            a.appendChild(img);
            this.m_aConf["node"].appendChild(a);
            actpos++;
            if(img.offsetWidth==0)
            {
                this.m_iLastTS    =    new Date().getTime();    /* preloading not finished */
                delChilds(this.m_aConf["node"]);
                this.m_aConf["node"].appendChild(document.createTextNode("Bitte warten... Slideshow wird geladen."));
                return 0;
            }
            actwidth+=img.offsetWidth;
            if(actwidth>=this.m_aConf["displaySize"]+this.m_aConf["bufferSize"])
                break;
            this.m_aImageSizes[id]    =    img.offsetWidth;
        }
        this.m_bRenderAll    =    false;
    }
    
    this.m_aConf["node"].style.marginLeft    =    "-"+this.m_iPos+"px";
    return 1;
}

////////////////////////////////////////////////////////////////
slideshow.prototype.preload    =    function(){
    for(var i=0; i<this.m_aConf["images"].length; i++)
    {
        var img    =    new Image();
        img.src    =    this.m_aConf["images"][i][0];
    }
    return 1;
}

////////////////////////////////////////////////////////////////
/**
 * Movent process for moving the slideshow ...
 * This process should be called in an interval
 */
slideshow.prototype.move    =    function(){
    var TS        =    new Date().getTime();
    var speed    =    this.m_aConf["stdSpeed"];
    
    if(this.m_iStatus&SLIDESHOW_STATUS_MOVE)
        this.m_iPos    +=    (TS-this.m_iLastTS)/1000*speed;
    
    if(this.m_iPos>=this.m_aConf["bufferSize"]/4*3)    {
        var movesize    =    0;
        var moveid        =    this.m_iFRImage;
        
        while(true)
        {
            if(this.m_aImageSizes[moveid]+movesize<this.m_aConf["bufferSize"]/4*3)    {
                movesize    +=    this.m_aImageSizes[moveid]+this.m_aConf["margin"];
                moveid        =    (moveid+1)%this.m_aConf["images"].length;
            }    else    break;
        }
        
        this.m_iFRImage    =    moveid;
        this.m_iPos        -=    movesize;
        this.m_bRenderAll    =    true;
    }
    this.m_iLastTS    =    TS;
    
    if(this.m_iStatus&SLIDESHOW_STATUS_MOVE)
        this.render();
}

////////////////////////////////////////////////////////////////
slideshow.prototype.init    =    function(){
    this.m_iStatus    =    SLIDESHOW_STATUS_MOVE;
    if(!this.m_aConf["node"]){
        var o    =    document.createElement("div");
        o.className    =    this.m_aConf['className'];
        var o2    =    document.createElement("div");
        document.getElementsByTagName("body")[0].appendChild(o);
        o.appendChild(o2);
        this.m_aConf["node"]    =    o2;
    }
    this.render();
    setInterval("$get("+this.m_iUid+").move();",1000/this.m_aConf["fps"]);
}

////////////////////////////////////////////////////////////////
slideshow.prototype.changeStatus    =    function(stat){
    if(stat)
        this.m_iStatus    =    stat-1;
    else
        this.m_iStatus    =    (this.m_iStatus+1)%2;
    this.m_iLastTS    =    new Date().getTime();
}

////////////////////////////////////////////////////////////////  



