triggerArray = [], rollovers = [] ;
var productstageCount = 0;

/**
 * animate productstage
 *
 * @return void
 */
 

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

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

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

    if ( typeof liveSearch_Init == 'function' )
        liveSearch_Init () ;

    var cnt = 1 ;

    $( '.tripple .item, .teaserhome .teaser' ).each ( function () {
        if ( ( cnt++ % 3 ) == 0 )
            $( this ).addClass ( 'nomr' ) ;
    } ) ;
	
	
	
	if ( productstageCount == 0 && $('#productstage').length > 0 ) {
	
		function setupBox(item, aniright) {
			var $box = $(item);
			function phase( aniright ) {
				$('#productstage .inner').css( 'right', 0 );
				$box.animate({ right: aniright }, ( productstageCount * 3000 ), 'linear', function() { phase(aniright); } );
			}
			$box.hover(function() {
				$box.pause();
			}, function() {
				$box.resume();
			});
			phase( aniright );
		}
		
		productstageCount = $('#productstage .item').length;
		$('#productstage').width($('#productstage').width());
		
		var innerHtml = $('#productstage .inner').html();
		var innerWidth = $('#productstage .item').width();
		innerWidth = innerWidth * productstageCount;
		$('#productstage .inner').html( innerHtml + innerHtml );
		$('#productstage .inner').width( innerWidth*2 );
		
		$('#productstage').css( 'height', $('#productstage .item').height() );
		
		$('#productstage .item').hover( 
			function () {
				$('.tooltip', this).slideDown()
			}
			,
			function () {
				$('.tooltip', this).slideUp()
			}
		);
			
		setupBox( $('#productstage .inner'), innerWidth*-1 );
	}
	
} ) ;


// function animateProductstage( width ) {
//	$('#productstage .inner').css( 'left', 0 );
//	$('#productstage .inner').startAnimation( {"left": width}, 10000, 'linear', function(){ animateProductstage( width ) } );
// }



/**
 * 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.msie) {
        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){
//     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 () {}
//         }
//     } else {
//         window.console.log ( outputtext ) ;
//     }
}

$(document).ready(function() {
    $('.warenkorblink').each( function() {
        this.href='javascript:void(0)';
    } );

//     $('.warenkorblink').each( function() {
//
//         this.href = this.href+'&navigation=0';
//         //         tb_init('a.thickbox');
//     } );
//     $("a[fancy=warenkorblink],area[fancy=warenkorblink]").fancybox({
//         'titlePosition'     : 'inside',
//         'transitionIn'      : 'none',
//         'transitionOut'     : 'none',
//         'autoScale'         : false,
//         'height'            : '90%',
//         'width'             : 800,
//         'type'              : 'iframe'
//     });
    if ( sortiment == 'da' ) {
        $("a[rel=bilder]").fancybox({
    		'transitionIn'	      :	  'fade',
    		'transitionOut'	      :	  'fade',
    		'speedIn'		      :	  500,
    		'speedOut'		      :	  500,
            'autoScale'           :   true,
            'titleShow'           :   false,
            'width'               :   600,
            'height'              :   550,
            'type'                :   'iframe',
            'showNavArrows'       :   false
        });
    } else {
        $("a[rel=bilder]").fancybox({
    		'transitionIn'	      :	  'fade',
    		'transitionOut'	      :	  'fade',
    		'speedIn'		      :	  500,
    		'speedOut'		      :	  500,
    		'titleShow'           :   false
        });
    }

    $("a.fancybox").fancybox({
    	'transitionIn'	      :	  'fade',
    	'transitionOut'	      :	  'fade',
    	'speedIn'		      :	  500,
    	'speedOut'		      :	  500,
    	'titleShow'           :   false
    });

    $("a.fancyhtml").fancybox({
    	'transitionIn'	      :	  'fade',
    	'transitionOut'	      :	  'fade',
    	'speedIn'		      :	  500,
    	'speedOut'		      :	  500,
        'autoScale'           :   false,
        'titleShow'           :   false,
        'width'               :   550,
        'height'              :   550,
        'type'                :   'iframe'
    });
    $("a.fancyhtmlagb").fancybox({
        'transitionIn'        :   'fade',
                              'transitionOut'       :   'fade',
                              'speedIn'             :   500,
                              'speedOut'            :   500,
                              'autoScale'           :   false,
                              'titleShow'           :   false,
                              'width'               :   700,
                              'height'              :   550,
                              'type'                :   'iframe'
    });


});

function makeTabActive ( elem ) {
    if ( $( 'ul.tabs' ).is( '*' ) && $( elem ).is( '*' ) ) {
        $( 'ul.tabs > li' ).each( function () {
            if ( $( this ).attr( 'id' ) != $( elem ).attr ( 'id' ) ) {
                $( this ).removeClass( 'active' );
            } else {
                $( this ).addClass ( 'active' );
            }
        } );
    }
}

var TimeOut ;
function showHideElement ( sel1, sel2 ) {
    clearTimeout ( TimeOut );
    $(sel1).show( );
    $(sel2).hide();
}
function showHideElementTimeout ( sel1, sel2 ) {
    TimeOut = setTimeout ( function () {
        showHideElement ( sel1, sel2 ) ;
    } , 500 );
}
