triggerArray = [], rollovers = [];

$( document ).ready ( function () {
    initTriggers();
    readyRollovers();

    $( "a" ).focus ( function () {
        this.blur () ;
    } ) ;

    $( "input" ).each ( function () {
        $( this ).addClass ( $( this ).attr( 'type' ) ) ;
    } ) ;

    $( "textarea" ).each ( function () {
        $( this ).addClass ( 'text' ) ;
    } ) ;

    $( "#shopnav h1.closed" ).parent().find("div:first").slideUp();
    $( "#shopnav h1" ).click ( function () {
        $(this).parent().find("div:first").slideToggle () ;
    } ) ;


    $("#mSearchModule").each ( function () {
        this.onfocus = function () { liveSearch_ClearSearch ( false ) ; }
        this.onchange = this.onblur = function () { document.getElementById( "mSearchPattern" ).focus () ; } ;
    } ) ;

    $("#mSearchPattern").each ( function () {
        this.onkeypress = this.onfocus = function () { window.setTimeout ( "liveSearch_ExecuteSearch();" , 300 ) ; } ;
        this.onblur = function () { window.setTimeout ( "liveSearch_ClearSearch( false )" , 2000 ) ; }
    } ) ;

    $('.tooltips a, .showtooltip a').Tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        opacity: 0.95
    });

    liveSearch_Init () ;
} ) ;

/**
  * evaluate all trigger functions
  * @return void
*/
function initTriggers(){
    for ( var i = 0 ; i < triggerArray.length ; i++ ) {
        eval ( triggerArray [ i ] ) ;
    }
};

/**
  * set IDs for LI Elements and highlight the right elements
  * @param   function   elements
  * @return void
*/
initRollovers = function ( elements ) {
    debugalert('initRollovers: '+elements)

    $(".navigation").find("> li").each(function(){
        if (this.id == ''){
            dieID = $(this).find('> a').text();
            this.id = dieID.toLowerCase();
            debugalert('ID zugewiesen:'+dieID);
        }
    });
    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
  * @param   function   elements
  * @return void
*/
readyRollovers = function(){
    imagesExp= /((\.jpg)|(\.png)|(\.gif))/;

    //remove IE background flickering
    try {
        document.execCommand('BackgroundImageCache', false, true);
    } catch(e) {}

    // Alle Bilder mit der Klasse active sollen mit dem _f2 versehen werden.
    $("img.active").each( function (){
         this.src = this.src.replace(imagesExp,'_f2$1');
    });

    // Alle Bilder mit Klasse "rollover" aber nicht "active" sollen vorgeladen werden und einen hover bekommen.
    $("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
  *
  * @param  function   elements
  * @return void
  */
resetRollovers = function () {
    $( '.active' ).each ( function () { $( this ).removeClass ( 'active' ) } ) ;
    $( '.open' ).each ( function () { $( this ).removeClass ( 'open' ) } ) ;
};

/**
 * function to preload images
 */
jQuery.preloadImages = function () {
	for ( var i = 0 ; i < arguments.length ; i++ ) {
		jQuery ( "<img>" ).attr ( "src" , arguments[ i ] ) ;
	}
}

/**
 * collect all function
 *
 * @param   function  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){
    if ( !window.console || !console.firebug ) {
        var names = [ "log" , "debug" , "info" , "warn" , "error" , "assert" , "dir" , "dirxml" , "group" , "groupEnd" , "time" , "timeEnd" , "count" , "trace" , "profile" , "profileEnd" ] ;

        window.console = {} ;

        for (var i = 0 ; i < names.length ; ++i ) {
            window.console[ names[ i ] ] = function () {

            }
        }

        return ;
    } else {
        window.console.log ( outputtext ) ;
    }
};

