function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

$( document ).ready( function() {
	
	var snd = getFlashMovieObject( 'playsound' );

	$( "#thumbs img" ).mouseenter( function() {

		$( this ).animate( {
			opacity: 0.75,
			border: '1px solid #716d6c'
		}, 250 );

		snd.Play();
	} ).mouseleave ( function() {
		$( this ).animate( {
			opacity: 1.00,
			border: '1px solid #e4e5e6'
		}, 150 );

		snd.GotoFrame( 1 );
	} );

	// get handle to the scrollable DIV
	var scroll = $( "#thumbs" );

	var rangemax = $( "#rangemax" ).attr( 'total' );

	$( "#scrollbar" ).attr( 'max', rangemax );
	$( "#scrollbar" ).attr( 'value', rangemax );
	

	// initialize rangeinput
	$( "#scrollbar" ).rangeinput( {
	
		// slide the DIV along with the range using jQuery's css() method
		onSlide: function( ev, step ) {
			scroll.animate( {
				bottom: rangemax - step
			}, 1 );
		},
	
		// display progressbar
		progress: true,
	
		// this is called when the slider is clicked. we animate the DIV
		change: function( e, i ) {
			scroll.animate( {
				bottom: rangemax - i
			}, "fast" );
		},
	
		// disable drag handle animation when slider is clicked
		speed: 0
	
	} );
	
    // Correct a bug in jQuery 1.4.4
    $( "#scrollbar" ).bind( 'onSlide', function(){} ); 
	// END of Correct a bug in jQuery 1.4.4
	
	var sbar = $( "#scrollbar" ).data( 'rangeinput' );

	var openGallery = false;

	var animatePosQuadInOut = function( pos ) {	
		return( pos <= 0.5 ) ? Math.pow( 2 * pos, 2 ) / 2 : ( 2 - Math.pow( 2 * ( 1 - pos ), 2 ) ) / 2;
	}

    function sbarAnimate( startVal, stopVal, t, startT ) {
		var d = new Date();

		var now = d.getTime()		

		if( ! startT) {
			startT = now;
		}

		var delta = animatePosQuadInOut( Math.min( 1, (now - startT ) / t ) );
		var goTo = (stopVal - startVal) * delta + startVal;

		sbar.setValue( goTo );
    	
    	if( Math.max( 1, (now - startT ) / t ) == 1 ) {
	    	sbarAnimate( startVal, stopVal, t, startT );
    	}
    
    } 
    
    $( '.galleria_box' ).each( function( idx, obj ) {
		$( this ).find( '.galleria_elements' ).galleria( { width: 800, height: 530 } );
    } );

/*
	$( '.galleria-images' ).click( function( e ) {

		openGallery = false;		

		var galleriaBox = $( this ).parent().parent().parent().parent().attr( 'class' );
		
		$( '.' + galleriaBox ).animate( {
			width: '1'
		}, 500 );
		$( '.' + galleriaBox ).find( '.galleria-elements' ).animate( {
			right: '-900'
		}, 250 );
		
    } );
*/

	var showGallery = function( idGalleryObj, idPhoto ) {

		openGallery = true;

		if( ! idPhoto ) {
			idPhoto = 1;
		}
		
		var w = $( window ).width() / 2 + 160;

		$( idGalleryObj ).find( '.galleria_elements' ).data( 'galleria' ).rescale( w, 530, function( gal ) {

				$( '.galleria-images' ).click( function( e ) {
			
					openGallery = false;		
			
					var galleriaBox = $( this ).parent().parent().parent().parent().attr( 'class' );
					
					$( '.' + galleriaBox ).animate( {
						width: '1'
					}, 500 );
					$( '.' + galleriaBox ).find( '.galleria-elements' ).animate( {
						right: '-900'
					}, 250 );
					
			    } );
		
		} );

		$( idGalleryObj ).find( '.galleria-container' ).css( {
			width: w
		} )

		$( idGalleryObj ).find( '.galleria_elements' ).data( 'galleria' ).show( idPhoto );
		$( idGalleryObj ).animate( {
			width: ( w + 70 )
		}, 450 );
		$( idGalleryObj ).find( '.galleria_elements' ).animate( {
			width: w,
			right: '55'
		}, 500 );
/*		$( idGalleryObj ).find( '.close_galleria' ).animate( {
			opacity: 1.0
		}, 750 );
*/
	}

	$( '.lnkimg' ).click( function( e ) {

		e.preventDefault();
		
		var rel = $( this ).find( 'img' ).attr( 'rel' );
		var pic = $( this ).attr( 'thumbnum' );
		
		showGallery( '#gal_' + rel, pic );
		
	} );

	$( '.categories' ).each( function( idx, obj ) {
		$( this ).attr( "row", $( '#' + $( this ).attr( 'id' ) + '00jpg' ).parent().attr( 'now' ) );
	} ).click( function( e ) {

		e.preventDefault();

		var rel = $( this ).attr( 'id' );

//		sbar.setValue( rangemax - $( this ).attr( 'row' ) * 13.8 );
    	sbarAnimate( sbar.getValue(), rangemax - $( this ).attr( 'row' ) * 13.8, 250 );
		if( openGallery ) {
			$( '.galleria_box' ).each( function( idx, obj ) {
				if( $( this ).width() > 1 ) {
					// The gallery is open... let's close it!
					$( this ).find( '.galleria-images' ).trigger( 'click' );
				}
			} );

		showGallery( '#gal_' + rel );

//		$( '#gal_' + rel ).find( '.galleria_elements' ).data( 'galleria' ).show( $( '#' + rel + '00jpg' ).attr( 'thumbnum' ) );
		}

	} );

	$( window ).resize( function() {

		var w = $( window ).width() / 2 + 150;
		
		$( '.galleria_box' ).each( function( idx, obj ) {
			$( this ).find( '.galleria_elements' ).data( 'galleria' ).rescale( w, 530, function( gal ) {
			
				$( '.galleria-images' ).click( function( e ) {
			
					openGallery = false;		
			
					var galleriaBox = $( this ).parent().parent().parent().parent().attr( 'class' );
					
					$( '.' + galleriaBox ).animate( {
						width: '1'
					}, 500 );
					$( '.' + galleriaBox ).find( '.galleria-elements' ).animate( {
						right: '-900'
					}, 250 );
					
			    } );
			
			} );
	
			$( this ).find( '.galleria-container' ).css( {
				width: w
			} )
	
			if( $( this ).width() > 1 ) {
				$( this ).css( {
					width: ( w + 70 )
				} );
			}
	
			$( this ).find( '.galleria_elements' ).css( {
				width: w,
				right: '55'
			} );
		} );

	} );

} );
