var nokiaMapLoader = {};

nokiaMapLoader.url = "http://maptile.svc.nokia.com.edgesuite.net/players/map/noauth/js/14"; 

nokiaMapLoader.options = {
    urlAuth: "http://maptile.svc.nokia.com.edgesuite.net/players/map/auth/js/14",
    path: {
        pfwPath: nokiaMapLoader.url + "/pfw/",
        dfwPath: nokiaMapLoader.url + "/dfw/",
        mapplayerPath: nokiaMapLoader.url + "/mapplayer/"
    },
    playerDomId: "player",
    defaultLanguage: "en-GB",
    language: "",
    jsPlugin: "supported",
    token: "",
    onInitDone: null,
    playerName: "player"
};

nokiaMapLoader.getPlayer = function(aOptions) {
    if (aOptions !== undefined && aOptions !== null) {
        nokiaMapLoader.combineOptions(aOptions);
    }
    if (!nokiaMapLoader.options.uiLanguage) {
        nokiaMapLoader.options.uiLanguage = nokiaMapLoader.options.defaultLanguage;
    }
    if (nokiaMapLoader.options.token !== null && nokiaMapLoader.options.token !== undefined) {
        nokiaMapLoader.token = nokiaMapLoader.options.token;
    }
    //downloading the css
    nokiaMapLoader.fetchFile(nokiaMapLoader.options.path.mapplayerPath + "stylesheets/web/import_mapplayer.css", null, false);
    nokiaMapLoader.loadLanguageFile();
    //test if ie then also download ie css file
    var _userAgent = navigator.userAgent.toLowerCase();
    if (/msie/.test(_userAgent)) {
        nokiaMapLoader.fetchFile(nokiaMapLoader.options.path.mapplayerPath + "stylesheets/web/ie_mapplayer.css", null, false);
    }
    //downloading the js file(s)
    if (typeof nokiaMapLoader.options.urlAuth !== "undefined" && typeof nokiaMapLoader.options.urlAuth !== "null") {
        nokiaMapLoader.fetchFile(nokiaMapLoader.options.urlAuth + "/mapplayer/javascripts/nokia.mapplayer.js", nokiaMapLoader.token, true);
    } else {
        nokiaMapLoader.fetchFile(nokiaMapLoader.options.path.javascriptPath + "nokia.mapplayer.js", null, false);
    }
};

nokiaMapLoader.fetchFile = function(aFile, aToken, aWithToken) {
    if (aFile !== undefined && aFile !== null) {
        var elem = null;
        if (aFile.indexOf(".css") >= 0) {
            elem = document.createElement('link');
            elem.setAttribute("rel", "stylesheet");
            elem.setAttribute("type", "text/css");
            elem.setAttribute("href", aFile);
        } else {
            elem = document.createElement('script');
            var url = aFile;
            if (aToken !== null && aToken !== undefined && aWithToken) {
                url += "?token=" + aToken + "&referer="+ nokiaMapLoader.getHost();
            }
            elem.src = url;
        }
        var head = document.getElementsByTagName("head");
        var head_0 = head[0];
        document.getElementsByTagName("head")[0].appendChild(elem);
    }
};

nokiaMapLoader.onMapPlayerloaded = function() {
    if (typeof __locale == "undefined" || __locale == null ){
        ++nokiaMapLoader.loadCounter;
        if (nokiaMapLoader.loadCounter > 50){
            window.alert("can not not load the translation file, please check you configuration");
            return;
        }
        window.setTimeout(nokiaMapLoader.onMapPlayerloaded, 100);
        return;
    }
    window[nokiaMapLoader.options.playerName] = new nokia.maps.mapplayer.web.WebMapPlayer(document.getElementById(nokiaMapLoader.options.playerDomId), nokiaMapLoader.options);
    if (typeof nokiaMapLoader.options.onInitDone == "function") {
        window.player.addEventHandler(nokia.maps.pfw.Player.EVENT_INITIALIZATION_DONE, nokiaMapLoader.options.onInitDone);
    }
};

nokiaMapLoader.supportedLocales = {
    'cs-cz': 1,
    'cs': 1,
    'da-dk': 1,
    'da': 1,
    'de-de': 1,
    'de': 1,
    'el-gr': 1,
    'el': 1,
    'en-gb': 1,
    'en': 1,
    'en-us': 1,
    'es-ca': 1,
    'es-co': 1,
    'es-es': 1,
    'es-eu': 1,
    'es-gl': 1,
    'es': 1,
    'es-mx': 1,
    'fi-fi': 1,
    'fi': 1,
    'fr-ca': 1,
    'fr-fr': 1,
    'fr': 1,
    'hr-hr': 1,
    'hr': 1,
    'hu-hu': 1,
    'hu': 1,
    'id-id': 1,
    'id': 1,
    'is-is': 1,
    'is': 1,
    'it-it': 1,
    'it': 1,
    'ko-kr': 1,
    'ko': 1,
    'pl': 1,
    'pl-pl': 1,
    'pt-br': 1,
    'pt': 1,
    'pt-pt': 1,
    'ro': 1,
    'ro-ro': 1,
    'ru': 1,
    'ru-ru': 1,
    'sk': 1,
    'sk-sk': 1,
    'sv': 1,
    'sv-se': 1,
    'th': 1,
    'th-th': 1,
    'tr': 1,
    'tr-tr': 1};

nokiaMapLoader.combineOptions = function(newOptions) {
    for (var x in newOptions) {
        if (x === "path") { //copies the path one by one
            for (var y in newOptions[x]) {
                nokiaMapLoader.options[x][y] = newOptions[x][y];
            }
        } else { //overwrites the settings
            nokiaMapLoader.options[x] = newOptions[x];
        }
    }
};

nokiaMapLoader.loadLanguageFile = function() {
    var lang = nokiaMapLoader.options.uiLanguage || nokiaMapLoader.options.defaultLanguage;
    nokiaMapLoader.options.uiLanguage = lang = lang.toLowerCase();
    var splitLang = lang.split("-");
    var path = "";
    if (!nokiaMapLoader.supportedLocales[lang]) {
        if (splitLang.length == 2){
            if (nokiaMapLoader.supportedLocales[splitLang[0]]){
                splitLang = [splitLang[0]];
                nokiaMapLoader.options.uiLanguage = splitLang[0];
            } else {
                splitLang = [nokiaMapLoader.options.defaultLanguage];
                nokiaMapLoader.options.uiLanguage = nokiaMapLoader.options.defaultLanguage;
            }
        } else {
            nokiaMapLoader.options.uiLanguage = nokiaMapLoader.options.defaultLanguage;
            splitLang = [nokiaMapLoader.options.defaultLanguage];
        }
    }
    
    for (var i = 0, length = splitLang.length; i < length; i++) {
        path += splitLang[i] + "/";
    }
    nokiaMapLoader.fetchFile(nokiaMapLoader.url + "/i18n/" + path + "locale.json", "blabla", false);
};

nokiaMapLoader.getHost = function(){
    if (location !== undefined && location !== null) {
        return (location.hostname || "localhost");
    } else {
        return "";
    }
}


/**
 * this must be removed later on
 */
var onMapPlayerloaded = function() {
    nokiaMapLoader.onMapPlayerloaded();
};
