/**
 * ISLS.js, initializes ISLS object for use in other scripts on ISLS.com
 * Written by Andrew Hedges, andrew (at) hedges (dot) name, http://andrew.hedges.name
 * All rights reserved by Dana Garrison, dana (at) isls (dot) com
 */

if (undefined === ISLS) {
	var ISLS = {};
}

/**
 * ISLS.ui handles interface-related functions, such as image rollovers
 */
ISLS.ui = (function () {
	return {
		img: {
			hi: function (id) {
				var el;
				el = $(id);
				el.src = el.src.replace(/-lo\./, '-hi.');
			},
			lo: function (id) {
				var el;
				el = $(id);
				el.src = el.src.replace(/-hi\./, '-lo.');
			}
		}
	}
})();

/**
 * ISLS.maps initializes and manages the Google Maps on the homepage of ISLS.com
 * Written by Andrew Hedges, andrew (at) hedges (dot) name, http://andrew.hedges.name
 * All rights reserved by Dana Garrison, dana (at) isls (dot) com
 *
 * Requires: prototype.js and maps.google.com to be loaded prior to initialization
 */

ISLS.maps = (function () {
	/*********************/
	/* private variables */
	var _template, _locations, _gmap, _bounds;
	
	// template for HTML in info balloons
	_template  = new Template('<div class="isls-info"><h3><a href="#{href}"><img src="#{img}" alt="" />#{name}</a></h3><p>#{desc}</p><p><a href="#{href}">Find out more&#8230;</a></p></div>');
	
	// location information, used to create markers
	_locations = [
		{
			name : 'San Jose (San Pedro)',
			desc : 'Capital City Suburb.<br/>Near University.',
			href : '/costarica/schools/sanjose.cfm',
			img  : '/images/2007/costarica/sanpedro-school.jpg',
			lat  : 9.928462,
			lng  : -84.052320
		},
		{
			name : 'Arenal (La Fortuna)',
			desc : 'Active Volcano with Amazing<br/>Light Show at Night!',
			href : '/costarica/schools/arenal.cfm',
			img  : '/images/2007/costarica/schools_students.jpg',
			lat  : 10.470848,
			lng  : -84.642663
		},
		{
			name : 'Heredia',
			desc : 'Central Valley Town.<br/>30 minutes to Capital.',
			href : '/costarica/schools/heredia.cfm',
			img  : '/images/2007/costarica/heredia_inside.jpg',
			lat  : 9.982038,
			lng  : -84.091072
		},
		{
			name : 'Monteverde',
			desc : 'Cloud Forest Preserve.<br/>Bird Watching Galore!',
			href : '/costarica/schools/monteverde.cfm',
			img  : '/images/2007/costarica/monteverde-students.jpg',
			lat  : 10.307826,
			lng  : -84.805870
		},
		{
			name : 'Turrialba',
			desc : 'Mountain Valley Town.<br/>Very Safe &amp; Immersive.',
			href : '/costarica/schools/turrialba.cfm',
			img  : '/images/2007/costarica/turrialba_class.jpg',
			lat  : 9.903414,
			lng  : -83.677883
		},
		{
			name : 'Playa Dominical',
			desc : 'Small Rainforest Town.<br/>Great Surf!',
			href : '/costarica/schools/playadominical.cfm',
			img  : '/images/2007/costarica/dominical_class.jpg',
			lat  : 9.254699,
			lng  : -83.863106
		},
		{
			name : 'Playa Tamarindo',
			desc : 'Beach Resort in Dry Forest<br/>Area. Night Life!',
			href : '/costarica/schools/playatamarindo.cfm',
			img  : '/images/2007/costarica/tamarindo_school.jpg',
			lat  : 10.292119,
			lng  : -85.842104
		},
		{
			name : 'Playa Manuel Antonio',
			desc : 'Rainforest Resort Town.<br/>National Park!',
			href : '/costarica/schools/playamanuelantonio.cfm',
			img  : '/images/2007/costarica/manuelantonio-students.jpg',
			lat  : 9.393855,
			lng  : -84.151325
		}
	];
	
	/*******************/
	/* private methods */
	var _addMarker, _preloadImg, _openInfoWindow;
	
	// add a marker to the map
	_addMarker = function (location, bounds) {
		var point, marker;
		point        = new GLatLng(location.lat, location.lng);
		marker       = new GMarker(point);
		marker.props = location;
		GEvent.addListener(marker, 'click', _openInfoWindow);
		_gmap.addOverlay(marker);
		_bounds.extend(point);
	};
	
	// preload a location image
	_preloadImg = function (location) {
		var img;
		img = new Image();
		img.src = location.img;
	};
	
	// open an info balloon when a marker is clicked
	_openInfoWindow = function () {
		var html;
		with (this.props) {
			html = _template.evaluate({href: href, name: name, img: img, desc: desc});
		}
		this.openInfoWindow(html);
	};
	
	/********************************/
	/* public variables and methods */
	return {
		// initialize the widget
		init: function () {
			// local variables
			var zoom, types, bounds, centerLat, centerLng;
			if (GBrowserIsCompatible()) {
				// prepare the map
				zoom    = new GLargeMapControl();
				types   = new GHierarchicalMapTypeControl();
				_bounds = new GLatLngBounds();
				_gmap   = new GMap2(document.getElementById('google-map'));
				_gmap.setCenter(new GLatLng(9.7, -84.25), 7); // center must be set before markers can be added
				_gmap.addControl(zoom);
				_gmap.addControl(types);
				
				// add the locations
				$A(_locations).each(function (location) {
					_addMarker(location);
					_preloadImg(location);
				});
				
				centerLat = (_bounds.getNorthEast().lat() + _bounds.getSouthWest().lat()) / 2;
				centerLng = (_bounds.getNorthEast().lng() + _bounds.getSouthWest().lng()) / 2;
				_gmap.setCenter(new GLatLng(centerLat, centerLng), _gmap.getBoundsZoomLevel(_bounds));
				
				// make sure we don't leak memory in IE
				Event.observe(window, 'unload', GUnload);
			}
		}
	};
})();

/**
 * ISLS.topics initializes and manages the animated featured topics area of ISLS.com
 * Written by Andrew Hedges, andrew (at) hedges (dot) name, http://andrew.hedges.name
 * All rights reserved by Dana Garrison, dana (at) isls (dot) com
 *
 * Requires: prototype.js and animator.js to be loaded prior to initialization
 */

ISLS.topics = (function () {
	/*********************/
	/* private variables */
	var _classNames, _styles, _styleObjs, _options, _that, _topics, _boundFunctions;
	
	// CSS class names
	_classNames = {
		open: 'open',
		closed: 'closed'
	};
	
	// CSS styles
	_styles = {
		open: 'border-color: #7196f3;height: 190px;',
		closed: 'border-color: #cccccc;height: 22px;'
	};
	
	// create objects that can be used by Element.setStyle
	_styleObjs = $H({});
	
	// animation options
	_options = {
		duration: 250, // how long the animation will take, in milliseconds
		interval: 13, // how long between iterations
		onComplete: function () {
			_restoreEventListeners();
		}
	};
	
	_boundFunctions = {};
	
	/*******************/
	/* private methods */
	var _restoreEventListeners, _removeEventListeners;
	
	// activate mouseover events
	_restoreEventListeners = function () {
		_topics.each(function (topic) {
			Event.observe(topic, 'mouseover', _boundFunctions[topic.id]);
		});
	}
	
	// de-activate mouseover events
	_removeEventListeners = function () {
		_topics.each(function (topic) {
			Event.stopObserving(topic, 'mouseover', _boundFunctions[topic.id]);
		});
	}
	
	/********************************/
	/* public variables and methods */
	return {
		// initialize the widget
		init: function () {
			// local variables and methods
			var styleStringToObj;
			
			// convert a string of CSS styles to an object
			styleStringToObj = function (s) {
				var styleObj, styles, props;
				styleObj = $H({});
				styles = s.split(';');
				for (var i = 0, len = styles.length - 1; i < len; ++i) {
					props = styles[i].split(':');
					styleObj[props[0].strip()] = props[1].strip();
				}
				return styleObj;
			}
			
			// convert style strings to objects
			_styleObjs.open   = styleStringToObj(_styles.open);
			_styleObjs.closed = styleStringToObj(_styles.closed);
			
			// store the current context (ISLS.topics)
			_that = this;
			
			// get all topic divs
			_topics = $$('div.topic div.container');
			
			// for each topic, track the open div
			_topics.each(function (topic) {
				// save bound event listener functions
				_boundFunctions[topic.id] = _that.animateDivs.bindAsEventListener(topic);
				
				(topic.childElements()).each(function (subjectDiv) {
					if (_classNames.open === subjectDiv.className) {
						subjectDiv.setStyle(_styleObjs.open);
						topic.openDiv = subjectDiv;
					} else {
						subjectDiv.setStyle(_styleObjs.closed);
					}
				});
			});
			
			// initialize event handlers
			_restoreEventListeners();
		},
		// animate the divs
		animateDivs: function (e) {
			// local variables
			var el, animation;
			
			// get the element that was hit by the event
			el = Event.findElement(e, 'DIV');
			
			if (_classNames.closed === el.className) {
				// don't let any other events interfere
				_removeEventListeners();
				
				// set up the animation				
				animation = new Animator(_options)
					.addSubject(new CSSStyleSubject(this.openDiv, _styles.open, _styles.closed))
					.addSubject(new CSSStyleSubject(el, _styles.closed, _styles.open))
				;
				
				// swap the class names
				el.className = _classNames.open;
				this.openDiv.className = _classNames.closed;
				
				// track the open div
				this.openDiv = el;
				
				// start the animation				
				animation.seekTo(1);
			}
		}
	}
})();

/*	Initialize surf photos gallery
*/
document.observe('dom:loaded', function() {
	$$('#photo-thumbs img').each(function (img) {
		Event.observe(img, 'click', function () {
			$$('#photo-holder img')[0].src = this.src;
		});
	});
});
