
SwfViewer = function(containerId, options)
{
    var me = this;

     if ((options) && (options["width"])) this.width = options["width"];
    else this.width = 500;
     if ((options) && (options["height"])) this.height = options["height"];
    else this.height = 400;

    this.isFullScreen = false;

    this.container = document.getElementById(containerId);
    this.container.className = "swf-viewer";
    this.container.style.position = "static";
    this.container.style.width = this.width+"px";
    this.container.style.height = this.height+"px";

    this.divMovie = document.createElement("DIV");
    this.divMovie.className = "swf-mov";

    this.container.appendChild(this.divMovie);

    this.divBar = document.createElement("DIV");
    this.divBar.className = "swf-bar";

    this.buttonPrev = document.createElement("IMG");
    this.buttonPrev["src"] = "images/resultset_previous.png";
    this.buttonPrev["className"] = "swf-button";
    this.buttonPrev["onclick"] = function() { me.previous(); };
    this.divBar.appendChild(this.buttonPrev);

    this.spanCurrFrame = document.createElement("SPAN");
    this.spanCurrFrame["className"] = "swf-frame";
    this.divBar.appendChild(this.spanCurrFrame);
    this.divBar.appendChild(document.createTextNode(" / "));
    this.spanTotalFrames = document.createElement("SPAN");
    this.spanTotalFrames["className"] = "swf-frame";
    this.divBar.appendChild(this.spanTotalFrames);

    this.buttonNext = document.createElement("IMG");
    this.buttonNext["src"] = "images/resultset_next.png";
    this.buttonNext["className"] = "swf-button";
    this.buttonNext["onclick"] = function() { me.next(); };
    this.divBar.appendChild(this.buttonNext);

    this.buttonZoomIn = document.createElement("IMG");
    this.buttonZoomIn["src"] = "images/magnifier_zoom_in.png";
    this.buttonZoomIn["className"] = "swf-button";
    this.buttonZoomIn["onclick"] = function() { me.zoomIn(); };
    this.divBar.appendChild(this.buttonZoomIn);

    this.buttonZoomOut = document.createElement("IMG");
    this.buttonZoomOut["src"] = "images/magnifier_zoom_out.png";
    this.buttonZoomOut["className"] = "swf-button";
    this.buttonZoomOut["onclick"] = function() { me.zoomOut(); };
    this.divBar.appendChild(this.buttonZoomOut);

    this.buttonFull = document.createElement("IMG");
    this.buttonFull["src"] = "images/picture_empty.png";
    this.buttonFull["className"] = "swf-button";
    this.buttonFull["onclick"] = function() { me.fullScreen(); };
    this.divBar.appendChild(this.buttonFull);

    this.spanLoadProgress = document.createElement("SPAN");
    this.spanLoadProgress["className"] = "swf-frame";
    this.divBar.appendChild(this.spanLoadProgress);

    this.container.appendChild(this.divBar);
    this.currentFrame = null;

    if ((options) && (options["url"]))
    {
        this.src = options["url"];
        this.loadSwf();
    }

    this.container.swfviewer = this;
}

SwfViewer.prototype.loadSwf = function(url)
{
    var me = this;
    if (!url) url = this.src;
    var width = this.container.clientWidth - 20;
    var height = this.container.clientHeight - this.divBar.clientHeight - this.divBar.offsetHeight - 20;

    var html = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' WIDTH='"+width+"' HEIGHT='"+height+"'><PARAM NAME='movie' VALUE='"+url+"'><PARAM NAME='quality' VALUE='high'><EMBED src='"+url+"' quality='high' WIDTH='"+width+"' HEIGHT='"+height+"' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'></EMBED></OBJECT>";
    this.divMovie.innerHTML = html;
    if (typeof(this.divMovie.firstChild.PercentLoaded) != "undefined")
    {
        this.movie = this.divMovie.firstChild;
    }
    else
    {
        this.movie = this.divMovie.firstChild.childNodes[2];
    }

    setTimeout(function() { me.loadProgress(); }, 100);
}

SwfViewer.prototype.loadProgress = function()
{
    var me = this;

    var progress = this.movie.PercentLoaded();
    if (progress >= 100)
    {
        if (this.currentFrame) this.movie.GotoFrame(this.currentFrame-1);
        else this.movie.Rewind();
        this.spanCurrFrame.innerHTML = this.movie.TGetPropertyAsNumber("/", 4);
        this.spanTotalFrames.innerHTML = this.movie.TGetPropertyAsNumber("/", 5);
        this.spanLoadProgress.innerHTML ="";
    }
    else
    {
        this.spanLoadProgress.innerHTML = progress + "% geladen";
        setTimeout(function() { me.loadProgress(); }, 100);
    }
}

SwfViewer.prototype.previous = function()
{
    // / 4 is the index of the property for _currentFrame
    var currentFrame  = this.movie.TGetPropertyAsNumber("/", 4);
    if (currentFrame <= 1) return;
    this.movie.GotoFrame(currentFrame - 2);
    this.spanCurrFrame.innerHTML = this.movie.TGetPropertyAsNumber("/", 4);
}

SwfViewer.prototype.next = function()
{
    var currentFrame = this.movie.TGetPropertyAsNumber("/", 4);
    var totalFrames = this.movie.TGetPropertyAsNumber("/", 5);
    if (currentFrame >= totalFrames) return;
    this.movie.GotoFrame(currentFrame);
    this.spanCurrFrame.innerHTML = this.movie.TGetPropertyAsNumber("/", 4);
}

SwfViewer.prototype.zoomIn = function()
{
    this.movie.Zoom(50);
}

SwfViewer.prototype.zoomOut = function()
{
    this.movie.Zoom(200);
}

SwfViewer.prototype.fullScreen = function()
{
    this.currentFrame = this.movie.TGetPropertyAsNumber("/", 4);
    if (this.isFullScreen == false)
    {
        this.container.style.position = "absolute";
        this.container.style.left = "0";
        this.container.style.top = "0";
        this.container.style.width = "100%";
        this.container.style.height = "100%";

        this.loadSwf();
        window.scrollTo(0, 0);

        this.isFullScreen = true;
    }
    else
    {
        this.container.style.position = "static";

        this.container.style.width = this.width+"px";
        this.container.style.height = this.height+"px";

        this.loadSwf();
        window.scrollTo(this.container.offsetTop, this.container.offsetLeft);

        this.isFullScreen = false;
    }
}