/**************************************************************************************
*File: locator.js
*Contents: Locator Class.
*All rights reserved.
**************************************************************************************/


function Locator(baseURL,phpDir,locatorName,areaName,businessClass,businessType,businessXmlTag,app,dbTable,map,areaCenter,boards, columnIds, maxEntriesCol, lng,photosPath) {
   this.baseURL = baseURL;
   this.phpDir = phpDir;
   this.name = locatorName;
   this.areaName = areaName;
   this.businessType = businessType;
   this.businessXmlTag = businessXmlTag;
   this.map = map;
   this.app = app;
   this.dbTable = dbTable;
   this.areaCenter = areaCenter;
   this.zoomLevel = map.getZoom();
   this.boards = boards;
   this.columnIds = columnIds;
   this.maxEntriesCol = maxEntriesCol;
   
   if(lng == null)
      this.lng = 'eng';
   else
      this.lng = lng.toLowerCase();

   this.cashe = new Cashe(this,4,columnIds,maxEntriesCol,this.lng);

   if(photosPath == null)
      photosPath = '/photos';

   initMarker(map,photosPath);
   this.businessClass = businessClass;
   
}

Locator.prototype.select = LocatorSelect;
Locator.prototype.search = LocatorSearch;
Locator.prototype.show = LocatorShow;
Locator.prototype.hide = LocatorHide;
Locator.prototype.showWithWeb = LocatorShowWithWeb;
Locator.prototype.hideWithWeb = LocatorHideWithWeb;
Locator.prototype.neighbors = LocatorNeighbors;
Locator.prototype.showHere = LocatorShowHere;
Locator.prototype.locateNeighbors = LocatorLocateNeighbors;
Locator.prototype.adjustView = LocatorAdjustView;




function LocatorSelect() {
   var req = this.baseURL + this.phpDir + "/search.php?action=browse&app=" + this.app + "&root=" + this.businessXmlTag + "s" + "&tag=" + this.businessXmlTag;
   this.cashe.request(req); 
}

function LocatorSearch(action,params) {
   var req = this.baseURL + this.phpDir + "/search.php?action=" + action + "&app=" + this.app + "&" + params + "&root=" + this.businessXmlTag + "s" + "&tag=" + this.businessXmlTag;
   this.cashe.request(req);
}


function showArea(map,business) {
    for(i in business) {
       business[i].show(map);
    }
}

function LocatorShowWithWeb() {
   var withWeb = new Array();
   
    for(i in this.cashe.curFrame) {
       if(this.cashe.curFrame[i].site != null) {
          this.cashe.curFrame[i].show();
          withWeb.push(this.cashe.curFrame[i]);
       }
    }
    this.adjustView(withWeb);
}

function LocatorHideWithWeb() {
    for(i in this.cashe.curFrame) {
       if(this.cashe.curFrame[i].site != null) {
          this.cashe.curFrame[i].hide();
       }
    }
}



function LocatorHide() {
   for(var i in this.cashe.curFrame) {
       this.cashe.curFrame[i].hide();
    }
}
 
function goToArea(map,position) {
    map.panTo(position);
}

function LocatorShow() {
   this.adjustView(this.cashe.curFrame);
   showArea(this.map,this.cashe.curFrame);
}

function LocatorNeighbors(position,distance) {
   var neighbors = new Array();
   for (var i in this.business) {
      var d = this.business[i].position.distanceFrom(position)/1000.0;
      if( d <= distance)
         neighbors.push(this.business[i]);
   }
   return neighbors;
}

function LocatorShowHere(position) {
   
   
   if(hereMarker != null)
      this.map.removeOverlay(hereMarker);

 
   hereMarker = new GMarker(position,{icon:hereIcon});
   this.map.addOverlay(hereMarker);
   this.map.setCenter(position);

   if(this.lng == 'fr')
      this.map.openInfoWindowHtml(position,"<div style=\"color:#FF1904;font-weight:bold;font-size:18px;\"> Vous &#234;tes ici!</div><br/>");
   else
     this.map.openInfoWindowHtml(position,"<div style=\"color:#FF1904;font-weight:bold;font-size:18px;\"> You are here!</div><br/>");

}

function LocatorLocateNeighbors(address,distance) {
   var l = this;
   var geocoder = new GClientGeocoder();
   geocoder.getLatLng(address, function(position) {
      if(!position)
      {
         if(l.lng == 'eng') {
            document.getElementById("locate_status").innerHTML = "<div style=\"color:red;\">Google Could not locate your address, don't forget to specify the <strong>city</strong>.</div>";
         }
         else if(l.lng == 'fr')
            document.getElementById("locate_status").innerHTML = "<div style=\"color:red;\">Google n'a pas r&#233;ussi &#224; localiser cette adresse...N\'oubliez pas de pr&#233;ciser la <strong>ville</strong>.</div>";
      }
      else {
         //alert("lat = " + position.lat() + " lng = " + position.lng());
         var neighbors = l.neighbors(position,distance);
         l.hide();
         l.showNeighbors(address,position,neighbors);
      }
   });
}

function LocatorAdjustView(business) {
   if(business.length <= 1)
      return;

   var minLat = business[0].position.lat();
   var minLng = business[0].position.lng();

   var maxLat = business[0].position.lat();
   var maxLng = business[0].position.lng();

   for(var i in business) {
      if(business[i].position.lat() < minLat)
         minLat = business[i].position.lat();
      if(business[i].position.lat() > maxLat)
         maxLat = business[i].position.lat();

      if(business[i].position.lng() < minLng)
         minLng = business[i].position.lng();
      if(business[i].position.lng() > maxLng)
         maxLng = business[i].position.lng();

   }

   var sw = new GLatLng(minLat,minLng);
   var ne = new GLatLng(maxLat,maxLng);
   var box = new GLatLngBounds(sw,ne);
   var center  = new GLatLng((sw.lat() + ne.lat())/2.0,(sw.lng() + ne.lng())/2.0);
   var zoom = this.map.getBoundsZoomLevel(box);
   this.map.setCenter(center,zoom);

}





