$(document).ready(function(){

	$('.top-search input').focus(function(){
		$(this).val('');
	});
	
	$('.top-search input').blur(function(){
		if($(this).val() == ''){
			$(this).val('SEARCH')
		};
	});	

	if($('#top-slider').length){

		$('#top-slider ul').cycle({ 
		    fx:  'fade', 
		    pause: true,
		    speed:  'slow', 
		    timeout: 4000
		});	
		
	};
	
	// when the button is clicked, run toggle function
	$('#btn-donate').click(toggleDonate);
	
	
	function toggleDonate(){
	
		// if it's open already, close it and remove open class
		if($('#btn-donate').hasClass('open')){
		
			$('div#div-donate').animate({left: '-390'});		
			
			$('a#btn-donate').animate({
				left: '0'
			},function(){
			
				$('a#btn-donate').removeClass('open');
			
			});						
			
		}
		
		// if it's not open, open it and add open class
		else{
		
			$('div#div-donate').animate({left: '0'});		
			
			$('a#btn-donate').animate({
				left: '390'
			},function(){
			
				$('a#btn-donate').addClass('open');
			
			});			
		
		}
	
	};
	
	// if anything other than the donate div is clicked
	$('body > div:not(#div-donate)').click(function(){
					
		if($('a#btn-donate').hasClass('open')){
			toggleDonate();
		}
	
	});
	
	
	
	if($('#map').length){
	
		jQuery.metadata.setType("attr", "data");
		
		$("#map").goMap({
			//latitude: 38.627063,
			//longitude: -90.314484,
			address: 'St. Louis, Missouri',
			zoom:7,
			maptype: 'ROADMAP',
		    navigationControl: true,
			scrollwheel: true,
		    mapTypeControl: false,
			disableDoubleClickZoom: false,
			addMarker: false,
			icon: 'http://stl-salvationarmy.org.s51588.gridserver.com/wp-content/themes/salvationarmy/images/pin_sm.png'	
		});
		
		// get all of the address divs
		var addresses = $('.addressblock');
		
		// loop through each one
		$.each(addresses, function(i,address){
			
			// access the element
			var thisNode = $(address);
			
			// get the id
			var thisID = thisNode.attr('id');	
			
			// get the content
			var thisContent = thisNode.html();	
			
			// access meta data
			var myMeta = thisNode.metadata();
			
			// set long/lat
			var thisLatitude = myMeta.latitude;
			var thisLongitude = myMeta.longitude;
			
			// add to the map
			$.goMap.createMarker({
				latitude: thisLatitude,
				longitude: thisLongitude,
				id: thisID,
				html: {
					content: thisContent
					}		
			});
			
			// setup a click event
			$(this).click(function(){ 
				google.maps.event.trigger($($.goMap.mapId).data(thisID), 'click');
			});	
		
		});
		
		$('.addressblock').click(function(){
						
		    var new_position = $('#map').offset();
		    		    
		    window.scrollTo(new_position.left,new_position.top-15);
		    return false;			
		
		});
		
		$('.addressblock h4').attr('title','Click to view on map');
		
	
	
	};
	
	
	

	
	
});
