
/*===============================================================================
	web.js
	John Larson
	9/27/08
	
	All page-specific JavaScript for web capabilities.

===============================================================================*/

window.addEvent('domready', function() {
	
	myDatePicker = new DatePicker('web20SendDate', 'sendDate',
		{dateFormat:		'%m/%D/%y',
		 direction:			.5,
		 pickerPositionOptions:	{offset: {x: 0, y: 0}},
		 showOnInputFocus:	false,
		 inputClass:		'textInput' });
	
	timePicker = new TimePicker('web20SendTime', 'sendTime',
		{showOnInputFocus:	false, pickerPositionOptions:	{offset: {x: 80, y: 0}},
		 inputClass:		'textInput' });
	
	prepWeb20Effects();
	
});
	



/****************************************************************************
//	SECTION::Design and Layout
*/
	
	function viewOldWebDesign() {
		if($('webDesignPopUp').style.visibility == 'visible')
			$('newWebDesignImage').fade();
		else {
			$('newWebDesignImage').style.visibility = 'hidden';
			$('webDesignPopUp').appear();
		}
	}
	
	function viewNewWebDesign() {
		if($('webDesignPopUp').style.visibility == 'visible')
			$('newWebDesignImage').appear();
		else {
			$('newWebDesignImage').style.visibility = 'visible';
			$('webDesignPopUp').appear();
		}
	}
	
/*
//	End SECTION::Design and Layout
****************************************************************************/



/****************************************************************************
//	SECTION::Goals Based Planning
*/
	
	function closeGoalsPopUp() {
		$('goalsPopUp').fade();
		resetGoalsPopUp.delay(1000);
	}
	function resetGoalsPopUp() {
		$('goalsContact').style.display = 'none';
		$('goalsThanks').style.display = 'none';
		$('goalsFairEnough').style.display = 'none';
		$('goalsQuestion').appear();
		$('goalsQuestionForm').reset();
	}
	
	function submitGoalsContact(theForm) {
		with(theForm) {
			
			// filter out blank submissions:
			if(name.value == ''  &&  email.value == '')
				return false;
			
			if(!isValidEmailAddress(email.value)) {
				alert('We\'ll need a valid email address in order to get in touch.  Thanks!');
				email.focus();
				return false;
			}
		}
		new Ajax('contact.asp?pageAction=submit', {
			method: 'post',
			data: theForm,
			evalScripts: true,
			onComplete: function() {
				swapSections('goalsContact', 'goalsThanks');
				theForm.reset();
			}
		}).request();
		return false;
	}
	
/*
//	End SECTION::Goals Based Planning
****************************************************************************/




/****************************************************************************
//	SECTION::Web 2.0
*/
	
	var web20Tween1, web20Tween2;
	function goToWeb20Step1() {
		web20Tween1.start('height', 0);
		web20Tween2.start('height', 0);
	}
	
	
	function prepWeb20Effects() {
		
		web20Tween1 = new Fx.Tween($('web20Step0'), {duration: 1000 });
		web20Tween2 = new Fx.Tween($('web20Step0BarHolder'), {duration: 1000 });
		
		$('presentSet').getElements('img').each(function(thePresent, index) {
			thePresent.addEvents({
				mouseover: setPresentDescription.pass(index),
				mouseout:  resetPresentDescription
			});
			thePresent.makeDraggable({
				droppables: $('presentDropZone'),
				container: $('web20Step1Presents'),
				onStart: function() {
					thePresent.setStyle('z-index', 2);
				},
				onEnter: function(thePresent, theDropZone) {
					theDropZone.addClass('hot');
				},
				onLeave: function(thePresent, theDropZone) {
					theDropZone.removeClass('hot');
				},
				onDrop: function(thePresent, theDropZone) {
					dbug.log('dropZone: ' + theDropZone + ', added ' + index);
					
					
					thePresent.setStyle('z-index', 0);
					
					if(!theDropZone) {
						// Put it back:
						thePresent.style.left = ((index % 3) * 66) + 'px';
						thePresent.style.top  = (Math.floor(index / 3) * 65) + 'px';
						chosenPresentSet.remove(index);
						updatePresentDisplay();
						return;
					}
					// Entered into the box, so...
					theDropZone.removeClass('hot');
				//	thePresent.style.display = 'none';
					
					savePresentSelection(index);
				}
			});
		});
	}
	
	function resetPresents() {
		for(var i=0; i < 6; i++)
			resetPresent(i);
		chosenPresentSet = [];
		$('presentCounter').setText(0);
	}
	function resetPresent(index) {
		var thePresent = $('presentSet').getChildren()[index];
		thePresent.style.left = ((index % 3) * 66) + 'px';
		thePresent.style.top  = (Math.floor(index / 3) * 65) + 'px';
		thePresent.setStyle('z-index', 0);
	}
	
	var chosenPresentSet = [];
	function savePresentSelection(index) {
		dbug.log('savePresentSelection' + ' / ' + index);
		chosenPresentSet.remove(index);
		chosenPresentSet.push(index);
		if(chosenPresentSet.length > 3) {
			var lostPresent = chosenPresentSet.shift();
			resetPresent(lostPresent);
		}
		updatePresentDisplay();
	}
	
	function updatePresentDisplay() {
		$('presentCounter').setText(chosenPresentSet.length);
		
		chosenPresentSet.each(function(thePresentIndex, index) {
			var thePresent = $('presentSet').getChildren()[thePresentIndex];
			thePresent.style.left = (338 - index*34) + 'px';
			thePresent.style.top  = (60 + index*8) + 'px';
			thePresent.setStyle('z-index', index+1);
		});
	}
	
	function saveWeb20Step1() {
		dbug.log(chosenPresentSet);
		if(chosenPresentSet.length == 0)
			return;
		
		new Ajax('AJAXCapabilities.asp?a=submitWeb20Step1', {
			method: 'post',
			data: 'itemList=' + chosenPresentSet,
			onComplete: function() {
				swapSections('web20Step1', 'web20Step2');
			}
		}).request();
	}
	
	function saveWeb20Step2(theForm) {
		with(theForm) {
			
			if(recipientName.value.trim() == '') {
				alert('Please indicate the name of the recient.  Thanks!');
				recipientName.focus();
				return false;
			}
			
			if(!isValidEmailAddress(recipientEmail.value)) {
				alert('We\'ll need a valid email address in order to send your birthday gift.  Thanks!');
				recipientEmail.focus();
				return false;
			}
		}
		new Ajax('AJAXCapabilities.asp?a=submitWeb20Step2', {
			method: 'post',
			data: theForm,
			evalScripts: true,
			onComplete: function() {
				swapSections('web20Step2', 'web20Step3');
				$('web20ThanksPopUp').appear();
			//	$('web20Step2Form').reset();
				resetPresents();
			}
		}).request();
		return false;
	}
	
	function resetWeb20() {
		swapSections('web20Step2', 'web20Step1');
		swapSections('web20Step3', 'web20Step1');
		$('web20ThanksPopUp').fade();
		resetPresents();
		$('web20Step2Form').reset();
		web20Tween1.start('height', 323);
		web20Tween2.start('height', 325);
	}
	
	
	var presentDescriptionSet = [
		'A jolly good new buddy', 'A pony!', 'Fame & Fortune',
		'Some random fun', 'Someone to dance with', 'Good times & Good Karma'
	];
	function setPresentDescription(index) {
		$('presentDefaultInfo').setStyle('display', 'none');
		$('presentOtherInfo').setText(presentDescriptionSet[index]);
		$('presentOtherInfo').setStyle('display', 'block');
	}
	function resetPresentDescription() {
		$('presentOtherInfo').setStyle('display', 'none');
		$('presentDefaultInfo').setStyle('display', 'block');
	}
	
/*
//	End SECTION::Web 2.0
****************************************************************************/




