


/*--- ------------------------------------------------------------------------- ----
	
	File:		form.js
	Author:		Ben Nadel
	Desc:		
				
	Sample Code:
		
				N/A
				
	Update History:
	
				04/20/2006 - Ben Nadel
				Handles form related javascript.
	
----- --------------------------------------------------------------------//// ---*/

// Basically a short hand for changing the location of the current page 
// when leaving a data form without saving.
function Cancel( strLocation ){
	window.location.href = strLocation;
}
/*--- ------------------------------------------------------------------------- ----
	
	File:		custom.js
	Author:		Ben Nadel
	Desc:		Custom JS functions.
				
	Sample Code:
		
				N/A
				
	Update History:
	
				09/19/2006 - Ben Nadel
				Built first run of the page.
	
----- --------------------------------------------------------------------//// ---*/

function LaunchSkinSpiderCodeViewer( strCodeModule, strFileRequest ){
	var objViewer = null;
	var strUrl = (strWebRoot + "index.cfm?dax=skinspider.viewcode&code_module=" + strCodeModule);
	
	// Check to see if we have a file request.
	if (strFileRequest != null){
		strUrl = (strUrl + "&file_request=" + strFileRequest);
	}
	
	// Open the window.
	objViewer = window.open(
		strUrl,
		"SkinSpiderCodeViewer",
		"resizable,scrollbars"
		);

}

$(
	function(){
		// Get DOM references.
		var jPhotoArea = $( "#site-photography" );
		var jPhoto = $( "#site-photography-photo" );
		var jPhotoDetails = $( "#site-photography-details" );
		var jPhotoDescription = $( "#site-photo-details-description" );
		var jPhotoLink = $( "#site-photo-details-link" );
		var jPhotoContacts = $( "#site-photo-details-contacts" );
		var jPhotoLeft = $( "#site-photography-left" );
		var jPhotoRight = $( "#site-photography-right" );
		var jAjaxLoader = $( "#site-photography-loader" );
		
		// Keep track of properties of the photo details.
		var objBottomProperties = {
			Min: "-98px",
			Max: "0px"
			};
		
		// The timer for mouseing out of the site photo area.
		var objMouseOutTimeout = null;
		
		// Keep track of which direction we are animating in.
		var objIsAnimatingSitePhotoDetails = {
			Show: false,
			Hide: false
			};
			
		// Keep track of site photo XHR request.
		var objSitePhotoRequest = null;
		
		
		// I show the site photo details (if necesssary).
		function ShowSitePhotoDetails(){
			// We only want to animate the Show if we are:
			// 1. Not currently showing the photo details.
			// 2. Currently hiding the photo details.
			if (
				!objIsAnimatingSitePhotoDetails.Show ||
				objIsAnimatingSitePhotoDetails.Hide
				){
				
				// Check to see if we need to stop any animation.
				if (objIsAnimatingSitePhotoDetails.Hide){
					jPhotoDetails.stop();
				}
				
				// Flag the animations.
				objIsAnimatingSitePhotoDetails.Show = true;
				objIsAnimatingSitePhotoDetails.Hide = false;
				
				// Stop any existing animation and show the details.
				jPhotoDetails.animate(
					{
						bottom: objBottomProperties.Max
					},
					{
						duration: 150,
						
						// When complete, flag all animations as being done.
						complete: function(){
							objIsAnimatingSitePhotoDetails.Show = false;
							objIsAnimatingSitePhotoDetails.Hide = false;
						}
					}
					);
			}
		}
		
		
		// I hide the site photo details.
		function HideSitePhotoDetails(){
			// When mousing down, set the animation flags.
			objIsAnimatingSitePhotoDetails.Show = false;
			objIsAnimatingSitePhotoDetails.Hide = true;
		
			// Slide details down.
			jPhotoDetails
				.animate(
					{
						bottom: objBottomProperties.Min
					},
					{
						duration: 100,
						
						// When complete, flag all animations as being done.
						complete: function(){
							objIsAnimatingSitePhotoDetails.Show = false;
							objIsAnimatingSitePhotoDetails.Hide = false;
						}
					}
					)
				.fadeTo( 1, 1 )
			;
		}
		
		
		// I show the prev/next links.
		function ShowPrevNextArrows(){
			jPhotoLeft.show();
			jPhotoRight.show();
		}
		
		
		// I hide the prev/next links.
		function HidePrevNextArrows(){
			jPhotoLeft.hide();
			jPhotoRight.hide();
		}
		
		
		// I handle the mouse over functionality for both the photo and 
		// the photo details as they are going to act in the same fashion.
		function SitePhotoMouseOverHandler(){
			// Clear any mouse out time out so that our details don't disaapear.
			clearTimeout( objMouseOutTimeout );
			
			// Show the photo details.
			ShowSitePhotoDetails();
			
			// Show prev / next arrows.
			ShowPrevNextArrows();
		}
		
		
		// I handle the mouse out functionality for both the photo and 
		// the photo details as they are going to act in the same fashion.
		function SitePhotoMouseOutHandler(){
			// Because of the way that events happen, set a timeout for this mouse
			// out action. This will give the mouse-over action a change to 
			// cancel the bubble.
			objMouseOutTimeout = setTimeout(
				function(){
					HideSitePhotoDetails();
					HidePrevNextArrows();
				},
				100
				);
		}
		
		
		// I get the next site photo.
		function GetNextSitePhoto(){
			// Get next photo.
			GetNewSitePhotoDetails( "getnext" );
		}
		
		
		// I get the previous site photo.
		function GetPreviousSitePhoto(){
			// Get next photo.
			GetNewSitePhotoDetails( "getprev" );
		}
		
		
		// I get the new site photo (prev and next) base on given action.
		function GetNewSitePhotoDetails( strAction ){
			// Check to see if there is a current request. If so, then return out
			// as we don't want to do anything until the request returns.
			if (objSitePhotoRequest){
				return;
			}
			
			// Show ajax loader.
			jAjaxLoader.fadeIn( 100 );
			
			// Get the new photo (be sure to store request).
			objSitePhotoRequest = $.ajax(
				{
					method: "get",
					url: (strWebRoot + "index.cfm"),
					data: {
						dax: "sitephotos.api",
						action: strAction,
						index: jPhoto.attr( "rel" )
						},
					dataType: "json",
					
					// Handle the response.
					success: function( objResponse ){
						// Check to see if the response was successful.
						if (objResponse.SUCCESS){
							SetNewSitePhotoDetails( objResponse.DATA );
						} else {
							alert( "There was an error getting the photo" );
						}
						},
						
					// If the request errored, something went seriously wrong!
					error: function(){
						alert( "There was an error getting the photo" );
					},
					
					complete: function(){
						// Clear the request no matter what.
						objSitePhotoRequest = null;
						
						// Hide ajax loader.
						jAjaxLoader.stop().fadeOut();
					}
				}
				);
		}
		
		
		// I take there returned phot and move the details into the DOM. 
		function SetNewSitePhotoDetails( objPhoto ){
			// Build up the new ALT tag.
			var strAlt = objPhoto.DESCRIPTION;
			
			// Add contact names to alt.
			$.each(
				objPhoto.CONTACTS,
				function( intI, objContact ){
					strAlt += (" " + ((intI == 0) ? "" : "and ") + objContact.NAME);
				}
				);
		
			// Set the photo src.
			jPhoto.attr( 
				{
					src: jPhoto.attr( "src" ).replace( new RegExp( "[^\\\\/]+$", "i" ), objPhoto.SRC ),
					alt: strAlt.replace( new RegExp( "\"", "g" ), "'" ),
					rel: objPhoto.INDEX
				}
				);
				
			// Set the description.
			jPhotoDescription.html( objPhoto.DESCRIPTION );
			
			// Set the photo link.
			jPhotoLink.attr(
				"href",
				jPhotoLink.attr( "href" ).replace( new RegExp( "\\d+$", "i" ), objPhoto.INDEX )
				);
				
			// Update the contacts list. In order to do that, we have to build up the elements.
			// Start out by clearing what's there.
			jPhotoContacts.empty();
			
			// Loop over each contact.
			$.each(
				objPhoto.CONTACTS,
				function( intI, objContact ){
					var arrParts = [];
				
					// Add start LI (with potential class).
					arrParts.push( "<li " + ((intI == 0) ? "class='first'" : "" ) + ">" );
					
					// Add name.
					arrParts.push( "<strong>" + objContact.NAME + "</strong><br />" );
				
					// Add website if it exists.
					if (objContact.URL.length){
						arrParts.push( "<a href=\"" + objContact.URL + "\" target=\"_blank\">Website</a>" );
					}
					
					// Add pipe if necessary.
					if (objContact.URL.length && objContact.TWITTER.length){
						arrParts.push( " <span> | </span> " );
					}
					
					// Add twitter if it exists.
					if (objContact.TWITTER.length){
						arrParts.push( "@<a href=\"http://www.twitter.com/" + objContact.TWITTER + "\" target=\"_blank\">" + objContact.TWITTER + "</a>" );
					}
				
					// Add closing LI.
					arrParts.push( "<br /></li>" );
					
					// Add item to list.
					jPhotoContacts.append( arrParts.join( "" ) );
				}
				);
		}
		
		
		// Initialize the left photo arrow.
		jPhotoLeft
			.attr( "href", "javascript:void( 0 )" )
			.click(
				function(){
					GetPreviousSitePhoto();
					return( false );
				}
				)
		;
		
		// Initialize the right photo arrow.
		jPhotoRight
			.attr( "href", "javascript:void( 0 )" )
			.click(
				function(){
					GetNextSitePhoto();
					return( false );
				}
				)
		;
		
		// Initialized mouse over / out for left / right.
		$ ( [] ).add( jPhotoLeft ).add( jPhotoRight )
			.mouseover(
				function(){
					jPhotoDetails.fadeTo( "fast", .3 );
				}
				)
			.mouseout(
				function(){
					jPhotoDetails.fadeTo( "fast", 1 );
				}
				)
		;
		
		
		// I handle the mouse over of the photo area.
		$( [] ).add( jPhotoArea ).add( jPhotoDetails ).add( jPhotoLeft ).add( jPhotoRight ).mouseover(
			function(){
				SitePhotoMouseOverHandler();
				return( false );
			}
			);
		
		// I handle the mouse out of the photo area.
		$( [] ).add( jPhotoArea ).add( jPhotoDetails ).add( jPhotoLeft ).add( jPhotoRight ).mouseout(
			function(){
				SitePhotoMouseOutHandler();
				return( false );
			}
			);
	}
	);



// Load last twitter when the page can be used.
$(
	function(){
		var jTwitter = $( "#recent-twitter-status" );
		var jTwitterText = jTwitter.find( "span.twitter-text" );
		var jTwitterDate = jTwitter.find( "span.twitter-date" );
		
		// Get recent twitter status.
		$.ajax(
			{
				method: "get",
				url: (strWebRoot + "index.cfm?dax=twitter"),
				dataType: "json",
				
				// Load the twitter status.
				success: function( objResponse ){
					// Check to see if response was successful.
					if (objResponse.SUCCESS){
					
						// Load the text.
						var strText = objResponse.DATA.TEXT;
						// var strText = "@SirRawlins You can record voice in the video capture."; // 55
						// var strText = "@CFJamesAllen Jing also lets you grab screen images and then annotate them (arrows, text, etc.) really awesome."; // 112
						// var strText = "@SirRawlins Let me know if you need any help getting FTP working off of Jing - it was a bit funky the first time I tried."; // 122
						// var strText = "@cfjedimaster Ray, try a different web site; I have found that some sites (moviefone) don't allow purchased when others do (fandango)"; // 134
						// var strText = "@SirRawlins You can record voice in the video capture.";
					
						// Load status. NOTE: text() method will escape HTML characters!!
						jTwitterText.text( strText );
						// jTwitterDate.html( " (" + objResponse.DATA.DATEPOSTED + ")" );
					
					} else {
					
						// Show error message.
						jTwitterText.text( "Could not load data..." );
					
					}
				},
				
				// If AJAX fails, just put in dummy text.
				error: function(){
					jTwitterText.text( "Could not load data..." );
				}
			}
			);
		
	}
	);





