$( document ).ready( function()
{
	if( $.browser.msie )
	{
		$( "ul.navigation li" ).hover(
			function() { $( this ).addClass( "hover" ); },
			function() { $( this ).removeClass( "hover" ); }
		);

		$( ".type-filter fieldset" ).hover(
			function() { $( this ).addClass( "hover" ); },
			function() { $( this ).removeClass( "hover" ); }
		);
	}

	CatalogueVisuals.Initialize();
	Portfolio.Initialize();
});

var DemoReel = new function()
{
	this.Initialize = function( reelURL )
	{
		flowplayer( "demo-reel", "/inc/interactive/flowplayer/flowplayer-3.1.5.swf", {
			clip: {
				url: reelURL,
				autoPlay: false
			}
		});

		$( ".watch-demo-reel" ).click( function()
		{
			flowplayer( "demo-reel" ).play();
		});
	};
};

var Portfolio = new function()
{
	this.Initialize = function()
	{
		this.BindTypeImage();
		this.BindTypeVideo();
		this.BindTypeFlash();
	};

	this.BindTypeImage = function()
	{
		if( $( "ul .gallery a.type\\=image" ).length > 0 )
		{
			$( "ul .gallery a.type\\=image" ).overlay(
			{ 
				target: '#gallery'
			}).gallery(
			{
				speed: 300
			});
		}
	};

	this.BindTypeVideo = function()
	{
		if( $( "ul .gallery a.type\\=video" ).length > 0 )
		{
			$( "#video-container" ).flowplayer( "/inc/interactive/flowplayer/flowplayer-3.1.5.swf" );

			$( "ul .gallery a.type\\=video" ).click( function( e )
			{
				var params = Portfolio.GetParameters( $( this ) );

				$( "#video, #video-container" ).css( "width", params[ "width" ] + "px" );
				$( "#video, #video-container" ).css( "height", ( parseInt( params[ "height" ] ) + 26 ) + "px" );

				$.data( $( "#video" ).get( 0 ), "href", $( this ).attr( "href" ) );
				$.data( $( "#video" ).get( 0 ), "duration", params[ "duration" ] );

				$( "#video" ).overlay(
				{
					onLoad: function( e )
					{
						var videoURL = $.data( $( "#video" ).get( 0 ), "href" );
						var duration = $.data( $( "#video" ).get( 0 ), "duration" );

						if( duration != undefined )
						{
							var clipConfig = { url: videoURL, duration: duration };
						}
						else
						{
							var clipConfig = { url: videoURL };
						}

						$( "#video-container" ).flowplayer( 0 ).load();
						$( "#video-container" ).flowplayer( 0 ).play( clipConfig );
					}, 

					onClose: function()
					{
						$( "#video-container" ).flowplayer( 0 ).unload();
					},

					api: true

				}).load();

				e.preventDefault();
			});
		}
	};

	this.BindTypeFlash = function()
	{
		if( $( "ul .gallery a.type\\=flash" ).length > 0 )
		{
			$( "ul .gallery a.type\\=flash" ).click( function( e )
			{
				var params = Portfolio.GetParameters( $( this ) );

				$( "#flash, #flash-container" ).css( "width", params[ "width" ] + "px" );
				$( "#flash, #flash-container" ).css( "height", params[ "height" ] + "px" );

				$.data( $( "#flash" ).get( 0 ), "href", $( this ).attr( "href" ) );

				$( "#flash" ).overlay(
				{
					onLoad: function( e )
					{
						var flashURL = $.data( $( "#flash" ).get( 0 ), "href" );
						$( "#flash-container" ).flashembed( { src: flashURL, wmode: "opaque" } );
					}, 

					onClose: function()
					{
						$( "#flash-container" ).find( "object" ).each( function( i )
						{
							$( this ).remove();
						});
					},

					api: true

				}).load();

				e.preventDefault();
			});
		}
	};

	this.GetParameters = function( scope )
	{
		var params = new Array();
		var keys = new Array( "type", "width", "height", "duration" );

		for( var key in keys )
		{
			var param = keys[ key ];

			if( ( pos = scope.attr( "class" ).indexOf( param + "=" ) ) != -1 )
			{
				params[ param ] = scope.attr( "class" ).substr( pos + param.length + 1 ).split( " " )[ 0 ];
			}
		}

		return( params );
	};
};

var CatalogueVisuals = new function()
{
	this.Initialize = function()
	{
		$( "#catalogue-items img" ).live( "mouseover", function( e )
		{
			var tip = $( this ).attr( "title" );
			var imagePath = $( this ).attr( "src" ).split( "/" );
			var image = imagePath[ imagePath.length - 1 ];

			if( $( "#tooltip" ).length == 0 )
			{
				$( "body" ).append( '<div id="tooltip"><img /></div>' );
				$( "#tooltip" ).hide();
			}

			$( "#tooltip img" ).attr( "src", "/inc/images/catalogue/raw/" + image );
			$( "#tooltip" ).css( CatalogueVisuals.GetTooltipCSSPosition( e, $( "#tooltip" ) ) );
			$( "#tooltip" ).fadeIn( 200 );

		}).live( "mousemove", function( e )
		{  
			$( "#tooltip" ).css( CatalogueVisuals.GetTooltipCSSPosition( e, $( "#tooltip" ) ) );

		}).live( "mouseout", function()
		{  
			$( "#tooltip" ).stop( true, true ).hide();
		});  
	};

	this.GetTooltipCSSPosition = function( e, tooltip )
	{
		var margin = 10;
		var distanceFromCursorX = 20;
		var distanceFromCursorY = 10;

		var y = e.pageY - ( tooltip.outerHeight( true ) / 3 );
		var rightOfCursorX = e.pageX + distanceFromCursorX;
		var leftOfCursorX = e.pageX - tooltip.outerWidth( true ) - distanceFromCursorX;
		var centerOfCursorX = e.pageX - ( tooltip.outerWidth( true ) / 2 );
		var isOffRight = rightOfCursorX + tooltip.outerWidth( true ) + margin > $( window ).width();
		var isOffLeft = leftOfCursorX < margin;

		y = Math.min( y, $( window ).height() - tooltip.outerHeight( true ) - margin );
		y = Math.max( y, margin );

		var placeLeftOfCursor = false;
		var placeRightOfCursor = false;
		var placeCenterOfCursor = false;
		
		if( isOffRight && !isOffLeft )
		{
			placeLeftOfCursor = true;
		}
		else if( isOffLeft && !isOffRight )
		{
			placeRightOfCursor = true;
		}
		else
		{
			placeRightOfCursor = true;
		}

		if( placeLeftOfCursor )
		{
			var x = leftOfCursorX;
		}
		else if( placeRightOfCursor )
		{
			var x = rightOfCursorX;
		}	
		else
		{
			var x = centerOfCursorX;
		}

		return( { top: y, left: x } );
	};
};
