﻿// Wrapper functions for Ajax calls
EPiGoogleAjax.prototype.EPiGoogleMap = null;
EPiGoogleAjax.prototype.OutputDiv = null;

EPiGoogleAjax.prototype.Output = function(text) {
    if (this.OutputDiv != null)
        this.OutputDiv.innerHTML += text + '<br />';
}

EPiGoogleAjax.prototype.LoadObject = function(objectId, centerOnLoadedObject) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=loadobject&id=' + objectId.toString(),
        function(data) {
            if (data.Message != undefined) {
                me.Output(data.Message);
            }
            else {
                if (centerOnLoadedObject == true)
                    me.EPiGoogleMap.AddObjectAndCenter(data);
                else
                    me.EPiGoogleMap.AddObject(data);
            }
        });
}

EPiGoogleAjax.prototype.LoadCapitexObject = function(capitexObjectId, centerOnLoadedObject) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=loadcapitexobject&id=' + capitexObjectId.toString(),
        function(data) {
            if (data.Message != undefined) {
                me.Output(data.Message);
            }
            else {
                if (centerOnLoadedObject == true)
                    me.EPiGoogleMap.AddCapitexObjectAndCenter(data);
                else
                    me.EPiGoogleMap.AddCapitexObject(data);
            }
        });
}


EPiGoogleAjax.prototype.LoadCluster = function(clusterId) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=loadcluster&id=' + clusterId.toString(),
    function(data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            me.EPiGoogleMap.AddCluster(data);
        }
    });
}

EPiGoogleAjax.prototype.LoadCategory = function(categoryId, doGoogleSearch, categoryObjects, loaderImage, checkBoxElement) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=loadcategory&id=' + categoryId.toString(),
    function(data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            me.EPiGoogleMap.AddCategory(data, doGoogleSearch, categoryObjects, loaderImage, checkBoxElement);
        }
    });
}


EPiGoogleAjax.prototype.GetCategories = function(callback) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=getcategories',
    function(data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            callback(data);
        }
    });
}

EPiGoogleAjax.prototype.GetCategoryObjects = function(id, callback) {
    var me = this;
    $.getJSON('/MapJasonHandler.axd?method=getcategoryobjects&id=' + id.toString(),
    function(data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            callback(data);
        }
    });
}


EPiGoogleAjax.prototype.GetClusters = function (lang, callback) {
    var me = this;
    if (lang == null) lang = 'sv';
    $.getJSON('/MapJasonHandler.axd?method=getclusters&lang=' + lang.toString(),
    function (data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            callback(data);
        }
    });
}

function EPiGoogleAjax(ePiGoogleMap, outputDiv) {
    this.EPiGoogleMap = ePiGoogleMap;

    var Me = this;
    $.getJSON('/MapJasonHandler.axd?method=getdefaultlocalsearchicon',
    function(data) {
        if (data.Message != undefined) {
            me.Output(data.Message);
        }
        else {
            Me.EPiGoogleMap.DefaultLocalSearchMapIconJson = data;
        }
    });
}
