﻿
var index = 0;
var map;
var allPinsLoaded = false;
//Initialisiert die Virtual Earth Karte
//und zentriert sie über Deutschland
function initMap(longitude, latitude, isFixed, zoom)
{

 VEShape.prototype.CustomId = '';

var LA = new VELatLong(34.0540, -118.2370);
	
	if(!longitude)
		longitude = 51.165;
	
	if(!latitude)
		latitude = 10.455278;
		
    if(!zoom)
        zoom = 6;
		
	map = new VEMap("myMap");
	 
	map.LoadMap(new VELatLong(longitude, latitude, 0, VEAltitudeMode.Default),
				zoom,
				VEMapStyle.Road,
				isFixed,
				VEMapMode.Mode2D,
				true,
				0
			);
    
    map.AttachEvent("onchangeview", GetPinData);
        
	if(document.getElementById('Longitude') && document.getElementById('Latitude'))
    {
        if(document.getElementById('Longitude').value && document.getElementById('Latitude').value)
        {
            reInitMap(document.getElementById('Longitude').value, document.getElementById('Latitude').value);
        }
    }				
			
}


function initSearchMap(longitude, latitude, isFixed, zoom)
{

 VEShape.prototype.CustomId = '';

var LA = new VELatLong(34.0540, -118.2370);
	
	if(!longitude)
		longitude = 51.165;
	
	if(!latitude)
		latitude = 10.455278;
		
    if(!zoom)
        zoom = 6;
		
	map = new VEMap("myMap");
	 
	map.LoadMap(new VELatLong(longitude, latitude, 0, VEAltitudeMode.Default),
				zoom,
				VEMapStyle.Road,
				isFixed,
				VEMapMode.Mode2D,
				true,
				0
			);
    
    if(document.getElementById('Longitude') && document.getElementById('Latitude'))
    {
        if(document.getElementById('Longitude').value && document.getElementById('Latitude').value)
        {
            reInitMap(document.getElementById('Longitude').value, document.getElementById('Latitude').value);
        }
    }				
			
}

function GetPinData(e)
{
    //Bei Zoomstufe 4 ist die ganze Karte sichtbar
    if(map.GetZoomLevel() >= 4 && !allPinsLoaded)
    {
        map.Clear();
        LoadCustomersClusteredAsync();
        
        if(map.GetZoomLevel() == 4)
            allPinsLoaded = true;
    }
}

function initMapForAdministration(longitude, latitude)
{
    initMap();
    var latLong = new VELatLong(latitude, longitude);
    var pin = new VEShape(VEShapeType.Pushpin, latLong);
    pin.SetCustomIcon(GetSchadenFixHost() + 'content/images/pin.png');
    map.AddShape(pin);
    
}

function reInitMap(longitude, latitude)
{
    
    if(longitude  && latitude)
    {
        
        CenterMapForReInit(longitude, latitude, 15);
                   
        var latLong = new VELatLong(latitude, longitude, 0, VEAltitudeMode.Default);
        var pin = new VEShape(VEShapeType.Pushpin, latLong);
        pin.SetCustomIcon(GetSchadenFixHost() + 'content/images/pin.png');
        map.AddShape(pin);
    }
}

function CenterMapForReInit(longitude, latitude, zoom)
{
    var veLatLong = new VELatLong(latitude, longitude);
    map.SetCenterAndZoom(veLatLong, zoom);    
}

//Nimmt ein Array von Kunden-Objekten entgegen
//und platziert die dazugehörigen Pins auf 
//der Karte
function SetCustomerPins(customers)
{
   var index  = customers.length;
   for(var i = 0; i < customers.length; i++)
   {  
		var customer = customers[i];
        
        var premiumInfo = "";
        var officeContact = "";
       
        
        if(customer.IsPremium){
            premiumInfo = "E-Mail: " + "<a href=\'mailto:" + customer.EMail + "\'>" + customer.EMail +  "</a>" + "<br/>";												

						    
		    if(customer.WebUrl != '' && customer.WebUrl != null)
            {
                premiumInfo = premiumInfo + "<a href='" + GetFullUrl(customer.WebUrl) + "'>" + customer.WebUrl + "</a><br/><br/>"
            }				    
			
			//premiumInfo = premiumInfo + "<a href=\'" + GetSchadenFixHost() + customer.CityTag + "/" + customer.SchadenFixIdentifier +"\'>Zum SchadenFix-Portal der Kanzlei</a><br/><br/>" +
			//			    "<a href=\'" + GetSchadenFixHost() + customer.CityTag + "/" + customer.SchadenFixIdentifier +"/homepage/form\'>Schaden melden</a><br/>";
        
            if(customer.Contact != '')
            {
                officeContact = customer.Contact + "<br/>";
            }
        }
        
		
   		var latLong = new VELatLong(customer.Latitude, customer.Longitude); 
		var pin = new VEShape(VEShapeType.Pushpin, latLong);
		pin.SetTitle(customer.OfficeName);
		pin.SetDescription(
		                    officeContact +
							customer.Street + "<br/>" +
							customer.PostalCode + " " + customer.City + "<br/>" +
							"Tel.: " + customer.Phone + "<br/>" +
						    "Fax: " + customer.Fax + "<br/>" +
							premiumInfo
						   );
		
		if(IsIE6())
		{			
			var img = "<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + GetSchadenFixHost() + "dot.ashx?number=" + customer.ID +"', sizingMethod='image'); height:40px;width:35px;}\" ></div>";
		    pin.SetCustomIcon(img);
		}
		else
		{
		    pin.SetCustomIcon(GetSchadenFixHost() + 'dot.ashx?number=' + customer.ID);
		}
	    //pin.SetCustomIcon(GetSchadenFixHost() + 'content/pics/dot.png');
	    pin.CustomId = customer.SchadenFixIdentifier;
        var index = index - 1;
		map.AddShape(pin);

    }
}


function GetFullUrl(sourceUrl)
{

    if(!sourceUrl)
        return '';
    
    if(sourceUrl == '')
        return '';
      
    if(sourceUrl.substring(0, 7) != 'http://')
    {

        return 'http://' + sourceUrl;
    }
    else
        return sourceUrl;
}

function LoadCustomersAsync()
{
    $.getJSON(GetSchadenFixHost() + "Home/GetCustomerMapDataAsynch", null, CustomersLoaded);	
}


function LoadCustomersClusteredAsync()
{
    var mWidth = 590;
    var mHeight = 200;

    var latLongInformation = map.PixelToLatLong(new VEPixel(0, 0));

    var latitudeNW = latLongInformation.Latitude;
    var longitudeNW = latLongInformation.Longitude;
        
    latLongInformation = map.PixelToLatLong(new VEPixel(mWidth,mHeight));
    var latitudeSE = latLongInformation.Latitude;
    var longitudeSE = latLongInformation.Longitude;
    
    $.getJSON(GetSchadenFixHost() + "Home/GetCustomerMapDataClusteredAsync?longitudeNW=" + longitudeNW + "&latitudeNW=" + latitudeNW + "&longitudeSE=" + longitudeSE + "&latitudeSE=" + latitudeSE, null, CustomersLoaded);
    
}

function CustomersLoaded(data)
{
    SetCustomerPins(data)
}

//Sendet eine Anfrage an Virtual Earth
function submitRequestToVirtualEarth(street, postalCode, city)
{
	var searchTerm = street + ", " + postalCode + " " + city + ", Deutschland";
	
	map.Find(null, 
			searchTerm, 
			null,
			null,
			0,
			10,
			true,
			true,
			true,
			true,
			locationFound);	
	
}

function locationFound(shapeLayer, findResults, places, hasMore, veErrorMessage)
{
	var pin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
	pin.SetCustomIcon(GetSchadenFixHost() + 'content/images/pin.png');
	map.AddShape(pin);
	index++;
	document.getElementById('Latitude').value = places[0].LatLong.Latitude;
	document.getElementById('Longitude').value = places[0].LatLong.Longitude;
}

function checkForGeoData()
{

    var street = document.getElementById('Street').value;
    var postalCode = document.getElementById('PostalCode').value;
    var city = document.getElementById('City').value;
	if(street && postalCode && city )
    {
		submitRequestToVirtualEarth(street, postalCode, city)
        return;
    }
    
    map.Clear();   
    document.getElementById('Latitude').value = "";
	document.getElementById('Longitude').value = "";
}

function navigateToInputAdress()
{
        var searchTerm = document.getElementById('psNavigationString').value;

    	map.Find(null, 
			searchTerm, 
			null,
			null,
			0,
			10,
			true,
			true,
			true,
			true,
			locationFound);	
}

function CenterMap(longitude, latitude, zoom)
{
    map.Clear();
    var veLatLong = new VELatLong(latitude, longitude);
    map.Clear();
    map.SetCenterAndZoom(veLatLong, zoom);    
}

function AddPinFor(latitude, longitude, officeName, street, postalCode, city, phone, fax, cityTag, schadenFixIdentifier, eMail, isPremium, index, url, contact)
{
    var latLong = new VELatLong(latitude, longitude); 
	var pin = new VEShape(VEShapeType.Pushpin, latLong);
	
    var premiumInfo = "";
    var officeContact = "";
    
    if(isPremium == 'True'){
       premiumInfo = "E-Mail: " + "<a href=\'mailto:" + eMail + "\'>" + eMail +  "</a><br/>" ;
       
        if(url != '' && url != null)
        {
            premiumInfo = premiumInfo + "<a href='" + GetFullUrl(url) + "'>" + url + "</a><br/><br/>"
        }
       
	    premiumInfo = premiumInfo +	"<a href=\'" + GetSchadenFixHost() + cityTag + "/" + schadenFixIdentifier +"\'>Zum SchadenFix-Portal der Kanzlei</a><br/><br/>" +
					    "<a href=\'" + GetSchadenFixHost() + cityTag + "/" + schadenFixIdentifier +"/homepage/formular\'>Schaden melden</a><br/>";

        
        if(contact != '')
        {
            officeContact = contact + "<br/>";
        }
					    
    }
	
	pin.SetTitle(officeName);
	pin.SetDescription(
	                    officeContact +
						street + "<br/>" +
						postalCode + " " + city + "<br/>" +
						"Tel.: " + phone + "<br/>" +
						"Fax: " + fax + "<br/>" +
					    premiumInfo						
					   );
	
	
    if(IsIE6())
	{			
		var img = "<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + GetSchadenFixHost() + "dot.ashx?number=" + index +"', sizingMethod='image'); height:40px;width:35px;}\" ></div>";
	    pin.SetCustomIcon(img);
	}
	else
	{
		pin.SetCustomIcon(GetSchadenFixHost() + 'dot.ashx?number=' + index);
	}
	//pin.SetCustomIcon(GetSchadenFixHost() + 'content/pics/dot.png');
	pin.CustomId = schadenFixIdentifier;

	map.AddShape(pin);
}

function SelectPin(identifier)
{

    IterateThroughShapsesAndChangeIcon(identifier, 'content/pics/dot_highlight.png');   
}

function DeselectPin(identifier)
{

    IterateThroughShapsesAndChangeIcon(identifier, 'content/images/pin.png');
}

function IterateThroughShapsesAndChangeIcon(identifier, path)
{
    var shapeLayerCount = map.GetShapeLayerCount();
    var pinFound = false;
    
    for(var i = 0; i < shapeLayerCount; i++)
    {
        var shapeLayer = map.GetShapeLayerByIndex(i);
        var shapeCount = shapeLayer.GetShapeCount();
        for(var j = 0; j < shapeCount; j++)
        {            
            var shape = shapeLayer.GetShapeByIndex(j);                   
            if(shape.CustomId == identifier)
            {

                shape.SetCustomIcon(GetSchadenFixHost() + path);
                return;
            }                
        }
        if(pinFound)
        {
            
            return;
        }
    }
}


function IsIE6()
{
	var browser = navigator.appName;
	var version = navigator.appVersion.substring(0, 1); 
	return ((browser == "Microsoft Internet Explorer") && (version <= 6));

}

function GetSchadenFixHost()
{
    return "/";
}

function GetSecureSchadenFixHost()
{
	return "/";
}

function GetSecureHost()
{
    return "https://secure.e-consult-ag.de/";
}