/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 99 2008-02-04 16:31:09Z emartin24 $
 *
 */

$(document).ready(function () {
	$('a#ppModal').click(function (e) {
		//alert(document.getElementById("url").value);
		e.preventDefault();

		varHTML = '' +
'<div id="myMap" style="position:relative; height:400px; width:600px;"></div>' +
'' +
'<div id="dirSearch" style="position:relative; left:50%; margin-left:-303px; height:35px; width:600px; border-right:3px solid #CCCCCC; border-bottom:3px solid #CCCCCC; border-left:3px solid #CCCCCC; background:#EFEFDE url(/images/directionSearch.png) no-repeat right top;">' +
'  <div style="float:right;">' +
'    <table>' +
'      <tr>' +
'        <td class="bodyText">' +
'          <input id="fromAddress" type="text" value="Enter your address here for driving directions" onFocus="this.value=\'\'" onBlur="if (this.value==\'\') this.value=\'Enter your address here for driving directions\'" style="width: 320px; border: 1px solid #C6C0C2; border-top: 1px solid #7C7C7A; font-size: 10px; color: #666666;" />' +
'        </td>' +
'        <td>' +
'          <input name="image" id="CB2" onclick="getDirections(document.getElementById(\'fromAddress\').value)" type="image" style="margin: 0 0 0 9px;" src="/images/btn_google_search_go.gif" width="28" height="29" />' +
'        </td>' +
'      </tr>' +
'    </table>' +
'  </div>' +
'</div>';

		$(varHTML).modal({
				close: true,
				containerId: 'modalContainer',
				onOpen: modalOpen,
				onClose: modalClose

			});
	});
});

function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.animate({
			height: document.getElementById("hgt").value
		}, function () {
			
				dialog.container.animate({
					width: document.getElementById("wdt").value
				}, function () {						
					dialog.data.show();	
					GetMap();
				});				
			
		});
	});
}

function modalClose (dialog) {
	//alert(hgt);
	dialog.container.animate({
			height: 60
		}, function () {
			
				dialog.container.animate({
					width:60
				}, function () {	
						//dialog.container.fadeOut(1000);
						dialog.container.animate({
							height:0,
							width:0
						}, function () {									
								dialog.container.hide();
								dialog.overlay.fadeOut('slow', function () {
									dialog.container.fadeOut('slow', function () {
										$.modal.close();
									});
									//$.modal.close();
								});									
						});						
				});				
			
		});
}

var map = null;
      var pinid = 0;
            
      function GetMap()
      {
         map = new VEMap('myMap');
		 //map.AttachEvent("onchangeview", AddPushpin());
         map.LoadMap();
		 FindLoc();
		 setTimeout('AddPushpin()',3000);	
		 
		 
		 map.AttachEvent("onclick", onMouseOverCallback);



      }
	  function onMouseOverCallback(e)
		{
		   // check to see if we have hovered over a pushpin
		   if(e.elementID != null)
		   {
			//map.GetDirections(["Seattle", "Bellevue WA", "Microsoft"]);
		   }
		}
      function FindLoc()
      {
         try
         {	
			 //alert(document.getElementById("address").value + " " + document.getElementById("city").value + " " + document.getElementById("state").value);		
            map.Find(null, document.getElementById("address").value + " " + document.getElementById("city").value + ", " + document.getElementById("state").value);			

         }
         catch(e)
         {
            alert(e.message);
         }
      }
	  function AddPushpin()
      {
          
		  var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
          shape.SetTitle('');
          shape.SetDescription('');
          
          map.AddShape(shape);	  

      }

	    function getDirections(val)
      {
          var options = new VERouteOptions();
          options.RouteCallback = onGotRoute;
		  map.GetDirections([val, document.getElementById("address").value + " " + document.getElementById("city").value + ", " + document.getElementById("state").value], options);  

      }

	  function onGotRoute(route)
         {
           // Unroll route
           var legs     = route.RouteLegs;
           var turns    = "Total distance: " + route.Distance.toFixed(1) + " mi\n";
           var numTurns = 0;
           var leg      = null;

           // Get intermediate legs
            for(var i = 0; i < legs.length; i++)
            {
               // Get this leg so we don't have to derefernce multiple times
               leg = legs[i];  // Leg is a VERouteLeg object
                  
               // Unroll each intermediate leg
               var turn = null;  // The itinerary leg
                  
               for(var j = 0; j < leg.Itinerary.Items.length; j ++)
               {
                  turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
                  numTurns++;
                  turns += numTurns + ".\t" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)\n";
               }
            }

			alert(turns);		
         }


