// JavaScript Document

var j$ = jQuery;

j$(document).ready(init);
j$(window).load(loaded);

var windowWidth = 400;
var windowHeight = 400;

var $toAddress = 'boo';



function init()
{
	//attach extended stylesheet
	j$('head').append('<link href="assets/templates/ask/css/extended.css" rel="stylesheet" type="text/css" media="screen" />');
	
	
	//ASSIGN VALUES TO GLOBALS
	windowWidth = j$(window).width();
	windowHeight = j$(window).height();

	//TABLE RESET
	j$('table').attr({'cellspacing':0, 'width':'100%'});
	j$('td').attr({'valign':'top'});
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
	//apply target="blank" to OUTGOING LINKS
	j$('a').each(function(i){
			var h=j$(this).attr('href');
			if(h.indexOf('http://')>-1)
			{
				var cI = h.indexOf('ask-law');
				if(cI<0 || cI>12)
				{
					j$(this).attr({'target':'_blank'});
				}
			}
		})
}

function loaded()
{
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
	//directions map
	
	if(j$('body').hasClass('locationPage'))
	{
		 $toAddress = j$('#ourAddress address').text();
		 $toAddress = j$.trim($toAddress);
		 
		 var directionsDisplay;
		 var directionsService;
		 var geocoder;
		 var map;
		 function initMap()
		 {
			 geocoder = new google.maps.Geocoder();
			 var latlng = new google.maps.LatLng(-34.397, 150.644);
			 var myOptions = {
				 zoom:14,
				 center: latlng,
				 mapTypeId: google.maps.MapTypeId.ROADMAP
				}
			map = new google.maps.Map(document.getElementById("mapCanvas"),myOptions);
		 }
		 
		 function codeAddress(a)
		 {
			 if(geocoder)
			 {
				 geocoder.geocode({'address':a},function(results,status)
				  {
					  if(status == google.maps.GeocoderStatus.OK)
					  {
						  map.setCenter(results[0].geometry.location);
						  var marker = new google.maps.Marker({'map':map, 'position':results[0].geometry.location});
					  }
					 else
					 {
						 alert("Unsuccessful Geocoding: " + status)
					 }
				  });
			 }
			 else
			 {
				 alert('could not geolocate');
			 }
		 }
		 
		 function directionsHandler()
		 {
			 directionsService = new google.maps.DirectionsService();
			 directionsDisplay = new google.maps.DirectionsRenderer();
			 directionsDisplay.setMap(map);
			 directionsDisplay.setPanel(document.getElementById("directions"));
			 
			 var start = 
					  document.getElementById("street").value
					  + " " + document.getElementById("city").value
					  + " " + document.getElementById("state").value
					  + " " + document.getElementById("zip").value;
			 var end = $toAddress;
			 
			 var request =
			 {
				 origin:start,
				 destination:end,
				 travelMode: google.maps.DirectionsTravelMode.DRIVING
			 };
			 directionsService.route(request, function(result,status){
				if(status == google.maps.DirectionsStatus.OK)
				{
					directionsDisplay.setDirections(result);
				}
				else
				{
					alert('oops');
				}
			});
			
			return false;
		 }
		 
		 initMap();
		 codeAddress($toAddress);
		 
		 
		j$('#directionForm input').focus(function(){
			  j$('#directionForm').bind('submit',directionsHandler);
			  j$('#directionForm input').unbind();
		  });
	}
}

function displayPoint(marker)
{
	var pointname = marker.name;
	var contents = "<h4>"+ pointname +"</h4>";
	
//			map.panTo(marker.getPoint());
	var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
	j$("#message").show().css({ top:markerOffset.y, left:markerOffset.x, position:'absolute' }).html(contents);
}
function hidePoint(marker)
{
	j$("#message").hide();
}

function overlayDirections()
{
	fromAddress =
	  document.getElementById("street").value
	  + " " + document.getElementById("city").value
	  + " " + document.getElementById("state").options[document.getElementById("state").selectedIndex].value
	  + " " + document.getElementById("zip").value;
	gdir.load("from: " + fromAddress + " to: " + $toAddress);
}

