﻿/// <reference path="jQuery/jquery-1.3.2-vsdoc2.js" />

var map; // Kartan
var gXmlPath;
var gPrintPath;
var gAboutPath;
var searchValueToPrint; // Sökfunktionsvariabel till printsidan
var gConfigPath;
var gDynamicImagePath;
var gPrintServiceUrl;
var bbox = null;

function masterInitJsonInHref(sImagePath, sHrefXmlContainer, sPrintPath, sAboutPath, sDynamicImagePath, sPrintServiceUrl) {
    OpenLayers.ImgPath = sImagePath;
    var elmXml = document.getElementById(sHrefXmlContainer);
    gXmlPath = elmXml.href;
    gPrintPath = sPrintPath;
    gAboutPath = sAboutPath;
    gDynamicImagePath = sDynamicImagePath;
    gPrintPath = sPrintServiceUrl;
    gPrintServiceUrl = sPrintServiceUrl;

    jQuery(document).ready(function() {
        Vtab.Map.init();
        Vtab.Application.InitTabs();
        Vtab.Application.InitDialog();
        Vtab.Application.Events();
        Vtab.Map.setMapOptions();
        Vtab.Map.setMapControls();
        Vtab.Application.fillMunicipalityDdl();
        return null;
    });
}

function onClose() {
        OpenLayers.Event.stopObservingElement(window);
}

Vtab.Application.About = function() {
    window.open(gAboutPath, null, "left= 200, top=200, width=400, height=600, scrollbars=yes,status=no,toolbar=no, resizable=yes");
}


Vtab.Application.fillMunicipalityDdl = function() {
    var dropDownListElm = document.getElementById("municipalityddl");
    dropDownListElm.options[0] = new Option("Hela länet", "default");
    KommunObj = Vtab.Application.municipalityObj;
    for (var i = 0; i < KommunObj.Municipalities.length; i++) {
        var newOption = new Option(KommunObj.Municipalities[i].kommun, KommunObj.Municipalities[i].koord);
        dropDownListElm.options[i + 1] = newOption;
    }
}
//Function envoked on select change, filters municipality WMS and zooms to chose.
Vtab.Application.filterMunicipalities = function() {
    if (jQuery('#municipalityddl').val() != "default") {
        var chosenMunicipality = jQuery('#municipalityddl :selected').text()
        var chosenMunicipalityKoords = jQuery('#municipalityddl').val();
        var koordsXY = chosenMunicipalityKoords.split(',');

        var wmsLayer = map.getLayersByName("Kommuner")[0];
        var maskLayer = map.getLayersByName("Sverige")[0];
        var filter = "KOMMUNNAMN NOT LIKE '" + chosenMunicipality + "'";
        wmsLayer.mergeNewParams({ cql_filter: filter });
        map.setCenter(new OpenLayers.LonLat(koordsXY[0], koordsXY[1]), 5);
        wmsLayer.setVisibility(true);
        maskLayer.setVisibility(true);
    }
    else {
        var wmsLayer = map.getLayersByName("Kommuner")[0];
        var maskLayer = map.getLayersByName("Sverige")[0];
        wmsLayer.setVisibility(false);
        maskLayer.setVisibility(false);
        Vtab.Map.setMapOptions();
    }
}

Vtab.Application.InitTabs = function() {
    jQuery("#tabs").tabs();
}

function removeMask() {
    var wmsLayers = map.getLayersByName("Kommuner");
    wmsLayers[0].setVisibility(false);
    var wmsLayer = map.getLayersByName("Sverige");
    wmsLayer[0].setVisibility(false);

}
//function addMask() {
//    var wmsLayers = map.getLayersByName("Kommuner");
//    wmsLayers[0].setVisibility(true);
//    var wmsLayer = map.getLayersByName("Sverige");
//    wmsLayer[0].setVisibility(true);

//}

Vtab.Application.InitDialog = function() {

    jQuery.ui.dialog.defaults.bgiframe = true;

    jQuery("#saveDialog").dialog({
        title: "Spara kartbild",
        maximize: false,
        autoOpen: false,
        height: 300,
        width: 260,
        position: 'middle',
        modal: false
    });
}