HAR.SaleSoldLeaseEnum = {none:0,sale:1,sold:2,lease:3};

HAR.SaleSoldLeaseFlag = {none:0,sale:1,sold:2,lease:4};

HAR.PropertyTypeEnum = {
    singleFamily:1,
    townhouseCondo:2,
    residentialLotsLand:3,
    multiFamily:4,
    homesAndOrAcreage:5,
    highriseCondominium:6
};

HAR.PropertyTypeFlag = {
    singleFamilyHomes:1,
    townhouseCondo:2,
    residentialLots:4,
    multiFamily:8,
    homesAndOrAcreage:16,
    highriseCondominium:32
};

HAR.VEMapUtil = function(config){
    
    var mapObj = ({
        map : null,
        tlInsetLat : 0,
        tlInsetLong : 0,
        brInsetLat : 0,
        brInsetLong : 0,
        pushPinLayer : this.pushPinLayer,
        polylineLayer : this.polylineLayer,
        polygonLayer : this.polygonLayer,
        pushPinPoints : [],
        polylinePoints : [],
        polygonPoints : [],
        
        deleteAllShapes : function(){
            this.map.DeleteAllShapes();
            
            this.polylineShape = null;
            this.polygonShape = null;
            
            this.pushPinPoints.length = 0;
            this.polylinePoints.length = 0;
            this.polygonPoints.length = 0;
			//alert('in deleteallshapes, polygonshape is set to null');
        },
        
        getMapCorners : function(){
            var rect = this.map.GetMapView();
            return {
                topLeftLatitude : rect.TopLeftLatLong.Latitude,
                topLeftLongitude : rect.TopLeftLatLong.Longitude,
                bottomRightLatitude : rect.BottomRightLatLong.Latitude,
                bottomRightLongitude : rect.BottomRightLatLong.Longitude
            };
        },
        
        getVisibleOffsetLatLongRectangle : function(){
            var veLatLongRectangle = this.map.GetMapView();
            veLatLongRectangle.TopLeftLatLong.Latitude -= this.tlInsetLat;
            veLatLongRectangle.TopLeftLatLong.Longitude += this.tlInsetLong;
            veLatLongRectangle.BottomRightLatLong.Latitude += this.brInsetLat;
            veLatLongRectangle.BottomRightLatLong.Longitude -= this.brInsetLong;
            
            return veLatLongRectangle;
        },
        
        getPushPinPointsLatLongRectangle : function(){
            
            var latLongRect = null;
            
            var pointCount = this.pushPinLayer.GetShapeCount();
            var latLongArray = [];
            
            if(pointCount > 0){
                for(i = 0; i < pointCount; i++){
                    latLongArray.push(this.pushPinLayer.GetShapeByIndex(i).GetPoints()[0]);
                }
                
                latLongRect = this.getLatLongRectangleFromVELatLongArray(latLongArray);
            }
            
            return latLongRect;
        },
        
        getPolylineLatLongRectagle : function(){
            
            var latLongRect = null;
            
            if(this.polylineShape){
                latLongRect = this.getLatLongRectangleFromVELatLongArray(this.polylineShape.GetPoints());
            }
            
            return latLongRect;
        },
        
        getPolygonLatLongRectagle : function(){
            
            var latLongRect = null;
            var polygonShape = null;
			
			//alert(this.polygonLayer.GetShapeCount() );
            
            if(this.polygonLayer.GetShapeCount() > 0){
				//alert('initialize polygonshape 1');
                polygonShape = this.polygonLayer.GetShapeByIndex(0)
            }else{
                var baseLayer = this.map.GetShapeLayerByIndex(0);
                if(baseLayer.GetShapeCount() > 0){
                    var shape = baseLayer.GetShapeByIndex(0);
                    if(shape.GetType() == VEShapeType.Polygon){
                        polygonShape = shape;
						//alert('initialize polygonshape 2');
                    }
                }
            }
            
            if(polygonShape != null){
                var veLatLongArray = polygonShape.GetPoints();
                latLongRect = this.getLatLongRectangleFromVELatLongArray(veLatLongArray);
            }
            
            return latLongRect;
        },
        
        getLatLongRectangleFromVELatLongArray : function(veLatLongArray){
            var tlLat = 0, tlLong = 0, brLat = 0, brLong = 0, latLong;
            
            var l = veLatLongArray.length;
            if( l > 0){
            
                tlLat = veLatLongArray[0].Latitude;
                tlLong = veLatLongArray[0].Longitude;
                brLat = veLatLongArray[0].Latitude;
                brLong = veLatLongArray[0].Longitude;
                
                for(var i = 0; i < l; i++){
                    latLong = veLatLongArray[i];
                    if(latLong.Latitude > tlLat){
                        tlLat = latLong.Latitude;
                    }
                    if(latLong.Longitude < tlLong){
                        tlLong = latLong.Longitude;
                    }
                    if(latLong.Latitude < brLat){
                        brLat = latLong.Latitude;
                    }
                    if(latLong.Longitude > brLong){
                        brLong = latLong.Longitude;
                    }
                }
            }
            
            var rect = new VELatLongRectangle(new VELatLong(tlLat, tlLong), new VELatLong(brLat, brLong));
            
            return {
                topLeftLatitude : rect.TopLeftLatLong.Latitude,
                topLeftLongitude : rect.TopLeftLatLong.Longitude,
                bottomRightLatitude : rect.BottomRightLatLong.Latitude,
                bottomRightLongitude : rect.BottomRightLatLong.Longitude
            }
        },
        
        getPushPinLatLongArray : function(){
        },
        
        getPolylineLatLongArray : function(){
        },
        
        getPolygonLatLongArray : function(){
            var veLatLongArray;
            var latLongArray = [];
            
            if(this.polygonPoints){
                for(var i = 0, l = this.polygonPoints.length; i < l; i++){
                    latLongArray.push([this.polygonPoints[i].Latitude, this.polygonPoints[i].Longitude]);
                }
            }
            
            return latLongArray;
        },
        
        addPropertyPoint : function(property, titleTmplt, descriptionTmplt, customIcon){
			//alert('I am her vemaputile'+ property.cid);
			//alert(property.detailurl);
            var title = '';
                title = property.address;
         
            var description = '';
            if(descriptionTmplt){
                description = descriptionTmplt;
            }else{
			//	prompt(property.detailurl);
                if(property.propertyClassId != HAR.PropertyTypeEnum.highriseCondominium 
                    && property.propertyClassId != HAR.PropertyTypeEnum.townhouseCondo){

                    description = "<div style='width:200px'>"+
                        "<iframe src='sliderollenh.cfm?mlnum="+property.mlnum+"&cid="+property.cid+"&sitetype="+property.sitetype+"&pid="+property.propertyClassId+"' width='200' height='155px' "+
                            "scrolling='no' frameborder='0' marginwidth='0' marginheight='0'></iframe>"+
                        "<div  id='propDetailDiv' style='font-size:10px; background-color:ffffff;width:200px'>"+
                            "<span class='itemLabel'>"+property.listingPrice+"</span>&nbsp;(" +property.propertyClass+")- " + property.status;

                    if(property.bedRoomCount > 0){
                        description += "<div><span class='itemLabel' style='margin-right:27px;'>Bed: </span><span  class='itemText'>"+property.bedRoomCount+" bedroom(s)</span></div>";
                    }
                    if(property.bathRoomCount != ''){
                        description += "<div><span class='itemLabel' style='margin-right:24px;'>Bath: </span><span class='itemText'>"+property.bathRoomCount+" bathroom(s)</span></div>";
                    }
                    
                    if(property.squareFeet != ''){
                        description += "<div><span class='itemLabel' style='margin-right:21px;'>SQFT: </span><span  class='itemText'>"+property.squareFeet+"</span>";
                    }
                    if(property.squareFeetSource != ''){
                        description += property.squareFeetSource;
                    }
					description+="</div>";
                    description += "<div><span class='itemLabel' style='margin-right:27px;'>City: </span><span  class='itemText'>"+property.city+"</span></div>";
					description += "<div><span class='itemLabel' style='margin-right:2px;'>Zip code: </span><span  class='itemText'>"+property.zip+"</span></div>";
					
                    description += "<br><input type=button class='goButton' value='DETAILS' style='cursor:pointer;' onclick=\"window.open('"+property.detailurl+property.mlnum+"&class="+property.propertyClassId+"&sType=0')\"></a></div><br>";

                }else if(property.propertyClassId == HAR.PropertyTypeEnum.highriseCondominium 
                    || property.propertyClassId == HAR.PropertyTypeEnum.townhouseCondo){
                //    alert('I am here'+property.cid);
                    description = "<div style='width:210px'>"+
                        "<iframe src='multiLists.cfm?mlnum="+property.mlnum+"&cid="+property.cid+"&sitetype="+property.sitetype+"&pid="+property.propertyClassId+"' width='200px' height='360px' "+
                            "scrolling='auto' frameborder='0' marginwidth='0' marginheight='0'></iframe>";
                }

               /* description += "<p style='font-size:10px; padding:0;'>Drive Direction <a  href=\"javascript:showDriveDiv('drivedivfrom');\">From Here</a> "+
                    " <a href=\"javascript:showDriveDiv('drivedivto');\">To Here</a></p>";*/
                    
                //{{{ get drive to/from
               /* var driveDivHTMLfrom = 
                    "<div id='drivedivfrom' style='display:none;width:200px;backgroudcolor:ffffff;font-size:10px;font-family:Arial'>"+
                    "<input style='font-size:10px;font-family:Arial' id='txtFrom' type='text' name='txtFrom' value=' "+property.address+','+property.city+','+property.zip+"' size='40'><br>"+
                    "To:<input style='font-size:10px;font-family:Arial' id='fromtxtTo' type='text' name='fromtxtTo' value='' size='40'><br>"+
                    "<input style='font-size:10px;font-family:Arial' id='find' type='button' value='Find' onclick=\"FindLoc('from');\" ></div>";

                var driveDivHTMLto =
                    "<div id='drivedivto' style='display:none;width:200px;backgroudcolor:ffffff;font-size:10px;font-family:Arial'>"+
                    "<input style='font-size:10px;font-family:Arial' id='totxtFrom' type='text' name='totxtFrom' value='' size='40'><br>"+
                    "To:<input style='font-size:10px;font-family:Arial' id='txtTo' type='text' name='txtTo' value=' "+property.address+','+property.city+','+property.zip+"' size='40'><br>"+
                    "<input  style='font-size:10px;font-family:Arial' id='find' type='button' value='Find' onclick=\"FindLoc('to');\" ></div>";
*/
                //}}}
                
                description += "</div>";
                
            }
            
            //}}}
            
            var icon = customIcon ? customIcon : (this.customIcon ? this.customIcon : '')
            
            //return this.addPushPinPoint(property.latitude, property.longitude, title, description, icon)
			return HAR.plotPin({
				lat: property.latitude,
				lng: property.longitude,
				html: description,
				pinIcon: icon,
				getId: true
			});
            
        },
        
        addPushPinPoint : function(latitude, longitude, title, description, customIcon){
            veLatLong = new VELatLong(latitude, longitude);
            var pushPinShape = new VEShape(VEShapeType.Pushpin, veLatLong);
			
            if(title){
                pushPinShape.SetTitle(title);
            }
            if(description){
                pushPinShape.SetDescription(description);
            }
            var icon = customIcon ? customIcon : (this.customIcon ? this.customIcon : '')
            if(icon){
                pushPinShape.SetCustomIcon(icon);
            }
            
            this.pushPinLayer.AddShape(pushPinShape);
            
            return pushPinShape;
        },
        
        addPolylinePoint : function(latitude, longitude){
            
            if(typeof latitude == 'object'){
                this.polylinePoints.push(latitude);
            }else{
                this.polylinePoints.push(new VELatLong(latitude, longitude));
            }
            
            if(this.polylinePoints.length > 1){
                if(this.polylineShape){
                    this.polylineShape.SetPoints(this.polylinePoints);
                }else{
                    this.polylineShape = new VEShape(VEShapeType.Polyline, this.polylinePoints);
                    this.polylineLayer.AddShape(this.polylineShape);
                }
            }
            
        },
        
        addPolygonPoint : function(latitude, longitude){
			//alert('when this addplygonpoint is called?');
            if(typeof latitude == 'object'){
                this.polygonPoints.push(latitude);
            }else{
                this.polygonPoints.push(new VELatLong(latitude, longitude));
            }
            
            if(this.polygonPoints.length > 2){
			
                if(this.polygonShape){
                    this.polygonShape.SetPoints(this.polygonPoints);
                }else{
                    this.polygonShape = new VEShape(VEShapeType.Polygon, this.polygonPoints);
                   this.polygonLayer.AddShape(this.polylgonShape);
                }
				 
					//alert('shap added to polygonshape');
            }
			//alert(this.polygonLayer);
        },
        
        drawPolyline : function(latitude, longitude){
            var veLatLong;
            
            if(typeof latitude == 'object'){
                veLatLong =latitude;
            }else{
                veLatLong =new VELatLong(latitude, longitude);
            }
            
            if(this.polylinePoints.length > 0){
                if(this.polylineShape){
                    var veLatLongArray = this.polylinePoints.slice(0);
                    veLatLongArray.push(veLatLong);
                    this.polylineShape.SetPoints(veLatLongArray);
                }else{
                    var veLatLongArray = this.polylinePoints.slice(0);
                    veLatLongArray.push(veLatLong);
                    this.polylineShape = new VEShape(VEShapeType.Polyline, veLatLongArray);
                    this.polylineShape.HideIcon();
                    this.polylineLayer.AddShape(this.polylineShape);
                }
            }
        },
        
        drawPolygon : function(latitude, longitude){
            var veLatLong;
            //alert('in drawPlygon()' + this.polygonShape);
            if(typeof latitude == 'object'){
                veLatLong =latitude;
            }else{
                veLatLong =new VELatLong(latitude, longitude);
            }
            
            if(this.polygonPoints.length > 1){
                if(this.polygonShape){
					var veLatLongArray = this.polygonPoints.slice(0);
                    veLatLongArray.push(veLatLong);
                    this.polygonShape.SetPoints(veLatLongArray);
                }else{
                    var veLatLongArray = this.polygonPoints.slice(0);
                    veLatLongArray.push(veLatLong);
                    this.polygonShape = new VEShape(VEShapeType.Polygon, veLatLongArray);
                    this.polygonShape.HideIcon();
                    this.polygonLayer.AddShape(this.polygonShape);
                }
            }
        },
        
        panAndZoom : function(latLongRect){
		//	alert(this.polygonPoints.length);
            if(Ext.isSafari){
                this.map.SetViewport(latLongRect.TopLeftLatLong.Latitude, latLongRect.TopLeftLatLong.Longitude, latLongRect.BottomRightLatLong.Latitude, latLongRect.BottomRightLatLong.Longitude)
            }else{
			//	alert(this.polygonPoints.length);
			//	alert(this.polygonShape);
                this.map.SetMapView(latLongRect);
            }
        },
        
        panAndZoomToPushPinPoints : function(){
            this.panAndZoom(this.pushPinLayer.GetBoundingRectangle());
        },
        
        panAndZoomToPolylinePoints : function(){
            this.panAndZoom(this.polylineLayer.GetBoundingRectangle());
        },
        
        panAndZoomToPolygonPoints : function(){
			//alert('will do poly'+ this.polygonLayer.GetBoundingRectangle());
			//alert(url);
			
            this.panAndZoom(this.polygonLayer.GetBoundingRectangle());
			//alert('in panandzoomtopolygonpoints'+gMapObj.polygonShape);
        }
        
    });
    
   /* for(var p in config){
        mapObj[p] = config[p];
    }

    mapObj.map = new VEMap(config.mapContainer);
    mapObj.map.LoadMap(new VELatLong(29.763324999999998,-95.379634999999993), 10);
    mapObj.mapGUID = mapObj.map.GUID;

    mapObj.pushPinLayer = new VEShapeLayer();
    mapObj.polylineLayer = new VEShapeLayer();
    mapObj.polygonLayer = new VEShapeLayer();

    mapObj.map.AddShapeLayer(mapObj.pushPinLayer);
    mapObj.map.AddShapeLayer(mapObj.polylineLayer);
    mapObj.map.AddShapeLayer(mapObj.polygonLayer);*/
    
    return mapObj;
    
};


