var facebookInitialized = false;

$( document ).ready( function()
{
	$( "#dock" ).Fisheye(
		{
			maxWidth: 45,
			items: 'li',
			itemsText: 'span',
			container: '#dock-container',
			itemWidth: 48,
			proximity: 30,
			alignment : 'center',
			valign: 'bottom',
			halign : 'center'
		}
	)
	
	// HOME PAGE EVENTS SCROLLER
	$( "#events" ).scrollable(
	{
		size: 1,
		items: '.events-reel',
		easing: 'swing',
		clickable: false,
		next: '.next',
		prev: '.prev',
		onSeek: function() 
		{ 			
			if( this.getPageIndex() == 0 )
			{
				$( ".prev" ).css({ backgroundPosition: "-9999px 0px" });	
			}
			else
			{
				$( ".prev" ).css({ backgroundPosition: "0px 0px" });
			}
			
			if( this.getPageIndex() == this.getPageAmount() - 1 )
			{
				$( ".next" ).css({ backgroundPosition: "-9999px 0px" });
			}
			else
			{
				$( ".next" ).css({ backgroundPosition: "0px 0px" });
			}
    	}
	});
	
	// HOME PAGE HERO SPOT
	var timer = setInterval("swapImages()", 10000);
	
	$( "#hero div" ).each( function( i )
	{
		$( "#current-hero" ).append( "<li>" + ( i + 1 ) + "</li>" );
	});
	
	$( "#current-hero li:first" ).addClass( "active" );
	
	$( "#current-hero li" ).click( function()
	{		
		clearInterval( timer );
		timer = setInterval("swapImages()", 10000);
		
		var position = $( "#current-hero li" ).index( this );
		
		$( "#hero div" ).hide();
		$( "#hero div:eq(" + position + ")" ).fadeIn();				
	
		$( "#current-hero li" ).removeClass( "active" );
		$( this ).addClass( "active" );
	});
	
	// PROFILE PAGE TABS
	$( "ul.profile-tabs" ).tabs( ".profile-content > div",
	{
		current: "active",
		effect: "ajax",
		onClick: function()
		{
			InitFacebook();
		}
	}).history();

	$( "a.gallery" ).live( "click", function( e )
	{
		e.preventDefault();

		var galleryName = $( this ).attr( "href" ).replace( window.location.protocol + "//" + window.location.hostname + window.location.pathname.replace( "/index.jsf", "" ), "" );
		//var galleryName		= $( this ).attr( "href" );
		var api				= $( "ul.profile-tabs" ).tabs();

		api.click( galleryName );
		document.location.hash = galleryName;
	});
	
	if( $( "ul.profile-tabs > li > a" ).length > 0 )
	{
					$( "ul.profile-tabs > li > a" ).onHash( function( e, hash )
					{
									if( hash.indexOf( $( "input[name='galleries-link']" ).val() ) != -1 )
									{
													$( "ul.profile-tabs > li > a[href='" + $( "input[name='photos-link']" ).val() + "']" ).addClass( "active" );
									}
					});
	}
	
	

	// PROFILE YOUTUBE
	$( "#youtube-playlist li" ).live( "click", function()
	{
					var video = $( this ).find( "input" ).attr( "value" );
					var title = $( this ).find( "p" ).text();
					var videoID = $( this ).attr( "class" ).split( " " )[ 0 ];

					var width = $( "#youtube object" ).attr( "width" );
					var height = $( "#youtube object" ).attr( "height" );
					$( "#youtube" ).empty();
					$( "#youtube" ).append( "<object width='" + width + "' height='" + height + "' type='application/x-shockwave-flash' data='" + video + "'><param name='movie' value='" + video + "'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param></object>" );

					$( "input[ name='name' ]" ).val( videoID );
					
					$( "#youtube-title" ).text( title );
					
					$( "#videos > .share-video" ).attr( "href", "http://www.facebook.com/share.php?u=" + video );

					//document.location.hash = videoID;

					// uncomment below line on phase 2
					//RefreshFacebookConnect( "video" );
	});

	// PROFILE PHOTOS
	$( "#photo-playlist li a" ).live( "click", function( e )
	{
		e.preventDefault();

		$( "#photo > img" ).attr( "src", $( this ).attr( "href" ) );
		$( "#photo-title" ).text( $( this ).parent().find( "p" ).text() );
		$( "input[ name='name' ]" ).val( $( this ).closest( "li" ).attr( "class" ).split( " " )[ 0 ] );

		var sharephoto = $( "#photo > img" ).attr( "src" );
		
		$( "#photos > .share-photo" ).attr( "href", "http://www.facebook.com/share.php?u=" + location.protocol + "//" + location.host + sharephoto );

		// uncomment below line on phase 2
		//RefreshFacebookConnect( "photo" );
	});

	// INITIALIZE YOUTUBE AND FACEBOOK
	if( $( "#youtube-playlist" ).length > 0 )
	{
		var selector = "#youtube-playlist li." + document.location.hash.replace( "#", "" );
		
		if( $( selector ).length > 0 )
		{
			$( selector ).click();
		}
	}
	else
	{
		//InitFacebook();
	}

	InitFacebook(); // uncomment InitFacebook just above on phase 2
});

function InitFacebook()
{
	FB.init( $( "input[ name='facebookApiKey' ]" ).val(), "/cpo/mc/aboutus/community/catchair/inc/interactive/xd_receiver.html" );
	facebookInitialized = true;
}

function RefreshFacebookConnect( type )
{
	var fbConnect	= $( "#facebook-connect" ).children().eq( 0 );
	var fbIframe	= $( "#facebook-connect iframe" );

	var tag			= $( "input[ name='tag' ]" ).val();
	var name		= $( "input[ name='name' ]" ).val();
	var oldID		= fbConnect.attr( "xid" );

	if( type == "video" )
	{
		var newID	= "catchair--" + tag + "--video--" + name;
	}
	else
	{
		var gallery = $( "input[ name='gallery' ]" ).val();
		var newID	= "catchair--" + tag + "--photo--" + gallery + "--" + name;
	}

	fbConnect.attr( "xid", newID );

	if( ! facebookInitialized )
	{
		InitFacebook();
	}

	if( fbIframe.length > 0 )
	{
		var updatedSrc = fbIframe.attr( "src" ).replace( "xid=" + oldID, "xid=" + newID );

		fbIframe.attr( "src", updatedSrc );
	}
}

function swapImages()
{
	var position = $( "#hero div" ).index( $( "#hero div:visible" ) );
	var next = position + 1;
	
	if( ( position + 1 ) == $( "#hero div" ).length )
	{
		next = 0;	
	}
	
	$( "#hero div:eq(" + position + ")" ).fadeOut();
	$( "#hero div:eq(" + next + ")" ).fadeIn();
	
	$( "#current-hero li:eq(" + position + ")" ).removeClass( "active" );
	$( "#current-hero li:eq(" + next + ")" ).addClass( "active" );
}
