/**
 * Reload page for references on the same site as calling site
 *
 * @access public
 * @return void
 */
function reloadReference(url) {
	window.location = url;
	window.location.reload();
}


/**
 * Switch displayed container
 *
 * @access public
 * @param array container array with used containers
 * @param string action string with action to do
 * @param integer counter integer which describes first displayed container
 * @return integer
 */
function switchContainer( container, action, offset ) {
    interval = 2;
    containersShownSameTime = 2;

    if( action == 'forward' ) {
        offset += interval;
    } else {
        offset -= interval;
    }

    // hide all containers
    for( i = 0; i < container.length; ++i ) {
        if( container[i] ) {
            container[i].style.display = 'none';
        }
    }

    // show next n elements beginning at offset
    for( i = offset; i < offset + containersShownSameTime; ++i ) {
        if( container[i] ) {
            container[i].style.display = '';
        }
    }

    // now show/hide scroll buttons
    if( ( offset + interval + 1 ) >= container.length ) {
        if( next ) {
            next.style.display = 'none';
        }
    } else {
        if( next ) {
            next.style.display = '';
        }
    }
    if( offset >= interval ) {
        if( next ) {
            last.style.display = '';
        }
    } else {
        if( next ) {
            last.style.display = 'none';
        }
    }

    return offset;
}

/**
 * Shows specified newsletter page in popup
 *
 * @access public
 * @param string page the page-number
 * @return false
 */
function showNewslinePage( issue, article, page ) {
    if( document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-1' ) ) {
        document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-1' ).style.display = 'none';
    }
    if( document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-2' ) ) {
        document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-2' ).style.display = 'none';
    }
    if( document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-3' ) ) {
        document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-3' ).style.display = 'none';
    }

    if( document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-'+page ) ) {
        document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-'+page ).style.display = 'block';
    } else {
        if( document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-1' ) ) {
            document.getElementById( 'newsletterFulltext-'+issue+'-'+article+'-1' ).style.display = 'block';
        }
    }

    return false;
}

/**
 * Staff object. Used to display staff.
 *
 * @access public
 */
function tgStaff( popup ) {

    /**
     * Contains the popup object used to display staff
     *
     * @access public
     * @var tgPopup
     */
    this.popup = popup;

    /**
     * Shows a staffmember.
     *
     * @access public
     * @param string id id of the staffmember to show
     * @return void
     */
    this.show = function( id )
    {
        closeButton     = '<div class="navigationRow"><a href="javascript: popup.hide();" class="close">X</a></div>';
        distance        = '<div class="distance"></div>';
        this.popup.showContent( document.getElementById( id ).innerHTML + distance + closeButton );
        this.popup.show();

    }

    /**
     * hides a staffmember.
     *
     * @access public
     * @return void
     */
    this.hide = function()
    {
        this.popup.hide();
    }
}

/**
 * Reference object. Used to display references and reference groups.
 *
 * @access public
 */
function tgReference( popup ) {

    /**
     * Contains the popup object used to display references
     *
     * @access public
     * @var tgPopup
     */
    this.popup = popup;

    /**
     * Contains the url pointing to the service url delivery reference views
     *
     * @access public
     * @var string
     */
    this.grabTarget = '';

    /**
     * Contains the url pointing to the service url delivery reference views
     *
     * @access public
     * @var string
     */
    this.failureMessage = 'Leider konnte die gew&uuml;nschte Referenz nicht gefunden werden.';

    /**
     * Inits the object. Unused yet.
     *
     * @access private
     */
    this.init = function()
    {}

    /**
     * Shows a reference-unterbeitrag content
     *
     *
     * @access public
     * @param int containerId id of the first unterbeitrag to show

     * @return void
     */
    this.show = function( containerId )
    {
        if( ! containerId ) {
            return;
        }

        this.popup.showProgress();
        this.popup.show();

        url = this.grabTarget + '' + containerId + '';

        YAHOO.util.Connect.asyncRequest( 'GET', url, this, null );
        return false;
    }

    /**
     * Handles the callback action for a successful ajax request.
     *
     * @access public
     * @param Object success object returned by YAHOO.util.Connect.asyncRequest()
     * @return void
     */
    this.success = function( result )
    {
        this.popup.showContent( result.responseText );
    }

    /**
     * Handles the callback action for a failed ajax request.
     *
     * @access public
     * @param Object failure object returned by YAHOO.util.Connect.asyncRequest()
     * @return void
     */
    this.failure = function( result )
    {
        this.popup.showContent( this.failureMessage );
    }
}


/**
 * Popup object. Used to hide/show the website popup and display loading progress.
 *
 * @access public
 * @param String content id of the element that contains the popup content
 */
function tgPopup( container, content )
{
    /**
     * Contains the id of the result target container.
     *
     * @access public
     * @var Object
     */
    this.effectContainer = document.getElementById( container );

    /**
     * Contains the id of the result target container content.
     *
     * @access public
     * @var Object
     */
    this.contentContainer = document.getElementById( content );

    /**
     * Contains the id of the contentArea.
     *
     * @access public
     */
    this.contentArea = document.getElementById('contentArea');

    /**
     * Contains the id of the inactive contentArea.
     *
     * @access public
     */
    this.contentAreaInactive = document.getElementById('contentAreaInactive');

    /**
     * Stores the css class used for showing the loading progress.
     *
     * @access public
     * @var string
     */
    this.progressCssClass = 'loadingProgress';

    /**
     * Stores the height of the contentArea
     *
     * @access public
     * @var int
     */
    this.contentAreaHeight = this.contentArea.offsetHeight;

    /**
     * Stores the effect
     *
     * @access public
     * @var string
     */
    this.effect = null;

    /**
     * stores the height of the popup
     *
     * @access public
     * @var int
     */
    this.height = 380;

    /**
     * stores the duration of the popup
     *
     * @access public
     * @var int
     */

    this.duration = 200;

    /**
     * Inits the object. Unused yet.
     *
     * @access private
     */
    this.init = function()
    {
        this.effect = new fx.Styles( this.effectContainer,
                                    {     duration: this.duration
                                        , transition: Fx.Transitions.linear
                                    });

        this.contentAreaInactive.style.height = this.contentAreaHeight + 'px';
        this.hideEffect = new fx.Styles( this.contentAreaInactive,
                                    {     duration: this.duration
                                        , transition: Fx.Transitions.linear
                                    });
    }

    /**
     * removes px from string
     *
     * @access public
     */
    this.getRealHeight = function()
    {
        this.actualHeight = this.effectContainer.style.height;
        this.intHeight = this.actualHeight.substr( 0, this.actualHeight.length - 2 );
    }

    /**
     * Shows the loading progress in the popup container.
     *
     * @access public
     */
    this.showProgress = function()
    {
        this.contentContainer.innerHTML = '';
        YAHOO.util.Dom.addClass( this.contentContainer, this.progressCssClass );
    }

    /**
     * Hides the loading progress in the popup container.
     *
     * @access public
     */
    this.hideProgress = function()
    {
        this.contentContainer.innerHTML = '';
        YAHOO.util.Dom.addClass( this.contentContainer, this.progressCssClass );
    }

    /**
     * Sets the content to show, shows the popup and hides the loading progress.
     * hideProgress() and * show() are called implicit.
     *
     * @access public
     * @param String the content to show - pure HTML
     */
    this.showContent = function( content )
    {
        this.hideProgress()
        YAHOO.util.Dom.removeClass( this.contentContainer, this.progressCssClass );
        this.contentContainer.innerHTML = content;
    }

    /**
     * Shows the popup. Turns it visibility to "visible".
     *
     * @access public
     */
    this.show = function()
    {
        this.getRealHeight();

        // this is just to fix a bug when used with interred!
        this.contentAreaHeight = this.contentArea.offsetHeight;
        this.init();
        // end of bugfixing

        if( this.intHeight == 0 ) {

            this.hideEffect.custom({
                'opacity': [ 0, 0.7 ]
            });

            this.effect.custom({
                'height': [ 0, this.height ],
                'opacity': [ 0, 100 ]
            });
        }
    }

    /**
     * Shows the popup. Turns it visibility to "visible".
     *
     * @access public
     */
    this.showWithoutDelay = function()
    {
        this.getRealHeight();

        // this is just to fix a bug when used with interred!
        this.contentAreaHeight = this.contentArea.offsetHeight;
        this.init();
        // end of bugfixing

        if( this.intHeight == 0 ) {

            this.hideEffect.custom({
                'opacity': [ 0.7, 0.7 ]
            });

            this.effect.custom({
                'height': [ this.height, this.height ],
                'opacity': [ 100, 100 ]
            });
        }
    }

    /**
     * Hides the popup. Turns it visibility to "invisible".
     *
     * @access public
     */
    this.hide = function()
    {
        this.getRealHeight();

        if( this.intHeight > 0 ) {

            this.hideEffect.custom({
                'opacity': [ 0.7, 0 ]
            });

            this.effect.custom({
                'height': [ this.height, 0 ],
                'opacity': [ 100, 0 ]
            });
        }
    }

    this.init();
}

/**
 * Newsline object. Used to display newsline articles.
 *
 * @access public
 */
function tgNewsline( popup ) {

    /**
     * Contains the popup object used to display newsline
     *
     * @access public
     * @var tgPopup
     */
    this.popup = popup;

    /**
     * Stores all article ids grouped by issues
     *
     * @access public
     * @var array[object]
     */
    this.issuesArticles = new Array();

    /**
     * Stores the currently viewed issue id
     *
     * @access public
     * @var int
     */
    this.currentIssue = false;

    /**
     * Stores the currently viewed article id
     *
     * @access public
     * @var int
     */
    this.currentArticle = false;

    /**
     * Stores the table of contents controller object
     *
     * @access public
     * @var object
     */
    this.toc = new Object();

    /**
     * Stores the current visible table of contents
     *
     * @access public
     * @var string
     */
    this.toc.currentIssue = '';

    /**
     * Shows an article.
     *
     * @access public
     * @param string issue id of the issue to show
     * @param string article id of the article from issue to show
     * @param boolean nodelay set to true to display immediately without transition (optional)
     * @return void
     */
    this.show = function( issue, article, nodelay )
    {
        // hide all articles
        for( var i = 0; i < this.issuesArticles.length; ++i ) {
            for( var j = 0; j < this.issuesArticles[i].articles.length; ++j ) {
                document.getElementById( 'article-' + this.issuesArticles[ i ].issue + '-'
                    + this.issuesArticles[ i ].articles[ j ] ).style.display = 'none';
            }
        }

        if( document.getElementById( 'article-' + issue + '-' + article ) ) {
            document.getElementById( 'article-' + issue + '-' + article ).style.display = 'block';

            if( document.getElementById( 'distance-' + this.currentIssue ) ) {
                document.getElementById( 'distance-' + this.currentIssue ).style.display = 'none';
            }
            if( document.getElementById( 'navigationRow-' + this.currentIssue ) ) {
                document.getElementById( 'navigationRow-' + this.currentIssue ).style.display = 'none';
            }
            if( document.getElementById( 'distance-' + issue ) ) {
                document.getElementById( 'distance-' + issue ).style.display = 'block';
            }
            if( document.getElementById( 'navigationRow-' + issue ) ) {
                document.getElementById( 'navigationRow-' + issue ).style.display = 'block';
            }

            if( nodelay ) {
                this.popup.showWithoutDelay();
            } else {
                this.popup.show();
            }

            this.currentIssue = issue;
            this.currentArticle = article;
        }
    }

    /**
     * hides a newsline article.
     *
     * @access public
     * @return void
     */
    this.hide = function()
    {
        if( window.location.hash ) {

            // this is a workaround for firefox
            if( typeof navigator != undefined
             && navigator.userAgent != null
             && navigator.userAgent.search(/Gecko/) > 1
            ) {
                window.location.hash = '#';
            } else {
                window.location.hash = '';
            }
        }

        this.popup.hide();
    }

    /**
     * Adds an article id to the article list
     *
     * @access public
     * @param string issue
     * @param string article
     * @return void
     */
    this.addArticle = function( issue, article )
    {
        var issueExists = false;

        for( var i = 0; i < this.issuesArticles.length; ++i ) {
            if( this.issuesArticles[ i ].issue == issue ) {
                this.issuesArticles[ i ].articles.push( article );
                issueExists = true;
            }
        }

        if( !issueExists ) {
            var myIssue = new Object();
            myIssue.issue = issue;
            myIssue.articles = new Array();
            myIssue.articles.push( article );

            this.issuesArticles.push( myIssue );
        }
    }

    /**
     * Shows the previous article.
     *
     * @access public
     * @return void
     */
    this.showPrevious = function()
    {
        for( var i = 0; i < this.issuesArticles.length; ++i ) {
            for( var j = 0; j < this.issuesArticles[i].articles.length; ++j ) {
                if( this.issuesArticles[ i ].issue == this.currentIssue
                 && this.issuesArticles[ i ].articles[ j ] == this.currentArticle
                 && this.issuesArticles[ i ].articles[ ( j - 1 ) ]
                ) {
                    //this.show( this.articles[ ( i - 1 ) ] );
                    window.location.hash = this.currentIssue + "-" + this.issuesArticles[ i ].articles[ ( j - 1 ) ] + "-i";
                    location.reload();
                    break;
                }
            }
        }
    }

    /**
     * Shows the next article.
     *
     * @access public
     * @return void
     */
    this.showNext = function()
    {
        for( var i = 0; i < this.issuesArticles.length; ++i ) {
            for( var j = 0; j < this.issuesArticles[i].articles.length; ++j ) {
                if( this.issuesArticles[ i ].issue == this.currentIssue
                 && this.issuesArticles[ i ].articles[ j ] == this.currentArticle
                 && this.issuesArticles[ i ].articles[ ( j + 1 ) ]
                ) {
                    //this.show( this.articles[ ( i + 1 ) ] );
                    window.location.hash = this.currentIssue + "-" + this.issuesArticles[ i ].articles[ ( j + 1 ) ] + "-i";
                    location.reload();
                    break;
                }
            }
        }
    }

    /**
     * Initializes the table of contents view
     *
     * @access public
     * @return void
     */
    this.toc.init = function()
    {
        if( newsline.issuesArticles[ 0 ]
         && document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ 0 ].issue )
        ) {
            document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ 0 ].issue ).style.display = "block";
        }

        for( var i = newsline.issuesArticles.length - 1; i >= 0; --i ) {
            if( newsline.issuesArticles[ i + 1 ] ) {
                if( document.getElementById( 'forwardArrow-' + newsline.issuesArticles[ i ].issue ) ) {
                    document.getElementById( 'forwardArrow-' + newsline.issuesArticles[ i ].issue ).style.visibility = "visible";
                }
            }
            if( newsline.issuesArticles[ i - 1 ] ) {
                if( document.getElementById( 'backwardArrow-' + newsline.issuesArticles[ i ].issue ) ) {
                    document.getElementById( 'backwardArrow-' + newsline.issuesArticles[ i ].issue ).style.visibility = "visible";
                }
            }
        }

        this.currentIssue = 0;
    }

    /**
     * Shows the previous table of contents issue.
     *
     * @access public
     * @return void
     */
    this.toc.backward = function()
    {
        if( document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue ].issue ) ) {
            document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue ].issue ).style.display = "none";
        }

        if( document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue - 1 ].issue ) ) {
            document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue - 1 ].issue ).style.display = "block";
        }

        this.currentIssue = this.currentIssue - 1;
    }

    /**
     * Shows the next table of contents issue.
     *
     * @access public
     * @return void
     */
    this.toc.forward = function()
    {
        if( document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue ].issue ) ) {
            document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue ].issue ).style.display = "none";
        }

        if( document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue + 1 ].issue ) ) {
            document.getElementById( 'newslineTOC-' + newsline.issuesArticles[ this.currentIssue + 1 ].issue ).style.display = "block";
        }

        this.currentIssue = this.currentIssue + 1;
    }
}

    /**
     * gets flashmovie-Object
     *
     * @access public
     * @return object
     */
    function getFlashMovie(movieName) {   
        var isIE = navigator.appName.indexOf("Microsoft") != -1;   
        return (isIE) ? window[movieName] : document[movieName];  
    }  
    
    /**
     * send close to actionscript
     *
     * @access public
     * @return void
     */
    function closeSend() {   
        getFlashMovie("referenzFilm").sendCloseToFlash('close'); 
    } 
    
    /**
     * get close-messages from actionscript
     *
     * @access public
     * @return void
     */
    function getCloseFromFlash( txt ) {
    }
