/// <reference path="Modes.js" />
/// <reference path="Layers.js" />

// settings.js - contains map settings


/*
    Settings - *touch with care*
    
    {% server_parameters %} can be looked-up in zMapLib\zMap.MapParametersFormatter class.
*/


// root folder
var _baseHost = "http://maps.pointthailand.com/v1";

Settings = {

    // ____________________________________________________________________________________________
    //                                                                         Operating Parameters
    
    MapIP:                      "webmap-1",
    MapVersion:                 "1.0.3534.24290",
    CurrentHost:                _baseHost,

    // *experimental* - Enable this key if you experience any encoding problems.
    //ServicesEncoding:           "windows-874",

    SmMapHost:                  "http://pointnetwork.pointasia.com/imap/Sm256/",
    ImageHost:                  _baseHost + "/images/",
    MapHost:                    _baseHost + "/",
    LegacyServicesHost:         _baseHost + "/services/legacy/",
    ServicesHost:               _baseHost + "/services/",
    InfHost:                    _baseHost + "/moreinfo.aspx",
    FileHost:                   _baseHost + "/",

    Email:                      "http://gw.pointasia.com/pointme3/P_Send.aspx",
    //POISearchURL:             null, // auto-detected

    AvailableLanguages:         { th: "ไทย", en: "English" },
    
    ServicesEncoding:           "windows-874",


    // ____________________________________________________________________________________________
    //                                                                             Startup Defaults

    ControlSet:                 "full",
    Language:                   "th",
    Mode:                       "road",
    LandmarksVisible:           true,
    
    Lat:                        13.75,
    Lon:                        100.5,
    Zoom:                       11,
    Text:                       "",

    // most features are disabled by default, for more information, please check the wiki.
    ContextMenuInfoEnabled:     false,
    LeftClickInfoEnabled:       false,


    // ____________________________________________________________________________________________
    //                                                                             Feature-specific

    // see http://wiki/zMap%20ContextMenuInfoHost.ashx
    ContextMenuInfoHost:        _baseHost + "/test.fragment.html",

    // see http://wiki/zMap%20LeftClickInfoHost.ashx
    LeftClickInfoHost:          _baseHost + "/test/echo.aspx",
    LeftClickInfoBalloonSize:   4,


    // ____________________________________________________________________________________________
    //                                                                      User Interface Settings

    CanvasIndex:                20, 
    BalloonIndex:               400,
    ControlsIndex:              410,

    ToggleColor1:               "#343a7c",
    ToggleColor2:               "#ffffff",

    // background color of top toolbar panel
    PanelBackgroundColor:       "#ffffff"


};



// configure settings
Settings.initialize = function() {
    Settings.Mode = Modes.coerceMode(Settings.Mode);
    if (!Settings.ServicesEncoding)
        Settings.ServicesEncoding = 'windows-874';
};/// <reference path="settings.js" />


// Modes.js - Contains modes layers and landmarks definition.


var Modes = {

    // don't change modes name, change layer list only
    
    satellite:    { layers:       { th: ["satellite"],
                                    en: ["satellite"] },
                    landmarks:    { th: ["poithsatellite"],
                                    en: ["poiensatellite"] }
                  },

    road:         { layers:       { th: ["roadline", "bkkbuilding", "boundarynamethroad", "roadnamethroad"],
                                    en: ["roadline", "bkkbuilding", "boundarynameenroad", "roadnameenroad"] },
                    landmarks:    { th: ["poithroad"],
                                    en: ["poienroad"] }
                  },
                    
    combined:     { layers:       { th: ["satellite", "transroad", "vector", "roadnamethsatellite"],
                                    en: ["satellite", "transroad", "vector", "roadnameensatellite"] },
                    landmarks:    { th: ["poithsatellite"],
                                    en: ["poiensatellite"] }
                  }


};

Modes.initialize = function() {

    // co-erce all old-style modes to new-style modes and vice-versa
    // this is for backward compatibility support
    var pairs = { sate: "satellite", comb: "combined" };

    for (var key in pairs) {
        if (!Modes[key]) Modes[key] = Modes[pairs[key]];
        if (!Modes[pairs[key]]) Modes[pairs[key]] = Modes[key];
    }

};

// Build layer set from specified mode/language
Modes.getLayers = function(mode, lang) {
    return Modes[mode].layers[lang];
};

Modes.getLandmarks = function(mode, lang) {
    return Modes[mode].landmarks[lang];
};


// co-erce all style modes to use new mode names
Modes.coerceMode = function(mode) {

    var mapping = { sate: "satellite", comb: "combined" };

    mode = mode.toLowerCase();

    if (mapping[mode]) return mapping[mode];

    return mode;

};


// gets the currently visible layer set
Modes.getCurrentLayersSet = function() {

    var mode = Modes.coerceMode(Settings.Mode);
    var lang = Settings.Language || Settings.AvailableLanguages[0];

    var layers = Modes.getLayers(mode, lang);
    var landmarks = Modes.getLandmarks(mode, lang);


    if (Settings.LandmarksVisible)
        layers = layers.concat(landmarks);


    return layers;
};

Modes.getCurrentLayersHosts = function() {

    var layerSet = Modes.getCurrentLayersSet();

    var hosts = [];

    for (var idx in layerSet) {

        var layerName = layerSet[idx];
        var host = Layers[layerName].host;
        
        if (layerName == "satellite") host += "?jpg";

        hosts.push(host);
    };

    return hosts;

};/// <reference path="settings.js" />
/// <reference path="modes.js" />


var Layers = {

    // don't rename the satellite layer!
    satellite:                { index:  1, host: "http://sat.img.pointthailand.com/"             },

    // baseline layers
    transroad:                { index:  2, host: "http://trd.img.pointthailand.com/"             },
    roadline:                 { index:  3, host: "http://rd.img.pointthailand.com/"              },
    bkkbuilding:              { index:  4, host: "http://bkkbuilding.img.pointthailand.com/"     },
    vector:                   { index:  5, host: "http://vec.img.pointthailand.com/"             },
    boundarynamethroad:       { index:  6, host: "http://btr.img.pointthailand.com/"             },
    boundarynamethsatellite:  { index:  7, host: "http://bts.img.pointthailand.com/"             },
    boundarynameenroad:       { index:  8, host: "http://ber.img.pointthailand.com/"             },
    boundarynameensatellite:  { index:  9, host: "http://bes.img.pointthailand.com/"             },
    roadnamethroad:           { index: 10, host: "http://rtr.img.pointthailand.com/"             },
    roadnamethsatellite:      { index: 11, host: "http://rts.img.pointthailand.com/"             },
    roadnameenroad:           { index: 12, host: "http://rer.img.pointthailand.com/"             },
    roadnameensatellite:      { index: 13, host: "http://res.img.pointthailand.com/"             },
    poithroad:                { index: 14, host: "http://ptr.img.pointthailand.com/"             },
    poithsatellite:           { index: 15, host: "http://pts.img.pointthailand.com/"             },
    poienroad:                { index: 16, host: "http://per.img.pointthailand.com/"             },
    poiensatellite:           { index: 17, host: "http://pes.img.pointthailand.com/"             },
    busline:                  { index: 18, host: "http://pointnetwork.pointasia.com/imap/bus256/"}
 
};

// initialize layers with properties
Layers.initialize = function() {

    for (var key in Layers) {

        // add visible flag
        Layers[key].visible = false;

        // add the tiles grid
        Layers[key].tiles = new Array();

    }
};

// provided for backward-compatibility so application which used to
// reference layers by IDs still function
Layers.findLayer = function(layerId) {

    // if it's available, just return the layer
    if (Layers[layerId])
        return Layers[layerId];

    // otherwise search the collection for a matching index
    for (var layerKey in Layers) {
        var layer = Layers[layerKey];
        if (layer.index == layerId)
            return layer;
    }

    return null;
};


// checks wether satellite image should be visible
Layers.isSatelliteVisible = function() {

    var mode = Modes.coerceMode(Settings.Mode);

    return mode == "sate";
};

// Util.js - provide various unrelated helper methods.

Util = new function() {

    // base 64 encode/decode ______________________________________________________________________
    // code obtained from: http://www.webtoolkit.info/javascript-base64.html
    
    this._base64key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    this.base64Encode = function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    };

    // public method for decoding
    this.base64Decode = function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    };

    // private method for UTF-8 encoding
    this._utf8_encode = function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    };

    // private method for UTF-8 decoding
    this._utf8_decode = function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    };


    // configuration object helpers _______________________________________________________________
    this.config = function(userConf, defaultConf) {

        if (!userConf) userConf = { /* empty object */ };

        for (var k in defaultConf)

            // only check for explicitly undefined values
            // if user explicitly supplied a null value, don't override it, use the null value as-is
            if (userConf[k] == undefined && !userConf.hasOwnProperty(k))
                userConf[k] = defaultConf[k];

        return userConf;

    };

};/// <reference path="Modes.js" />
/// <reference path="Layers.js" />
/// <reference path="settings.js" />

//                                                                                
//                                                                                
//                          MMMM     ZMMMM                                        
//                         .MMMM~   .MMMMM                                        
//                         MMMMMD  .MMMMMM                                        
//        MMMMMMMMMMMMM   .MMMMMM .MMMMMMI      MMMMMMMMMM    MMM~ .MMMMMM        
//        MMMMMMMMMMMMO   DMM.MMM MMM.MMM.   7MMMMMMMMMMMM    MMM.MMMMMMMMM       
//               MMMM     MMM.MMM8MM  MMM.  MMMMM     MMMM    MMMMMM  .?MMMM      
//             MMMMM.     MMM.DMMMM$ ~MMM. DMMM8     ~MMM.    MMMMO     MMMM      
//           +MMMM       MMM. :MMMM  MMMZ  MMMM      MMMM.   ~MMMM     .MMMM      
//          MMMMM.       MMM. .MMM   MMM   MMM8      MMMM.   MMMM.     MMMM.      
//        8MMMM         DMMN   ...  .MMM  8MMM,    .MMMM:    MMMM.    .MMMM       
//       MMMMM          MMM         7MMM  IMMMD   8MMMMM     MMM.     MMMM        
//     ~MMMMMMMMMMMM:  .MMM         MMM8  .MMMMMMMMMOMMM.   MMMMMMMMMMMMM         
//     MMMMMMMMMMMMM   MMM+         MMM.    MMMMMMM .MMM    MMMMMMMMMMM           
//                                           .             .MMM:  ..              
//                                                         MMMM                   
//                                                        .MMMM                   
//                                                         MMM=                   
//
//
//                     Copyright (c) Ecartstudio Co., Ltd.
//



// main.js - preload/launch the application


// global typedefs

Settings.initialize();
Layers.initialize();
Modes.initialize();



// initialization script
function Script(src) {
    document.write('<script src="' + src + '" type="text/javascript"></script>');
}

Script(Settings.MapHost + "maps1.aspx?v=" + Settings.MapVersion);


//document.write(unescape("%3Cscript src='maps1.aspx?v=" + Settings.MapVersion + "' type='text/javascript'%3E%3C/script%3E"));

//document.write('\x3Cscript src="maps1.aspx?v=' + Settings.MapVersion + '" type="text/javascript"\x3E\x3C/script\x3E');

//var script = document.createElement("script");
//script.src = "maps1.aspx?v=1234";
//script.type = "text/javascript";
//document.getElementsByTagName("head")[0].appendChild(script);
