// Global variables
 var slid = 0;
 var busy = false;
 var icon = new GIcon();
 var map;

// Bind event listeners when DOM is loaded
 Event.observe(window, 'load', eventsListeners);
 
// Events bindings
 function eventsListeners(){
   if ($('events_wrapper')) {
     if ($('events_previous'))
	     $('events_previous').observe('click', slideRight);
	 if ($('events_next'))
	     $('events_next').observe('click', slideLeft);
     $$('#events_soon li a').each(function(el){
       el.observe('click', changeDate);
     });

     $$('#events_featured .pages a').each(function(el){
       el.observe('click', changeFeatured);
     });

     $$('#events_today .pages a').each(function(el) {
       el.observe("click",changeTodays);
     });
     $$('.emailit_link').each(function(el) {
     	el.observe("click",emailIt);
     });
     $$('.suitcaseit_link').each(function(el) {
     	el.observe("click",suitcaseIt);
     });
     if ($("events_search_home"))
     	$("events_search_home").observe("submit", function(ev) {
     		Event.stop(ev);
     		document.location.href = "/events/type/time/time/all/keywords/" + escape($("events_keywords").value) + "/";
     	});
     events_init();
   }
   if ($('events_module')) {
     $$('#events_module .pages a').each(function(el){
       el.observe('click', changeFeatured);
     });
   }
 }
 
 function events_init() {
   if (GBrowserIsCompatible()) {
     // initialize map
     if (document.getElementById("events_map")) {
	     map = new GMap2(document.getElementById('events_map'));
	     var visitors_center = new GLatLng('39.284187', '-76.613025'); // 401 Light St, Baltimore, MD 21202
	     map.setCenter(visitors_center, 13);
	     map.addControl(new GSmallMapControl());
	     eventsLoadToday();
	 }
   }
 }
 function slideLeft(ev) {
   Event.stop(ev);
   if (slid == 2 || busy == true)
   	return;
   busy = true;
   new Effect.MoveBy('events_cal', 0, -413, { afterFinish: function() { busy = false; } });
   slid++;
 }
 function slideRight(ev) {
   Event.stop(ev);
   if (slid == 0 || busy == true)
   	return;
   busy = true;
   new Effect.MoveBy('events_cal', 0, 413, { afterFinish: function() { busy = false; } });
   slid--;
 }
 function changeDate(ev) {
   Event.stop(ev);
   $$('#events_soon li a.active')[0].removeClassName('active');
   this.addClassName('active');
   $("events_comingsoon_list").innerHTML = '<img src="white-loader.gif" alt="Loading..." style="margin-left: 355px; margin-top: 100px;" />';
   new Ajax.Updater("events_comingsoon_list","ajax/event_soon_list.php?date=" + this.name);
 }
 function changeFeatured(ev) {
   //$$('#events_featured .pages a.active')[0].removeClassName('active');
   this.up().select('a.active')[0].removeClassName('active');
   this.addClassName('active');
   id = this.name;
   new Ajax.Updater("events_featured_details","ajax/event_featured_details.php?id=" + id, { evalScripts: true });
   Event.stop(ev);
 }

	function changeTodays(ev) {
		Event.stop(ev);
		id = this.name;
		if (id == "previous" || id == "next") {
			current = $$('#events_today .pages a.active')[0];
			if (id == "next")
				newid = current.next("a");
   			else
   				newid = current.previous("a");
   			if (newid.name == "next" || newid.name == "previous")
   				return;
   			current.removeClassName('active');
   			newid.addClassName("active");
   			$("events_todays_details").innerHTML = '<img src="blue-loader.gif" alt="Loading..." style="margin-left: 205px; margin-top: 105px;" />';
   			new Ajax.Updater("events_todays_details","ajax/event_todays_details.php?id=" + newid.name, { evalScripts: true });
   			return;
   		}
		$$('#events_today .pages a.active')[0].removeClassName('active');
		this.addClassName('active');
		$("events_todays_details").innerHTML = '<img src="blue-loader.gif" alt="Loading..." style="margin-left: 205px; margin-top: 105px;" />';
   		new Ajax.Updater("events_todays_details","ajax/event_todays_details.php?id=" + id, { evalScripts: true });
	}
	
	function loadMiniMap(id) {
		new Ajax.Request("ajax/map_action.php?action=point&type=event&id=" + id);
	}
	
	function emailIt(ev) {
		Event.stop(ev);
		var el = Event.element(ev);
		offset = el.cumulativeOffset();
		new Ajax.Request("ajax/email_it.php?left=" + offset[0] + "&top=" + offset[1] + "&id=" + el.name, { evalScripts: true });
	}
	
	function suitcaseIt(ev) {
		Event.stop(ev);
		var el = Event.element(ev);
		offset = el.cumulativeOffset();
				new Ajax.Request("ajax/login.php?left=" + offset[0] + "&top=" + offset[1], { evalScripts: true });
			}