/****************************************************************
****** Script:   mapViewerWindow.js
****** Author:   Violeta
****** Copyright: Sintagma
****** Create date: 2008-03-04
****** Description: MapViewer window handler
****************************************************************/

var mapViewer = new MapViewerWindow("mapViewerWindow","https://www.geoportal.lt/map");

function MapViewerWindow(mapViewerWindowName, mapViewerUrl){

    //----------------------------------------
    // ------- attributes (begin) ------------
    //----------------------------------------
    this.mapViewerUrl = mapViewerUrl;
    this.mapViewerWindowName = mapViewerWindowName;
    this.mapViewerWindow = null;
    // mapViewer'io lango parametrai. Esant reikalui, galima bus juos parametrizuoti, dabar sudedu kaip konstantas
    this.left = 10;
    this.top = 10;
    this.width = 1024;
    this.height = 768;
    this.toolbar = 1;
    this.location = 0;
    this.status = 1;
    this.resizable = 1;
    this.scrollbars = 1;
    // Konstantos requestui. Savavaliskai nekeisti!
    // url pattern'ai. Priklauso nuo mapViewerio aplikacijos. 
    this.URL_OPEN_DEFAULT = "openDefault";
    this.URL_OPEN_SERVICE = "openService";
    this.URL_OPEN_SAVEDMAP = "openSavedMap";
    // parametru pavadinimai
    this.PARAM_NAME_SERVICE_ORDER_ID = "serviceOrderId";
    this.PARAM_NAME_SERVICE_PRODUCT_ID = "serviceProductId";    
    this.PARAM_NAME_SERVICE_TYPE = "serviceType";    
    this.PARAM_NAME_SERVICE_NAME = "serviceName";
    this.PARAM_NAME_SERVICE_URL = "serviceUrl";
    this.PARAM_NAME_TICKET = "ticket";
    this.PARAM_NAME_LANGUAGE = "lang";   
    this.PARAM_NAME_SAVED_MAP_ID = "savedMapId";
    this.PARAM_NAME_PORTAL_ACTION = "portalAction";   
    this.PARAM_NAME_WINDOW_IS_OPENING = "windowIsOpening";
            
    //----------------------------------------
    // ------- attributes (end) --------------
    //----------------------------------------
    
    //----------------------------------------
    // -------- methods (begin) --------------
    //----------------------------------------
    
    // Atidaro MapViewer'i su defaultiniais resursais ir defaultiniais nustatymais.
    this.startMapViewer = function(language, ticket) {
        var action = this.URL_OPEN_DEFAULT;
        this.openMapViewer(action, language, ticket, '', '', '', '', '', true, '');
    }
    
    // Atidaro pasirinkta paslauga mapviewer'yje. Atidaro mapviewerio langa, jeigu mapvieweris jau buvo atidarytas, prideda esame lange ir fokusuoja i mapviewerio lang1
    this.openService = function(serviceOrderId, serviceType, serviceName, serviceProductId, serviceUrl, language, ticket) {
        serviceName = encodeURI(serviceName);
        var action = this.URL_OPEN_SERVICE;
        this.openMapViewer(action, language, ticket, serviceOrderId, serviceType, serviceName, serviceProductId, serviceUrl, true, '');
    }
    
    // Atidaro isaugota zemelapi. 
    this.openSavedMap = function(savedMapId, language, ticket) {
        var action = this.URL_OPEN_SAVEDMAP;
        this.openMapViewer(action, language, ticket, '', '', '', '', '', true, savedMapId);
    }
    
    // Atidaro mapviewer'i arba fokusuoja i ji, jeigu jau yra atidarytas. 
    // Paduodamas mapviewer'io aplikacijos urlas
    // Funkcija vidiniam naudojimui.
    this.openMapViewer = function(action, language, ticket, serviceOrderId, serviceType, serviceName, serviceProductId, serviceUrl, /* @deprecated */windowIsOpening, savedMapId ) {
		var params = "left=" + this.left ;
		params += ",top=" + this.top;
		params += ",width=" + this.width;
		params += ",height=" + this.height;
		params += ",toolbar=" + this.toolbar;
		params += ",location=" + this.location;
		params += ",status=" + this.status;
		params += ",resizable=" + this.resizable;
		params += ",scrollbars=" + this.scrollbars + ";";
		// Naudojam GET uzklausa
		var openingUrl = this.createOpeningGETUrl(action, language, serviceOrderId, serviceType, serviceName, serviceProductId, serviceUrl, windowIsOpening, savedMapId);
		this.mapViewerWindow = window.open(openingUrl, this.mapViewerWindowName, params);
        this.mapViewerWindow.focus();
    }

    this.createOpeningGETUrl = function (action, language, serviceOrderId, serviceType, serviceName, serviceProductId, serviceUrl, /* @deprecated */windowIsOpening, savedMapId) {
                // kuriam  url
        var openingUrl = this.mapViewerUrl + "/#";
		//action
		openingUrl = openingUrl + this.PARAM_NAME_PORTAL_ACTION + '=' + this.encodeURIComponent(action);
        // language element
		openingUrl = openingUrl + '&' + this.PARAM_NAME_LANGUAGE + '=' + this.encodeURIComponent(language);
        // pozymis, ar langas atidaromas, ar tiesiog perkraunamas
		openingUrl = openingUrl + '&' + this.PARAM_NAME_WINDOW_IS_OPENING + '=' + this.encodeURIComponent(windowIsOpening);
        if ( action == this.URL_OPEN_SERVICE ) {
			// serviceOrderId element
			openingUrl = openingUrl + '&' + this.PARAM_NAME_SERVICE_ORDER_ID + '=' + this.encodeURIComponent(serviceOrderId);
			// serviceName element
			//nededame nes GET'ui per ilgas ()Map viewer viska gauna is uzsakymo)
			//openingUrl = openingUrl + '&' + this.PARAM_NAME_SERVICE_NAME + '=' + this.encodeURIComponent(serviceName);
			//
			// serviceType element
			openingUrl = openingUrl + '&' + this.PARAM_NAME_SERVICE_TYPE + '=' + this.encodeURIComponent(serviceType);
			// serviceUrl element
			openingUrl = openingUrl + '&' + this.PARAM_NAME_SERVICE_URL + '=' + this.encodeURIComponent(serviceUrl);
			// serviceProductId element
			openingUrl = openingUrl + '&' + this.PARAM_NAME_SERVICE_PRODUCT_ID + '=' + this.encodeURIComponent(serviceProductId);
        }
        if ( action==this.URL_OPEN_SAVEDMAP ) {
            // savedMapId element
			openingUrl = openingUrl + '&' + this.PARAM_NAME_SAVED_MAP_ID + '=' + this.encodeURIComponent(savedMapId);
        }
     
        return openingUrl;    
    }
	
	this.encodeURIComponent = function(value){
		return encodeURIComponent(value);
	}
    
    //----------------------------------------
    // ---------- methods (end) --------------
    //----------------------------------------
}
