var gettingHeader = false;
function getNewHeader(target) {
	if (!gettingHeader) {
		gettingHeader= true;
		var queryValues = target.toQueryParams();
		queryValues.ajaxContent = 'yes';
		queryValues._header 	= 'yes';
		new Ajax.Request(target, {
			method: 'get',
			parameters: queryValues,
			onSuccess: function(transport) {
				var splitResponse = transport.responseText.split("\n");
				if (splitResponse.length > 3) {
					$('headerSlideshow').update(transport.responseText);
				} else {
					$('headerOvertop').innerHTML = transport.responseText;
					new Effect.Appear($('headerOvertop'), {
						duration: 1,
						afterFinish: function() {
							$('headerSlideshow').innerHTML = $('headerOvertop').innerHTML;
							$('headerOvertop').style.display = 'none';
							$('headerOvertop').innerHTML = '';
							gettingHeader = false;
						}
					});
				}
			},
			onFailure: function(transport) {
				$('headerSlideshow').update(transport.responseText);
			}
		});
	}
}

function getNewLeftMenu(target) {
	var queryValues = target.toQueryParams();
	queryValues.ajaxContent = 'yes';
	queryValues._menu 		= 'yes';
	new Ajax.Request(target, {
		method: 'get',
		parameters: queryValues,
		onSuccess: function(transport) {
			$('leftMenuContainer').update(transport.responseText);
			setupAjaxLinks('#leftMenuContainer');
		},
		onFailure: function(transport) {
			$('leftMenuContainer').update(transport.responseText);
			setupAjaxLinks('#leftMenuContainer');
		}
	});
}

function getNewContent(target) {
	getNewLeftMenu(target);
	getNewHeader(target);
	
	try {
		$('mainContentText').innerHTML = '<center><br /><br /><img src="staticfiles/loader-circle.gif" /><br /><br /></center>';
	} catch (caught) {};
	
	var theQueryValues = target.toQueryParams();
	theQueryValues.ajaxContent = 'yes';
	
	new Ajax.Request(target, {
		method: 'get',
		parameters: theQueryValues,
		onSuccess: function (transport) {
		
			$('mainContentText').update(transport.responseText);
			setupAjaxLinks('#mainContentText');
			try {
				loadContactMap();
			} catch(caught) {}
		},
		onFailure: function(transport) {
			$('mainContentText').update(transport.responseText);
			setupAjaxLinks('#mainContentText');
		}
	});
}

function bindAjaxSearch(theForm) {
	queryValues = theForm.serialize();
	newTarget = "index.php?" + queryValues;
	
	getNewContent(newTarget);
}

function setupAjaxLinks(container) {
	if (container != undefined)
		var theLinks = $$(String(container) + ' a');
	else
		var theLinks = $$('a');
		
	for (var i = 0; i < theLinks.length; i++) {
		if ((theLinks[i].readAttribute('target') != '' && theLinks[i].readAttribute('target') != null) || (theLinks[i].readAttribute('rel') != null) || (theLinks[i].readAttribute('href') == '#')) {
			
		} else {
			var thisLink = theLinks[i];
			
			new Event.observe(thisLink, 'click', function() {
					getNewContent(this.readAttribute('href'));
					return false;
				}
			);
			theLinks[i].writeAttribute('onclick', 'return false;');
		}
	}
}

function toggleText(id) {
	if (!$('theRestOfIt_' + String(id)).visible()) {
		$('readMore_' + String(id)).style.display = 'none';
		new Effect.Appear($('theRestOfIt_' + String(id)), {
			from: 0.0,
			to: 1.0,
			duration: .4
		});
	} else {
		new Effect.Fade($('theRestOfIt_' + String(id)), {
			from: 1.0,
			to: 0.0,
			duration: .4,
			afterFinish: function() {
				$('readMore_' + String(id)).style.display = 'inline';
			}
		});
	}
}

var togglingMap = false;
function toggleMap() {
	if ($('worldMapContainer').visible()) {
		togglingMap = true;
		new Effect.Fade($('worldMapContainer'), { duration: .3, afterFinish: function() { togglingMap = false; } });
	} else
		showMap();
}

function showMap() {
	togglingMap = true;
	new Effect.Appear($('worldMapContainer'), { 
		duration: .3,
		afterFinish: function() {
			togglingMap = false;
			loadWorldMap();
		}
	});
}

function codeAddress(address, targetMap) {
	var geocoder;
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { address: address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: targetMap,
					icon: 'staticfiles/mapMarker.png'
				});
				
				var infowindow = new google.maps.InfoWindow({
					content: address
				});
				
				google.maps.event.addListener(marker, 'click', function() {
					infowindow.open(targetMap, marker);
				});
			}
		} else {
			//alert("Geocode was unsuccessful due to: " + status);
		}
	});
}
var map;
function loadWorldMap() {
	if ($('worldMap') != undefined) {
		var latlng = new google.maps.LatLng(40.5, -113.644);
		var mapOptions = {
			zoom: 4,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.TERRAIN
		};
		map = new google.maps.Map(document.getElementById("worldMap"), mapOptions);
		codeAddress("2801 Alaskan Way, Ste. 200, Seattle, WA 98121", map);
	}
}

new Event.observe(window, 'load', function() { setupAjaxLinks(); });

var contactMap;
function loadContactMap() {
	var latlng = new google.maps.LatLng(40.5, -113.644);
	var mapOptions = {
		zoom: 4,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.TERRAIN
	};
	contactMap = new google.maps.Map(document.getElementById("contactMap"), mapOptions);
	codeAddress("2801 Alaskan Way, Ste. 200, Seattle, WA 98121", contactMap);
}

function submitContactForm() {
	var goodToGo = true;
	
	if ($F('contactName') == '') {
		$('contactName').addClassName('errorHere');
		goodToGo = false;
	} else
		$('contactName').removeClassName('errorHere');
		
	if ($F('contactEmail') == '') {
		$('contactEmail').addClassName('errorHere');
		goodToGo = false;
	} else
		$('contactEmail').removeClassName('errorHere');
		
	if ($F('contactMessage') == '') {
		$('contactMessage').addClassName('errorHere');
		goodToGo = false;
	} else
		$('contactMessage').removeClassName('errorHere');
	
	if (goodToGo) {
		queryValues = $('contactForm').serialize(true);
		queryValues.task = 'sendEmail';
		new Effect.Fade('contactFormContainer', {
			duration: .3, 
			from: 1.0,
			to: 0.0,
			afterFinish: function() {
				new Ajax.Request('staticfiles/contactForm.php', {
					method: 'post',
					parameters: queryValues,
					onSuccess: function(transport) {
						$('contactFormContainer').update(transport.responseText);
						new Effect.Appear($('contactFormContainer'), { duration: .3, from: 0.0, to: 1.0 });
					}
				});
			}
		});
	}
}

function toggleSubMenu(button, targetMenu) {
	if (!$(targetMenu).visible()) {
		button.innerHTML = '&laquo;';
		new Effect.BlindDown($(targetMenu), {
			duration: .4
		});
	} else {
		new Effect.BlindUp($(targetMenu), {
			duration: .4,
			afterFinish: function() {
				button.innerHTML = '&raquo;';
			}
		});
	}
}

var currentPicture = 0;
function moveEGalPictureSliderLeft() {
	var totalPictures = $$('.singlePicture');
	totalPictures = totalPictures.length;
	new Effect.Fade($('eGalPicture_' + String(currentPicture)), {
		duration: .3,
		from: 1.0,
		to: 0.0,
		afterFinish: function() {
			newIndex = currentPicture - 1;
			if (newIndex == -1)
				newIndex = totalPictures - 1;
			
			currentPicture = newIndex;
			new Effect.Appear($('eGalPicture_' + String(currentPicture)), {
				duration: .3,
				from: 0.0,
				to: 1.0
			});
		}
	});
}

function moveEGalPictureSliderRight() {
	var totalPictures = $$('.singlePicture');
	totalPictures = totalPictures.length;
	new Effect.Fade($('eGalPicture_' + String(currentPicture)), {
		duration: .3,
		from: 1.0,
		to: 0.0,
		afterFinish: function() {
			newIndex = currentPicture + 1;
			if (newIndex >= totalPictures)
				newIndex = 0;
			
			currentPicture = newIndex;
			new Effect.Appear($('eGalPicture_' + String(currentPicture)), {
				duration: .3,
				from: 0.0,
				to: 1.0
			});
		}
	});
}

/*function menuButtonActivate(theButton, selfScope) {
	//morph height 46, line-height to 39, margin-top: 12
	theButton.addClassName('hover');
	new Effect.Morph(theButton, {
		style: 'height: 46px; line-height: 39px; margin-top: 12px;', // CSS Properties
		duration: 0.2, // Core Effect properties
		queue: { position: 'end', scope: selfScope, limit: 2 }
	});
}

function menuButtonDeactivate(theButton, selfScope) {
	//morph height 32, line-height to 32, margin-top 26
	new Effect.Morph(theButton, {
		style: 'height: 32px; line-height: 32px; margin-top: 26px;', // CSS Properties
		duration: 0.2, // Core Effect properties
		afterFinish: function() {
			theButton.removeClassName('hover');
		},
		queue: { position: 'end', scope: selfScope, limit: 2 }
	});
}

function bubbledFromChild(element, event)  { 
	var target = event.element(); 
	if (target === element) target = event.relatedTarget; 
		return (target && target.descendantOf(element)); 
} 

// onmouseover="menuButtonActivate($(this), 'menubutton4');" onmouseout="menuButtonDeactivate($(this), 'menubutton4');"

new Event.observe(window, 'load', function() {
	$$('div.mainMenu.menuButton').each(function(item) {
		item.observe('mouseover', function(event) { 
			if (!bubbledFromChild(this, event)) 
				menuButtonActivate(this, this.id + 'scope');
		}).observe('mouseout', function(event) { 
			if (!bubbledFromChild(this, event)) 
				menuButtonDeactivate(this, this.id + 'scope'); 
		}); 
	});
});*/

function menuButtonActivate(theButton, selfScope) {
	//morph height 46, line-height to 39, margin-top: 12
	theButton.addClassName('hover');
	new Effect.Morph(theButton, {
		style: 'height: 46px; line-height: 39px; margin-top: 12px;', // CSS Properties
		duration: 0.2, // Core Effect properties
		queue: { position: 'end', scope: selfScope, limit: 1 },
  		transition: Effect.Transitions.sinoidal,
		afterFinish: function(transport) {
			new Effect.Morph(theButton, {
				style: 'height: 32px; line-height: 32px; margin-top: 26px;', // CSS Properties
				duration: 0.5, // Core Effect properties
				afterFinish: function() {
					theButton.removeClassName('hover');
				},
				queue: { position: 'end', scope: selfScope, limit: 1 },
				transition: Effect.Transitions.sinoidal
			});
		}
	});
}

function bubbledFromChild(element, event)  { 
	var target = event.element(); 
	if (target === element) target = event.relatedTarget; 
		return (target && target.descendantOf(element)); 
} 

// onmouseover="menuButtonActivate($(this), 'menubutton4');" onmouseout="menuButtonDeactivate($(this), 'menubutton4');"

new Event.observe(window, 'load', function() {
	$$('div.mainMenu.menuButton').each(function(item) {
		item.observe('mouseover', function(event) { 
			if (!bubbledFromChild(this, event)) 
				menuButtonActivate(this, this.id + 'scope');
		});
	});
});
