﻿// Copyright(c) 2008-2009 IT One, Ltd, Slovakia
// Do not use, change or distribute.

var jsonPropsMap;
var jsonObjects;

function initJSONObjects() {
    jsonObjects = new Object();
    jsonObjects.layers = new Array();
}

function InitJSONPropertyMappings() {
    CleverGlobeService.GetJSONPropsMapping(function(res) {
        jsonPropsMap = new Object();
        jsonPropsMap.catIDs = new Array();
        jsonPropsMap.propIDs = new Array();
        jsonPropsMap.jsonPropNames = new Array();

        for (i = 0; i < res.length; i += 3) {
            jsonPropsMap.catIDs.push(Number(res[i]));
            jsonPropsMap.propIDs.push(Number(res[i + 1]));
            jsonPropsMap.jsonPropNames.push(res[i + 2]);
        }
    });
}

function getJSONPropFromID(layerID, propID) {
    for (i = 0; i < jsonPropsMap.catIDs.length; i++) {
        if (jsonPropsMap.propIDs[i] == propID && jsonPropsMap.catIDs[i] == layerID) {
            return jsonPropsMap.jsonPropNames[i];
        }
    }

    return "";
}

function GetObjectsFromJSONQuery(layer, bounds, propID) {
    if (layer.checked == true) {
        var querySplitted = layer.JSONQuery.split(";");
        var query = querySplitted[0];
        var root = querySplitted[1];
        query = query.replace("{lat}", map.getCenter().lat().toString());
        query = query.replace("{lng}", map.getCenter().lng().toString());
        query = query.replace("{west}", bounds.getSouthWest().lng().toString());
        query = query.replace("{south}", bounds.getSouthWest().lat().toString());
        query = query.replace("{north}", bounds.getNorthEast().lat().toString());
        query = query.replace("{east}", bounds.getNorthEast().lng().toString());

        CleverGlobeService.DoJSONQuery(query, function(res) {
            var data = JSON.parse(res);
            var dataRoot = eval("data." + root);

            var jsonLayer = AddJSONLayer(layer.id);
            jsonLayer.data = dataRoot;

            for (di = 0; di < dataRoot.length; di++) {
                var jsonProp = getJSONPropFromID(layer.id, -1);
                var lat = GetObjectPropertyValue(dataRoot[di], jsonProp);
                jsonProp = getJSONPropFromID(layer.id, -2);
                var lng = GetObjectPropertyValue(dataRoot[di], jsonProp);
                jsonProp = getJSONPropFromID(layer.id, propID);
                var propVal = GetObjectPropertyValue(dataRoot[di], jsonProp);

                createJSONMarker(layer.id, di, dataRoot[di], lat, lng, layer.icon, propVal, "#aaFFCF", layer.markerCallback);
            }

            if (getObjectsInProggress != true)
                textCenterMap.style.visibility = "hidden";
        });
    }
}

function GetObjectPropertyValue(obj, propName) {
    var val = null;

    try {
        val = eval("obj." + propName);
    }
    catch (errr) { }

    return val;
}

function AddJSONLayer(id) {
    for (i = 0; i < jsonObjects.layers.length; i++) {
        if (jsonObjects.layers[i].id == id) {
            //reset existing
            jsonObjects.layers[i].data = null;
            return jsonObjects.layers[i];
        }
    }
    if (i == jsonObjects.layers.length) { //add new
        var layer = new Object();
        layer.id = id;
        jsonObjects.layers.push(layer);
        return layer;
    }

    return 0;
}

function GetJSONObject(layerID, objectIndex) {
    for (i = 0; i < jsonObjects.layers.length; i++) {
        if (jsonObjects.layers[i].id == layerID) {
            return jsonObjects.layers[i].data[objectIndex];
        }
    }
}

function FillJSONObjectXml(xml, obj, marker) {
    var xmlDoc;
    var ie = false;

    try //Internet Explorer
  {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xml);
        ie = true;
    }
    catch (e) {
        try //Firefox, Mozilla, Opera, etc.
    {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(xml, "text/xml");
        }
        catch (e) {
            return;
        }
    }


    var res = xmlDoc.getElementsByTagName("label");
    var attrib;
    var i;
    for (i = 0; i < res.length; i++) {
        attrib = res[i].getAttribute("prop");
        if (attrib == null)
            continue;
        var jsonProp = getJSONPropFromID(marker.layerID, attrib);
        var val = GetObjectPropertyValue(obj, jsonProp);
        if (ie)
            res[i].text = val;
        else
            res[i].textContent = val;
    }

    res = xmlDoc.getElementsByTagName("img");
    for (i = 0; i < res.length; i++) {
        attrib = res[i].getAttribute("prop");
        if (attrib == null)
            continue;
        var jsonProp = getJSONPropFromID(marker.layerID, attrib);
        var val = GetObjectPropertyValue(obj, jsonProp);
        res[i].setAttribute("src", val);
    }

    res = xmlDoc.getElementsByTagName("a");
    for (i = 0; i < res.length; i++) {
        attrib = res[i].getAttribute("prop");
        if (attrib == null)
            continue;
        var jsonProp = getJSONPropFromID(marker.layerID, attrib);
        var val = GetObjectPropertyValue(obj, jsonProp);
        var txtVal = val;
        if (txtVal.length > 30) {
            txtVal = txtVal.substring(0, 30);
            txtVal += "..";
        }
        if (ie)
            res[i].text = txtVal;
        else
            res[i].textContent = txtVal;

        if (val.indexOf("http") == -1)
            val = "http://" + val;

        res[i].setAttribute("href", val);
    }

    if (ie == false) {
        var str = (new XMLSerializer()).serializeToString(xmlDoc);
        return str;
    }

    return xmlDoc.xml;
}