
$( document ).ready( function( )
{
	InitPopUpMenus( );
	$( '.GalleryRightArrow' ).click( GalleryRightClick );
	$( '.GalleryLeftArrow' ).click( GalleryLeftClick );


	
	var DefaultText = $( '#NewsletterName' ).val( );
	$( '#NewsletterName' ).data( 'DefaultText', DefaultText ).focus(
		function( )
		{
			$( this ).val( '' );
		}).blur(
		function( )
		{
			var Value = $( this ).val( );
			if( Value == '' )
			{
				var DefaultText = $( this ).data( 'DefaultText' );
				$( this ).val( DefaultText );
			}
		});

	var DefaultText = $( '#NewsletterEmail' ).val( );
	$( '#NewsletterEmail' ).data( 'DefaultText', DefaultText ).focus(
		function( )
		{
			$( this ).val( '' );
		}).blur(
		function( )
		{
			var Value = $( this ).val( );
			if( Value == '' )
			{
				var DefaultText = $( this ).data( 'DefaultText' );
				$( this ).val( DefaultText );
			}
	});

	for( x in GalleryNames )
	{
		$( "a[rel=GalGroup_" + GalleryNames[x] + "]" ).fancybox(
			{ 
				'transitionIn' : 	'none',
				'transitionOut' : 	'none',
				'titlePosition' : 	'over',
				'titleFormat' : function( title, currentArray, currentIndex, currentOpts )
				{
					return '<span id="fancybox-title-over">Image ' + ( currentIndex + 1 ) + ' / ' + currentArray.length + ( title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
				
		} );
	}
} );



function InitPopUpMenus( )
{
	$( 'div.PopUpMenu' ).each( function( index )
	{
		$( this ).data( "ClassStatus", $( this ).parent( ).hasClass( 'Selected' ) );
		$( this ).parent( ).hover( function( )
		{
			var Menu = $( this ).addClass( 'Selected' ).children( 'div.PopUpMenu' );
			$( Menu ).show( );
		},
		function( )
		{
			$( this ).children( 'div.PopUpMenu' ).hide( );
			if( !$( this ).data( "ClassStatus" ) )
				$( this ).removeClass( 'Selected' );
		});
	});
}



function GalleryLeftClick( )
{
	// Find the scroller
		var Scroller = $( this ).parent( ).find( ".GalleryScroller" );



	// Stop anything that may be goin' on
		$( Scroller ).stop( true, true );



	// Find current pos
		var PosNow = Math.round( Math.abs( parseInt( $( Scroller ).css( 'marginLeft' ) ) ) );



	// The next left most stop
		var NextStop = 0;
		$( Scroller ).find( "img" ).each(
		function( ) {
			var o = $( this ).position( );
			var x = Math.round( o.left );
			if( x < PosNow )
				NextStop = x;

			if( x > PosNow )
				return false; // Stop iterating
		});



	// Don't move too far
		if( NextStop < 0 )
			NextStop = 0;



	// Move to that pos
		if( PosNow != NextStop )
			$( Scroller ).animate( { 'margin-left': -NextStop } );
	return false;
}



function GalleryRightClick( )
{
	// Find the scroller
		var Frame = $( this ).parent( ).children( ".GalleryFrame" );
		var Scroller = $( this ).parent( ).find( ".GalleryScroller" );



	// Stop anything that may be goin' on
		$( Scroller ).stop( true, true );



	// Find the maximum width
		var LastPic = $( Scroller ).find( "img" ).last( );
		var o = $( LastPic ).position( );
		var MaxPos = o.left + $( LastPic ).width( );
		MaxPos -= $( Frame ).width( );
		if( MaxPos < 0 )
			MaxPos = 0;



	// Find current pos
		var PosNow = Math.round( Math.abs( parseInt( $( Scroller ).css( 'marginLeft' ) ) ) );



	// The next right most stop
		var NextStop = 0;
		$( $( Scroller ).find( "img" ).get( ).reverse( ) ).each(
		function( ) {
			var o = $( this ).position( );
			var x = Math.round( o.left );
			if( x <= PosNow )
				return false; // Stop iterating
			NextStop = x;
		});



	// Don't move too far
		if( NextStop > MaxPos )
			NextStop = MaxPos;



	// Move to that pos
		if( PosNow != NextStop )
			$( Scroller ).animate( { 'margin-left': -NextStop } );
	return false;
}
