﻿var map = null;
var routeLen = 0;
var pinID = 1;

function getMap(){
    if (map==null){
        map = new VEMap('map');
        map.LoadMap();  
    }
}

function getDD()
{
          
    map.GetRoute(saddr,
             daddr,
             null,
             null,
             onGotRoute);
}
function onGotRoute(route)
{

    routeLen = route.Itinerary.Segments.length;
    var routeinfo = "<table cellpadding='2'><tr><td colspan=\"2\">";
        routeinfo+="<div  id=\"routeSummary\"><div id=\"routeHeader\">Route info:</div>";
        routeinfo+="<table>"
            routeinfo+="<tr>"
            routeinfo+="<td id='startLabel'>Start: </td>"
            routeinfo+="<td style='font-weight:bold'>"
            routeinfo+=route.StartLocation.Address;
            routeinfo+="</td>"
            routeinfo+="</tr>"
            routeinfo+="<tr>"
            routeinfo+="<td id='endLabel'>End: </td>"
            routeinfo+="<td style='font-weight:bold'>"
            routeinfo+=route.EndLocation.Address ;
            routeinfo+="</td>"
            routeinfo+="</tr>"
        routeinfo+="</table>"
        routeinfo+="Total distance: ";
        routeinfo+= route.Itinerary.Distance+" ";
        routeinfo+= route.Itinerary.DistanceUnit+"<br />";
        routeinfo+="Total time: ";
        routeinfo+= route.Itinerary.Time+"</div>";   
        routeinfo+="<b>Driving Directions:</b>" 
    routeinfo+="</td>"
    routeinfo+="</tr>"

    var steps="";
    //steps+="<table cellpadding='2'>";
       for(var i = 0; i < routeLen ;i++)
       {
          steps+="<tr>"
          if (i==0)
            steps+="<td valign='top' width='30'><img src='images/mapicon_start.gif' border=0/></td>";
          else if (i+1 == routeLen)
            steps+="<td valign='top' width='30'><img src='images/mapicon_end.gif' border=0/></td>";
          else 
            steps+="<td valign='top' width='30' class='routeStepNumber'>" + i + "</td>";
         
          steps+="<td valign='top' nowrap>"
          steps+=route.Itinerary.Segments[i].Instruction+" (";
          steps+=route.Itinerary.Segments[i].Distance+") ";
          steps+=route.Itinerary.DistanceUnit+"</td>";
          steps+="<tr>"
       }
    steps+="</table>";
    routeinfo+=steps;

    $('#instr').html(routeinfo);
} 

function submitDD(){

    //get dd
    
    saddr = $('#inputStart').val();    
    daddr = $('#inputDest').val();  

    //todo: test variables
    if (this.saddr == '')
    {
        alert('Please enter the start address.');
        return;
    }
    
    if (this.daddr == '')
    {
        alert('Please enter the destination address.');
        return;
    }
    
    $('#instr').html(''); // todo: loading
    showRoute()
    deleteRoute();
    deletePin();    
    setTimeout("createDD()", 500);    
       
}

function createDD(){
    getMap();getDD();
}

 function deleteRoute()
 {
    try
    {
        map.DeleteRoute();
    }
    catch (err)
    {
        
    }
 }
 
function deletePin(id)
{  
    try
    { 
        if (id!=null)
        {
            map.DeletePushpin(id);
            pinID = pinID-1;
        }
        else
        {
            map.DeleteAllPushpins();
            pinID=1;
        }
    }
    catch (err)
    {
        
    }
} 

function submitMap(){

    
    this.whereRB = $('#inputWhere').val();    
    if (this.whereRB == 'Enter city, address or landmark') this.whereRB='';
    this.whatRB = $('#inputWhat').val();  
    if (this.whatRB == 'Enter business or category' ||
    this.whatRB == 'real estate' || 
    this.whatRB == 'real estate agent' ||
    this.whatRB == 'real estate broker' ||
    this.whatRB == 'real estate brokerage') this.whatRB='';
    
    //todo: test variables
    if (this.whatRB == '' && this.whereRB == '')
    {
        alert('Please enter either a location or a search term or both.');
        return;
    }
    
    // get map
    hideRoute();
    
    
    $('#instr').html('');
    deleteRoute();
    deletePin();
    setTimeout("createMap()", 500);     
    
}

function createMap(){
    getMap();
    if (this.whereRB!='' && this.whatRB!=''){
        find();
    }
    else if (this.whereRB==''){
        findNear();
    }
    else if (this.whatRB=='')
    {
        findLoc();
    }
}

function find(){
    //map.Find(this.whatRB, this.whereRB,'1',onFoundResultsFind);
    map.Find(this.whatRB, this.whereRB, VEFindType.Businesses, null, 0, 10, true, true, true, true, onFoundResultsFind);
    
}

function findNear()
{
    //map.FindNearby(this.whatRB, '1', onFoundResultsFindNear);
    map.Find(this.whatRB, null, VEFindType.Businesses, null, 0, 10, true, true, true, true, onFoundResultsFindNear);
        
}

function findLoc()
{
    //map.FindLocation(this.whereRB, onFoundResultsFindLoc);
    map.Find(null, this.whereRB, VEFindType.Businesses, null, 0, 10, true, true, true, true, onFoundResultsFindLoc);
}

function onFoundResultsFind(findResults)
{

    addPin($('#inputWhere').val(), '');
    if (findResults == null) return;
    for (r=0; r < findResults.length; r++)
    {
          addPin(findResults[r].Name, findResults[r].Description);          
    }
}

function onFoundResultsFindNear(findResults)
{
    if (findResults == null) return;
    for (r=0; r < findResults.length; r++)
    {
          addPin(findResults[r].Name, findResults[r].Description);          
    }
}

function onFoundResultsFindLoc(findResults)
{
    addPin($('#inputWhere').val(), '');
}

 function addPin(title, description)
 {   
    var pin = new VEPushpin(
       pinID, 
       map.GetCenter(), 
       null, 
       title, 
       description
       );
    map.AddPushpin(pin);
    pinID++;
 }   

function showRoute(){
    $('#instr').show();
}

function hideRoute(){

    $('#instr').hide();
}

function setMapWidth(){
    $('#map').width($('#mapHolder').width());
}

function load(){
    

    resizePhoto();
    setMapWidth();
    
    if (startPage == 'directions'){
        $('#container').tabs( 2, {
            fxSlide: false, fxFade: true, fxSpeed: 'fast'});  
    }
    else {
    $('#container').tabs( 1, {
            fxSlide: false, fxFade: true, fxSpeed: 'fast'});  
    }
    
    
    if (startPage == "directions" && saddr != "" && daddr != ""){
        $('#inputWhere').val(daddr);
        $('#inputStart').val(saddr);    
        $('#inputDest').val(daddr);                     
        showRoute();
        getMap();
        submitDD();
    }    
    else if (startPage == "directions" && daddr != ""){
        $('#inputWhere').val(daddr);
        $('#inputStart').val(saddr);    
        $('#inputDest').val(daddr);            
        this.whereRB = $('#inputWhere').val();
        createMap();
    }
    else if (startPage != "directions" && addr != "") {
        $('#inputWhere').val(addr);
        hideRoute();   
        submitMap();
    }
    else{
        hideRoute();   
        getMap();
    }
}

function resizePhoto(){
    
    if (imgPhoto.match(/nophoto_member/)) 
    {   
        $("#imgPhoto").css({display:"inline"});
        return;
    }
    pImg = new Image();
    pImg.src = imgPhoto;
    pImg.onLoad=photoLoaded();
}

function photoLoaded(){
    
    var w = 0;
    var h = 0;
    try{
        var w = pImg.width;
        var h = pImg.height;
    }catch(e){}
    
//    if (h>90){
//        nh = 90;
//        nw = Math.round(w*nh/h);
//        if (nw>120)
//        {
//            nw = 120;
//            nh = Math.round(nw*nh/w);
//        }
//    }
//    else{
//        if (w >= h){

//            nw = 120;
//            nh = Math.round(nw*h/w);

//        }
//        else{  
//   
//            nh = 90;
//            nw = Math.round(w*nh/h);

//        }
//    }
    
//    $("#imgPhoto").width(nw);            
//    $("#imgPhoto").height(nh);
               
    $("#imgPhoto").css({display:"inline"});
}


//$(function(){ 
//	$('input:text').hint();
//})

jQuery.fn.hint = function() {
	return this.each(function(){
		var t = $(this); // get jQuery version of 'this'
		var title = t.attr('title'); // get it once since it won't change
		
		if (title) { // only apply logic if the element has the attribute
			
			// on focus, set value to blank if current value matches title attr
			t.focus(function(){
				if (t.val() == title) {
				  t.val('');
				  t.removeClass('blur');
				}
			});

			// on blur, set value to title attr if text is blank
			t.blur(function(){
				if (t.val() == '') {
				  t.val(title);
				  t.addClass('blur');
				}
			});

			// now change all inputs to title
			t.blur();
		}
	})		;		
}



		
