/**************************************************************************
* Home page panel switcher
*
* Requires Prototype / Scriptaculous
**************************************************************************/
var mhp = {
	_data: [],
	_index: 0,
	_image_index: 0,
	_interval: 5,
	_effect_duration: 1,
	_loaded_data_index: 0,
	_loaded_image_index: 0,
	_preload_image: 0,
	_timer: -1,
	
	init: function(data) {
		// only add panels that have at least one image
		mhp._data = new Array();
		mhp._index = 0;
		mhp._image_index = 0;
		mhp._loaded_data_index = 0;
		mhp._loaded_image_index = 0;
		
		for(var i = 0, j = data.length; i < j; ++i) {
			if (data[i].images && data[i].images.length > 0) mhp._data.push(data[i]);
		}
		
		if (mhp._data.length > 0) {			
			// preload images
			mhp._preload_image = new Image();
			mhp._preload_image.onload = mhp._load_next_image;
			mhp._preload_image.src = mhp._data[0].images[0].replace(/\s/g, '%20');
			
			$('slideshow').down('.first').setAttribute('src', mhp._data[mhp._index].images[mhp._image_index].replace(/\s/g, '%20'));
			$('slideshow').down('.second').setAttribute('src', 
				mhp._data[((mhp._image_index + 1) % mhp._data[mhp._index].images.length == 0) ? (mhp._index + 1) % mhp._data.length : mhp._index].images[(mhp._image_index + 1) % mhp._data[mhp._index].images.length].replace(/\s/g, '%20')
			);
			$('slideshow').down('.second').hide();
			
			$('slideshow-text').down('.title').update(mhp._data[mhp._index].title);
			$('slideshow-text').down('.description').update(mhp._data[mhp._index].description);
			$('slideshow-text').down('.link').setAttribute('href', mhp._data[mhp._index].link);
			
			if (mhp._data[mhp._index].images.length > 1 || mhp._data.length > 1) {
				mhp._image_index = (mhp._image_index + 1) % mhp._data[mhp._index].images.length;
				mhp._index = (mhp._image_index == 0) ? (mhp._index + 1) % mhp._data.length : mhp._index;
				
				mhp._timer = window.setInterval(mhp._cycle_image, mhp._interval * 1000);
			}
		}
	}, 
	
	_cycle_panel: function() {
		mhp._index = (mhp._index + 1) % mhp._data.length;
		
		$('slideshow-text').visualEffect('fade', { duration: mhp._effect_duration / 2, afterFinish:function() {
				$('slideshow-text').down('.title').update(mhp._data[mhp._index].title);
				$('slideshow-text').down('.description').update(mhp._data[mhp._index].description);
				$('slideshow-text').down('.link').setAttribute('href', mhp._data[mhp._index].link);
				
				$('slideshow-text').visualEffect('appear', { duration: mhp._effect_duration / 2 });
			}
		});
		
		//mhp._cycle_image();
	},
	
	_cycle_image: function() {
		if (mhp._image_index == 0) {
			mhp._cycle_panel();
		}
		
		$('slideshow').down('.second').show();
		$('slideshow').down('.first').visualEffect('fade', { duration: mhp._effect_duration, afterFinish:function() {
				// clean up
				$('slideshow').down('.first').setAttribute('src', mhp._data[mhp._index].images[mhp._image_index].replace(/\s/g, '%20'));
				$('slideshow').down('.first').show();
				
				mhp._image_index = (mhp._image_index + 1) % mhp._data[mhp._index].images.length;
				$('slideshow').down('.second').hide().setAttribute('src', 
					mhp._data[
						(mhp._image_index == 0) ? (mhp._index + 1) % mhp._data.length : mhp._index
					].images[mhp._image_index].replace(/\s/g, '%20')
				);					
			}
		});
	},
	
	_load_next_image: function() {
		mhp._loaded_image_index = (mhp._loaded_image_index + 1) % mhp._data[mhp._loaded_data_index].images.length;
		
		if (mhp._loaded_image_index == 0) {
			mhp._loaded_data_index = (mhp._loaded_data_index + 1) % mhp._data.length;
			if (mhp._loaded_data_index == 0) return;
		}
		
		mhp._preload_image.src = mhp._data[mhp._loaded_data_index].images[mhp._loaded_image_index].replace(/\s/g, '%20');
	}
};

function check_submit() {
	var enabled = true;
	
	$('agreement-boxes').select('input[type=checkbox]').each(function(s) {
		if ($F(s) != 1) {
			enabled = false;
		}
	});
	
	$('submit-button').disabled = !enabled;
}

/**************************************************************************
* Google Maps
**************************************************************************/
var metro_maps = {
	
	init: function (el, latitude, longitude, marker_title) {
		var loc = new google.maps.LatLng(latitude, longitude);
		var options = {	zoom: 11, mapTypeId: google.maps.MapTypeId.ROADMAP };
		var map = new google.maps.Map(el, options);
		
		var marker = new google.maps.Marker({
			position: loc, 
			map: map, 
			title: marker_title
		});
		
		map.setCenter(loc);
		
	}
}
