$(document).ready(function(){
    jQuery.fn.exists = function(){return jQuery(this).length>0;}
	
	if($("#slideshow").exists()){
		$("#slideshow").after('<a href="#" id="prev">&laquo;</a><a href="#" id="next">&raquo;</a>');	
		$("#slideshow").cycle({
			prev:   '#prev', 
		    next:   '#next',
			delay:  4000, 
			speed:  1000,
			pause:  1
		});
		
	}
	
	if($("a.image").exists()){
		$("a.image").fancybox();
	}
	
	if (document.getElementById('map')){

		function parseError() {
				$("#route").append('<p id="error">Invalid or incomplete address</p>');
		}

			if (GBrowserIsCompatible()) {

					var map = new GMap2(document.getElementById("map"), {navigationControl: true});
					map.setMapType(G_HYBRID_MAP);
					map.setCenter(new GLatLng(41.757807, -72.627367), 17);								
					var point = new GLatLng(41.757807, -72.627367);
					map.addOverlay(new GMarker(point));
					var mapControl = new GSmallZoomControl();
					map.addControl(mapControl);
					var directionsPanel = document.getElementById("route");
					var setDirections = new GDirections(map, directionsPanel);
					GEvent.addListener(setDirections, "error", function(){ parseError(); });
	      	}
	
			$("#inputAddress").focus(function() {
				if($(this).attr('value') == 'Enter address'){
					$(this).attr({value: ''});
				}
			});
			
			$("#inputAddress").blur(function() {
				if($(this).attr('value') == ''){
					$(this).attr({value: 'Enter address'});
				}
			});
			
			$("#calcRoute").submit(function() {
					if($("#inputAddress").val() != '' && $("#inputAddress").val() != 'Enter address') {
					$("#error").remove();
					
					var getAddress = $("#inputAddress").val();
						setDirections.load("from: " + getAddress + " to: 409 Silver Lane, East Hartford, CT 06118");
					}			
			      return false;
			    });
			
				
			
	
	}
	
});