var Waqss2_zoomableMap;

Waqss2_zoomableMap = function(waqssDate, paramId) {

    this.currentWaqssDate = waqssDate;

    this.init = function(waqssDate, paramId) {

        var page = this;

        page.w2params = new Waqss2_params(paramId);
        page.w2params.onChange = function() {
            page.calendar.refresh();
            page.refresh();
        };
        var w2calendar = new Waqss2_calendar(page);
        page.calendar = w2calendar.initCalendar(waqssDate);
        page.w2map = new Waqss2_olMap();
        page.w2map.initMap();
        page.w2legend = new Waqss2_legend();
        page.refresh();
    };

    this.refresh = function() {
        var waqssDate = this.currentWaqssDate;
        var paramId = this.w2params.getCurrentParam();
        this.w2map.setMapImages(waqssDate, paramId);
        this.w2legend.update(waqssDate, paramId);
        this.calendar.setDate(convertWaqssToJsDate(this.getCurrentWaqssDate()));
    };

    this.dateChanged = function(calendar) {
        // Beware that this function is called even if the end-user only
        // changed the month/year.  In order to determine if a date was
        // clicked you can use the dateClicked property of the calendar:

        //	  if (calendar.dateClicked) {
        // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
        this.setCurrentWaqssDate(createWaqssDate(calendar.date));
        this.refresh();
    };

    this.zoomToResult = function() {
        this.w2map.zoomToResult();
    };

    this.getCurrentParam = function() {
        return this.w2params.getCurrentParam();
    };

    this.setCurrentWaqssDate = function(waqssDate) {
        this.currentWaqssDate = waqssDate;
        this.refresh();
    };

    this.getCurrentWaqssDate = function() {
        return this.currentWaqssDate;
    };

    this.init(waqssDate, paramId);

};

var waqss2_zoomableMap;

function init() {

    // 1. Header
    new Waqss2Header();

    // Default-Datum
    var waqssDate = allDataEndDay;
    // Default-Parameter
    var paramId = "chl";

    // Aufruf-Parameter abfragen
    var reqParams = getRequestParameter();

    for (var i = 0; i < reqParams.length; i++) {
        var paramName = reqParams[i][0];
        var value = reqParams[i][1];
        if ("param" == paramName) {
            paramId = value;
        } else if ("date" == paramName) {
            waqssDate = value;
        }
    }

    waqss2_zoomableMap = new Waqss2_zoomableMap(waqssDate, paramId);

}

function openAnimationWindow(a, scene) {
    a.href = "waqss2_animation.html";
    a.href += "?date=" + waqss2_zoomableMap.getCurrentWaqssDate();
    a.href += "&param=" + waqss2_zoomableMap.getCurrentParam();
    a.href += "&scene=" + scene;
    return false;
}

function openMonthlyOverview(a) {
    a.href = "waqss2_overview.html";
    a.href += "?date=" + waqss2_zoomableMap.getCurrentWaqssDate();
    a.href += "&param=" + waqss2_zoomableMap.getCurrentParam();
    return false;
}

function openReport(a) {
    a.href = "waqss2_report.html";
    a.href += "?date=" + waqss2_zoomableMap.getCurrentWaqssDate();
    a.href += "&param=" + waqss2_zoomableMap.getCurrentParam();
    var extend = waqss2_zoomableMap.w2map.map.getExtent();
    var bounds = extend.toArray();
    a.href += "&bounds=";
    a.href += bounds[0] + "_";
    a.href += bounds[1] + "_";
    a.href += bounds[2] + "_";
    a.href += bounds[3];
    return false;
}

