﻿Type.registerNamespace('casapadova.realestate.vemap');

casapadova.realestate.vemap.vecontrol = null;
casapadova.realestate.vemap.isLoaded = false;
casapadova.realestate.vemap.startingZoomLevel = 15;
casapadova.realestate.vemap.longitude = 0;
casapadova.realestate.vemap.latitude = 0;
casapadova.realestate.vemap.pin = null;
casapadova.realestate.vemap.showed = false;
casapadova.realestate.vemap.pinIconUrl = "/Content/Images/Marker.png";
casapadova.realestate.vemap.scriptUrl = "http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&mkt=it-IT&onScriptLoad=createMap";
casapadova.realestate.vemap.scriptCulture = "it-IT";
casapadova.realestate.vemap.scriptLoadCallback = "";
casapadova.realestate.vemap.pinDescription = "";
casapadova.realestate.vemap.showPin = false;
casapadova.realestate.vemap.dragShape = null;


$.getScript = function(url, callback, cache) {
	$.ajax({
		type: "GET",
		url: url,
		success: callback,
		dataType: "script",
		cache: cache
	});
};

casapadova.realestate.vemap.createCircle = function (loc, radius) {
	var ray = 6371; // earth's mean radius in km
	var lat = (loc.Latitude * Math.PI) / 180; //rad
	var lon = (loc.Longitude * Math.PI) / 180; //rad
	var diam = parseFloat(radius) / ray;  // diam = angular distance covered on earth's surface
	var locs = new Array();
	for (var 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(diam) + Math.cos(lat) * Math.sin(diam) * Math.cos(brng));
		p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(diam) * Math.cos(lat), Math.cos(diam) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
		p2.Latitude = (p2.Latitude * 180) / Math.PI;
		locs.push(p2);
	}
	return locs;
}

casapadova.realestate.vemap.createMap = function () {
	casapadova.realestate.vemap.addLoadEvent(function () {
		$("#mapLoader").hide();
		casapadova.realestate.vemap.vecontrol = new VEMap("vemapcontainer");
		//casapadova.realestate.vemap.vecontrol.LoadMap();
		casapadova.realestate.vemap.vecontrol.onLoadMap = casapadova.realestate.vemap.onMapLoaded;
		casapadova.realestate.vemap.vecontrol.LoadMap(new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude), casapadova.realestate.vemap.startingZoomLevel, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 0);
	});
}

casapadova.realestate.vemap.createAdminMap = function() {
    //alert("Start");
    casapadova.realestate.vemap.vecontrol = new VEMap("vemapcontainer");
    casapadova.realestate.vemap.vecontrol.onLoadMap = casapadova.realestate.vemap.onAdminMapLoaded;
    var options = new VEMapOptions();
    options.EnableBirdseye = false;
    casapadova.realestate.vemap.vecontrol.LoadMap(
		new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude), casapadova.realestate.vemap.startingZoomLevel, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 0,
		options
	);
    //alert("End");
}

casapadova.realestate.vemap.mouseHandler = function(e) {
    if (e.eventName == "onmousedown" && e.elementID != null) {
        casapadova.realestate.vemap.dragShape = casapadova.realestate.vemap.vecontrol.GetShapeByID(e.elementID);
        return true;
    } else if (e.eventName == "onmouseup") {
        casapadova.realestate.vemap.dragShape = null;
    } else if (e.eventName == "onmousemove" && casapadova.realestate.vemap.dragShape != null) {
        var x = e.mapX;
        var y = e.mapY;
        pixel = new VEPixel(x, y);
        var LL = casapadova.realestate.vemap.vecontrol.PixelToLatLong(pixel);
        casapadova.realestate.vemap.dragShape.SetPoints(LL);
        $("#Longitude").val(LL.Longitude);
        $("#Latitude").val(LL.Latitude);
        return true; // prevent the default action 
    }
}


casapadova.realestate.vemap.addLoadEvent = function (func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{ window.onload = func; }
	else {
		window.onload = function ()
		{ oldonload(); func(); }
	}
}

casapadova.realestate.vemap.onMapLoaded = function () {
	if (casapadova.realestate.vemap.showPin) {
		casapadova.realestate.vemap.pin = new VEShape(VEShapeType.Pushpin, new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude));
		casapadova.realestate.vemap.pin.SetCustomIcon(casapadova.realestate.vemap.pinIconUrl);
		casapadova.realestate.vemap.pin.ShowIcon();
		casapadova.realestate.vemap.pin.SetDescription(casapadova.realestate.vemap.pinDescription);
		casapadova.realestate.vemap.vecontrol.AddShape(casapadova.realestate.vemap.pin);
	} else {
		//casapadova.realestate.vemap.createPushpinArea();
	}
	casapadova.realestate.vemap.vecontrol.Resize();
	casapadova.realestate.vemap.vecontrol.SetMapView(new VEMapViewSpecification(new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude), casapadova.realestate.vemap.startingZoomLevel));
}

casapadova.realestate.vemap.createPushpinArea = function () {
	try
	{
		var ll = new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude);
		var shape = new VEShape(VEShapeType.Polyline, casapadova.realestate.vemap.createCircle(ll, 0.25));
		shape.HideIcon();
		casapadova.realestate.vemap.vecontrol.AddShape(shape);
	}
	catch(e)
	{
	  //alert(e.message);
	}
}

/*casapadova.realestate.vemap.createPushpinArea = function () {
	var ll = new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude);
	var shape = new VEShape(VEShapeType.Polyline, casapadova.realestate.vemap.createCircle(ll, 100));
	shape.HideIcon();
	casapadova.realestate.vemap.vecontrol.AddShape(shape);
}*/

casapadova.realestate.vemap.onAdminMapLoaded = function() {
    casapadova.realestate.vemap.vecontrol.AttachEvent("onmousedown", casapadova.realestate.vemap.mouseHandler);
    casapadova.realestate.vemap.vecontrol.AttachEvent("onmouseup", casapadova.realestate.vemap.mouseHandler);
    casapadova.realestate.vemap.vecontrol.AttachEvent("onmousemove", casapadova.realestate.vemap.mouseHandler);

    casapadova.realestate.vemap.pin = new VEShape(VEShapeType.Pushpin, new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude));
    casapadova.realestate.vemap.pin.SetCustomIcon(casapadova.realestate.vemap.pinIconUrl);
    casapadova.realestate.vemap.pin.ShowIcon();
    casapadova.realestate.vemap.pin.SetDescription(casapadova.realestate.vemap.pinDescription);
    casapadova.realestate.vemap.vecontrol.AddShape(casapadova.realestate.vemap.pin);
    casapadova.realestate.vemap.vecontrol.Resize();
    casapadova.realestate.vemap.vecontrol.SetMapView(new VEMapViewSpecification(
		new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude), casapadova.realestate.vemap.startingZoomLevel
	));
}

casapadova.realestate.vemap.loadVEAPI = function() {
    if (!casapadova.realestate.vemap.isLoaded) {
        // set a nice animated gif to show the map is loading
        $("#mapLoader").show();
        if (!($.browser.msie) && !($.browser.mozilla) && !($.browser.safari)) {
            $("#vemapcontainer").html("Virtual Earth is not supported by your browser.")
        }
        else {
            if (!($.browser.msie)) {
                //work around for non ie
                $.getScript("http://ecn.dev.virtualearth.net/mapcontrol/v6.2/js/atlascompat.js", "", true);
               }
            $.getScript("http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&mkt=" + casapadova.realestate.vemap.scriptCulture + "&onScriptLoad=" + casapadova.realestate.vemap.scriptLoadCallback, "", true);
        }
        casapadova.realestate.vemap.isLoaded = true;
    }
}
