
var map = null; // declare the map variable

// Set the values of controls
var tbAddress_ID = 'address';
var tbCity_ID = 'city';
var ddlState_ID = 'state';
var tbZip_ID = 'zip';
var hLatitude_ID = 'hLatitude';
var hLongitude_ID = 'hLongitude';
var hZoomLevel_ID = 'hZoomLevel';
var imgLoadMap_ID = 'imgLoadMap';
var divresetPin = 'resetpin';

var control = null;

var varZoomLevel = 8; // default zoom level
var moving;
// check if address fields are filled
function hasAddressValues()
		{
			// get address fields
			var tbAddress = document.getElementById(tbAddress_ID);
			var tbCity = document.getElementById(tbCity_ID);
			var ddlState = document.getElementById(ddlState_ID);
			var tbZip = document.getElementById(tbZip_ID);
			
			return (tbAddress.value != "" && tbCity.value.length > 1 && ddlState.value != "" && tbZip.value.length == 5);
		}
// get the map object
function GetMap() {


    if (map == null) {
//        options = new VEMapOptions();

//        options.EnableBirdseye = VEOrientation.North;

        map = new VEMap('myMap');
        map.onLoadMap = ve_MapLoaded;
            // Load map
            map.LoadMap(null, 19, VEMapStyle.Aerial, false, VEMapMode.Mode2D, false, 1);

            //        map.LoadMap(null, 19, VEMapStyle.Aerial, false, VEMapMode.Mode2D, false, 1, options);
            map.AttachEvent("onobliqueenter", OnObliqueEnterHandler);
            // set the default address
            map.Find(null, '2516 San Carlos Ave., Castro Valley, CA, 94546', null, null, 0, 1, true, true, false, true, findCallBack);

    }


}
// Event to set the birds eye view if available
function OnObliqueEnterHandler() {
    if (varZoomLevel == 17) {

        if (map.IsBirdseyeAvailable()) {
            var TopOfNeedle = new VELatLong(document.getElementById(hLatitude_ID).value, document.getElementById(hLongitude_ID).value);
            map.SetBirdseyeScene(TopOfNeedle);
        }
    }
}
// Show the map
function ShowMap() {

    if (map == null) 
    {
    }
    else {
    // check if address is available
        if (hasAddressValues()) {
            var tbAddress = document.getElementById(tbAddress_ID);
            var tbCity = document.getElementById(tbCity_ID);
            var ddlState = document.getElementById(ddlState_ID);
            var tbZip = document.getElementById(tbZip_ID);
            varZoomLevel = 17; // set the zoom level
            map.AttachEvent("onobliqueenter", OnObliqueEnterHandler);

            ve_setLocation(tbAddress.value + ", " + tbCity.value + ", " + ddlState.value + " " + tbZip.value);
            ve_addPushPin();
            
        }
        else {
            alert("Please enter all address details to view the map!");
        }
    }
}
function ve_setLocation(address) {
//    map.SetZoomLevel(varZoomLevel);
    map.Find(null, address, null, null, 0, 1, true, true, false, true, findCallBack);
}
// call back once an address is found
function findCallBack(layer, resultsArray, places, hasMore, veErrorMessage) {
// set the zoom level
    map.SetZoomLevel(varZoomLevel);
    // add push pin
    ve_addPushPin();
    // add control for push pin // not required at the movement
    ve_addControl();
    // if zoom level is 17, check if the birds eye view is available, if available, then set it
    if (varZoomLevel == 17) {
        if (map.IsBirdseyeAvailable()) {
            var TopOfNeedle = new VELatLong(document.getElementById(hLatitude_ID).value, document.getElementById(hLongitude_ID).value);
            map.SetBirdseyeScene(TopOfNeedle);
        } 
    }


}

// Add the push pin 
function ve_addPushPin() {
    map.DeleteAllShapes();
    //add pushpin/sun icon
    var centerLatLong = map.GetCenter();
    //pushp = new VEShape(VEShapeType.Pushpin, centerLatLong);
    //vesun.SetCustomIcon("Images/sun.png");
    //ve_map.AddShape(pushp);
    //ve_map.AddPushpin(centerLatLong);

    pushp = new VEShape(VEShapeType.Pushpin, centerLatLong);
    //				 pushp.SetCustomIcon("images/pushpin.png");
    //pushp.SetTitle('PushPin');
    //pushp.SetDescription('Please drag and drop the push pin onto the roof that you would like to install solar on.');
    map.AddShape(pushp);

    // set lat/lon fields
    				var hLatitude = document.getElementById(hLatitude_ID);
    hLatitude.value = centerLatLong.Latitude; // set the latitude value

				var hLongitude = document.getElementById(hLongitude_ID);
    hLongitude.value = centerLatLong.Longitude; // set the longitude value
   // ShowBirdsEyeView(centerLatLong.Latitude, centerLatLong.Longitude);

}

// add control.. not in use
function ve_addControl() {
//    				if(control==null && map != null)
    {
        //control = document.createElement("div"); 
//        					control = document.getElementById(divresetPin);
//        control.style.display = "";
//        control.onclick = ve_addPushPin;
/*        control.onmouseover = function(){this.style.fontWeight="bold";};
        control.onmouseout = function(){this.style.fontWeight="normal";};
        control.onmousedown = function(){this.style.fontWeight="normal";};
        control.onmouseup = function(){this.style.fontWeight="bold";};*/
        //map.AddControl(control);
        //addShim(control);
    }
}
// Set the map events
function ve_MapLoaded() {
    //add map event handlers and disable birdseye
    map.AttachEvent("onkeydown", keydownCB);
    map.AttachEvent("onmousemove", mouseMoveHandlerADD);
    map.AttachEvent("onmousedown", mouseDownHandlerADD);
    map.AttachEvent("onmouseup", mouseUpHandlerADD);
    map.AttachEvent("onchangeview", recordZoom);

    // set location
    var tbAddress = document.getElementById(tbAddress_ID);
    var tbCity = document.getElementById(tbCity_ID);
    var ddlState = document.getElementById(ddlState_ID);
    var tbZip = document.getElementById(tbZip_ID);
    ve_setLocation(tbAddress.value + ", " + tbCity.value + ", " + ddlState.value + " " + tbZip.value);

    var hZoomLevel = document.getElementById(hZoomLevel_ID);
    hZoomLevel.value = map.GetZoomLevel();
}

//disable birdseye
function keydownCB(e) {
    if (e.keyCode == 66 || e.keyCode == 79) {
        return true;
    }
}

function recordZoom() {
    var hZoomLevel = document.getElementById(hZoomLevel_ID);
    hZoomLevel.value = map.GetZoomLevel();
}

//move icon/mouse handlers
var movingOffsetX = 0;
var movingOffsetY = 0;

function mouseDownHandlerADD(e) {
    if (e.elementID) {
        currentShapePin = map.GetShapeByID(e.elementID);
        //var pinpix = map.LatLongToPixel(currentShapePin.GetPoints());
        //document.getElementById("mouselatlong").innerHTML = "MouseX:"+Math.round(e.mapX)+" MouseY:"+Math.round(e.mapY);
        //document.getElementById("pinlatlong").innerHTML = "PinX:"+Math.round(pinpix.x)+" PinY:"+Math.round(pinpix.y);
        //movingOffsetX = e.mapX-pinpix.x;
        //movingOffsetY = e.mapY-pinpix.y;
        moving = true;
        map.vemapcontrol.EnableGeoCommunity(true);
        document.getElementById("myMap").style.cursor = 'move';
    }
}

function mouseMoveHandlerADD(e) {
    var locMain = map.PixelToLatLong(new VEPixel(e.mapX - movingOffsetX, e.mapY - movingOffsetY));
    try {
        if (moving) {
            currentShapePin.SetPoints(locMain);

            var hLatitude = document.getElementById(hLatitude_ID);
            hLatitude.value = locMain.Latitude;

            var hLongitude = document.getElementById(hLongitude_ID);
            hLongitude.value = locMain.Longitude;

            //document.getElementById("status").innerHTML = "http://solarlease.solarcity.com/solarbidlite/map.aspx?Latitude="+hLatitude.value+"&Longitude="+hLongitude.value+"&ZoomLevel="+hZoomLevel.value;
        }

    } catch (err) {
    }
}
// Mouseup event handler
function mouseUpHandlerADD(e) {
    if (moving) {
        moving = false;
        var locMain = map.PixelToLatLong(new VEPixel(e.mapX - movingOffsetX, e.mapY - movingOffsetY));
        currentShapePin.SetPoints(locMain);
        map.vemapcontrol.EnableGeoCommunity(false);
        document.getElementById("myMap").style.cursor = '';

        var hLatitude = document.getElementById(hLatitude_ID);
        hLatitude.value = locMain.Latitude;

        var hLongitude = document.getElementById(hLongitude_ID);
        hLongitude.value = locMain.Longitude;
    }
}
				   
