﻿/** Globals **/
var gAllListings;
var gMap = null;
var gMarkerMgr = null;
var gGeoCoder = null;
var gTooltip = null;
var gTooltipCntnt = null;

var gStartPoint = new GLatLng(40.76858174460535,-73.96682739257812);
var gUpperE = new GLatLng(40.77625179166221,-73.95661354064941);
var gUpperW = new GLatLng(40.78444086438884,-73.9859676361084);
var gBelowFN = new GLatLng(40.73672207747068,-73.99892807006836);
        
var gSaleIcon = new GIcon(G_DEFAULT_ICON);
gSaleIcon.image = 'images/maps/iconS.png';

var gRentalIcon = new GIcon(G_DEFAULT_ICON);
gRentalIcon.image = 'images/maps/iconR.png';

var gHoveredIcon = new GIcon(G_DEFAULT_ICON);
gHoveredIcon.image = 'images/maps/iconH.png';


var gIMarkerCount = 0;

var timeOuts= new Array();

var gHfZoom = null;
var gHfCategory = null;
var gHfMinPrice = null;
var gHfMaxPrice = null;
var gHfRooms = null;
var gHfBeds = null;
//** Global Methods **//
function clearAllTimeouts(){   
    for(key in timeOuts){
        clearTimeout(timeOuts[key]);   
    }   
}
function scrollToRow(_mDataItem){
    var mRowElement = _mDataItem.get_element();
    var mScrollArea = get_Grid().GridDataDiv;
    if((mRowElement.offsetTop - mScrollArea.scrollTop) + mRowElement.offsetHeight + 20 > mScrollArea.offsetHeight){
        mScrollArea.scrollTop = mScrollArea.scrollTop + (mRowElement.offsetTop - mScrollArea.scrollTop) + (mRowElement.offsetHeight - mScrollArea.offsetHeight) + mRowElement.offsetHeight;
    }
    else if((mRowElement.offsetTop - mScrollArea.scrollTop) < 0){
      mScrollArea.scrollTop = mRowElement.offsetTop;
    }
}
/** Map **/
function map_Load(){
    if (GBrowserIsCompatible()) {
            //
            gMap=new GMap2(document.getElementById('map'));
            //Zoom Level
            var mZoom = document.getElementById(hfZoom);
            if(mZoom.value == 'UpperESide'){gMap.setCenter(gUpperE, 14);}
            else if(mZoom.value == 'UpperWSide'){gMap.setCenter(gUpperW, 14);}
            else if(mZoom.value == 'Below59'){gMap.setCenter(gBelowFN, 13);}
            else{gMap.setCenter(gStartPoint, 12);}
            //
            gMap.addControl(new GSmallMapControl());
            gMap.addControl(new GMapTypeControl());
            gMap.addControl(new PropertyTypeControl());
            gTooltip = document.getElementById("uxTTip");
            gTooltipCntnt = document.getElementById("uxTCntnt");
            //
            gMarkerMgr=new MarkerManager(gMap, {trackMarkers:true});
            window.setTimeout('set_mapMarkers()', 0);
    }
    else{
        
    }
}
function map_ReLoad(_mZoomLvl){
        clearAllTimeouts();
        gIMarkerCount = 0;
        //
        if(_mZoomLvl == 'UpperESide'){gMap.setCenter(gUpperE, 14);}
        else if(_mZoomLvl == 'UpperWSide'){gMap.setCenter(gUpperW, 14);}
        else if(_mZoomLvl == 'Below59'){gMap.setCenter(gBelowFN, 13);}
        else{gMap.setCenter(gStartPoint, 12);}
        //
        gMarkerMgr.clearMarkers();
        window.setTimeout('set_mapMarkers()', 0);
}
/** Map Markers **/
function set_mapMarkers(){
    if (gAllListings){
        var mIMarkerCount = gAllListings.listings.length;
        var mIMarkerID = 0;
        //
        var mListing;
        var mMarker;
        var mDataItems = get_Grid().get_masterTableView().get_dataItems();
        for(mIMarkerID=0;mIMarkerID<mIMarkerCount;mIMarkerID++){
            mListing = (gAllListings.listings[mIMarkerID]);
            mListing.dataItem = mDataItems[mIMarkerID];
            mMarker = mListing.setMarker(mListing);
            if (mMarker){
                gMarkerMgr.addMarker(mMarker,12);
            }
        }
    }
}
function select_Listing(webID)
{
    var mListing = gAllListings.getListing(webID);
    mListing.mapSelect();
}

/** Grid Events **/
var gRSd;
function dg_RSd(s, args){
    var e = args.get_domEvent();
    var targetElement = e.srcElement || e.target;
    if(targetElement != 'undefined' && targetElement != null){
        if(gRSd){
            window.clearTimeout(gRSd)
        }
        gRSd = setTimeout('var mListing=gAllListings.getListing('+args.getDataKeyValue('WEBID')+'); mListing.gridSelect();',1);
        return false;
    }
}
function dg_RDSd(s, args){
    var e = args.get_domEvent();
    var targetElement = e.srcElement || e.target;
    if(targetElement != 'undefined' && targetElement != null){
        //gRDSd = setTimeout('var mListing=gAllListings.getListing('+args.getDataKeyValue('WEBID')+'); mListing.mapWindowClose();',1);
    }
}
var gRMOver;
function dg_RMOver(s, args){
    var e = args.get_domEvent();
    var targetElement = e.srcElement || e.target;
    if(targetElement != 'undefined' && targetElement != null){
        if(gRMOver){
            window.clearTimeout(gRMOver)
        }
        gRMOver = setTimeout('var mListing=gAllListings.getListing('+args.getDataKeyValue('WEBID')+'); mListing.gridMouseOver();',1);
    }
}
var gRMOut;
function dg_RMOut(s, args){
    var e = args.get_domEvent();
    var targetElement = e.srcElement || e.target;
    if(targetElement != 'undefined' && targetElement != null){
        if(gRMOut){
            window.clearTimeout(gRMOut)
        }
        gRMOout = setTimeout('var mListing=gAllListings.getListing('+args.getDataKeyValue('WEBID')+'); mListing.gridMouseOut();',1);
    }
}
// Prototype GHKListings
function GHKListing(_mWebID,_mCategory,_mAddress,_mLat,_mLng,_mTooltip,_mInfoHTML){
    // object properties
    this.dataItem = null;
    this.marker = null;
    this.markerPos='undefined';
    
    this.webID=_mWebID;
    this.category=_mCategory;
    this.address=_mAddress;
    this.tooltip=_mTooltip;
    this.infoHTML=_mInfoHTML;
    this.point=null;
    this.lat=_mLat;
    this.lng=_mLng;
    
    this.isSelected=false;
    this.isHovering=false;
    
    this.iconAnchor = null;
    this.iconSize = null;
}
GHKListing.prototype.setMarker=function(_mListing){
    if (!this.point){
        if(this.lat && this.lng){
            this.point = new GLatLng(this.lat,this.lng);
        }
    }
    if(this.point){
        var mMarkerOptions={icon:gRentalIcon, zIndexProcess:function(){return 1;}};
        if (this.category=='Sale') {
            mMarkerOptions = {icon:gSaleIcon, zIndexProcess:function(){return 2;}};
        }
        this.marker = new GMarker(this.point,mMarkerOptions);
        GEvent.bind(gMap, "infowindowclose", this, this.mapWindowClose);
        GEvent.bind(this.marker, "click", this, this.mapSelect);
        GEvent.bind(this.marker, "mouseover", this, this.mapMouseOver);
        GEvent.bind(this.marker, "mouseout", this, this.mapMouseOut);
        this.iconAnchor=this.marker.getIcon().iconAnchor;
        this.iconSize=this.marker.getIcon().iconSize.width;
        
        return this.marker;
    }
    else{
        return null;
    }
}
GHKListing.prototype.mapWindowClose=function(){
    this.isSelected = false;
    this.dataItem.set_selected(false);
}
GHKListing.prototype.mapSelect=function(){
    if(!this.isSelected){    
        gMap.openInfoWindowHtml(this.point, this.infoHTML);

        var mMasterTable = get_Grid().get_masterTableView();
        mMasterTable.clearSelectedItems();
        
        this.dataItem.set_selected(true);
        scrollToRow(this.dataItem);
        
        gTooltip.style.visibility='hidden';
        
        this.isSelected = true;
    }
}
GHKListing.prototype.mapMouseOver=function(_mTooltip){
    if(!this.isHovering && !this.isSelected){
        var mMapZoom=gMap.getZoom()
        var mBoundsPoint=gMap.getCurrentMapType().getProjection().fromLatLngToPixel(gMap.getBounds().getSouthWest(),mMapZoom);
        var mMapOffset=gMap.getCurrentMapType().getProjection().fromLatLngToPixel(this.point,mMapZoom);
        this.markerPos=new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(mMapOffset.x - mBoundsPoint.x - this.iconAnchor.x + this.iconSize - 10,- mMapOffset.y + mBoundsPoint.y + this.iconAnchor.y - 25));
    	this.markerPos.apply(gTooltip);
    	if(this.markerPos != 'undefined'){
            gTooltip.style.visibility='visible';
            gTooltipCntnt.innerHTML=(this.tooltip);
            gTooltip.style.width = 'auto';
            gTooltip.style.width = gTooltip.clientWidth + 'px';
            this.dataItem.get_element().className = this.dataItem.get_element().className+=" over";
            scrollToRow(this.dataItem);
            this.marker.setImage(gHoveredIcon.image);
        }
        this.isHovering = true;
    }
}
GHKListing.prototype.mapMouseOut=function(){
    if(this.isHovering){
        gTooltip.style.visibility='hidden';
        this.dataItem.get_element().className = this.dataItem.get_element().className.replace(" over", "");
        if (this.category=='Sale') {
            this.marker.setImage(gSaleIcon.image);
        }
        else
        {
            this.marker.setImage(gRentalIcon.image);
        }
        this.isHovering = false;
    }
}
GHKListing.prototype.gridSelect=function(){
    if(!this.isSelected){
        if (this.marker){
            gMap.openInfoWindowHtml(this.point, this.infoHTML);
            gTooltip.style.visibility='hidden';
        }
        var mMasterTable = get_Grid().get_masterTableView();
        mMasterTable.clearSelectedItems();

        this.dataItem.set_selected(true);
        this.isSelected = true;
    }
}
GHKListing.prototype.gridMouseOver=function(){
    if(!this.isHovering && !this.isSelected){
        if (this.marker){
            var mMapZoom=gMap.getZoom()
            var mBoundsPoint=gMap.getCurrentMapType().getProjection().fromLatLngToPixel(gMap.getBounds().getSouthWest(),mMapZoom);
            var mMapOffset=gMap.getCurrentMapType().getProjection().fromLatLngToPixel(this.point,mMapZoom);
            this.markerPos=new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(mMapOffset.x - mBoundsPoint.x - this.iconAnchor.x + this.iconSize - 10,- mMapOffset.y + mBoundsPoint.y + this.iconAnchor.y - 25));
            if(this.markerPos != 'undefined'){
                this.markerPos.apply(gTooltip);
                gTooltip.style.visibility='visible';
                gTooltipCntnt.innerHTML=(this.tooltip);
                gTooltip.style.width = 'auto';
                gTooltip.style.width = gTooltip.clientWidth + 'px';
                this.isHovering = true;
                this.refreshMarker();
            }
        }
    }
}
GHKListing.prototype.gridMouseOut=function(){
    if(this.isHovering){
        gTooltip.style.visibility='hidden';
        this.isHovering = false;
        this.refreshMarker();
    }
}
var zIndex = 3;
GHKListing.prototype.refreshMarker=function(){
    if(this.point){
        gMarkerMgr.removeMarker(this.marker);
        var mMarkerOptions = null;
        if(this.isHovering){
            zIndex += 1;
            mMarkerOptions = {icon:gHoveredIcon, zIndexProcess:function(){return zIndex;}};
        }
        else{
            if (this.category=='Sale') {
                mMarkerOptions = {icon:gSaleIcon, zIndexProcess:function(){return zIndex}};
            }
            else
            {
                mMarkerOptions={icon:gRentalIcon, zIndexProcess:function(){return zIndex}};
            }
        }
        this.marker = new GMarker(this.point,mMarkerOptions);
        GEvent.bind(gMap, "infowindowclose", this, this.mapWindowClose);
        GEvent.bind(this.marker, "click", this, this.mapSelect);
        GEvent.bind(this.marker, "mouseover", this, this.mapMouseOver);
        GEvent.bind(this.marker, "mouseout", this, this.mapMouseOut);
        this.iconAnchor=this.marker.getIcon().iconAnchor;
        this.iconSize=this.marker.getIcon().iconSize.width;
        gMarkerMgr.addMarker(this.marker,12);
    }
    else{
    }
}
// Prototype GHKListings
function GHKListings(){
    this.listings = new Array();
}
GHKListings.prototype.getListing=function(_mWebID){
    var l=this.listings.length;
    var i=0;
    var mListing;
    for(i=0;i<l;i++){
        mListing = this.listings[i];
        if(mListing.webID==_mWebID){
            return mListing;
        }
    }
    return null;
}
GHKListings.prototype.addListing=function(_mListing){
        this.listings.push(_mListing)
}
// Prototype PropertyTypeControl
function PropertyTypeControl() {
}
PropertyTypeControl.prototype = new GControl();
PropertyTypeControl.prototype.initialize = function(map) {
    var container = document.getElementById('mapCtl');
    map.getContainer().appendChild(container);
    return container;
}
PropertyTypeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(107, 7));
}