function FindAndReplace( text, searchString, replacementString )
{   
    // Error checking
    if ( (text == null) || (searchString == null) ) { return null; }
    if ( (text.length == 0) || ( searchString.length == 0 ) ) { return text; }

    // no replacementString ?
    if ( ( replacementString == null ) || ( replacementString.length == 0 ) ) { replacementString = ""; }

    var searchStringNumber = searchString.length;
    var LaengereplacementString = replacementString.length;
    var Pos = text.indexOf(searchString, 0);

    while ( Pos >= 0 )
    {
        text = text.substring(0, Pos) + replacementString + text.substring( Pos + searchStringNumber );
        Pos = text.indexOf( searchString, Pos + LaengereplacementString );
    }
    
    return text;
} 

function initGallery() {
	$( '#gallery img' ).hide();
	$( '#photoWrap' ).hide();
	
	// preload
	$('#gallery img').each( function( i ) {
		
			$( this ).load( function() {
				$( this ).fadeIn();
		    });
		
		var src = $( this ).attr( 'src' ) ;
		src = FindAndReplace( src, 'team_', '' );
		var text = $( this ).attr( 'title' ) ;
		var textArray = text.split("_");

		var tmpTestJoin = src.split( "join" );
		
		// add gallery thumbnail image id				
		$( this ).attr( 'id', 'galleryImagesSmall' + i );
		$( this ).parent( 'a' ).attr( 'title', '' );
		$( this ).attr( 'title', '' );
		
		if( tmpTestJoin[1] == null ) {
			// qick fix -> first 4 people getting italic subline
			if( i < 4 ) {
				textArray[1] = '<em>' + textArray[1] + '</em>'
			}
			
			// add BigView
			$( '#photoWrap' ).append( '<div id="galleryImagesDiv' + i + '" class="bigView">'
										 + '<img id="galleryImages' + i + '" src="' + src + '" />'
										 + '<ul class="blockSmall round">'
											+ '<li><h2>' + textArray[0] + '</h2></li>'
											+ '<li class="name photoFont">' + textArray[1] + '</li>'
											+ '<li class="country photoFont italic">' + textArray[2] + '</li>'
										+ '</ul>'
								    + '</div>' );
		}
		
	} );

// hide big view images
$('#photoWrap .bigView').hide();

// nothing to click here
$('.gallery-icon a').click( function() { return false; } );

	
	$('.gallery-icon img').mouseenter( function() {	
		$( '#photoWrap' ).show();
		$( '#' + FindAndReplace( $( this ).attr( 'id' ) , 'Small', 'Div' ) ).fadeIn( 'slow' );
	}).mouseleave( function() {
			$( '#' + FindAndReplace( $( this ).attr( 'id' ) , 'Small', 'Div' )  ).fadeOut( 'slow' );
			$( '#photoWrap div' ).hide();
			$( '#photoWrap' ).hide();
	});
}

function initSideScroll() {
	var pane = $('#side');
	pane.jScrollPane( {
			showArrows: false,
			autoReinitialise: true,
			animateScroll: true
		});
}

function initSideScrollTech() {
	$( '#side ul li ul' ).hide();
//	$( '#side ul li:first ul' ).show();
//	$( '#side ul li:first' ).addClass( 'activ' );
				
	var api = $('#side').data('jsp');

	$('#side ul li.box').bind( 'click', function() {
			var tmpHeightVal = 0;

				// select active element
				$( '#side ul li ul' ).hide();
				$( '#side ul li' ).removeClass( 'activ' );
				$( this ).addClass( 'activ' );
				$( this ).children( 'ul' ).show();

			// auto height
			$( '#side ul.tech li.box' ).each( function() {
				
				if ( $( this ).hasClass( 'activ' ) == true ) {
					// if( tmpHeightVal != 0 )	tmpHeightVal += $( this ).height();
					return false;
				} else {
					tmpHeightVal += $( this ).height() +19;
				}
			} );

			// new height
			$('#side').jScrollPane(
					{
						showArrows: false,
						autoReinitialise: true,
						animateScroll: true
					}
				);				
			//scroll
			api.scrollTo( 0, tmpHeightVal );					
		}
	);
}

function initSideScrollDownload() {
	var api = $('#side').data('jsp');
			var tmpHeightVal = 0;

			// auto height
			$( '#side ul.downloads li.box' ).each( function() {
				
				if ( $( this ).hasClass( 'activ' ) == true ) {
					// if( tmpHeightVal != 0 )	tmpHeightVal += $( this ).height();
					return false;
				} else {
					tmpHeightVal += $( this ).height() +19;
				}
			} );
			
			//scroll
			api.scrollTo( 0, tmpHeightVal );					
}

function initAutoPager() {
	var num = $( 'div#blockWrap ul#autoPager li' ).length;
	var numSub = $( 'div#blockWrap ul#autoPager li li' ).length;
	num = num-numSub;
if( num > 1 ) {
	
	for( i=1; i <= num; i++ ) {
		var tmpHtmlInput = '';
		if( i == 1 ) { var tmpClassLink = ' class="activ"'; } else { var tmpClassLink = ''; }
		tmpHtmlInput = '<a href="#" id="pager-' + i + '"' + tmpClassLink + '>' + i + '</a>';

		$( '#page-control' ).append( tmpHtmlInput );
		
	}

	$( '#page-next' ).append( '<a href="#">></a>' );
	$( '#page-prev' ).append( '<a href="#" class="vh"><</a>' );
	
		function mycarousel_initCallback(carousel) {
			$('#page-control a').bind('click', function() {
			    carousel.scroll( $.jcarousel.intval( $(this).text() ) );

			    $( '#page-control a' ).removeClass( "activ" );
			    $( '#' + $( this ).attr( 'id' ) ).addClass( "activ" );	
			    
        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
        		if( $(this).text() == 1  ) { $( '#page-prev a' ).addClass( 'vh' ); }
        		if( num == $(this).text() ) { $( '#page-next a' ).addClass( 'vh' ); }
        		
			    return false;
			});
		    $('#page-next a').bind('click', function() {

		    	var tmpActivId = $( '#page-control a.activ' ).attr( 'id' );
				var maxNum = num;
				var activNum = FindAndReplace( $( '#' + tmpActivId ).attr( 'id' ), 'pager-', '' );

			    if( maxNum > activNum ) {
					$( '#page-control a' ).removeClass( "activ" );
					$( '#' + tmpActivId ).next().addClass( "activ" );
		        	carousel.next();
		        		
		        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
		        		if( (maxNum-1) == activNum ) { $( this ).addClass( 'vh' ); }
			    }			    
		        return false;
		    });
		    $('#page-prev a').bind('click', function() {
			    
				var tmpActivId = $( '#page-control a.activ' ).attr( 'id' );
				var activNum = FindAndReplace( $( '#' + tmpActivId ).attr( 'id' ), 'pager-', '' );

			    if( 1 < activNum ) {
					$( '#page-control a' ).removeClass( "activ" );
					$( '#' + tmpActivId ).prev().addClass( "activ" );
					carousel.prev();
					
	        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
	        		if( 2 == activNum ) { $( this ).addClass( 'vh' ); }					
			    }
		        
		        return false;
		    });
		}  

	$('#blockWrap ul#autoPager').jcarousel({
		scroll: 1,
		visible: 1,
		animation: 0,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: mycarousel_initCallback
	});
}
}

function initAutoPagerTwoColum() {
	var num = $( 'ul#autoPager li' ).length;
	var numSub = $( 'ul#autoPager li li' ).length;
	num = num-numSub;

	if( num > 2 ) {

		var z=0;
		for( i=1; i <= num; i = i+2 ) { z++;

			var tmpHtmlInput = '';
			if( z == 1 ) { var tmpClassLink = ' class="activ"'; } else { var tmpClassLink = ''; }
			 
			tmpHtmlInput = '<a href="#" id="pager-' + z + '"' + tmpClassLink + '>' + z + '</a>';
			$( '#page-control' ).append( tmpHtmlInput );
		}

		$( '#page-next' ).append( '<a href="#">></a>' );
		$( '#page-prev' ).append( '<a href="#" class="vh" ><</a>' );
		
			function mycarousel_initCallback(carousel) {
				$('#page-control a').bind('click', function() {
				    carousel.scroll( $.jcarousel.intval( ( $(this).text()*2 )-1 ) );
				    
				    $( '#page-control a' ).removeClass( "activ" );
				    $( '#' + $( this ).attr( 'id' ) ).addClass( "activ" );
				    
	        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
	        		if( ( $(this).text()*2 )-1 == 1  ) { $( '#page-prev a' ).addClass( 'vh' ); }
	        		if( num-1 <= ( $(this).text()*2 )-1 ) { $( '#page-next a' ).addClass( 'vh' ); }				    
	        		
				    return false;
				});
				
			    $('#page-next a').bind('click', function() {
				    
					var tmpActivId = $( '#page-control a.activ' ).attr( 'id' );
					var maxNum = Math.round( ( num / 2 ) );
					var activNum = FindAndReplace( $( '#' + tmpActivId ).attr( 'id' ), 'pager-', '' );

				    if( maxNum > activNum ) {
						$( '#page-control a' ).removeClass( "activ" );
						$( '#' + tmpActivId ).next().addClass( "activ" );
			        	carousel.next();
		        		
		        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
		        		if( (maxNum-1) == activNum ) { $( this ).addClass( 'vh' ); }			        	
				    }
			        return false;
			    });
			    
			    $('#page-prev a').bind('click', function() {
				    
					var tmpActivId = $( '#page-control a.activ' ).attr( 'id' );
					var activNum = FindAndReplace( $( '#' + tmpActivId ).attr( 'id' ), 'pager-', '' );

				    if( 1 < activNum ) {
						$( '#page-control a' ).removeClass( "activ" );
						$( '#' + tmpActivId ).prev().addClass( "activ" );
						carousel.prev();
						
		        		$( '#page-prev a,#page-next a' ).removeClass( 'vh');
		        		if( 2 == activNum ) { $( this ).addClass( 'vh' ); }							
				    }
			        
			        return false;
			    });
			}  

		$('ul#autoPager').jcarousel({
			scroll: 2,
			visible: 2,
			animation: 455,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: mycarousel_initCallback
		});

		var htmlHeader = $( '#articleHeadline' ).html();
		$( '#autoPager li:first' ).prepend( '<div id="articleHeadline">' + htmlHeader + '</div>' );
		$( '#articleHeadline' ).remove();

		
	}
}

function initClientsGallery() {
	// nothing to click here
	$('.gallery-icon a').click( function() { return false; } );
	
		var z = 0;
		var startLiObj = true;
		var htmlIn = '<ul id="autoPagerGal">';

			$( '.gallery-item' ).each( function() { 
					z++;
					if( startLiObj == true ) { htmlIn += '<li>'; startLiObj = false; }
						htmlIn += '<dl class="gallery-item">' + $( this ).html() + '</dl>';
					if( z%16 == 0 ) { htmlIn += '</li>'; startLiObj = true; } 
				} );
			htmlIn += '</ul>';
			
		$( '#gallery-1' ).remove();	
		$( '#gallery' ).prepend( '<div id="nxtBtn" class="fr clientGalleryBtn"><span></span></div><div id="preBtn" class="fl clientGalleryBtn"><span></span></div>' );
		$( '#gallery' ).prepend( htmlIn );

		$('ul#autoPagerGal').jcarousel({
			scroll: 1,
			visible: 1,
			wrap: 'circular',
			buttonNextHTML: '#nxtBtn',
			buttonPrevHTML: '#preBtn'
		});
}
