var _ie = false;
if( navigator.appName.indexOf("Microsoft Internet") != -1 ) _ie = true;
var _ie8 = false;
if( $.browser.msie && $.browser.version.substr(0,1) == '8') _ie8 = true;


$(function(){
	
	/*================================= Calendar Functions =================================*/
	
	var _calNextClass		= '';
	var _calPrevClass		= '';
	
	var _calLoadingContent	= '';
	
	var _date				= new Date();
	var _dateNext			= new Date( _date.getFullYear(), _date.getMonth()+1 );
	var _datePrev			= new Date( _date.getFullYear(), _date.getMonth()-1 );
	
	var _dateToday = String( _date.getFullYear() + '/' + Number( _date.getMonth()+1 ) + '/' + _date.getDate() );
	var _dateNextMonth = String( _dateNext.getFullYear() + '/' + Number( _dateNext.getMonth()+1 ) + '/' + _dateNext.getDate() );
	var _datePrevMonth = String( _datePrev.getFullYear() + '/' + Number( _datePrev.getMonth()+1 ) + '/' + _datePrev.getDate() );
	
	var _startDate;
	var _endDate;
	var _nextBookedDate;
	var _hoverDate;
	var _dateClickCount		= 0;
	
	var _startDateBtn;
	var _endDateBtn;
	
	var _selectClassName	= 'calBtnSelect';
	var _hoverClassName		= 'calBtnHover';
	var _unavailClassName	= 'disabled';
	var _dateBtnClass		= '#resCalendarCont .calendarTable tr td a';
	
	var _selectColor		= '#FFFFFF';
	var _unselectColor		= '#6D6D51';
	var _disableColor		= '#AAA';
	
	var _selectBgColor		= '#B4953D';
	var _unselectBgColor	= '#E5E6DE';
	var _selectHoverBgColor	= '#A08538';
		
	function _getNewCal( obj ){
		$( '#resCalendarCont' ).fadeOut( function () {
			$( this ).html( _calLoadingContent );
			$( this ).fadeIn( function () {
				$.ajax({
					url: 'inc/ajaxRequests.php',
					cache: false,
					type: "GET",
					//data: "name=John&location=Boston",
					data: obj,
					success: function ( data ) {
						//alert(data);
						/*
						 * overwrite .twoThree.left.focus form's innerHtml that has: table.contactForm in it.
						 * hide loading image
						 * fadeIn new content
						 */
						$( '#resCalendarCont' ).fadeOut( function () {
							$( '#resCalendarCont' ).html( data );
							$( '#resCalendarCont' ).fadeIn();
							_initCalValuesAndClasses();
							_addMouseEventListeners();
						});
						
						//alert('Load was performed.');
					},
					error: function ( data ) {
						alert( 'Load was not performed.' );
					}
				});				
			});
		});
		
	}
	
	
	
	function _findNextBookedDate( startDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > startDate.valueOf() && !$( this ).data( 'available' ) ) 
			{
				_nextBookedDate = thisDate;
				return false;
			}
			else _nextBookedDate = null;
			
		});
		return _nextBookedDate;
	}
	
	function _disableAfterDate( afterDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > afterDate.valueOf() ) 
			{
				if( !$( this ).data( 'booked' ) ) $( this ).data( 'available', false );
				if( !$( this ).hasClass( _unavailClassName ) )
				{
					$( this ).addClass( _unavailClassName );
					//$( this ).animate( { color: _disableColor }, 500 );
				}
			}
		});
	}
	
	function _disableBeforeDate( beforeDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() < beforeDate.valueOf() ) 
			{
				if( !$( this ).data( 'booked' ) ) $( this ).data( 'available', false );
				if( !$( this ).hasClass( _unavailClassName ) )
				{
					$( this ).addClass( _unavailClassName );
					//$( this ).animate( { color: _disableColor }, 500 );
				}
			}
		});
	}
	
	function _enableAfterDate( afterDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > afterDate.valueOf() && !$( this ).data( 'booked' ) ) 
			{
				$( this ).data( 'available', true );
				if( $( this ).hasClass( _unavailClassName ) )
				{
					$( this ).removeClass( _unavailClassName );
					//$( this ).animate( { color: _unselectColor }, 500 );
				}
			}
		});
	}
	
	function _enableBeforeDate( beforeDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			//console.log('booked: '+$( this ).data( 'booked' ) );
			if( thisDate.valueOf() < beforeDate.valueOf() && !$( this ).data( 'booked' ) ) 
			{
				$( this ).data( 'available', true );
				if( $( this ).hasClass( _unavailClassName ) )
				{
					$( this ).removeClass( _unavailClassName );
					//$( this ).animate( { color: _unselectColor }, 500 );
				}
			}
		});
	}
	
	function _selectBetween( startDate, endDate )
	{
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() >= startDate.valueOf() && thisDate.valueOf() <= endDate.valueOf() ) 
			{
				if( !$( this ).hasClass( _selectClassName ) ) $( this ).addClass( _selectClassName );
			} else {
				if( $( this ).hasClass( _selectClassName ) )
				$( this ).removeClass( _selectClassName );
				if( $( this ).hasClass( _hoverClassName ) ) $( this ).removeClass( _hoverClassName );
			}
		});
	}
	
	function _unselectBetween( startDate, endDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > startDate.valueOf() && thisDate.valueOf() <= endDate.valueOf() )
			{
				if( $( this ).hasClass( _selectClassName ) ) $( this ).removeClass( _selectClassName );
				if( $( this ).hasClass( _hoverClassName ) ) $( this ).removeClass( _hoverClassName );
			}
		});
	}
	
	function _hoverBetween( startDate, endDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > startDate.valueOf() && thisDate.valueOf() < endDate.valueOf() ) 
			if( !$( this ).hasClass( _hoverClassName ) ) $( this ).addClass( _hoverClassName );
		});
	}
	
	function _unhoverAfter ( afterDate ){
		$( _dateBtnClass ).each( function ( i ) {
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( thisDate.valueOf() > afterDate.valueOf() ) 
			if( $( this ).hasClass( _hoverClassName ) ) $( this ).removeClass( _hoverClassName );
		});
	}
	
	function _startDateString( s ){
		$( 'input#startDate' ).attr( 'value', s );
	}
	
	function _endDateString( s ){
		$( 'input#endDate' ).attr( 'value', s );
	}
	
	function _initCalValuesAndClasses(){
		// Initial a tag Setup
		$( _dateBtnClass ).each( function ( i ) {
			// Set data: date
			$( this ).data( 'date', $( this ).attr( 'rel' ) );
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			
			// Set data: available - based on unavailable class, and today's date
			if( $( this ).hasClass( _unavailClassName ) ){
				$( this ).data( 'available', false );
				$( this ).data( 'booked', true );
				if( !$( this ).hasClass( _unavailClassName ) ) $( this ).addClass( _unavailClassName );
				$( this ).attr( 'title', 'Date unavailable' );
			}else if( thisDate.valueOf() < _date.valueOf() ){
				$( this ).data( 'available', false );
				$( this ).data( 'booked', true );
				if( !$( this ).hasClass( _unavailClassName ) ) $( this ).addClass( _unavailClassName );
				$( this ).attr( 'title', 'Date Prior to Today\'s date' );
			}else{
				$( this ).data( 'available', true );
				$( this ).data( 'booked', false );
			}
			
			// Set data: available - based on start date and next booked date
			if( _startDate ){
				if(
				thisDate.valueOf() < _startDate.valueOf() ||
				( _nextBookedDate && thisDate.valueOf() > _nextBookedDate.valueOf() )
				){
					$( this ).data( 'available', false );
					if( !$( this ).hasClass( _unavailClassName ) ) $( this ).addClass( _unavailClassName );
					$( this ).attr( 'title', 'Date unavailable' );
				}
			}
			
			// Set data: select - based on start date and end date
			if( _startDate ){
				if( thisDate.valueOf() == _startDate.valueOf() )
				{
					$( this ).data( 'select', true );
					_startDateBtn		= $( this );
					if( !$( this ).hasClass( _selectClassName ) )
					{
						$( this ).addClass( _selectClassName );
						//$( this ).animate( { backgroundColor: _selectBgColor, color: _selectColor }, 500 );
					}
				}
			}
			
			if( _endDate ){
				if( thisDate.valueOf() == _endDate.valueOf() ){
					$( this ).data( 'select', true );
					_endDateBtn		= $( this );
					if( !$( this ).hasClass( _selectClassName ) ) $( this ).addClass( _selectClassName );
					_selectBetween( _startDate, _endDate );
				}
			}
		});
	}
	
	function _addMouseEventListeners(){
		$( _dateBtnClass ).click( function ( event ) {
			event.preventDefault();
			
			if( !$( this ).data( 'available' ) ) return;
			
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			
			// On first click
			if( _dateClickCount == 0 ){
				// Set start dates
				_startDate			= new Date();
				_startDateString( $( this ).data( 'date' ) );
				_startDate.setTime( thisDate.valueOf() );
				_startDateBtn		= $( this );
				// Disable dates
				var nextBookedDate	= _findNextBookedDate( _startDate );
				if( nextBookedDate ) _disableAfterDate( nextBookedDate );
				_disableBeforeDate( _startDate );
				// Select This
				$( this ).data( 'select', true );
				if( !$( this ).hasClass( _selectClassName ) ) $( this ).addClass( _selectClassName );
				// Set Titles of date buttons
				$( _dateBtnClass ).each( function () {
					if( !$( this ).data( 'booked' ) )
					{
						if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Select your Departure Date' );
						else $( this ).attr( 'title', 'Date unavailable' );
					}
				});
				_startDateBtn.attr( 'title', 'Reset Start Date' );
				// Set instuctions for user
				$( '.calInstructions' ).html( 'Select Departure Date' );
				// Change sumbit button
				$( 'input#submitStep2' ).attr( { 'readonly':'true', 'title':'Select Departure Date' } ).addClass( 'disabled' );
				// Set next step
				_dateClickCount		= 1;
				return;
			}
			// On second click
			if( _dateClickCount == 1 ){
				// 1. reset start date //
				if( thisDate.valueOf() == _startDate.valueOf() )
				{
					// Reset this button
					$( this ).data( 'select', false );
					if( $( this ).hasClass( _selectClassName ) ) $( this ).removeClass( _selectClassName );
					// Enable dates
					if( _nextBookedDate ) _enableAfterDate( _nextBookedDate );
					_enableBeforeDate( _startDate );
					// Reset start dates
					_startDateBtn		= null;
					_startDate			= null;
					_startDateString( '' );
					// Set Titles of date buttons
					$( _dateBtnClass ).each( function () {	
						if( !$( this ).data( 'booked' ) )
						{
							if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Select your Departure Date' );
							else $( this ).attr( 'title', 'Date unavailable' );
						}
					});
					// Change sumbit button
					$( 'input#submitStep2' ).attr( { 'title':'Select Arrival Date' } );
					// Set instructions for user
					$( '.calInstructions' ).html( 'Select Arrival Date' );
					// Set next step
					_dateClickCount		= 0;
				}
				else // 2. set end date //
				{
					// Select this
					$( this ).data( 'select', true );
					if( !$( this ).hasClass( _selectClassName ) ) $( this ).addClass( _selectClassName );
					// Set end dates
					_endDateBtn			= $( this );
					_endDate			= new Date();
					_endDate.setTime( thisDate.valueOf() );
					_endDateString( $( this ).data( 'date' ) );
					// Select inbetween Dates
					_selectBetween( _startDate, _endDate );
					// Set Titles of date buttons
					$( _dateBtnClass ).each( function ( i ) {
						if( !$( this ).data( 'booked' ) )
						{
							if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Reselect your Departure Date' );
							else $( this ).attr( 'title', 'Date unavailable' );
						}
					});
					_startDateBtn.attr( 'title', 'Reset Dates' );
					_endDateBtn.attr( 'title', 'Reset End Date' );
					// Set instructions for user
					$( '.calInstructions' ).html( 'Check your booking' );
					// Change sumbit button
					if( $( 'input#submitStep2' ).hasClass( 'disabled' ) ) $( 'input#submitStep2' ).attr( { 'readonly':'', 'title':'Check your dates are available' } ).removeClass( 'disabled' );
					// Set next step
					_dateClickCount		= 2;
				}
				return;
			}
			// On third click
			if( _dateClickCount == 2 )
			{
				// 1. reset start and end date //
				if( thisDate.valueOf() == _startDate.valueOf() )
				{
					// Reset start and end button
					$( _startDateBtn ).data( 'select', false );
					$( _endDateBtn ).data( 'select', false );
					if( $( _startDateBtn ).hasClass( _selectClassName ) ) $( _startDateBtn ).removeClass( _selectClassName );
					if( $( _endDateBtn ).hasClass( _selectClassName ) ) $( _endDateBtn ).removeClass( _selectClassName );
					// Reset inbetween Dates
					_unselectBetween( _startDate, _endDate );
					// Enable dates
					if( _nextBookedDate ) _enableAfterDate( _nextBookedDate );
					_enableBeforeDate( _startDate );
					// Reset start and end dates
					_startDateBtn		= null;
					_startDate			= null;
					_startDateString( '' );
					_endDateBtn			= null;
					_endDate			= null;
					_endDateString( '' );
					// Set Titles of date buttons
					$( _dateBtnClass ).each( function ( i ) {
						if( !$( this ).data( 'booked' ) )
						{
							if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Select your Arrival Date' );
							else $( this ).attr( 'title', 'Date unavailable' );
						}
					});
					// Set instructions for user
					$( '.calInstructions' ).html( 'Select Arrival Date' );
					// Change sumbit button
					if( !$( 'input#submitStep2' ).hasClass( 'disabled' ) ) $( 'input#submitStep2' ).attr( { 'readonly':'true', 'title':'Select Arrival Date' } ).addClass( 'disabled' );
					// Set next step
					_dateClickCount		= 0;
				}
				else if( thisDate.valueOf() == _endDate.valueOf() ) // 2. reset end date //
				{
					// Reset this button
					$( this ).data( 'select', false );
					if( $( this ).hasClass( _selectClassName ) ) $( this ).removeClass( _selectClassName );
					// Reset inbetween Dates
					_unselectBetween( _startDate, _endDate );
					// Reset end date
					_endDateBtn			= null;
					_endDate			= null;
					_endDateString( '' );
					// Set Titles of date buttons
					$( _dateBtnClass ).each( function ( i ) {
						if( !$( this ).data( 'booked' ) )
						{
							if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Select your Departure Date' );
							else $( this ).attr( 'title', 'Date unavailable' );
						}
					});
					_startDateBtn.attr( 'title', 'Reset Dates' );
					// Set instructions for user
					$( '.calInstructions' ).html( 'Select Departure Date' );
					// Change sumbit button
					if( !$( 'input#submitStep2' ).hasClass( 'disabled' ) ) $( 'input#submitStep2' ).attr( { 'readonly':'true', 'title':'Select Departure Date' } ).addClass( 'disabled' );
					// Set next step
					_dateClickCount		= 1;
				}
				else // 3. reset end date and set new end date //
				{
					// Reset end button and select new one
					$( _endDateBtn ).data( 'select', false );
					$( this ).data( 'select', true );
					if( !$( this ).hasClass( _selectClassName ) ) $( this ).addClass( _selectClassName );
					// Select new end date
					_endDate			= new Date();
					_endDateBtn			= $( this );
					_endDate.setTime( thisDate.valueOf() );
					_endDateString( $( this ).data( 'date' ) );
					// Set inbetween Dates
					_selectBetween( _startDate, _endDate );
					// Set Titles of date buttons
					$( _dateBtnClass ).each( function ( i ) {
						if( !$( this ).data( 'booked' ) )
						{
							if( $( this ).data( 'available') ) $( this ).attr( 'title', 'Reselect your Departure Date' );
							else $( this ).attr( 'title', 'Date unavailable' );
						}
					});
					_startDateBtn.attr( 'title', 'Reset Dates' );
					_endDateBtn.attr( 'title', 'Reset End Date' );
					// Set instructions for user
					$( '.calInstructions' ).html( 'Check your booking' );
				}
				return;
			}
		});
		
		$( _dateBtnClass ).hover( function ( event ) {
			
			if( !$( this ).data( 'available') ) return;
			
			var thisDate = new Date( Date.parse( $( this ).data( 'date' ) ) );
			if( _startDate && !_endDate )
			{
				_hoverBetween( _startDate, thisDate );
				_unhoverAfter( thisDate );
			}
		},function(){
			
		});
	}
	
	/*================================= End Calendar Functions =================================*/
	
	/*================================= Reservations =================================*/
	
	var _luxuryNumRooms;
	
	$( '#clientNumberRooms' ).blur( function () {
		if( $( this ).attr( 'value' ) > 8 ) $( this ).attr( 'value', 8 );
	});
	
	$( 'select option#honeymoonInputStep1:selected' ).each( function () {
		if( !$( '#clientNumberRooms' ).hasClass( 'disabled' ) )
		{
			$( '#clientNumberRooms' ).addClass( 'disabled' );
			_luxuryNumRooms = $( '#clientNumberRooms' ).attr( 'value' );
			$( '#clientNumberRooms' ).attr( { 'value':1, 'readonly':'true' } );
		}
	});
		
	$( '.reservations select' ).change( function () {
		$( 'select option#honeymoonInputStep1:selected' ).each( function () {
			if( !$( '#clientNumberRooms' ).hasClass( 'disabled' ) )
			{
				$( '#clientNumberRooms' ).addClass( 'disabled' );
				_luxuryNumRooms = $( '#clientNumberRooms' ).attr( 'value' );
				$( '#clientNumberRooms' ).attr( { 'value':1, 'readonly':'true' } );
			}
		});
		$( 'select option#luxuryInputStep1:selected' ).each( function () {
			if( $( '#clientNumberRooms' ).hasClass( 'disabled' ) )
			{
				$( '#clientNumberRooms' ).removeClass( 'disabled' );
				$( '#clientNumberRooms' ).attr( { 'value':_luxuryNumRooms, 'readonly':'' } );
			}
		});
	});
	
	//$( 'input#submitStep2' ).attr( 'value', 'Confirm Details' );
	//$( 'input#step2S' ).attr( 'value', '3' );
	$( 'div.jsDetails' ).show();
	if( !$( 'div.resCalendar' ).hasClass( 'holdHeight' ) ) $( 'div.resCalendar' ).addClass( 'holdHeight' );
	
	$( '.monthJump span.calInstructions' ).html( 'Select Arrival Date' );
	$( '.monthJump span.dropDown' ).css( { 'display':'inline' } );
	$( 'input#submitStep2' ).attr( { 'readonly':'true', 'title':'Select Arrival Date' } ).addClass( 'disabled' );
	
	_calLoadingContent = $( '#resCalendarCont' ).html();
	$( '#resCalendarCont' ).css( { 'minHeight':'100px' } );
	_getNewCal( { startDate:'' } );
	
	$( '.monthJump select' ).change( function ( event ) {
		event.preventDefault();
		var newCalDate = $( '.monthJump select option:selected' ).attr( 'value' );
		_getNewCal( { startDate: newCalDate } );
	});
	
	/*================================= CTAs Hover =================================*/
	
	//var _ctaImgHeight		= $( 'a.cta img' ).height(); // commented out because of chrome and safari image loading
	var _ctaImgHeight		= 113;
	var _ctaSpanHeight		= $( 'a.cta span' ).height();
	var _ctaExtraHeight		= 20;
	var _ctaCombHeight		= _ctaImgHeight + _ctaSpanHeight + _ctaExtraHeight;
	var _ctaHoverSpeed		= 300;
	var _ctaImgAlpha		= 0.7;
	
	$( 'a.cta' ).css( { 'display':'block', 'height':_ctaCombHeight + 'px', 'position':'relative' } );
	//$( 'a.cta img' ).css( { 'position':'absolute', 'top':0, 'left':0 } );
	$( 'a.cta span' ).css( { 'position':'absolute', 'bottom':0, 'left':0 } );
	
	$( 'a.cta' ).each( function () {
		$( this ).children( 'span' ).clone().appendTo( this );
		$( this ).children( 'span:nth(0)' ).css( { 'backgroundPosition':'top left' } );
		$( this ).children( 'span:nth(1)' ).css( { 'backgroundPosition':'bottom left' } );
		
		$( this ).hover( function ( event ) {
			$( this ).children( 'span' ).each( function () {
				var _bgPos = $( this ).css( 'backgroundPosition' );
				$( this ).css( { 'backgroundPosition':_bgPos } );
			})
			$( this ).children( 'img' ).css( { 'filter':'alpha(opacity=' + _ctaImgAlpha*100 + ')', '-moz-opacity':_ctaImgAlpha, '-khtml-opacity':_ctaImgAlpha, 'opacity':_ctaImgAlpha } ).fadeTo( _ctaHoverSpeed, 1 );
			$( this ).children( 'span:nth(1)' ).fadeTo( _ctaHoverSpeed, 0 );
		},function(){
			$( this ).children( 'img' ).fadeTo( _ctaHoverSpeed, _ctaImgAlpha );
			$( this ).children( 'span:nth(1)' ).fadeTo( _ctaHoverSpeed, 1 );
		});
		
	} );
	
	/*================================= Referrals =================================*/
	
	
	var _referralsMin = $( '#friendDetailsTable tr.friendDetailNames' ).size();
	
	function _reconfigureDetailsTableInputValues () {
		// logic for min referrals
		var numReferralsNum = $( '#friendDetailsTable tr.friendDetailNames' ).size();
		if( numReferralsNum <= _referralsMin ) {
			if( _ie ) $( '#friendDetailsTable tr a.removeFriend' ).hide(0);
			else $( '#friendDetailsTable tr a.removeFriend' ).fadeOut();
		} else {
			if( _ie ) $( '#friendDetailsTable tr a.removeFriend' ).show(0);
			else $( '#friendDetailsTable tr a.removeFriend' ).fadeIn();
		}
		
		// sets values of inputs
		$( '#friendDetailsTable tr.friendDetailValues' ).each( function ( i ) {
			$( this ).find( 'td:nth-child(1) input' ).attr( 'name', 'fname'+i );
			$( this ).find( 'td:nth-child(2) input' ).attr( 'name', 'lname'+i );
			$( this ).find( 'td:nth-child(3) input' ).attr( 'name', 'email'+i );
			$( this ).find( 'td:nth-child(4) input' ).attr( 'name', 'cell'+i );
		});
		
		_initReferralForm();
	}
	
	$('#container .twoThree .addFriend, #container .twoThree .removeFriend').addClass( 'js' );
	$( '#friendDetailsTable tr a.removeFriend' ).hide(0);
	
	$( '#friendDetailsTable' ).delegate('.removeFriend', 'click', function ( event ) {
		event.preventDefault();
		
		// logic for min referrals
		var numReferralsNum = $( '#friendDetailsTable tr.friendDetailNames' ).size();
		if( numReferralsNum <= _referralsMin ) return;
		
		if( $( '#friendDetailsTable tr' ).size() > 3 ) {
			var removeSecondRow = $( this ).parents( 'tr' );
			var removeFirstRow = removeSecondRow.prev();
			removeSecondRow.fadeOut( 300, function () {
				$( this ).remove();
			});
			if( $( '#friendDetailsTable tr' ).size() < 6 ) {
				if( _ie ) $( '#friendDetailsTable tr a.removeFriend' ).hide(0);
				else $( '#friendDetailsTable tr a.removeFriend' ).fadeOut();
			}
			removeFirstRow.fadeOut( 301, function () {
				$( this ).remove();
				_reconfigureDetailsTableInputValues();
			});
		}
	}).delegate('.removeFriend', 'mouseover', function ( event ) {
		event.preventDefault();
		if( !$( this ).parents( 'tr.friendDetailValues' ).hasClass( 'hover' ) ) {
			$( this ).parents( 'tr.friendDetailValues' ).addClass( 'hover' );
			$( this ).parents( 'tr.friendDetailValues' ).prev().addClass( 'hover' );
		}
	}).delegate('.removeFriend', 'mouseout', function ( event ) {
		event.preventDefault();
		if( $( this ).parents( 'tr.friendDetailValues' ).hasClass( 'hover' ) ) {
			$( this ).parents( 'tr.friendDetailValues' ).removeClass( 'hover' );
			$( this ).parents( 'tr.friendDetailValues' ).prev().removeClass( 'hover' );
		}
	});
	
	$('._jsEmail').live('blur', function () {
		var $this		= $(this),
			val			= $this.val(),
			newVal		= val.replace(/\s/g,"");
		
		$this.val(newVal);	
	});
	
	$('#container .twoThree .addFriend').click( function ( event ) {
		event.preventDefault();
		var firstRow		= $( '#friendDetailsTable tr.friendDetailNames:first' );
		var secondRow		= $( '#friendDetailsTable tr.friendDetailValues:first' );
		firstRow.clone().appendTo( '#friendDetailsTable' );
		secondRow.clone().appendTo( '#friendDetailsTable' );
		
		var recentlyAddedValues = $( '#friendDetailsTable tr.friendDetailValues:last input' );
		var recentlyAddedNames = $( '#friendDetailsTable tr.friendDetailNames:last' );
		
		if( recentlyAddedValues.hasClass( 'valFailed' ) ) recentlyAddedValues.removeClass( 'valFailed' );
		if( recentlyAddedValues.hasClass( 'valPassed' ) ) recentlyAddedValues.removeClass( 'valPassed' );
		
		recentlyAddedNames.hide().fadeIn();
		recentlyAddedValues.hide().val( '' ).fadeIn();
		
		_reconfigureDetailsTableInputValues();
		setupBlurValidate();
	});
	
	
	/*================================= Mini Gallery =================================*/
	
	
	// Set vars
	var _thumbHolderClass		= '#fold .galleryThumbs';
	var _thumbClass				= '#fold .galleryThumbs a';
	
	var _galleryClass			= '#fold .twoThree.left.focus';
	var _galleryHeaderClass		= '#fold .twoThree.left.focus h1:first';
	var _galleryInfoClass		= '#fold .twoThree.left.focus .info';
	
	var _mgHolder				= '#mgHolder';
	var _mgNav					= '#mgNav';
	var _mgLoader				= '#mgLoader';
	var _mgClose				= '#mgClose';
	var _mgNext					= '#mgNext';
	var _mgPrev					= '#mgPrev';
	var _mgInfo					= '#mgInfo';
	var _mgTitle				= '#mgTitle';
	var _mgReadMore				= '#mgReadMore';
	var _mgDescription			= '#mgDescription';
	var _hasMiniGallery			= false;
	
	$(_mgHolder).each(function(){
		_hasMiniGallery = true;
	});
	
	if( _hasMiniGallery ) {
		// Set vars
		var _originalTitle		= $( _galleryHeaderClass ).html();
		var _originalHeight		= $( _galleryClass ).height();
		var _diffInOffsetTop	= $(_thumbHolderClass).offset().top - $(_galleryClass).offset().top;
		var _diffInHeight		= $(_thumbHolderClass).outerHeight() - $(_galleryClass).css('marginBottom').replace('px','')*3 - $(_thumbClass).css('marginBottom').replace('px','');
		var _newSlideToHeight	= _diffInHeight + _diffInOffsetTop;
		if( $(_galleryClass).height() > ( $(_thumbHolderClass).height() + _diffInOffsetTop ) ) _newSlideToHeight = 'auto';
		var _slideToHeight		= 389;
		var _thumbHoverSpeed	= 200;
		var _thumbImgAlpha		= 0.7;
		var _mgInfoTopJustTitle;
		var _mgInfoTopShowAll;
		var _mgInfoTopHide		= $( _mgHolder ).height();
		var _mgFirstImage		= true;
		
		// Set image array
		var _mgImagesArray		= [];
		$( _thumbClass ).each( function ( i ) {
			if( $( this ).attr( 'href' ) != '?/info/map' ) {
				_mgImagesArray.push( this );
				$( this ).data('mgNumber',i);
				$( this ).data('select',false);
				$( _mgNext+', '+_mgPrev).show(0);
			}
		});
		var _mgCurrNum;
		var _mgMaxNum			= _mgImagesArray.length-1;
		
		// Set content height
		$( _galleryClass ).css( { 'height':_newSlideToHeight } );
		
		// Set mgHolder positioning.
		var regExpContainsPhoto = /_photo/i;
		if( regExpContainsPhoto.test(location.search) ){
			$(_mgHolder).css({'position':'relative'});
		}
		
		
		// If its image viewing time
		$( _mgHolder ).children('img').each( function () {
			$( _galleryInfoClass ).hide( 1 );
			$( _galleryClass ).css( { 'height':_slideToHeight } );
			_mgFirstImage = false;
			_mgCurrNum = Number( $( _mgHolder ).attr( 'rel' ) );
		});
		
		// Next and Prev image navigation
		$( _mgNext ).click( function ( event ){
			event.preventDefault();
			_mgViewImageFromThumb ( _mgCurrNum+1 );
		});
		$( _mgPrev ).click( function ( event ){
			event.preventDefault();
			_mgViewImageFromThumb ( _mgCurrNum-1 );
		});
		
		// Thumbs fading on hover
		$( _thumbClass ).fadeTo( 1, 0.7 ).hover( function ( event ) {
			if( !$( this ).data('select') ) {
				$( this ).css( { 'filter':'alpha(opacity=' + _thumbImgAlpha*100 + ')', '-moz-opacity':_thumbImgAlpha, '-khtml-opacity':_thumbImgAlpha, 'opacity':_thumbImgAlpha } ).fadeTo( _thumbHoverSpeed, 1 );
				if( _ie ) $(this).fadeTo( 1, 1 );
				else $(this).fadeTo( _thumbHoverSpeed,1 );
			}
		},function(){
			if( !$( this ).data('select') ) {
				if( _ie ) $(this).fadeTo( 1, _thumbImgAlpha );
				$(this).fadeTo( _thumbHoverSpeed, _thumbImgAlpha );
			}
		});
		
		$( _thumbClass ).click( function ( event ) {
			event.preventDefault();
			if( $( this ).attr( 'href' ) == '?/info/map' ) location.href =  $( this ).attr( 'href' );
			_mgViewImageFromThumb( $( this ).data('mgNumber') );
		});
		
		$( _mgClose ).click( function ( event ) {
			event.preventDefault();
			_mgCloseGallery();
		});
		
		function _mgCloseGallery () {
			$(_mgHolder).css({'position':'absolute'});
			if( _ie8 ) {
				$( _mgHolder ).css({ 'visibility':'hidden' });
				$( _mgHolder ).children( 'img:first' ).remove();
			}else{
				$( _mgHolder ).fadeOut( function () {
					$( this ).children( 'img:first' ).remove();
				});
			}
			
			$( _mgInfo ).css('top',_mgInfoTopHide);
			$( _galleryInfoClass ).css( { 'height':'auto', 'overflow':'visible' } );
			$( _galleryClass ).css( { 'height':_newSlideToHeight } );
			$( _galleryInfoClass ).fadeIn();
			_mgFirstImage = true;
			
			// Set selected
			$( _thumbClass ).each( function ( i ) {
				$( this ).data('select',false).fadeTo( 1, _thumbImgAlpha );
				if( $( this ).hasClass( 'selected' ) ) $( this ).removeClass( 'selected' );
			});
			
			$( _galleryHeaderClass ).html( _originalTitle ).css('visibility','visible');
		}
		
		function _mgViewImageFromThumb ( num ) {
			
			// Show loading image
			if( _ie ) $( _mgLoader ).show(0);
			else $( _mgLoader ).fadeIn();
			
			// Set _mgCurrNum and get object
			_mgCurrNum = num;
			if( _mgCurrNum > _mgMaxNum ) _mgCurrNum = 0;
			if( _mgCurrNum < 0 ) _mgCurrNum = _mgMaxNum;
			var showThis = _mgImagesArray[ _mgCurrNum ];
			
			// Set vars
			var _imgObj			= jQuery.parseJSON($( showThis ).attr( 'rel' ));
			var _imgSrc			= _imgObj.imgUrl;
			var _imgTitle		= _imgObj.title;
			var _imgDescription	= _imgObj.description;
			//var _imgSrc			= $( showThis ).children( 'img' ).attr( 'src' );
			//var _imgTitle		= $( showThis ).children( 'img' ).attr( 'alt' );
			//var _imgDescription	= $( showThis ).attr( 'rel' );
			
			// Set description
			$(_mgDescription).html( _imgDescription );
			
			// Set selected
			$( _thumbClass ).each( function ( i ) {
				if( showThis != this ) {
					$( this ).data('select',false).fadeTo( 1, _thumbImgAlpha );
					if( $( this ).hasClass( 'selected' ) ) $( this ).removeClass( 'selected' );
				}else{
					if( _ie ) $( showThis ).data('select',true).fadeTo( 1, 1 );
					else $( showThis ).data('select',true).fadeTo( _thumbHoverSpeed,1 );
					if( !$( this ).hasClass( 'selected' ) ) $( this ).addClass( 'selected' );
				}
			});
			
			// Show photo holder
			if( _ie8 ) $( _mgHolder ).css({ 'visibility':'visible' });
			else $( _mgHolder ).fadeIn();
			
			// Insert img tag into img container
			if( $( _mgHolder ).children( 'img' ).length ) {
				$( _mgHolder ).children( 'img' ).after('<img src="' + _imgSrc + '" alt="' + _imgTitle + '" />');
				$( _mgHolder ).children( 'img:last' ).hide();
			}else{
				$( _mgHolder ).prepend('<img src="' + _imgSrc + '" alt="' + _imgTitle + '" />');
			}
			
			// On image load
			$( _mgHolder ).children( 'img:last' ).load( function () {
				
				// Fade out information
				$( _galleryInfoClass ).fadeOut(function(){
					$('html, body').animate({ 'scrollTop':$(_galleryClass).offset().top-20 });
					$(_mgHolder).css({'position':'relative'});
				});
				$( _galleryClass ).css( { 'height':_slideToHeight, 'overflow':'visible' } );
				
				// Change title and description
				$( _mgTitle ).html( _imgTitle );
				if( $(_mgTitle).html() == '' ) $(_mgTitle).html( '&nbsp;' )
				$( _galleryHeaderClass ).css( { 'visibility':'hidden' } );
				
				// Description hide/show
				if( $(_mgDescription).html() == '' || $(_mgDescription).html() == '&nbsp;' ) $(_mgDescription).css('visibility','hidden');
				else $(_mgDescription).css('visibility','visible');
				
				// FadeOut old image
				if( $( _mgHolder ).children('img').size() > 1 ) {
					$( _mgHolder ).children( 'img:last' ).fadeIn( function () {
						$( _mgHolder ).children( 'img:first' ).remove();
					})
				}
				else $( _mgHolder ).children( 'last' ).fadeIn();
				
				// Hide loading image
				if( _ie ) $( _mgLoader ).hide(0);
				else $( _mgLoader ).fadeOut();
				
				// Read more or not
				if( $( _mgDescription ).html() != '' ) {
					$( _mgTitle ).append('<a id=\'mgReadMore\' href=\'javascript:\'>(Read More)</a>');
					_mgInfoTopShowAll = _mgInfoTopHide - $( _mgInfo ).outerHeight();
					$( _mgReadMore ).show(0).toggle(function(){
						$( _mgInfo ).stop().animate({ 'top':_mgInfoTopShowAll });
						$( _mgReadMore ).html('(Read Less)');
					},function(){
						$( _mgInfo ).animate({ 'top':_mgInfoTopJustTitle });
						$( _mgReadMore ).html('(Read More)');
					});
				}
				
				// Animate Info bar
				if( !_mgInfoTopJustTitle ) _mgInfoTopJustTitle = $( _mgInfo ).css( 'top' ).replace('px','');
				if( $( _mgTitle ).html() == '' || $( _mgTitle ).html() == '&nbsp;' ) {
					if( _mgFirstImage ) {
						_mgFirstImage = false;
						$( _mgInfo ).css({ 'top':_mgInfoTopHide });
					}else $( _mgInfo ).animate({ 'top':_mgInfoTopHide });
					
				}else{
					if( _mgFirstImage ) {
						_mgFirstImage = false;
						$( _mgInfo ).css({ 'top':_mgInfoTopHide }).delay(300).animate({ 'top':_mgInfoTopJustTitle });
					}else $( _mgInfo ).animate({ 'top':_mgInfoTopJustTitle });
				}
			})
		}
	}
	
	/*================================= End of Mini Gallery =================================*/
	
	/*================================= View Newsletter in popup =================================*/
	
		
		$('._jsInitNewsletterPopup').click(function( event ){
			event.preventDefault();
			
			var $this = $(this);
			var nlUrl = $this.attr('href');
			var $iframe = $('<iframe src="'+nlUrl+'" width="700" height="600"<p>Your browser does not support iframes.</p></iframe>');
			$.popup({
				onPopupCreation:function($this){
					$this.find('.popUpContainer')
						.empty()
						.append($iframe);
				}
			});
			
		});
		
	
	/*================================= End View Newsletter in popup =================================*/
	
	/*================================= Testimonials =================================*/
	
		
		
		$('#writeTestimonial').click(function( event ){
			event.preventDefault();
			if( $.support.opacity ) $('.testimonialSubmitContainer').fadeIn(500)
			else $('.testimonialSubmitContainer').show()
		});
		
		if( location.hash == '#writeTestimonial' ) $('#writeTestimonial').click();
	
	
	/*================================= End of Testimonials =================================*/
	
	/*================================= Blog Archives =================================*/
	
		
		$('#section .archive .liHeading').each(function(){
			
			if( $(this).parent().hasClass('open') ) $(this).data('open', true);
			else $(this).data('open', false);
			
			$(this).click(function( event ){
				
				event.preventDefault();
				var $this = $(this);
				
				var $parentWrapper = $this.parent().parent().find('._jsYearWrapper');
				
				var $jsLoader = $this.parent().parent().find('._jsLoader');
				
				
				
				if( $this.parent().hasClass('yearArchive') ){
					
					if( $.support.opacity ) $this.parent().siblings().fadeIn(200);
					else $this.parent().siblings().show();
					$this.parent().unwrap().remove();
					$('.yearArchive').first().show();
					
					if( $this.hasClass('_jsShowNextYearMonths') && !$this.hasClass('_jsNoAJAX') ){
						
						// Grab the year to send through to AJAX //
						var year = $this.find('a').attr('rel');
						
						// Show ajax loader here //
						$jsLoader.fadeIn();
						
						$.ajax({
							url: '?/blog',
							type:'POST',
							data: {
								year		: year,
								ajax_on		: true
							},
							dataType:'json',
							success: function(data){
								
								// Hide ajax loader here //
								$parentWrapper.children().each( function(){
									var $each = $(this);
									var month = $each.attr('rel');
									var html = '';
									for(var x in data.articles){
										if(data.articles[x]['article_month'] == month){
											html += '<li><a href="?/blog/_id=' + data.articles[x]['article_id'] + '" title="Click to read post">' + data.articles[x]['article_title'] + '</a></li>';	
										}
									}
									$each.after( html );
								});
								
								$jsLoader.fadeOut();
							}
						});
					}
					
					
				} else{
					var $siblings = $this.siblings();
					var isOpen = $this.data('open');
					if( isOpen ){
						if( $.support.opacity ) $siblings.stop(true,true).fadeOut(100);
						else $siblings.stop(true,true).hide();
						$this.removeClass('open');
					}else{
						if( $.support.opacity ) $siblings.stop(true,true).fadeIn(200);
						else $siblings.stop(true,true).show();
						$this.addClass('open');
					}
					$this
						.data( 'open', !isOpen )
						.parent().removeClass('open');
				}
			});
		});
			
	
	/*================================= End of Blog Archives =================================*/
	
});
