// ===================================================================================
//  js_flash
// ===================================================================================

function geber_lib_isValidFlashObject(movie) {
return true;
}

function geber_lib_findFlash(flash) {

var browser = geber_getBrowser();

if (typeof(flash) != 'string' || flash == "")
    return;

   
    // detect for mozilla based browsers and opera
    // detect for netscape browsers
    if (window.document[flash])
    {
       try {
           var movie = window.document[flash];
           if (geber_lib_isValidFlashObject(movie))
               return movie;
       } catch(e) {}
    }


    // detect for IE 5
    if (window[flash])
    {
       try {
           var movie = window[flash];
           if (geber_lib_isValidFlashObject(movie))
               return movie;
       } catch (e) {}
    }


    // detect for IE
    if (!browser.isOpera && document.all && document.all[flash])
    {
       try {
           var movie = document.all[flash];
           if (geber_lib_isValidFlashObject(movie))
               return movie;
       } catch (e) {}
    }



    // detect for mozilla based browsers and opera
    if (!browser.isIE && document.embeds[flash])
    {
       try {
           var movie = document.embeds[flash];
           if (geber_lib_isValidFlashObject(movie))
               return movie;
       } catch (e) {}
    }
 


    // *** at least use DOM to get the player

    if (!browser.hasDOM) 
        return;



    // try to find object tag
    try {
        var movie = document.getElementByName(flash)[0];
        if (geber_lib_isValidFlashObject(movie))
           return movie;
     } catch(e) {}



    // try to find object tag
    var movie = document.getElementById(flash);
    if (geber_lib_isValidFlashObject(movie))
       return movie;
 

    // try to find an embed tag for the movie
    var movies = document.getElementsByTagName('embed');
    if (!movies || !movies.length) {
      return;
    }

    
    for (var i=0; i<movies.length; i++)
    {
        if ((movies[i].getAttribute("name") == flash || movies[i].getAttribute("id") == flash) && 
            geber_lib_isValidFlashObject(movies[i]))
        {
            return movies[i];
        }
    }
    

    return;
}






var geber_flashMovies = new Array();

function geber_flash_registerFlash(movieTagName, startFrame, messageFieldID, flashLayerID, noFlashLayerID)
{
    var flashMovie = new geber_flash_Movie(movieTagName, startFrame, messageFieldID, flashLayerID, noFlashLayerID);
    geber_flashMovies[geber_flashMovies.length] = flashMovie;
}


function geber_flash_showFlashMovies()
{
    var browser = geber_getBrowser();

    try
    {
        if (geber_flashMovies.length > 0 || !browser || typeof(browser) != 'object' || !browser.isPageLoaded())   
            window.setTimeout("geber_flash_showFlashMovies();", 250);
    } 
    catch(e) 
    {
            window.setTimeout("geber_flash_showFlashMovies();", 500);
    }


    // get all movies and try to show them
    var newMovieList = new Array();
    for (var i=0; i < geber_flashMovies.length; i++)
    {
         var movie  = geber_flashMovies[i];
         try 
         { 
             movie.showFlash(); 

            // remember the movie for next time if flash is not yet visible
            if (movie.needNextTryToShow())
            {
                newMovieList[newMovieList.length] = movie;
            }

         } catch(e) {}
    }

    geber_flashMovies = newMovieList;
}
geber_flash_showFlashMovies();


function geber_flash_Movie(movieTagName, startFrame, messageFieldID, flashLayerID, noFlashLayerID)
{
    this.movieTagName = (typeof(movieTagName) == 'string' && movieTagName.length > 0) ? movieTagName : "nomovietagname__";
    this.startFrame = (typeof(startFrame) == 'number' && startFrame > 0) ? startFrame : 0;
    this.messageFieldID = (typeof(messageFieldID) == 'string' && messageFieldID.length > 0) ? messageFieldID : "nomessagefield__";

    this.flashLayerID = (typeof(flashLayerID) == 'string' && flashLayerID.length > 0) ? flashLayerID : "flashLayerNoID__";
    this.noFlashLayerID = (typeof(noFlashLayerID) == 'string' && noFlashLayerID.length > 0) ? noFlashLayerID : "noFlashLayerNoID__";

    this.maxRetry = 50;
}



geber_flash_Movie.prototype.needNextTryToShow = function()
{
    return (this.maxRetry > 0) ? true : false;
}


geber_flash_Movie.prototype.writeMessage = function(msg)
{
//    geber_lib_writeMessage(this.messageFieldID, msg);
}


geber_flash_Movie.prototype.isLoaded = function()
{
    if (!this.isValid())
    {
        this.writeMessage("Flash is invalid: " + this.movieTagName);
        return false;
    }
    else
    {
        try
        {
            return (this.movie.PercentLoaded() == 100) ? true : false;
        }
        catch (e) {}
    }
    return false;
}

geber_flash_Movie.prototype.isActive = function()
{
    if (!this.isValid() || !this.isLoaded())
    {
        return false;
    }
    else
    {
        try
        {
             var currentFrame = parseInt(this.movie.TCurrentFrame("/"));
             this.writeMessage("flash '" + this.movieTagName + "' is on current frame: " + currentFrame);

             if (this.startFrame <= 0 || currentFrame >= this.startFrame)
                 return true;
 
             // start the movie if the current frame indicates a not playing movie
             else if (currentFrame < 0)
             {
                 this.movie.GotoFrame(0);
                 this.movie.Play();
             }
 
        }
        catch (e) {this.writeMessage("<pre>" + e.message + "</pre>");}
    }
    return false;
}


geber_flash_Movie.prototype.isValid = function()
{
    if (typeof(this.movie) != 'undefined' && geber_lib_isValidFlashObject(this.movie))
        return true;


    if (typeof(this.movieTagName) == 'string' && this.movieTagName.length > 0)
    {
            this.movie = geber_lib_findFlash(this.movieTagName);
    }


    if (typeof(this.movie) != 'undefined' && geber_lib_isValidFlashObject(this.movie))
    {
        return true;
    }

//        this.writeMessage("Flash is still invalid after retry: " + this.movieTagName);


    return false;
}



geber_flash_Movie.prototype.activateFlashLayer = function()
{

 this.writeMessage("showing flash '" + this.movieTagName + "'");

        // the flash is active by now. show it 
    try
    {
        this.maxFlashSearch = 0;
        this.writeMessage("showing flash '" + this.movieTagName + "'" );

        var movieLayer = document.getElementById(this.flashLayerID);
        movieLayer.style.top = 0;
        movieLayer.style.left = 0;
        movieLayer.style.display = "block";
//        movieLayer.style.position = "relative";
    } catch(e) {}

    try
    {
        var alternativeLayer = document.getElementById(this.noFlashLayerID);
        alternativeLayer.style.display = "none";
//        alternativeLayer.innerHTML = "";
    } catch(e) {}
}




// *** show the flash movie only if it has been started to play
// some users my experience trouble if loading the XML file fails

geber_flash_Movie.prototype.showFlash = function ()
{
    var browser = geber_getBrowser();

    this.maxRetry--;

    if (typeof(browser) != 'undefined' && !browser.hasFlash )
    {
        this.writeMessage("no flash installed. Flash '" + this.movieTagName + "' can not be activated.");
        this.maxRetry = 0;         
        return;
    }
 

    // find the movie. Try until we have found it
    if (!this.isValid())
    {
        this.writeMessage("flash '" + this.movieTagName + "' not yet found: " + this.maxFlashSearch);
        return;
    }


    if (!this.isLoaded())
    {
        this.writeMessage("flash '" + this.movieTagName + "' not yet loaded");
        return;
    }

    // try to find the active flash
    if (!this.isActive())
    {
        this.writeMessage("waiting for flash '" + this.movieTagName + "' to become active on frame: " + this.startFrame);
        return;
    }


    this.maxRetry = 0;         
    this.activateFlashLayer();
}


function geber_lib_writeFlash(flashID, tagID, movieURL, baseURL, noPrint, isPrintPopUp, width, height, language, flashvars, wmode, startFrame, requestParams, bgcolor)
{

    if (bgcolor == null) 
    {
       bgcolor = "#ffffff";
    }

    if (String(noPrint).length <= 0 || '1' != String(isPrintPopUp))
    {

         var requestWidth, requestHeight;
         try { requestWidth = parseInt(geber_lib_getQueryParameter("width")); } catch(e) {}
         try { requestHeight = parseInt(geber_lib_getQueryParameter("height")); } catch(e) {}
         
         if ((width == null || (width <= 0 && width != "100%")) && requestWidth != null && requestWidth > 0)
             width = requestWidth;
         if ((height == null || (height <= 0 && height != "100%"))  && requestHeight != null && requestHeight > 0)
             height = requestHeight;

         // set the height and width for the layer
         var flashLayerID = "flashLayerID" + flashID;
         var flashLayer = document.getElementById(flashLayerID);
         if (flashLayer != null)
         {
             flashLayer.style.width = width;
             flashLayer.style.height = height;
         }


         // flash data for chart generator
         var requestFlashVars = '';
         var queryParams = new Array("rand", "width", "height", "dir");
         if (requestParams && typeof(requestParams) == 'string' && requestParams.length > 0)
         {
             var addQueryParams = requestParams.split(",");
             for (var idx=0; idx < addQueryParams.length; idx++)
             {
                 queryParams[queryParams.length] = addQueryParams[idx];
             }
         }

         try
         {
             for (var idx = 0; idx < queryParams.length; idx++)
             {
                 var paramValue = geber_lib_getQueryParameter(queryParams[idx]);
                 if (paramValue != null && paramValue.length > 0)
                     requestFlashVars += '&' + queryParams[idx] + "=" + paramValue;
             }
         } catch(e) {}


         var flashAlternativeLayer = document.getElementById("flashnoscriptLayer" + flashID);
         var flashAlternative = "";
         if (flashAlternativeLayer && browser.isIE) 
             flashAlternative = flashAlternativeLayer.innerHTML;

         var movieTagName = tagID;
         if (movieTagName.length <= 0) movieTagName = "FALSCH_Movie" + flashID;

         if (String('').length <= 0) movieURL += "?lang="  + language + "&language=" + language;

         var so = new SWFObject(movieURL, movieTagName, width, height, "6", bgcolor );
         so.nonFlashHTML = flashAlternative;
         so.addParam("quality", "best");
         so.addParam("salign", "lt");
         so.addParam("swliveconnect", "true");
         so.addParam("FlashVars", "&dummy=1&lang="  + language + "&language="  + language + "&" + flashvars + "&uniqueId=" + Math.floor(Math.random()*99999) + "&" + requestFlashVars + "&tx=1");
         if (wmode != null && typeof(wmode) == "string" && wmode.length > 0)
             so.addParam("wmode", wmode);
         
         if (baseURL != null && baseURL.length > 0)
             so.addParam("base", baseURL);

         // show the flash
         var flashTagHTML = so.getSWFHTML();
         geber_lib_writeDocument(flashTagHTML);


         geber_flash_registerFlash(movieTagName, startFrame, "flashMessage" + flashID, 
                  "flashLayerID" + flashID + "Movie", "flashLayerID" + flashID + "NoFlash")

         window.setTimeout("geber_flash_showFlashMovies();", 50);
    }
}