$(document).ready(function(){
	$("#calendar_month_selected").toggle(function(){
		var height = $("ul#calendar_months").height();
		$("ul#calendar_months").css({opacity:0.0, height: 0}).show().animate({opacity: 0.9, height: height}, 100);
	}, function(){
		$("ul#calendar_months").hide();
	});
	
	$("a[href^=#/month/]").click(function(){
		$("#calendar_month_selected span").text($(this).text());
		$("ul#calendar_months").hide();
		var month_number = $(this).attr('href').replace('#/month/', '');
		$(".calendar_days").removeClass("active");
		$("#calendar_days_"+month_number).addClass("active");
	});
	
	// Year picker setup
	
	$("#calendar_year_selected").toggle(function(){
		var height = $("ul#calendar_years").height();
		$("ul#calendar_years").css({opacity:0.0, height: 0}).show().animate({opacity: 0.9, height: height}, 100);
	}, function(){
		$("ul#calendar_years").hide();
	});
	
	// Frontend GMap setup
	
	if($("#gmap_frontend").length > 0)
	{
		gmap_frontend_setup();
	}
	
	$("#search input").one('click', function(){
		$(this).val('');
		$(this).addClass('focus');
	});
	
	$("div.select").hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	
	$("div.select .options span").click(function(){
		$(this).parent().parent().parent().find('div.selected').contents().remove();
		var cloned = $(this).clone().appendTo($(this).parent().parent().parent().find('div.selected'));
		if($(this).parent().parent().parent().hasClass('select_location'))
		{
			$("input#search_location").val(cloned.attr('title'));
		}
		if($(this).parent().parent().parent().hasClass('select_time'))
		{
			$("input#search_time").val(cloned.attr('title'));
		}
	});
	
	$(".print").click(function(){
		window.print();
		return false;
	});
});

var gmap_frontend_setup = function(){
	if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("gmap_frontend"));
		map.enableContinuousZoom();
    map.enableScrollWheelZoom();
		var ui_options = new GMapUIOptions('small');
				ui_options.maptypes.normal = true;
				ui_options.maptypes.satellite = false;
				ui_options.maptypes.hybrid = false;
				ui_options.maptypes.physical = false;
		map.setUI(ui_options);
		
		var address = $(".gmap_address").text();
		
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	        //alert(address + " not found");
	      } else {
	        map.setCenter(point, 14);
	        var marker = new GMarker(point, {draggable: false});
					map.clearOverlays();
	        map.addOverlay(marker);
	        marker.openInfoWindowHtml($(".gmap_info").html());
	      }
	   	}
		);
	}
}