triggerArray = [], rollovers = [] ;

$( document ).ready ( function () {
    initTriggers () ;
    readyRollovers () ;
    initRequiredFields () ;
    stretchMe();
} ) ;

/**
 * evaluate all trigger functions
 *
 * @return void
 */
function initTriggers () {
    for ( var i = 0 ; i < triggerArray.length ; i++ ) {
        eval ( triggerArray [ i ] ) ;
    }
}

/**
 * @return void
 */
function initRequiredFields () {
    $( "input, select, textarea" ).each ( function () {
        if ( this.name.match( 'req_' ) ) {
            updateFieldStatus ( this ) ;

            this.onchange = function () {
                updateFieldStatus ( this ) ;
            }
        }
    } ) ;
} ;

/**
 * @param  object   pField
 * @return void
 */
function updateFieldStatus ( pField ) {
    if ( pField.value.length == 0 || pField.value == '{txt_ausfuellen}' ) {
        $("label[@for=" + pField.name + "]").addClass('required') ;
        $(pField).addClass('required');
    } else {
        $("label[@for=" + pField.name + "]").removeClass('required') ;
        $(pField).removeClass('required');
    }
} ;

/**
 * set IDs for LI Elements and highlight the right elements
 *
 * @param  array    elements
 * @return void
 */
initRollovers = function ( elements ) {
    debugalert ( 'initRollovers: ' + elements ) ;

    $( ".navigation" ).find( "> li" ).each( function () {
        if ( this.id == '' ) {
            this.id = $( this ).find( '> a' ).text().toLowerCase() ;
        }

        debugalert ( 'ID zugewiesen:' + this.id ) ;
    } ) ;

    for ( var i = 0 ; i < elements.length ; i++ ) {
        if ( elements[ i ] != '' ){
            element = $( "#" + elements[ i ].toLowerCase() ) ;

            $( element ).addClass( 'active' ).find( "> a" ).addClass( 'active' ).find( "> img" ).addClass( 'active' ).end().parents( 'li' ).each( function () {
                $( this ).addClass( 'open' ).find("> a").addClass( 'open' ) ;
            } ) ;
        }
    }
}

/**
 * prepare images for rollover function and preload them
 * also removes flickering effect for background-images in Internet Explorer
 *
 * @return void
 */
readyRollovers = function () {
    imagesExp= /((\.jpg)|(\.png)|(\.gif))/ ;

    if ( jQuery.browser.ie == true ) {
        try {
            document.execCommand ( 'BackgroundImageCache' , false , true ) ;
        } catch ( e ) {

        }
    }

    $( "img.active" ).each( function () {
         this.src = this.src.replace ( imagesExp , '_f2$1' ) ;
    } ) ;

    $( "img.rollover:not(.active)" ).each( function () {
        $.preloadImages ( this.src.replace ( imagesExp , '_f2$1' ) ) ;
    } ).hover (
        function () { this.src = this.src.replace ( imagesExp , '_f2$1' ) ; } ,
        function () { this.src = this.src.replace ( /_f2/ , '' ) ; }
    ) ;
}

/**
 * remove the classes "open" and "active" from all elements
 *
 * @return void
 */
resetRollovers = function () {
    $ ( '.active' ).each ( function () { $( this ).removeClass( 'active' ) } ) ;
    $ ( '.open' ).each ( function () { $( this ).removeClass( 'open' ) } ) ;
};

/**
 * function to preload images
 *
 * @return void
 */
jQuery.preloadImages = function () {
	for ( var i = 0 ; i < arguments.length ; i++ ) {
		jQuery ( "<img>" ).attr ( "src" , arguments[ i ] ) ;
	}
}

/**
 * collect all function
 *
 * @param   string  objects
 * @return  void
 */
collectTriggers = function ( objects ) {
    triggerArray.push ( objects ) ;
}

/**
 * display custom error messages in the firebug extension (http://www.getfirebug.com)
 *
 * @param   string  outputtext
 * @return  void
 */
function debugalert(outputtext){
    try {
        if ( window.console ) {
            window.console.log ( outputtext ) ;
        } else {
             throw ( false ) ;
        }
    } catch ( e ) {

    }
}


function stretchMe () {
    var heightest = 0;
    var widthRef = 0;
    var countToStretch = 0;
    var stretchMeWidth = 0;
    $( '#header_links' ).find ( 'ul#header_links_stretch > li' ).each ( function () {
          widthRef += this.offsetWidth + 2;
          if ( $ ( this ).attr('class') == 'stretchMe' ) countToStretch++;
    });
    if ( countToStretch != 0 ) {
        stretchMeWidth = parseInt( ( 630 - ( widthRef + 20 ) ) / ( countToStretch * 2 ) );
    }
    if ( stretchMeWidth > 0 ){
      $( '#header_links' ).find ( 'ul#header_links_stretch > li.stretchMe' ).each ( function () {
          $ ( this ).css('padding', stretchMeWidth + 'px');
      } ) ;
    }

}
