/**
 * Churches Class
 *
 * $Id: churches.js 103 2008-05-27 16:34:40Z samsoffes $
 *
 * @package	   One Prayer
 * @author     Sam Soffes
 * @copyright  (c) 2008 LifeChurch.tv
 */

var Churches = new Class({
		
	initialize: function() {

		this.container = $('churches_table_container');
		this.table = $('churches_table');
		
		// Display it
		//this.display('name');
		
		// Add events
		this.table.getElements('thead a').each(function(el, i) {
			el.addEvent('click', function(e) {
			
				// Change the color of the links
				el.getParent().getParent().getElements('a').each(function(temp, x) {
					temp.removeClass('selected');
				});
				
				el.addClass('selected');
							
				// Sort
				var field = el.getProperty('rel');
				this.display(field);
				
			}.bind(this));
		}.bind(this));
		
		// Add to top event
		$('church_table_totop').addEvent('click', function(e) {
			 var scroll = new Fx.Scroll(window).toElement('the_list');
		}.bind(this));
	},
	
	sortList: function(sortBy) {
		var list = list_of_churches;
		
		if(sortBy != 'attendance') {
			list.sort(
				function(a, b) {
					if(eval('a.'+sortBy).toLowerCase() <  eval('b.'+sortBy).toLowerCase()) return -1;
					else if(eval('a.'+sortBy).toLowerCase() >  eval('b.'+sortBy).toLowerCase()) return  1;
					else if(eval('a.'+sortBy).toLowerCase() == eval('b.'+sortBy).toLowerCase()) return  0;
				}
			);
		} else {
			list.sort(
				function(a, b) {
					if(parseInt(eval('a.'+sortBy)) <  parseInt(eval('b.'+sortBy))) return -1;
					else if(parseInt(eval('a.'+sortBy)) >  parseInt(eval('b.'+sortBy))) return  1;
					else if(parseInt(eval('a.'+sortBy)) == parseInt(eval('b.'+sortBy))) return  0;
				}
			);
		}
		
		return list;
	},
	
	display: function(sortBy) {
	
		var list = this.sortList(sortBy);
	
		// Remove any old data if it's there
		if(this.table.getElement('tbody')) {
			this.table.getElement('tbody').dispose();
			$('indicator').setStyle('display', 'block');
		}
		
		var tbody = new Element('tbody');
		
		// Loop throught the churches
		$each(list, function(church, key) {
			var tr = new Element('tr', {
				'class': ((key % 2) ? 'shaded' : 'no-shaded')
			});
			new Element('td', {
				'html': church.name
			}).inject(tr);
			new Element('td', {
				'html': church.pastor
			}).inject(tr);
			if(typeof(admin_view) != "undefined") {
				new Element('td', {
					'html': church.attendance
				}).inject(tr);
			}
			new Element('td', {
				'html': church.city+((church.country == 'US' && church.state != '') ? ', '+church.state : '')
			}).inject(tr);
			new Element('td', {
				'html': church.country
			}).inject(tr);
			tr.inject(tbody);
		});
		
		// Display it
		tbody.inject(this.table);
		$('indicator').setStyle('display', 'none');
		
		// Clean up
		delete list;
		delete tbody;
	}
});

var ChurchMap = new Class({
	
	map: false,
	
	initialize: function(element) {

		// Add button Event
		if($('open_map_button')) {
			var open = false;
			$('open_map_button').addEvent('click', function(e) {
				var height = ((open) ? 0 : 358);
				var myEffect = new Fx.Morph('map_outer', {duration: 500, transition: Fx.Transitions.Sine.easeOut}); 
				myEffect.start({
					'height': height
				});
				open = ((open) ? false : true);
				if(open) $('open_map_button').setHTML(close_map);
				else $('open_map_button').setHTML(view_map);
			});
		}

/*		// Be sure to unload the Google stuff
		window.addEvent('unload', function() {
			GUnload();
		});
		
		// Setup the custom icon
		var iconRed = new GIcon();
		iconRed.image = "/img/shared/marker_med_red.png";
		iconRed.shadow = "/img/shared/marker_med_shadow.png";
		iconRed.iconSize = new GSize(12, 20);
		iconRed.shadowSize = new GSize(22, 20);
		iconRed.iconAnchor = new GPoint(6, 20);
		iconRed.infoWindowAnchor = new GPoint(6, 20);
	
		if (GBrowserIsCompatible()) {

			// Setup map
			this.map = new GMap2(element);
			//this.map.addControl(new GSmallMapControl());
			//this.map.addControl(new GMapTypeControl());
			this.map.setCenter(new GLatLng(43.58039085560784, 13.359375));
			this.map.setZoom(1);
			this.map.setMapType(G_PHYSICAL_MAP);
						
			// Loop throught the churches
			$each(list_of_churches, function(church, key) {
				//alert(parseFloat(church.lat)+' '+parseFloat(church.lng))
				var point = new GLatLng(parseFloat(church.lat), parseFloat(church.lng));
				var marker = new GMarker(point, {icon:iconRed});
				GEvent.addListener(marker, 'click', function() {
					var href = ((church.website.substr(0, 4) != 'http') ? 'http://'+church.website : church.website);
					this.map.openInfoWindowHtml(point, '<div class="info_window"><h4>'+church.name+'</h4><p>'+church.pastor+'</p><p><a target="_blank" href="'+href+'">'+church.website+'</a></p></div>');
				}.bind(this));
				this.map.addOverlay(marker);
			}.bind(this));
		}
*/
	}
});

var churchmap;
var churches;
window.addEvent('domready', function() {
	//if($('map') && !Browser.Engine.trident4) {
		churchmap = new ChurchMap($('map'));
	//}
	
	churches = new Churches();
});