var gdir, fromAddress, toAddress;  

$(function(){
	   	initialize();
})
function initialize() {
  
    //settings
    var companyMarkerImage= "/open/img/siv-marker.png";
    var companyLatLng     = new google.maps.LatLng(DataBridge.latPos, DataBridge.longPos);
    //var companyMarkerSize = new GSize(33, 43); //width, height
	
	  
	
    toAddress = DataBridge.toAddress;

    var defaultZoomLevel  = 13;
    //end settings

    //setup elements
	
	var myOptions = {
		zoom: defaultZoomLevel,
	    center: companyLatLng,
	    mapTypeControl: true,
	    mapTypeControlOptions: {
	      style: google.maps.MapTypeControlStyle.DEFAULT
	    },
	    zoomControl: true,
	    zoomControlOptions: {
	      style: google.maps.ZoomControlStyle.LARGE
	    },
		streetViewControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
  	}
	var directionsService = new google.maps.DirectionsService();

    var map   = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    var gdir  = new google.maps.DirectionsRenderer();
	gdir.setMap(map);
	gdir.setPanel(document.getElementById("direction-instructions"));

	
	//var largecontrol = new GLargeMapControl3D() ;
	//map.addControl(largecontrol); 
	
	//var mapTypeControl = new GMapTypeControl();
	//map.addControl(mapTypeControl);

    //error handler
    //GEvent.addListener(gdir, "error", handleErrors);

    //set company marker
    //var companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize);

    //set map center
    //map.setCenter(companyLatLng, defaultZoomLevel);
    var marker = new google.maps.Marker({
      position: companyLatLng, 
      map: map, 
      title:DataBridge.venueName,
	  animation: google.maps.Animation.DROP

  	}); 
	
	$('#showDirections').click(function(){
		$('#directions-loading').show();
		var start = ""
		$('#directions-error').hide();
		$('#directions-loading').show();
		if($('#street').val() != "" && $('#street').val() != "Street Address") start+= $('#street').val() + ", ";
		if($('#city').val() != "" && $('#city').val() != "City") start+= $('#city').val() + ", ";
		if($('#postcode').val() != "" && $('#postcode').val() != "Post Code") start+= $('#postcode').val() + "";
		
	
		//var start = $('#street').val() + ", " + $('#city') + ", " + $('#postcode').val();
 	 	var end = companyLatLng;
		  var request = {
		    origin:start, 
		    destination:end,
		    travelMode: google.maps.DirectionsTravelMode.DRIVING
		  };
		  directionsService.route(request, function(response, status) {
		    if (status == google.maps.DirectionsStatus.OK) {
		      gdir.setDirections(response);
		    }else{
				handleError(status)
			}
		  });
		  $('#directions-loading').hide();
		 return false;

	
	})

	$('input').enablePlaceholder();

	

}


function handleError(status){
	error = "";
	if(status == google.maps.DirectionsStatus.NOT_FOUND){
		error = "Sorry, we can't find a specific point for your address, try giving us just a post code?";
	}else if(status == google.maps.DirectionsStatus.ZERO_RESULTS){
		error = "Sorry we can't find a valid route between your address and our venue.";
	}else if(status == google.maps.DirectionsStatus.MAX_WAYPOINTS_EXEEDED){
		error = "Sorry, you seem to have specified too many waypoints.";
	}else{
		error = "Sorry, we are currently experiencing a problem with this service, please try again later";
	}
	
	$('#directions-error').html(error).show();

}


  function handleErrors(){
     if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
       alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
       alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
       alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_BAD_KEY)
       alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
       alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
     else alert("An unknown error occurred.");
  }

