﻿// JScript File

function MapOn() {
    document.getElementById('wrapper-map').style.visibility = "visible";
    document.getElementById('wrapper-map').style.display = "inline";
    document.getElementById('wrapper-map').style.top = getTopPosition() + "px";
    document.getElementById('wrapper-map-bg').style.display = "block";
    return false;
}
function MapOff() {
    document.getElementById('wrapper-map').style.visibility = "hidden";
    document.getElementById('wrapper-map').style.display = "none";
    document.getElementById('wrapper-map-bg').style.display = "none";
}

function ToggleDemoMap() {
	var d_myMap = document.getElementById('wrapper-demomap')
	var d_myToggleLink = document.getElementById('showhide')
 if (d_myMap.style.visibility == "hidden") {
			d_myMap.style.visibility = "visible";	
			d_myMap.style.height = "auto";
			d_myToggleLink.innerText = "Hide Map"
    }else{
			d_myMap.style.visibility = "hidden";	
			d_myMap.style.height = "0";
			d_myToggleLink.innerText = "Show Map"
   }
 }


function AdvOn() {
    ShowAdvancedSearch();
    document.getElementById('wrapper-simpleSearch').style.display = "none";
    document.form1.searchStatus.value = "adv";
}
function SimOn() {
    document.getElementById('wrapper-simpleSearch').style.display = "block";
    HideAdvancedSearch();
    document.form1.searchStatus.value = "simple";
}

function MainMapOn() {
    document.getElementById('wrapper-mainMap').style.display = "block";
    document.getElementById('mapproperties').onclick = MainMapOff;
    document.getElementById('mapproperties').innerHTML = "Hide Map";
    // buildMainMap();
}
function MainMapOff() {
    document.getElementById('wrapper-mainMap').style.display = "none";
    document.getElementById('mapproperties').onclick = MainMapOn;
    document.getElementById('mapproperties').innerHTML = "View Properties on Map";
}


//function CitiesChange() {
//    var CitiesValue = document.getElementById('simplecitylist').value;
//    if (CitiesValue == "-") {
//        document.getElementById('btnSimpleSearch').disabled = true;
//    }
//    else {
//        document.getElementById('btnSimpleSearch').disabled = false;
//    }
//}

function AdvCitiesChange() {
    var selectbox = document.getElementById('advancedcitylistFS');
    restoreEmulateDisabled(selectbox);
    emulateDisabled(selectbox);
    selectbox = document.getElementById('advancedcitylistFL');
    restoreEmulateDisabled(selectbox);
    emulateDisabled(selectbox);
}

function emulateDisabled(selectBox) {
 for (var i=0, option; option == selectBox.options[i]; i++) {
        if (option.value == "-") {
                option.style.color = "graytext";
        }else{
                option.style.color = "menutext";
        }
 }
}
function restoreEmulateDisabled(selectBox) {
 for (var i=0, option; option == selectBox.options[i]; i++) {
        if(option.selected && option.value == "-"){
                option.selected=false;
        }
 }
}

function displaySearch() {
    if (document.form1.searchStatus.value == "adv")
        AdvOn();
    else
        SimOn();

}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getTopPosition() {
    var h = f_clientHeight();
    var t = f_scrollTop();
    if (h > 500)
        return t + (h-500)/2;
    else
        return t;
}



function drawCircle(bufRad, lat, lng)
{
    var pinLL = new VELatLong(lat, lng);
        
    var myCurrentShape = new VEShape(VEShapeType.Pushpin, pinLL);
    var bufferPoints = new Array();
    //Create a circle of a specified radius around the pushpin.
    bufferPoints = GetCircle(pinLL, bufRad);
    var bufferShape = new VEShape(VEShapeType.Polygon, bufferPoints);
    bufferShape.SetLineColor(new VEColor(255,0,0,0.5));
    bufferShape.SetFillColor(new VEColor(0, 0, 0, 0));
    bufferShape.SetCustomIcon(_blueIcon);
    bufferShape.SetTitle("");
    
    myCurrentShape.SetTitle("");
    myCurrentShape.SetDescription("");
    //myCurrentShape.SetCustomIcon(_blueIcon);
    _drawingLayer.DeleteAllShapes();
    // _drawingLayer.AddShape(myCurrentShape);
    _drawingLayer.AddShape(bufferShape);
    
    // Set Zoom level based on buffer radius
    if(bufRad == 1){
        map.SetCenterAndZoom(pinLL, 12);
    }
    else if (bufRad == 5){
        map.SetCenterAndZoom(pinLL, 11);
    }
    else if (bufRad == 10){
        map.SetCenterAndZoom(pinLL, 10);
    }
    else if(bufRad == 25){
        map.SetCenterAndZoom(pinLL, 8);
    }
}

//Draw Circle
function GetCircle(LatLong, sizeOfBuffer)
{   
    var R = 6371; // earth's mean radius in km 
    var miles_Radius = 3958.755; //Earth's mean radius in miles
    //var lat = (myPoints[0].Latitude * Math.PI) / 180; //rad
    //var lon = (myPoints[0].Longitude * Math.PI) / 180; //rad
    var lat = (LatLong.Latitude * Math.PI) / 180; //rad
    var lon = (LatLong.Longitude * Math.PI) / 180; //rad
    var d = parseFloat(sizeOfBuffer) / miles_Radius;  // d = angular distance covered on earth's surface
    myCirclePoints = new Array();
    for (x = 0; x <= 360; x++) 
    { 
        var p2 = new VELatLong(0,0)            
        brng = x * Math.PI / 180; //rad
        p2.Latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
        p2.Longitude = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(p2.Latitude))) * 180) / Math.PI;
        p2.Latitude = (p2.Latitude * 180) / Math.PI;
        myCirclePoints.push(p2);
    }
    return myCirclePoints;
}

//convert lat/long in degrees to radians
function latLonToRadians(point) 
{
	return point * Math.PI / 180;	
}