$(document).ready(function() {
	
	//+------------------------------
	// Mega menu
	//+------------------------------
    jQuery("#nav .top").megamenu({ 'show_method':'simple', 'hide_method': 'simple' });
    	
  	//+------------------------------
	// Publications UI fixes
	//+------------------------------ 
	
	// Zebra Stripe News Tables
	$('.news-block table tr:even').addClass('even');
	
	// Table Sorter
	$('#pool-cars, #pool-trucks').tablesorter({
		sortList: [[0,1],[1,0]],
		widgets: ['zebra']
	});
	
  	//+------------------------------
	// Executive Leadership
	//+------------------------------

	$(".executives").tabs(".executives div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 0});
	
  	//+------------------------------
	// Global Map
	//+------------------------------
	
	$(".global_map a").tooltip({relative: 'true', position: 'top center', predelay: 300, offset: [5, 0]});
	
	//+------------------------------
	// Form validation
	//+------------------------------
	
	$("#paw_reg_info").validator();
    
    
 	//+------------------------------
	// Events Panel
	//+------------------------------
	
	$('a.events_activate').click(function(event) {
		event.preventDefault();
		if($('#events_panel div').length === 0) {
			$('#events_panel').html('<p><img src="/gfx/ui/ajax-loader.gif" width="16" height="16" /></p>');
		}
		$('#events_panel').load('/site/news_events_panel');
		$('div#events_panel').slideToggle(300);
		$('a.events_activate').toggleClass("panel_open");
		return false;
	});
	
	$('#events_panel').live('mouseover mouseout',function(){
		$(".events_wrapper .event_info").tooltip({relative: 'true', position: 'top center', predelay: 300, offset: [5, 0]});
	});
	
    
	//+------------------------------
	// News Events Scroller
	//+------------------------------
		
	var news_count = 52;
	
	var noffset = 0;
	
	$('#news_next a').live('click', function(event){
		event.preventDefault();
		var newcount = news_count - 5;
		if(newcount < 0){
			return false;
		}
		noffset = noffset + 5;
		$('.news_wrapper ul').fadeTo("fast", 0, function() {
			$(this).load('/ari_news/news_list?offset='+noffset, function() {$(this).fadeTo("fast",1);});
		});
		$('#news_previous').html('<a href="/about/news/archive/"><img src="/gfx/ui/events_panel_arrows_previous.png" alt="Previous" /></a>');
		newcount = newcount - 5;
		if(noffset > newcount){
			$('#news_next').html('<img src="/gfx/ui/events_panel_arrows_next_off.png" alt="" />');
		} else {
			$('#news_next').html('<a href="/about/news/archive/"><img src="/gfx/ui/events_panel_arrows_next.png" alt="Next" /></a>');
		}
	});
	
	$('#news_previous a').live('click', function(event){
		event.preventDefault();
		if(noffset <= 0){
			$('#news_previous').html('<img src="/gfx/ui/events_panel_arrows_previous_off.png" alt="Previous" />');
			return false;
		}
		noffset = noffset - 5;
		$('.news_wrapper ul').fadeTo("fast", 0, function() {
			$(this).load('/ari_news/news_list?offset='+noffset, function() {$(this).fadeTo("fast",1);});
			$('#news_next').html('<a href="/about/news/archive/"><img src="/gfx/ui/events_panel_arrows_next.png" alt="Previous" /></a>');
			if(noffset <= 0){
				$('#news_previous').html('<img src="/gfx/ui/events_panel_arrows_previous_off.png" alt="Previous" />');
			} else {
				$('#news_previous').html('<a href="/about/news/archive/"><img src="/gfx/ui/events_panel_arrows_previous.png" alt="Previous" /></a>');
			}
		});
	});
	
	var ecount = 13;
	var eoffset = 0;
	
	$('#events_next a').live('click', function(event){
		event.preventDefault();
		var newecount = ecount - 6;
		if(newecount < 0){
			return false;
		}
		eoffset = eoffset + 6;
		$('.events_wrapper ul').fadeTo("fast", 0, function() {
			$(this).load('/calendar/events_list?offset='+eoffset, function() {$(this).fadeTo("fast",1);});
		});
		$('#events_previous').html('<a href="/about/calendar/"><img src="/gfx/ui/events_panel_arrows_previous.png" alt="Previous" /></a>');
		newecount = newecount -6;
		if(eoffset > newecount){
			$('#events_next').html('<img src="/gfx/ui/events_panel_arrows_next_off.png" alt="" />');
		} else {
			$('#events_next').html('<a href="/about/calendar/"><img src="/gfx/ui/events_panel_arrows_next.png" alt="Next" /></a>');
		}
	});
	
	$('#events_previous a').live('click', function(event){
		event.preventDefault();
		if(eoffset <= 0){
			$('#events_previous').html('<img src="/gfx/ui/events_panel_arrows_previous_off.png" alt="Previous" />');
			return false;
		}
		eoffset = eoffset - 6;
		$('.events_wrapper ul').fadeTo("fast", 0, function() {
			$(this).load('/calendar/events_list?offset='+eoffset, function() {$(this).fadeTo("fast",1);});
			$('#events_next').html('<a href="/about/calendar/"><img src="/gfx/ui/events_panel_arrows_next.png" alt="Previous" /></a>');
			if(eoffset <= 0){
				$('#events_previous').html('<img src="/gfx/ui/events_panel_arrows_previous_off.png" alt="Previous" />');
			} else {
				$('#events_previous').html('<a href="/about/calendar/"><img src="/gfx/ui/events_panel_arrows_previous.png" alt="Previous" /></a>');
			}
		});
	});
	
	//+------------------------------
	// Preload UI images
	//+------------------------------
	
	$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
	}

	// Usage:

	$(['/gfx/ui/events_panel_news_title.png','/gfx/ui/events_panel_events_title.png']).preload();
    
	//+------------------------------
	// Load Google Maps
	//+------------------------------
	
	if($('#map_canvas').length) {googlemaps();}
	
	$('#directions_form').submit(function(event){
		event.preventDefault();
		overlayDirections();
	});

}); //End $(document).ready

	var directionDisplay;
	var directionsService = new google.maps.DirectionsService();
	var map;
	var ari = new google.maps.LatLng(39.960038,-74.898575);
	var start;

	function googlemaps() {
		directionsDisplay = new google.maps.DirectionsRenderer();
		var ariOptions = {
			zoom: 9,
			center: ari,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		map = new google.maps.Map(document.getElementById("map_canvas"), ariOptions);
		directionsDisplay.setMap(map);
		directionsDisplay.setPanel(document.getElementById('directions'));
	
		var contentString = "<div>ARI Headquarters</div>";
		var infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		var marker = new google.maps.Marker({
			position: ari, 
			map: map,
			title:"ARI"
		});
		google.maps.event.addListener(marker, 'click', function() {infowindow.open(map,marker);}); 
	}
	
	function calcRoute(start) {
		var request = {
			origin: start, 
			destination: "4001 Leadenhall Road Mount Laurel, NJ 08054",
			travelMode: google.maps.DirectionsTravelMode['DRIVING']
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {directionsDisplay.setDirections(response);}
		});
		$('#directions .print').remove();
		$('#directions').prepend('<p class="print"><input type="button" class="form_but" value="Print Map" /></p>');
		$('#directions .print input').click(function() {
			window.print();
			return false;
		});
	}
	
	function overlayDirections() {
	    fromAddress =
	      document.getElementById("street").value
	      + " " + document.getElementById("city").value
	      + " " + document.getElementById("state").options[document.getElementById("state").selectedIndex].value
   		  + " " + document.getElementById("zip").value;
   		  calcRoute(fromAddress);
	}
