
/*===============================================================================
	develop.js
	John Larson
	9/28/08
	
	All page-specific JavaScript for development capabilities.

===============================================================================*/

window.addEvent('domready', function() {
	
	
	
	mobsterDebtorAdderPopUp = new PopUpWindow('Add New Debtor', 
		{injectLocation: $('mobsterDebtorAdderInjectDiv'),
		 contentDiv: $('mobsterDebtorAdderContent'),
		 width: '290px', left: '200px', top: '40px'});
	
	mobsterDebtPayerPopUp = new PopUpWindow('Make A Payment', 
		{injectLocation: $('mobsterDebtPayerInjectDiv'),
		 contentDiv: $('mobsterDebtPayerContent'),
		 width: '260px', left: '180px', top: '160px'});
		 
	prepConsultingDragging();
	
	toggleMessageBoardLoginMode();
	
});


/****************************************************************************
//	SECTION::eCommerce
*/
	var currentPlaygroundQuantity = 0;
	function addPlaygroundToCart() {
		if(currentPlaygroundQuantity == 0) {
			$('emptyCartContents').setStyle('display', 'none');
			$('nonEmptyCartContents').setStyle('display', 'block');
		}
		if(currentPlaygroundQuantity < 99) {
			currentPlaygroundQuantity++;
			$('playgroundQuantity').setText((currentPlaygroundQuantity + '').zeroise(2));
			$('playgroundTotal').setText((currentPlaygroundQuantity*100).numberFormat(0));
		}
		else {
			alert('You really want a lot of Playground!\n\n' +
				'We\'re impressed by your clicking tenacity!');
		}
	}
	
	function clearCart() {
		currentPlaygroundQuantity = 0;
		$('nonEmptyCartContents').setStyle('display', 'none');
		$('emptyCartContents').setStyle('display', 'block');
	}
	
	function playgroundCheckout() {
		$('thePayPalForm').quantity_1.value = currentPlaygroundQuantity;
		$('thePayPalForm').submit();
	}
	

/*
//	End SECTION::eCommerce
****************************************************************************/





/****************************************************************************
//	SECTION::Online Community
*/
	function toggleMessageBoardLoginMode() {
		var theLink = $('oldFriend').getElement('a');
		if(theLink.getText() == 'old friend?') {
			swapSections('createAccountButton', 'loginButton')
			theLink.setText('new user?');
			$('messageBoardLoginModeInput').value = 'oldUser';
		}
		else {
			swapSections('loginButton', 'createAccountButton')
			theLink.setText('old friend?');
			$('messageBoardLoginModeInput').value = 'newUser';
		}
	}
	function messageBoardLogin(theForm) {
		
		$('messageBoardLoginMessage').setText(
			theForm.messageBoardLoginModeInput == 'oldUser' ?
				'Logging in...' : 'Creating your account...');
		
		new Ajax('AJAXCapabilities.asp?a=messageBoardLogin', {
			method: 'post',
			data: theForm,
			evalScripts: true
		}).request();
		return false;
	}
	
	function messageBoardLogout() {
		new Ajax('AJAXCapabilities.asp', {
			method: 'post',
			data: 'a=messageBoardLogout',
			onComplete: function() {
				$('messageBoardLoginForm').reset();
				swapSections('messageBoardMain', 'messageBoardLogin');
				swapSections('messageBoardPostAdder', 'messageBoardLogin');
				$('messageBoardLoginMessage').setText('Thanks for visiting!');
			}
		}).request();
		return false;
		
	}
	
	function loadMessageBoardMainPage() {
		new Ajax('AJAXCapabilities.asp', {
			method: 'post',
			data: 'a=loadMessageBoardMainPage',
			update: 'messageBoardMain',
			onComplete: function() {
				swapSections('messageBoardLogin', 'messageBoardMain');
			}
		}).request();
	}
	
	function loadMessageBoardPosts() {
		new Ajax('AJAXCapabilities.asp?a=loadMessageBoardPosts', {
			method: 'post',
			data: 'q=' + encodeURIComponent($('messageBoardQ').value),
			evalScripts: true,
			update: 'messageBoardPosts',
			onComplete: function() {
				
			}
		}).request();
	}
	
	function loadMessageBoardPostPage(pageNumber) {
		new Ajax('AJAXCapabilities.asp?a=loadMessageBoardPostPage&pageNumber=' + pageNumber, {
			method: 'post',
			data: 'q=' + encodeURIComponent($('messageBoardQ').value),
			evalScripts: true,
			update: 'messageBoardPostListing',
			onComplete: function() {
				
			}
		}).request();
	}
	
	
	function loadMessageBoardPostAdder() {
		new Ajax('AJAXCapabilities.asp', {
			method: 'post',
			data: 'a=loadMessageBoardPostAdder',
			evalScripts: true,
			update: 'messageBoardPostAdder',
			onComplete: function() {
				prepFileInputStyling('messageBoardPostImageInput');
				swapSections('messageBoardMain', 'messageBoardPostAdder');
			}
		}).request();
	}
	
	function submitMessageBoardPost(theForm) {
	
		if(theForm.title.value.trim() == '') {
			alert('Please enter the title of your post!');
			theForm.title.focus();
			return false;
		}
		if(theForm.message.value.trim() == '') {
			alert('Please enter the message of your post!');
			theForm.message.focus();
			return false;
		}
		
		new Ajax('AJAXCapabilities.asp?a=submitMessageBoardPost', {
			method: 'post',
			data: theForm,
			evalScripts: true,
			update: 'messageBoardPosts',
			onComplete: function() {
				$('messageBoardQ').value = ''; // clear the search
				loadMessageBoardPosts();
				swapSections('messageBoardPostAdder', 'messageBoardMain');
			}
		}).request();
	}
	
	function cancelMessageBoardPost() {
		swapSections('messageBoardPostAdder', 'messageBoardMain');
	}
	
	function completeMessageBoardImageUpload(theFileName) {
		$('messageBoardImage').src = 'images/capabilitiesDevelop/messageBoardImages/'
			+ theFileName + '?x=' + Math.random();
	}
	
	
	function prepFileInputStyling(theRealFileInput) {
	//	dbug.log(theRealFileInput);
		theRealFileInput = $(theRealFileInput);
	//	dbug.log(theRealFileInput);
	//	dbug.log(theRealFileInput.parentNode);
		
		var fakeFileUpload = document.createElement('div');
		fakeFileUpload.className = 'fakeFile';
		var theFakeFileInput = new Element('input');
		theFakeFileInput.className = 'messageBoardTextInput';
		theFakeFileInput.type = 'text';
		fakeFileUpload.appendChild(theFakeFileInput);
		var image = document.createElement('img');
		image.src='images/buttons/buttonBrowse.gif';
		fakeFileUpload.appendChild(image);
		
		var parentDiv = theRealFileInput.parentNode;
		theRealFileInput.className = 'stealthFileInput';
		theRealFileInput.parentNode.appendChild(fakeFileUpload);
		theRealFileInput.onchange = theRealFileInput.onmouseout = function () {
			theFakeFileInput.value = this.value;
		}
	}
	

/*
//	End SECTION::Online Community
****************************************************************************/




/****************************************************************************
//	SECTION::Web 2.0 Interactivity
*/
	function giveWeb20Response() {
		swapSections('web20Buttons', 'web20Reply');
		setTimeout(function() {
			swapSections('web20Reply', 'web20Buttons');
		}, 5000);
	}
/*
//	End SECTION::Web 2.0 Interactivity
****************************************************************************/




/****************************************************************************
//	SECTION::Web Services Integration
*/

// Create our custom marker icons


	
	var googleMapIsLoaded = false;
	var googleMapPopUpIsOpen = false;
	function loadGoogleMap() {
		if(googleMapIsLoaded) return;
		
		if (GBrowserIsCompatible()) {
		
			// Custom marker icon for PlaygroundS:
			var PGMarkerIcon = new GIcon();
			PGMarkerIcon.image = "http://www.playgroundcreative.com/images/PGMapMarker.png";
			PGMarkerIcon.iconSize = new GSize(21, 40);
			PGMarkerIcon.shadow = "http://www.playgroundcreative.com/images/markerShadow.png";
			PGMarkerIcon.shadowSize = new GSize(48, 40);
			PGMarkerIcon.iconAnchor = new GPoint(10, 40);
			PGMarkerIcon.infoWindowAnchor = new GPoint(5, 1);
			
			var map = new GMap2($('googleMapContiner'));
			var thePoint = new GLatLng(38.594472, -90.231078);
			googleMapMarker = new GMarker(thePoint, PGMarkerIcon);
			googleMapMarker.info = $('googleMapLocationContentHolder').innerHTML;
			
			map.setCenter(thePoint, 13);
			map.addOverlay(googleMapMarker);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			GEvent.addListener(googleMapMarker, "click", toggleGoogleMapInfoWindow);

			map.setCenter(thePoint, 13);
		}
		
		googleMapIsLoaded = true;
	}
	
	function toggleGoogleMapInfoWindow() {
		if (!googleMapPopUpIsOpen) {
			googleMapMarker.openInfoWindowHtml(googleMapMarker.info);
			googleMapPopUpIsOpen = true;
		}
		else {
			googleMapPopUpIsOpen = false;
			googleMapMarker.closeInfoWindow();
		}
	}
	
	function loadDirectionsForm(toPlayground) {
		$('directionsLinks').style.display = 'none';
		if(toPlayground) {
			$('playgroundAddressInput').name = 'daddr';
			$('otherAddressInput').name = 'saddr';
		}
		else {
			$('playgroundAddressInput').name = 'saddr';
			$('otherAddressInput').name = 'daddr';
		}
		$('directionsForm').style.display = 'block';
	}
	
/*
//	End SECTION::Web Services Integration
****************************************************************************/





/****************************************************************************
//	SECTION::Web-Based Management Systems
*/

	function makeDebtorsTableSortable() {
		new TableSorter('debtorsTable',
			{columnDataTypes: 'string, money', rowsPerRecord: 1,
			 isStripedTable: true, cookieName: 'debtorsSort' });
	}
	function makeDebtsTableSortable() {
		new TableSorter('debtsTable',
			{columnDataTypes: 'string, money', rowsPerRecord: 1,
			 isStripedTable: true, cookieName: 'debtsSort' });
	}
	
	
	function deleteMobsterDebtor(theEvent, MobsterDebtorID) {
		theEvent = new Event(theEvent);
		theEvent.stop(); // cancel load of the debtor details
		var mobsterTR = $('mobsterDebtor' + MobsterDebtorID);
		var name = mobsterTR.getChildren()[0].getText();
		var debt = parseInt(mobsterTR.getChildren()[1].getText().substring(1));
		if(confirm('Are you sure you want to delete ' + name + '?')) {
			
			if(debt > 0  &&  !confirm('Really?  I mean, (s)he owes you, like, ' + debt + ' bucks.'))
				return;
			
			new Ajax('AJAXCapabilities.asp?a=deleteMobsterDebtor', {
				method: 'post',
				data: 'MobsterDebtorID=' + MobsterDebtorID,
				update: 'myMobsterDebtorListing',
				evalScripts: true,
				onComplete: function() {
					
				}
			}).request();
		}
	}
	
	function loadMobsterDebtorListing() {
		new Ajax('AJAXCapabilities.asp', {
			method: 'post',
			data: 'a=loadMobsterDebtorListing',
			update: 'myMobsterDebtorListing',
			evalScripts: true,
			onComplete: function() {
				
			}
		}).request();
	}
	
	
	function showMobsterDebtorAdder() {
		mobsterDebtorAdderPopUp.open();
	}
	
	function addMobsterDebtor(theForm) {
		if(theForm.name.value == '') {
			alert('Please enter the name of this new debtor.');
			theForm.name.focus();
			return false;
		}
		new Ajax('AJAXCapabilities.asp?a=addMobsterDebtor', {
			method: 'post',
			data: theForm,
			update: 'myMobsterDebtorListing',
			evalScripts: true,
			onComplete: function() {
				mobsterDebtorAdderPopUp.close();
				theForm.reset();
			}
		}).request();
	}
	
	
	function showMobsterDebtPayer(MobsterDebtID) {
		mobsterDebtPayerPopUp.open();
		$('payMobsterDebtForm').MobsterDebtID.value = MobsterDebtID;
	}
	
	function payMobsterDebt(theForm) {
		new Ajax('AJAXCapabilities.asp?a=payMobsterDebt', {
			method: 'post',
			data: theForm,
			update: 'myMobsterDebtListing',
			evalScripts: true,
			onComplete: function() {
				mobsterDebtPayerPopUp.close();
				theForm.reset();
			}
		}).request();
	}
	
	function deleteMobsterDebt(MobsterDebtID, MobsterDebtorID) {
		
		new Ajax('AJAXCapabilities.asp?a=deleteMobsterDebt', {
			method: 'post',
			data: 'MobsterDebtID=' + MobsterDebtID + '&MobsterDebtorID=' + MobsterDebtorID,
			update: 'myMobsterDebtListing',
			evalScripts: true,
			onComplete: function() {
				
			}
		}).request();
	}
	
	function loadMobsterDebtor(MobsterDebtorID) {
		new Ajax('AJAXCapabilities.asp?a=loadMobsterDebtor', {
			method: 'post',
			data: 'MobsterDebtorID=' + MobsterDebtorID,
			update: 'myMobsterDebtorDetail',
			evalScripts: true,
			onComplete: function() {
				swapSections('myMobsterMain', 'myMobsterDebtorDetail');
				$('myMobsterBackButton').appear();
				$('payMobsterDebtForm').MobsterDebtorID.value = MobsterDebtorID;
			}
		}).request();
	}
	
	
	function addMobsterDebt(theForm) {
		if(theForm.name.value == '') {
			alert('Please enter the name of this new debt.');
			theForm.name.focus();
			return false;
		}
		if(theForm.amountDue.value == '') {
			alert('Please enter the amount of this new debt.');
			theForm.amountDue.focus();
			return false;
		}
		new Ajax('AJAXCapabilities.asp?a=addMobsterDebt', {
			method: 'post',
			data: theForm,
			update: 'myMobsterDebtListing',
			evalScripts: true,
			onComplete: function() {
				theForm.reset();
			}
		}).request();
	}
	
	function forgiveMobsterDebt(MobsterDebtID) {
		alert('There is no forgiveness in The Family');
	}
	
	function goToMyMobsterMain() {
		loadMobsterDebtorListing();
		$('myMobsterBackButton').fade();
		swapSections('myMobsterDebtorDetail', 'myMobsterMain');
	}
/*
//	End SECTION::Web-Based Management Systems
****************************************************************************/


/****************************************************************************
//	SECTION::Consulting
*/
	function prepConsultingDragging() {
		$('yourBusiness').makeDraggable({
			handle: $('wingedBoy')
		});
		$('yourBusiness').setStyles({ top: '0px', left: '210px' })
	}
	
	function prepWingedBoy() {
		$('iCanFly').appear();
		setTimeout(function(){ $('iCanFly').fade(); }, 2000);
	}
	
	
/*
//	End SECTION::Consulting
****************************************************************************/
