Phx.namespace('Phx.Application.Modules.WahlzentraleAtlas', new function() {

    var config = {
        mouseover : false
    };
    var map; // the GMap2 itself
    var currentCountry = -2;
    var currentWahlkreis = -2;
    var wahlkreisPolygons = [];
    var bundeslaenderPolygons = [];
    var bundeslandMarkers = [];
    var manager;
    var markerInitialized = false;
    var data;
    var $headline;
    var $statusInfo;
    var headlineDefault;
    var documentTitleDefault;
    var colorDeutschlandBorder;
    var colorDeutschland;
    var opacityBl = 1.0;
    var OPACITY_NORMAL = 0.7;
    var OPACITY_CURRENT_WK = 0.0;
    var lastHash = null;
    var PARTEIEN = {'csu' : 'CSU', 'cdu' : 'CDU', 'spd' : 'SPD', 'piraten' : 'Piratenpartei', 'gruene' : 'Bündnis 90/Die Grünen', 'linke' : 'Die Linke', 'fdp' : 'FDP' };
    var heightOffset = 0;
    var viewMode;
    var staticServer='';
    var datasize = 'large';
    
    function bindSuggest() {
        $('#Wahlzentrale-Atlas-Suggest').unbind('click').bind('click', function (ev) { 
            //if ($('#Wahlzentrale-AtlasPagelet-loggedin').val() === 'false') {
                var url = document.location.href;
                url = url.replace('/viewMode/iframe', '');
                $(this).attr('href', $('#Wahlzentrale-AtlasPagelet-PlatformUrl1').val()+'/Suggest/Selection/?u='+encodeURIComponent(url)+'&desc='+encodeURIComponent(document.title)+'&prov=VZ-Netzwerke');
            //}
            
        });
        
    }
    
    function bindPlzSearch() {
        $('#Wahlzentrale-Atlas-PlzSearchResult .iconclose').unbind('click').bind('click', hidePlzSearchResult);
        
        $('#Wahlzentrale-Atlas-Plz').unbind('keypress').bind('keypress',
                function(ev) {
                    if(ev.keyCode === Phx.Util.KeyCodes.RETURN) {
                        searchPlz();        
                    }
                });
        $('#Wahlzentrale-Atlas-Plz-SearchBtn').unbind('click').bind('click',
                searchPlz);
    }

    function showPlzSearchResult(str) {
        $('#Wahlzentrale-Atlas-PlzSearchResult-Content').html(str);
        $('#Wahlzentrale-Atlas-PlzSearchResult').removeClass('hidden');
        $('#Wahlzentrale-Atlas-DetailView').addClass('hidden');
    }

    function showPlzSearchResultText(str) {
        $('#Wahlzentrale-Atlas-PlzSearchResult-Content').text(str)
        $('#Wahlzentrale-Atlas-PlzSearchResult').removeClass('hidden');
        $('#Wahlzentrale-Atlas-DetailView').addClass('hidden');                
    }

    function hidePlzSearchResult() {
        $('#Wahlzentrale-Atlas-PlzSearchResult-Content').html('');
        $('#Wahlzentrale-Atlas-PlzSearchResult').addClass('hidden');
        $('#Wahlzentrale-Atlas-DetailView').removeClass('hidden');
        $('#Wahlzentrale-Atlas-Plz').val('');
    }

    function searchPlz() {
        var plz = $('#Wahlzentrale-Atlas-Plz').val();
        var plzInfo = data.plzData[plz];
        if (plzInfo === null || plzInfo === undefined || plzInfo.length === 0) {
            showPlzSearchResultText(i18n.getText('atlas_noresults', plz));
        } else if (plzInfo.length === 1) {
            $('#Wahlzentrale-Atlas-PlzSearchResult-Content').html('');
            $('#Wahlzentrale-Atlas-PlzSearchResult').addClass('hidden');
            $('#Wahlzentrale-Atlas-DetailView').removeClass('hidden');
            document.location.hash = '#wahlkreis-'+plzInfo[0];
        } else {
            var i;
            var str = i18n.getText('atlas_results')+':<br/>';
            for (i = 0; i < plzInfo.length; i++) {
                var wk = data.wkData[plzInfo[i]];
                str += '<a href="#wahlkreis-'
                        + plzInfo[i] + '" >';
                str += wk.name;
                str += '</a>';
                if (i < plzInfo.length - 1) {
                    str += '<br/>';
                }
            }
            showPlzSearchResult(str);
        }
    }
    
    function bindHashTool() {
        setInterval('Phx.Application.Modules.WahlzentraleAtlas.hashLookup();', 10);    
    }
    
    function hashLookup() {
        var activeHash = document.location.hash;
        if (activeHash === lastHash) {
            return;
        }
        lastHash = activeHash;
        if(activeHash === '#deutschland') {
            showMainView ();
            showBundeslaender();
            $statusInfo.html('&nbsp;');
        }
        else if(activeHash.indexOf('#bundesland-')===0) {
            if(data.blData[activeHash.substring(12)]) {
                showMainView ();
                showWahlkreise(activeHash.substring(12));
                $statusInfo.html('&nbsp;');
            }
        }
        else if(activeHash.indexOf('#wahlkreis-')===0) {
            if(data.wkData[activeHash.substring(11)]) {
                showMainView ();
                showWahlkreis(activeHash.substring(11));
                $statusInfo.html('&nbsp;');
            }
        }
        else if(activeHash.indexOf('#landesliste-')===0) {
            var els = activeHash.split('-');
            if (PARTEIEN[els[1]]) {
                if(data.blData[els[2]]) {
                    showLandesliste(els[1], els[2]);
                }
            }
        }
    }
    
    function onUnload() {
        GUnload();
    }

    function initWahlkarte() {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("Wahlzentrale-Atlas-Map"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.enableContinuousZoom();
            map.enableScrollWheelZoom();
            map.setCenter(new GLatLng(data.center.lat, data.center.lng),
                    data.initialZoomLevel);

            $(window).bind('unload', onUnload);
            
            var activeHash = document.location.hash;
            lastHash = activeHash;
            if(activeHash === '#deutschland') {
                showBundeslaender();
                return;
            }
            else if(activeHash.indexOf('#bundesland-')===0) {
                if(data.blData[activeHash.substring(12)]) {
                    showBundeslaender(null, false);
                    showWahlkreise(activeHash.substring(12));
                }
                else {
                    showBundeslaender();
                }
                return;
            }
            else if(activeHash.indexOf('#wahlkreis-')===0) {
                if(data.wkData[activeHash.substring(11)]) {
                    showBundeslaender(null, false);
                    showWahlkreis(activeHash.substring(11));
                }
                else {
                    showBundeslaender();
                }
                return;
            }
            else if(activeHash.indexOf('#landesliste-')===0) {
                var els = activeHash.split('-');
                if (PARTEIEN[els[1]]) {
                    if(data.blData[els[2]]) {
                        showBundeslaender(null, false);
                        showWahlkreise(els[2]);
                        showLandesliste(els[1], els[2]);
                        return;
                    }
                }
            }

            
            var plz = $('#Wahlzentrale-AtlasPagelet-plz').val();
            var wahlkreis = $('#Wahlzentrale-AtlasPagelet-wahlkreis').val();
            var bundesland = $('#Wahlzentrale-AtlasPagelet-bundesland').val();
            
            if (plz != '') {
                showBundeslaender();
                $('#Wahlzentrale-Atlas-Plz').val(plz);
                searchPlz();
            } else if (wahlkreis != '0') {
                showBundeslaender(null, false);
                showWahlkreis(wahlkreis);
            } else if (bundesland != '0') {
                showBundeslaender(null, false);
                showWahlkreise(bundesland);
            } else {
                showBundeslaender();
            }

        }
    }

    function refreshMarkers() {
        manager.refresh();

    }

    function getPosition(type, key, coords) {
        if (type === 'bl') {
            if (key == 12) {
                return {
                    lat : coords.lat - 0.3,
                    lng : coords.lng
                };
            }
            if (key == 3) {
                return {
                    lat : coords.lat + 0.2,
                    lng : coords.lng - 0.5
                };
            }
            if (key == 5) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.5
                };
            }
            if (key == 7) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.5
                };
            }
            if (key == 8) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.7
                };
            }
            if (key == 13) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.5
                };
            }
            if (key == 14) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.3
                };
            }
            if (key == 15) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.5
                };
            }
            if (key == 16) {
                return {
                    lat : coords.lat,
                    lng : coords.lng - 0.5
                };
            }
        }

        return coords;
    }

    function getLabelHtml(label) {
        return '<nobr>' + label.split('-').join('-</nobr><br/><nobr>')
                + '</nobr>';
    }

    function showLaenderListe() {
        var str = '';
        var arr = [];
        var item;
        var i;
        for (item in data.blData) {
            arr.push({
                        key : item,
                        name : data.blData[item].name
                    });
        }
        arr.sort(function(a, b) {
                    if (a.name < b.name)
                        return -1;
                    if (a.name > b.name)
                        return 1;
                    return 0;
                });
        for (i = 0; i < arr.length; i++) {
            str += (i === 0) ? '' : '<br/>';
            str += '<a href="#bundesland-'
                    + arr[i].key
                    + '" >'
                    + arr[i].name
                    + '</a>';
        }

        $('#Wahlzentrale-Atlas-Navigation')
                .html('<h2>'+i18n.getText('atlas_navigation')+'</h2><div class="nav-block">' + str
                        + '</div>').removeClass('hidden');
    }
    
    function bindViewMode () {
        $('#Wahlzentrale-Atlas-Landesliste .button').click(function() {
                document.location.hash = 'bundesland-' + currentCountry;
            });
    }
    
    function calculateIframeHeight () {
        return Math.max(200, Math.max($(window).height(), $('#Grid-Page-Left').height())-heightOffset)+'px';
    }
    
    function bindResize () {
        $(window).bind('resize', updateIframeHeight);
    }
    
    function updateIframeHeight () {
        if (!$('#Wahlzentrale-Atlas-Landesliste').hasClass('hidden')) {
            $('#Wahlzentrale-Atlas-Landesliste iframe').css('height', calculateIframeHeight ());
        }
    }
    
    function showLandesliste (partei, bundesland) {
        currentCountry = bundesland;
        $('#Wahlzentrale-Atlas-Landesliste').removeClass('hidden');
        $('#Wahlzentrale-Atlas-Main').addClass('hidden');
        $('#Wahlzentrale-Atlas-Landesliste h2').html(i18n.getText('atlas_list')+' ' + data.blData[bundesland].name + ', ' + PARTEIEN[partei]);
        $('#Wahlzentrale-Atlas-Landesliste iframe').css('height', calculateIframeHeight ()).attr('src', $('#Wahlzentrale-AtlasPagelet-fileStoragePath').val()+'landesliste-'+partei+'-'+bundesland+'.html#'+i18n.getPlatformId()+';'+viewMode);
    }
    
    function checkResize () {
        try {
            if(map) {
                map.checkResize();
            }
        }
        catch(except) {
        }
    }
    
    function showMainView () {
        $('#Wahlzentrale-Atlas-Landesliste').addClass('hidden');
        $('#Wahlzentrale-Atlas-Main').removeClass('hidden');
        checkResize();
    }

    function showBundeslaender(landnr, setCenter) {
        setCenter = (setCenter === null || setCenter === undefined)
                ? true
                : setCenter;

        hidePlzSearchResult();
        $('#Wahlzentrale-Atlas-Bottom .defaultView').removeClass('hidden');
        $('#Wahlzentrale-Atlas-Bottom .wkView').addClass('hidden');
        showLaenderListe();
        $('#Wahlzentrale-Atlas-ListLinks').addClass('hidden');
        $headline.html(headlineDefault).attr('title', headlineDefault);
        document.title = documentTitleDefault;

        if (-1 === currentCountry)
            return;

        manager = new GMarkerManager(map);

        currentCountry = -1;
        currentWahlkreis = -1;
        if (setCenter) {
            map.setCenter(new GLatLng(data.center.lat, data.center.lng),
                    data.initialZoomLevel);
        }

        var i, j, k;

        if (wahlkreisPolygons.length > 0) {
            for (i = 0; i < wahlkreisPolygons.length; i++) {
                map.removeOverlay(wahlkreisPolygons[i]);
            }
        }
        wahlkreisPolygons = [];

        var index = 0;
        if (bundeslaenderPolygons.length === 0) {
            for (var item in data.blData) {
                if (landnr && landnr != item) {
                    continue;
                }

                var coords = getPosition('bl', item, data.blData[item].center);
                var latlng = new GLatLng(coords.lat, coords.lng);
                var icon = new GIcon();
                icon.image = staticServer+'/Img/trans_1x1.png';
                icon.iconSize = new GSize(1, 1);
                icon.iconAnchor = new GPoint(16, 16);
                var opts = {
                    "icon" : icon,
                    "clickable" : false,
                    "draggable" : false,
                    "labelText" : getLabelHtml(data.blData[item].name), 
                    "labelOffset" : new GSize(-16, -16)
                };
                var marker = new LabeledMarker(latlng, opts);
                marker.land_nr = data.blData[item].nr;
                bundeslandMarkers.push(marker);

                // if (index++ >= 0) break;
                for (i = 0; i < data.blData[item].data.length; i++) {
                    if (item == 3 && i == 11 && datasize == 'large') {
                        continue;
                    }
                    else if (item == 3 && i == 9 && datasize == 'small') {
                        continue;
                    }                    
                    
                    var polylines = [{
                                points : data.blData[item].data[i].points,
                                levels : data.blData[item].data[i].levels,
                                color : colorDeutschlandBorder,
                                opacity : 1.0,
                                weight : 1,
                                numLevels : 18,
                                zoomFactor : 2
                            }];
                    if (item == 12 && i == 0 && datasize == "large") {
                        polylines.splice(0, 0, {
                                    points : data.blData[item].data[i + 2].points,
                                    levels : data.blData[item].data[i + 2].levels,
                                    color : colorDeutschlandBorder,
                                    opacity : 1.0,
                                    weight : 1,
                                    numLevels : 18,
                                    zoomFactor : 2
                                });    
                        i+=2;
                    }
                    else if (item == 12 && i == 0 && datasize == "small") {
                        polylines.push({
                                    points : data.blData[item].data[i + 1].points,
                                    levels : data.blData[item].data[i + 1].levels,
                                    color : colorDeutschlandBorder,
                                    opacity : 1.0,
                                    weight : 1,
                                    numLevels : 18,
                                    zoomFactor : 2
                                });    
                        i++;
                    }
                    else if (item == 3 && i == 0 && datasize == 'small') {
                        polylines.push({
                                    points : data.blData[item].data[9].points,
                                    levels : data.blData[item].data[9].levels,
                                    color : colorDeutschlandBorder,
                                    opacity : 1.0,
                                    weight : 1,
                                    numLevels : 18,
                                    zoomFactor : 2
                                });
                    }
                    else if (item == 3 && i == 17 && datasize == 'large') {
                        polylines.push({
                                    points : data.blData[item].data[11].points,
                                    levels : data.blData[item].data[11].levels,
                                    color : colorDeutschlandBorder,
                                    opacity : 1.0,
                                    weight : 1,
                                    numLevels : 18,
                                    zoomFactor : 2
                                });
                    }

                    var polygon = GPolygon.fromEncoded({
                                polylines : polylines,
                                fill : true,
                                color : colorDeutschland,
                                opacity : opacityBl,
                                outline : true
                            });

                    polygon.land_nr = data.blData[item].nr;
                    polygon.index = i;

                    bundeslaenderPolygons.push(polygon);
                    map.addOverlay(polygon);

                    GEvent.addListener(polygon, "click", function(overlay) {
                                document.location.hash = '#bundesland-'+this.land_nr;
                            });

                    GEvent.addListener(polygon, "mouseover", function() {
                                $statusInfo.html('Bundesland '
                                        + data.blData[this.land_nr].name);
                            });

                    GEvent.addListener(polygon, "mouseout", function() {
                                $statusInfo.html('&nbsp;');
                            });
                }
            }
        } else {
            deactivateOpacity();
            for (i = 0; i < bundeslaenderPolygons.length; i++) {
                bundeslaenderPolygons[i].show();
            }

        }

        if (!markerInitialized) {
            manager.addMarkers(bundeslandMarkers, 6, 6);
            manager.refresh();
            markerInitialized = true;
        }
    }

    function activateOpacity() {
        if (opacityBl != OPACITY_NORMAL) {
            toggleBundeslaenderPolygons();
        }
    }

    function deactivateOpacity() {
        if (opacityBl == OPACITY_NORMAL) {
            toggleBundeslaenderPolygons();
        }
    }

    function toggleBundeslaenderPolygons() {
        var i;
        opacityBl = (opacityBl == OPACITY_NORMAL) ? 1.0 : OPACITY_NORMAL;
        var backgroundColor = (opacityBl == OPACITY_NORMAL)? '#999' : colorDeutschland;
        
        for (i = 0; i < bundeslaenderPolygons.length; i++) {
            bundeslaenderPolygons[i].setFillStyle({
                        color : backgroundColor,
                        opacity : opacityBl
                    });
        }

    }

    function showListLinks(nr) {
        $('#Wahlzentrale-Atlas-ListLinks')
                .html('<h2>'+i18n.getText('atlas_lists')+'</h2><div class="nav-block">'
                        + (nr != 9
                                ? '<a href="#landesliste-cdu-'+nr+'">'+PARTEIEN['cdu']+'</a><br/>'
                                : '<a href="#landesliste-csu-'+nr+'">'+PARTEIEN['csu']+'</a><br/>')
                        + '<a href="#landesliste-spd-'+nr+'">'+PARTEIEN['spd']+'</a><br/>'+
                        '<a href="#landesliste-fdp-'+nr+'">'+PARTEIEN['fdp']+'</a><br/>'+
                        '<a href="#landesliste-linke-'+nr+'">'+PARTEIEN['linke']+'</a><br/>'+
                        '<a href="#landesliste-gruene-'+nr+'">'+PARTEIEN['gruene']+'</a><br/>'+
                        '<a href="#landesliste-piraten-'+nr+'">'+PARTEIEN['piraten']+'</a></div>')
                .removeClass('hidden');
    }

    function markWahlkreis(nr) {
        var i;
        $headline
                .html(headlineDefault + ' - '+i18n.getText('atlas_wahlkreis')+' ' + data.wkData[nr].name).attr('title', headlineDefault + ' - '+i18n.getText('atlas_wahlkreis')+' ' + data.wkData[nr].name);
        document.title = documentTitleDefault + ' - '+i18n.getText('atlas_wahlkreis')+' ' + data.wkData[nr].name;
                
        $('#Wahlzentrale-Atlas-Bottom iframe').attr('src', $('#Wahlzentrale-AtlasPagelet-fileStoragePath').val()+'wahlkreis-'+nr+'.html#'+i18n.getPlatformId()+';'+viewMode);                

        $('#Wahlzentrale-Atlas-Bottom .defaultView').addClass('hidden');
        $('#Wahlzentrale-Atlas-Bottom .wkView').removeClass('hidden');
        $('#Wahlzentrale-Atlas-Navigation')
                .html('<h2>'+i18n.getText('atlas_navigation')+'</h2>'
                        + '<div class="nav-block"><a href="#deutschland">'+i18n.getText('atlas_germany')+'</a><br/>'
                        + '<a href="#bundesland-'
                        + currentCountry + '" >'
                        + data.blData[currentCountry].name + '</a>'
                        + '<br/>'+i18n.getText('atlas_wahlkreis')+':<br/>' + data.wkData[nr].name
                        + '<br/>'+i18n.getText('atlas_wahlkreisnr')+': ' + nr + '</div>')
                .removeClass('hidden');

        showListLinks(currentCountry);
        $('#Wahlzentrale-Atlas-Bottom .wkView .headline')
                .html(i18n.getText('atlas_candidates')+' ' + data.wkData[nr].name
                        + ', '+i18n.getText('atlas_wahlkreisnr')+': ' + nr + ' ('
                        + data.blData[currentCountry].name + ')');

        if (nr === currentWahlkreis)
            return;
        
        for (i = 0; i<wahlkreisPolygons.length; i++) {
            if(wahlkreisPolygons[i].wkr_nr == nr) {
                wahlkreisPolygons[i].marked = true;
                wahlkreisPolygons[i].setFillStyle({
                            opacity : OPACITY_CURRENT_WK
                        });                
            }
            else if(wahlkreisPolygons[i].marked == true) {
                wahlkreisPolygons[i].marked = false;
                wahlkreisPolygons[i].setFillStyle({
                            opacity : OPACITY_NORMAL
                        });                
            }
        }

        currentWahlkreis = nr;
    }

    function hideBundesland(nr) {
        var i;
        for (i = 0; i < bundeslaenderPolygons.length; i++) {
            if (bundeslaenderPolygons[i].land_nr === nr) {
                bundeslaenderPolygons[i].hide();
            }
        }
    }

    function showBundesland(nr) {
        var i;
        for (i = 0; i < bundeslaenderPolygons.length; i++) {
            if (bundeslaenderPolygons[i].land_nr === nr) {
                bundeslaenderPolygons[i].show();
            }
        }
    }

    function showWahlkreis(nr, hidePlzSearch) {
        showWahlkreise(data.wkData[nr].land, hidePlzSearch);
        markWahlkreis(nr);
    }

    function showWahlkreise(nr, hidePlzSearch) {
        $headline.html(headlineDefault + ' - ' + data.blData[nr].name).attr('title', headlineDefault + ' - ' + data.blData[nr].name);
        document.title = documentTitleDefault + ' - ' + data.blData[nr].name;

        if (hidePlzSearch || hidePlzSearch === undefined
                || hidePlzSearch === null) {
            hidePlzSearchResult();
        }

        $('#Wahlzentrale-Atlas-Bottom .defaultView').removeClass('hidden');
        $('#Wahlzentrale-Atlas-Bottom .wkView').addClass('hidden');
        $('#Wahlzentrale-Atlas-Navigation')
                .html('<h2>'+i18n.getText('atlas_navigation')+'</h2>'
                        + '<div class="nav-block"><a href="#deutschland" >'+i18n.getText('atlas_germany')+'</a><br/>'
                        + data.blData[nr].name + '</div>')
                .removeClass('hidden');
        showListLinks(nr);

        var i, j;

        currentWahlkreis = -1;
        if (nr === currentCountry) {
            for (i = 0; i<wahlkreisPolygons.length; i++) {
                if(wahlkreisPolygons[i].marked == true) {
                    wahlkreisPolygons[i].marked = false;
                    wahlkreisPolygons[i].setFillStyle({
                                opacity : OPACITY_NORMAL
                            });                
                }
            }            
            return;
        }
        
        activateOpacity();

        showBundesland(currentCountry);
        hideBundesland(nr);
        currentCountry = nr;

        map.setCenter(new GLatLng(data.blData[nr].center.lat,
                        data.blData[nr].center.lng),
                data.blData[nr].initialZoomLevel);

        if (wahlkreisPolygons.length > 0) {
            for (i = 0; i < wahlkreisPolygons.length; i++) {
                map.removeOverlay(wahlkreisPolygons[i]);
            }
        }
        wahlkreisPolygons = [];

        for (j = 0; j < data.blData[nr].wkr.length; j++) {
            var wkr = data.blData[nr].wkr[j];

            for (i = 0; i < data.wkData[wkr].data.length; i++) {
                var polygon = new GPolygon.fromEncoded({
                            polylines : [{
                                        points : data.wkData[wkr].data[i].points,
                                        levels : data.wkData[wkr].data[i].levels,
                                        color : colorDeutschlandBorder,
                                        opacity : 1.0,
                                        weight : 1,
                                        numLevels : 18,
                                        zoomFactor : 2
                                    }],
                            fill : true,
                            color : colorDeutschland,
                            opacity : OPACITY_NORMAL,
                            outline : true
                        });
                polygon.wkr_nr = wkr;
                wahlkreisPolygons.push(polygon);
                map.addOverlay(polygon);

                GEvent.addListener(polygon, "click", function(overlay) {
                            document.location.hash = '#wahlkreis-'+this.wkr_nr;
                        });

                GEvent.addListener(polygon, "mouseover", function() {
                            $statusInfo.html('Wahlkreis '
                                    + data.wkData[this.wkr_nr].name);
                        });

                GEvent.addListener(polygon, "mouseout", function() {
                            $statusInfo.html('&nbsp;');
                        });

            }
        }
    }
    
    function bindSizeLinks () {
        $('#Wahlzentrale-Atlas-ShowBigLink').unbind('click').bind('click',
                function(ev) {
                    Phx.Event.stop(ev);
                    $('#Wahlzentrale-AtlasPagelet').removeClass('small').removeClass('medium').addClass('big');
                    $statusInfo.html('&nbsp;');
                    checkResize();

                });
        $('#Wahlzentrale-Atlas-ShowMediumLink').unbind('click').bind('click',
                function(ev) {
                    Phx.Event.stop(ev);
                    $('#Wahlzentrale-AtlasPagelet').removeClass('small').removeClass('big').addClass('medium');
                    $statusInfo.html('&nbsp;');
                    checkResize();
                });            
        $('#Wahlzentrale-Atlas-ShowSmallLink').unbind('click').bind('click',
                function(ev) {
                    Phx.Event.stop(ev);
                    $('#Wahlzentrale-AtlasPagelet').addClass('small').removeClass('big').removeClass('medium');
                    $statusInfo.html('&nbsp;');
                    checkResize();
                });            

    }
    
    function window_onload () {
        initWahlkarte();

        bindSuggest();
        bindPlzSearch();
        bindHashTool();
        bindSizeLinks();
        bindViewMode();
        bindResize();        
    }

    this.hashLookup = hashLookup;

    this.init = function() {
        datasize = $('#Wahlzentrale-AtlasPagelet-datasize').val();
        heightOffset = parseInt($('#Wahlzentrale-AtlasPagelet-HeightOffset').val());
        colorDeutschland = $('#Atlas-Deutschland-Polygon').css('background-color');
        colorDeutschlandBorder = $('#Atlas-Deutschland-Border').css('color');
        viewMode = $('#Wahlzentrale-AtlasPagelet-viewMode').val();
        staticServer = $('#Wahlzentrale-AtlasPagelet-staticServer').val();

        $statusInfo = $('#Wahlzentrale-AtlasPagelet .statusInfo');
        $headline = $('h1');
        headlineDefault = $headline.html();
        documentTitleDefault = document.title;
        data = WahlzentraleAtlasData;

    }
    
    $(window).bind('load', window_onload);

}());