var Site = {
	
	map:Object,
	directionsDisplay:Object,
	directionsService:Object,
	
	init : function()
	{
		$(document).pngFix();
		
		Site.directionsService = new google.maps.DirectionsService();
		
		$window = $(window);
		$(window).scroll(function(){
			var yPos = -$window.scrollTop()/5;
			var coords = '50% '+ yPos + 'px';
			$('body').css({ backgroundPosition:coords });
		});
		
		
		// menu items
		$('.categories a.button').bind('click', Site.change_category);
		$('.events a.button').bind('click', Site.change_event);
		
		
		
		// google map
		Site.directionsDisplay = new google.maps.DirectionsRenderer();
		var latlng = new google.maps.LatLng(44.4631, -87.9799);
		var myOptions = {
			zoom: 15,
			center: latlng,
			disableDefaultUI: true,
			panControl: true,
			zoomControl: true,
			mapTypeControl: false,
			scaleControl: false,
			streetViewControl: false,
			overviewMapControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		Site.map = new google.maps.Map(document.getElementById("location-directions-map"), myOptions);
		Site.directionsDisplay.setMap(Site.map);
		
		var marker = new google.maps.Marker({
			position: latlng, 
			map: Site.map, 
			title:"a'Bravo Bistro & Wine Bar"
		});
		$('#submitaddress').bind('click', Site.get_directions);
		$('#newdirections').bind('click', Site.new_directions);
		
		$('a.mainmenu').bind('click', Site.scroll_area);
		
		$('h1, a.mainmenu').each(function(index) {
			var text = $(this).html().replace('&amp;','<span class="amp">&amp;</span>');
			$(this).html(text);
			
		});
		
		// map guard
		$('#mapguard').bind('click', Site.hide_guard);
		$(window).bind('scroll', Site.show_guard);
	},
	
	show_guard: function()
	{
		window.log('show');
		$('#mapguard').show();
		$(window).unbind('scroll', Site.show_guard);
		return false;
	},
	
	hide_guard: function()
	{
		$(window).bind('scroll', Site.show_guard);
		$('#mapguard').hide();
		return false;
	},
	
	change_category: function(){
		
		$('a.activemenu').removeClass('activemenu');
		$('div.activecategory').removeClass('activecategory');
		$(this).addClass('activemenu');
		var id = $(this).attr('rel');
		$('#category-'+id).addClass('activecategory');
		
		return false;
	},
	
	change_event: function(){
		
		$('a.activeevent').removeClass('activeevent');
		$('div.activeevent').removeClass('activeevent');
		$(this).addClass('activeevent');
		var id = $(this).attr('rel');
		$('#event-'+id).addClass('activeevent');
		
		return false;
	},
	
	get_directions: function(){
		
		var directionsRequest = {
			origin: $('#street_address').val()+" "+$('#zip_code').val(),
			destination: "2069 Central Court #77 Green Bay, WI 54311",
			provideRouteAlternatives: false,
			travelMode: google.maps.TravelMode.DRIVING,
			unitSystem: google.maps.UnitSystem.IMPERIAL
		};
		
		Site.directionsService.route(directionsRequest, function(result, status) 
		{
		    if (status == google.maps.DirectionsStatus.OK) {
			
				$('#directionserror').hide();
				
		    	Site.directionsDisplay.setDirections(result);
				Site.map.panBy(-200,0);
				$('#directionsform').hide();
				var route = result.routes[0].legs[0];
				
				var dist = 0;
				var dur = 0;
				for(var i=0; i<route.steps.length; i++)
				{
					dist += route.steps[i].distance.value*0.000621371192;
					dur += route.steps[i].duration.value/60;
				}
				$('#directionsdistance').html(dist.toFixed(1));
				$('#directionsduration').html(Math.round(dur));
				
				var saddr = encodeURIComponent(directionsRequest.origin);
				$('#directionsresults').show();	
				$('#directionsbutton').attr('href','http://local.google.com/maps?saddr=' + saddr +  '&daddr=2069+Central+Ct,+Green+Bay,+WI+54311&hl=en');
		    }
			else
			{
				$('#directionserror').fadeIn();
			}
		});
		
		return false;
	},
	
	new_directions: function(){
		
		$('#street_address').val('');
		$('#zip_code').val('');
		$('#directionsform').show();
		$('#directionsresults,#directionserror').hide();	
		
		return false;
	},
	
	scroll_area: function(){
		
		var id = $(this).attr('rel');
		var off = (id!='home') ? -100 : 0;
		$.scrollTo($('#'+id),2000,{easing:'easeInOutQuint',offset:off});
		
		$('a.mainmenu.active').removeClass('active');
		$(this).addClass('active');
		
		// return false;
	},
	
	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		var email_pattern = new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);

		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked) || 
				($(this).hasClass('email') && !email_pattern.test($(this).val()))) 
			{
				$(this).parents("div.form").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.form").removeClass('error');
			}			
		});
		if( !submit_it ) 
		{
			$('#error_message').html('Please review the highlighted, required fields and resubmit.').fadeIn(250);
			$.scrollTo('#error_message', 800, {easing:'easeInOutCubic'});
		}
		return submit_it;
	}
};

window.log=function(){log.history=log.history||[];log.history.push(arguments);if(this.console){console.log(Array.prototype.slice.call(arguments));}};

$(document).ready(function() {
	Site.init();
});

