var inaustria_map;




/// Accomodations other than type = 6 (apartaments) are temporarilly disabled
/// HACK #1 and HACK #2









function InaustriaMap( map_wrapper, map_element, map_button, google_map_params, map_button_max )
{
    var _this= this;

    this.init_done= false;

    this.filter= new Array();
    this.filter[ 'accomodation' ]= true;
    this.filter[ 'slope' ]= true;
    this.filter[ 'location' ]= true;
    


    this.accomodation_filter= new Array();
    this.location_filter= new Array();
        
    this.control_positions_fixed= false;
    
    
    
    this.controller_button_max= map_button_max;
    
    this.max_mode= false;
    
    this.icons= new Object();
    this.icons[ 'location' ]= '/main/img/city.png';
    this.icons[ 'slope' ]= '/main/img/ski.png';
    this.icons[ 'accomodation' ]= '/main/img/bed.png';

    this.on_icons= new Object();
    this.on_icons[ 'location' ]= '/main/img/city_on.png';
    this.on_icons[ 'slope' ]= '/main/img/ski_on.png';
    this.on_icons[ 'accomodation' ]= '/main/img/bed_on.png';


    this.added_points= new Object();


    this.last_req= '';
    

    this.land_data= new Object();
    
    this.land_data[ 'Vorarlberg' ]= [ 47.240519, 9.901428, 8 ];
    this.land_data[ 'Tirol' ]= [ 47.23536, 11.47522, 8 ];
    this.land_data[ 'Salzburg' ]= [ 47.525486, 13.07373, 8 ];
    this.land_data[ 'Karnten' ]= [ 46.782132, 13.683472, 8 ];
    this.land_data[ 'Steiermark' ]= [ 47.190584, 14.985352, 8 ];
    this.land_data[ 'Oberosterreich' ]= [ 48.115622, 13.914185, 8 ];
    this.land_data[ 'Niederosterreich' ]= [ 48.291356, 15.787354, 8 ];
    this.land_data[ 'Wien' ]= [ 48.206798, 16.377869, 10 ];
    this.land_data[ 'Burgenland' ]= [ 47.603324, 16.490479, 8 ];

        
    this.location_webservice= '/webserv/getMapPoints.php';
    
    this.step_count= 20;
    this.step_delay= 20;
    this.loop_handler= 0;

    this.map_box= map_wrapper;
    this.map_layer= map_element;
    this.step= 0;

    this.active= false;

    this.map_visible= false;

    this.last_map_zoom= 0;

    /*this.allowedBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng( 49.034027, 9.448242 ),
        new google.maps.LatLng( 46.47923, 17.270508 )
        );*/

    this.map_type_limit= 9;




    // Just a helper function - used to generate a unique identifier for specified object type
    this.getShortTypeIdForSubtype= function( ptype, subtype )
    {
        if( ptype == 'accomodation' )
        {
            switch( subtype )
            {
                case '3_star_hotel': return 'a0';
                case '4_star_hotel': return 'a1';
                case '5_star_hotel': return 'a2';
                case 'SPA_hotel': return 'a3';
                case 'guesthouse': return 'a4';
                case 'hostel': return 'a5';
                case 'private_lodgings': return 'a6';
                case 'agritouristic_house': return 'a7';
                default: return false;
            }
        }
        else if( ptype == 'location' )
        {
            
            if( typeof subtype == "string" )
                return 'l'+subtype;
            else
                return false;
        }
        else
            return false;
    }



    // Removes points from the time when our map was shown in bigger scale
    this.clearUngroupedPoints= function()
    {
        for( var key in _this.added_points )
        {
            _this.added_points[ key ].setMap( null );
            delete _this.added_points[ key ];
        }
    }


    // Removes points from the time when our map was shown in smaller scale
    this.clearGroupedPoints= function()
    {
        for( var key in _this.added_points )
        {
            _this.added_points[ key ].setMap( null );
            delete _this.added_points[ key ];
        }
    }





    this.prepareSubtypeFilterExpression= function()
    {

        var filter_panel= document.getElementById( 'gmapObjectFilter' );

        var filter_list= portableGetElementsByClassName( filter_panel, 'accomodationTypeFilterBox' );
        accomodation_filter= new Array();

        for( var itr= 0; itr < filter_list.length; itr++  )
            if( filter_list[ itr ].checked )
                accomodation_filter.push( 'a'+filter_list[ itr ].id.substr( 19 ) );


        var filter_list2= portableGetElementsByClassName( filter_panel, 'locationTypeFilterBox' );
        location_filter= new Array();
        for( var itr= 0; itr < filter_list2.length; itr++  )
            if( filter_list2[ itr ].checked )
                location_filter.push( 'l'+filter_list2[ itr ].id.substr( 15 ) );

        var type_filter= '(_';

        if( accomodation_filter.length > 0 )
            type_filter+= '|'+accomodation_filter.join( '|' );

        if( location_filter.length > 0 )
            type_filter+= '|'+location_filter.join( '|' );

        type_filter+= ')';

        var filter_ex= new RegExp( type_filter );

        return filter_ex;
    }






    
    this.checkMapBounds= function()
    {
//         if( _this.allowedBounds.contains(_this.google_map.getCenter()) )
//         {
//             return;
//         }
//         // It`s not OK, so find the nearest allowed point and move there
//         var C = _this.google_map.getCenter();
//         var X = C.lng();
//         var Y = C.lat();
// 
//         var AmaxX = allowedBounds.getNorthEast().lng();
//         var AmaxY = allowedBounds.getNorthEast().lat();
//         var AminX = allowedBounds.getSouthWest().lng();
//         var AminY = allowedBounds.getSouthWest().lat();
// 
//         if( X < AminX ){ X= AminX; }
//         if( X > AmaxX ){ X= AmaxX; }
//         if( Y < AminY ){ Y= AminY; }
//         if( Y > AmaxY ){ Y= AmaxY; }
// 
//         _this.google_map.setCenter( new google.maps.LatLng(Y,X) );
    }



    
    this.handleZoomChange= function()
    {
        if( ( _this.last_map_zoom < _this.map_type_limit ) &&
            ( _this.google_map.getZoom() >= _this.map_type_limit ) )
            _this.clearGroupedPoints();
            

        
        if( ( _this.last_map_zoom >= _this.map_type_limit ) &&
            ( _this.google_map.getZoom() < _this.map_type_limit ) )
            _this.clearUngroupedPoints();
    }








    
    this.hide= function()
    {

        this.loop_handler= window.setInterval( function(){ this.hideLoopIteration(); }, step_delay );

    }

    this.show= function()
    {
        this.map_visible= true;
        _this.map_box.style.width= "0px";
        _this.map_box.style.height= "0px";
        _this.map_box.style.display= "block";

/*        this.fixMapControlsPositions( map_layer );*/
    
        
        this.loop_handler= window.setInterval( function(){ this.showLoopIteration(); }, step_delay );
    }




    this.showLoopIteration= function()
    {
        this.map_visible= false;
        
        _this.map_box.style.width= _this.box_widths[ _this.step ];
        _this.map_box.style.height= _this.box_heights[ _this.step ];

        if( _this.step == _this.step_count )
        {
            window.clearInterval( _this.loop_handler );
            _this.loop_handler= 0;
            _this.active= false;
            _this.updateVisibleObjects();
/*            this.fixMapControlsPositions( map_layer );*/
            return;
        }

        ++_this.step;
    }


    this.hideLoopIteration= function()
    {
        _this.map_box.style.width= _this.box_widths[ _this.step ];
        _this.map_box.style.height= _this.box_heights[ _this.step ];

        if( _this.step == 0 )
        {
            window.clearInterval( _this.loop_handler );
            _this.loop_handler= 0;
            _this.active= false;
            return;
        }

        --_this.step;
    }



    this.switchView= function()
    {

        if( _this.active )
            return;
        
        _this.active= true;


        
        
        if( _this.step == 0 )
        {
            setCookieValue( 'googleMapHelper_open', 1 );
            _this.show();

        }
        else if( _this.step == _this.step_count )
        {
            setCookieValue( 'googleMapHelper_open', 0 );
            _this.hide();
        }
    }


    this.showLand= function( land_name )
    {
        if( this.step == 0 )
            this.switchView();
        
        var pos= this.land_data[ land_name ];

        this.center( pos[ 0 ], pos[ 1 ], pos[ 2 ] );
        
    }


    this.center= function( latitude, longitude, zoom )
    {
        var coords= new google.maps.LatLng( latitude, longitude, true );

        if( zoom )
            this.google_map.setZoom( zoom );

        this.google_map.panTo( coords );

    }




    this.showTooltip= function( event, element_type, id, position )
    {
        //console.log( 'Showing %s, %d', element_type, id );
    }
//         infowindow = new google.maps.InfoWindow({
//             'content': '<b>testsetsetsET</b>',
//             'position': position
//         }
//         );
//         infowindow.setMap( this.google_map );
        

//         var overlay = new BoundsBox(this.google_map, box,{
//             'fit':true,
//             'cssClass': 'color',
//             'html': 'html'
//         });
// 
// 
// 


//         var win= document.createElement( 'div' );
//         win.innerHTML= 'Example tooltip with some stuff';
//         win.style.backgroundColor= "black";
//         win.style.width= "100px";
//         win.style.height= "100px";
//         Add control to the map. Position is irrelevant.
//         this.google_map.controls[google.maps.ControlPosition.TOP].push( win );
// 
//         style.left = point.x + this.ANCHOR_OFFSET_.x + 'px';
//         this.node_.style.top = point.y + this.ANCHOR_OFFSET_.y + 'px';

        
        // Bind this OverlayView to the map so we can access MapCanvasProjection
        // to convert LatLng to Point coordinates.
//         this.setMap(map);

        // Register an MVC property to indicate whether this custom control
        // is visible or hidden. Initially hide control until mouse is over map.
//         this.set('visible', false);

//     }
// 
// 
// 
// 
// 
//     this.hideTooltip= function( event, element_type, id )
//     {
//         console.log( 'Hiding %s, %d', element_type, id );
//         
//         
//         
//     }






    this.addMapObjectMarker= function( point )
    {
/*        console.log( point );*/
        if( this.added_points[ point.type+point.id ] )
            return;

        if( !this.filter[ point.type ] )
            return;


       // console.log( point.subtype );
        ///
        ///////////// <--------------- here 
        var subtype= this.getShortTypeIdForSubtype( point.type, point.subtype );
        // use getShortTypeIdForSubtype
        // to add subtype to the point, somehow...
        // and later add clearing incorrect subtypes...
        // ehhhhhh
       // console.log( "Type: %s, subtype: %s", point.type, subtype );


        var ex= this.prepareSubtypeFilterExpression();
        
        if( subtype && ( !subtype.match( ex ) ) )
            return;
        
        
        this.added_points[ point.type+point.id ]= true;

        
        var lat_long= new google.maps.LatLng( point.lat, point.lng );

        
        
        var tmp_point= new google.maps.Marker({
            position: lat_long,
            map: this.google_map,
            draggable: false,
            icon: this.icons[ point.type ],
            title: point.name 
            });
        
        tmp_point.subtype= subtype;

        this.added_points[ point.type+point.id ]= tmp_point;


/*
        google.maps.event.addListener(
            tmp_point,
            'mouseover',
            function( event ) { _this.showTooltip( event, point.type, point.id, lat_long  ) }
            );
            
        google.maps.event.addListener(
            tmp_point,
            'mouseover',
            function( event ) { _this.hideTooltip( event, point.type, point.id  ) }
            );
        */
        if( point.id < 0 )
        {
            _this.google_ma
            google.maps.event.addListener(
                    tmp_point,
                    'click',
                    function( event ) {
                        _this.google_map.setZoom( 11 );

                        _this.google_map.panTo( lat_long );
                        _this.clearNegativeIDS();
                        }
                    );
        }
        else
        {
            if( point.type == "accomodation" )
            {
                var lnk= urlGenerator.accomodationLink( point.id );
                google.maps.event.addListener(
                    tmp_point,
                    'click',
                    function( event ) { self.location= lnk }
                    );

            }
            if( point.type == "slope" )
            {
                var lnk= urlGenerator.slopeLink( point.id );
                google.maps.event.addListener(
                    tmp_point,
                    'click',
                    function( event ) { self.location= lnk }
                    );

            }
            if( point.type == "location" )
            {

                var lnk= urlGenerator.locationLink( point.id );
               // console.log( lnk );
                google.maps.event.addListener(
                    tmp_point,
                    'click',
                    function( event ) { self.location= lnk }
                    );
            }
        }
        
    }



    this.clearNegativeIDS= function()
    {
        for( var key in this.added_points )
        {
            if( key.match( /[\w]+\-[\d]+/ ) )
            {
                this.added_points[ key ].setMap( null );
                delete this.added_points[ key ];
            }
        }
    }




    this.addMapObjectPolygon= function( polygon )
    {
        if( this.added_points[ polygon.type+polygon.id ] )
            return;


        if( !this.filter[ polygon.type ] )
            return;

        
        this.added_points[ polygon.type+polygon.id ]= true;

        
        var tmp_points= polygon.points.substr( 2, polygon.points.length-4 ).split( '\)\,\(' );

/*        console.log( tmp_points );*/
        
        var coords= new Array();
        for( var itr= 0; itr < tmp_points.length; itr++ )
        {
            var tmp= tmp_points[ itr ].split( ',' );
            var tmp_point= new google.maps.LatLng( tmp[ 0 ], tmp[ 1 ] );
            coords.push( tmp_point );
        }


/*        console.log( coords );*/
        
        var new_poly= new google.maps.Polygon({
            paths: coords,
            strokeColor: "#000000",
            strokeOpacity: 0.8,
            strokeWeight: 1,
            fillColor: "#FFB133",
            fillOpacity: 0.30
            });

            
        this.added_points[ polygon.type+polygon.id ]= new_poly;


//         GEvent.addListener(
//             polygon,
//             'mouseover',
//             function(){ console.log( "sdfsdfsdfsdf" ) }
//             );
// 
//         GEvent.addListener(
//             polygon,
//             'click',
//             function(){ console.log( "sdfsdfsdfsdf" ) }
//             );
/*            
        google.maps.event.addListener(
            polygon,
            'mouseover',
            function( event ) { this.highlightPolygon( event ) }
            );*/

            
        new_poly.setMap( this.google_map );
    }



    

    this.displayFetchedObjects= function( response )
    {
       // console.log( 'Running displayFetchedObjects()' );
        
        var res= JSON.parse( response.responseText );

       // console.log( res.points );
        for( var itr= 0; itr < res.points.length; itr++ )
        {
            this.addMapObjectMarker( res.points[ itr ] );
        }

        for( var itr= 0; itr < res.polygons.length; itr++ )
        {
            this.addMapObjectPolygon( res.polygons[ itr ] );
        }
    }


    this.updateVisibleObjects= function()
    {
        if( _this.step == 0 )
            return;

//         alert( 'updateVisibleObjects' );

/*        console.log( 'running updateVisibleObjects' );*/
        
        var bounds= this.google_map.getBounds();

    
        if( bounds )
        {
/*            this.fixMapControlsPositions( this.map_layer );*/
            var ne= bounds.getNorthEast();
            var sw= bounds.getSouthWest();

//             console.log( ne );
//             console.log( ne.lng() );
//
            var reqid= ""+_this.google_map.getZoom()+" "+ne.lng()+" "+sw.lat()+" "+sw.lng()+" "+ne.lat();

            if( reqid == _this.last_req )
                return;

            _this.last_req= reqid;
            
            advAJAX.get({
                url: _this.location_webservice,
                parameters : {
                    "zoom" : _this.google_map.getZoom(),
                    "n" : ne.lng(),
                    "w" : sw.lat(),
                    "s" : sw.lng(),
                    "e" : ne.lat(),
                    "af" : _this.filter[ 'accomodation' ],
                    "sf" : _this.filter[ 'slope' ],
                    "lf" : _this.filter[ 'location' ],
                    "afi" : _this.accomodation_filter.join( '.' ),
                    "lfi" : _this.location_filter.join( '.' )
                    },
                onSuccess: function( response ){ _this.displayFetchedObjects( response ) }
                });
        }

        
        
    }



    this.handleCityLinkClick= function( event )
    {
        var elem= getEventTarget( event );
/*        console.log( elem.rel );*/


        
        var pos= elem.rel.split( ',' );

        if( pos[ 0 ] > 20 )
            this.center( pos[ 0 ], pos[ 1 ], false );
    }




    this.fixMapControlsPositions= function( node )
    {
//         if( this.control_positions_fixed )
//             return;
//         
        var fixes= 0;
        
        for( var itr= 0; itr < node.childNodes.length; itr++ )
        {
            if( node.childNodes[ itr ].getAttribute( 'style' ) == "padding: 5px; position: absolute; top: 0px; left: 0px;" )
            {
                node.childNodes[ itr ].style.padding= "0px";
                node.childNodes[ itr ].style.top= "-7px";
                node.childNodes[ itr ].style.left= "-2px";
                node.childNodes[ itr ].firstChild.style.top= "9px";
                fixes++;
            }
            else if( node.childNodes[ itr ].getAttribute( 'style' ) == "padding: 2px 5px 2px 2px; position: absolute; bottom: 0px; left: 0px;" )
            {
                node.childNodes[ itr ].style.left= "48px";
                fixes++;
            }
        }
        if( fixes == 2 )
            this.control_positions_fixed= true;
        
    }
    

    this.addCityControlLayer= function()
    {
        var city_layer= document.getElementById( 'googleMapCitySelectorLayer' );
        city_layer.style.display= "block";
        addPortableEventListener( city_layer, 'DOMMouseScroll', eventCanceller );
        addPortableEventListener( city_layer, 'mousewheel', eventCanceller );


        var city_list= document.createElement( 'div' );

        city_list.id= 'googleMapCityList';
        city_layer.appendChild( city_list );


//         var cities= new Array()
//         for( var itr= 0; itr < city_data.length; itr++ )
//         {
//             cities[ cities.length ]= city_data[ itr ].name;
// //             console.log( city_data[ itr ] );
//
//         }


/*        var cities= [ 'Test', 'City 2', 'Abcdsf', 'Nextcity', 'Blah', 'Test', 'Test abc', 'City 324', 'Kraków', 'Tokio', 'Los Angeles', 'Paris', 'Wien', 'Biała Podlaska', 'Kartagina', 'Pekin', 'Bujumbura' ];*/


        for( var itr= 0; itr < city_data.length; itr++ )
        {
            var tmp_lay= document.createElement( 'a' );
            tmp_lay.innerHTML= city_data[ itr ].name;
            tmp_lay.rel= city_data[ itr ].x + ',' + city_data[ itr ].y;
            addPortableEventListener( tmp_lay, 'click', function( event ){ _this.handleCityLinkClick( event ); } );
            city_list.appendChild( tmp_lay );
        }



        $('#googleMapCityList').jScrollPane();

        this.left_tab_content= city_list.parentNode;
    }




    this.switchAccomodationFilter= function()
    {
        var filter= document.getElementById( 'accomodationFilterCheckbox' ).checked;
        var layer= document.getElementById( 'gmapObjectFilter' );
        
        var children= portableGetElementsByClassName( layer, 'accomodationTypeFilterBox' );

           
        if( filter )
            for( var itr= 0; itr < children.length; itr++ )
                {
                // HACK #2 BEGINS

                    if( children[ itr ].id == 'accomodationFilter_6' )
                        children[ itr ].parentNode.style.display= 'block';
                    else
                        children[ itr ].parentNode.style.display= 'none';
                // HACK #2 ENDS

                }
        else
            for( var itr= 0; itr < children.length; itr++ )
                children[ itr ].parentNode.style.display= 'none';

        if( this.init_done )
            $('#gmapObjectFilter').jScrollPane();
    }




    this.switchLocationFilter= function()
    {
        var filter= document.getElementById( 'locationFilterCheckbox' ).checked;
        var layer= document.getElementById( 'gmapObjectFilter' );

        var children= portableGetElementsByClassName( layer, 'locationTypeFilterBox' );


        if( filter )
            for( var itr= 0; itr < children.length; itr++ )
                children[ itr ].parentNode.style.display= 'block';
        else
            for( var itr= 0; itr < children.length; itr++ )
                children[ itr ].parentNode.style.display= 'none';

        if( this.init_done )
            $('#gmapObjectFilter').jScrollPane();
    }



    

    this.updateFilter= function( event )
    {
        this.filter[ 'accomodation' ]= document.getElementById( 'accomodationFilterCheckbox' ).checked;
        this.filter[ 'slope' ]= document.getElementById( 'skiFilterCheckbox' ).checked;
        this.filter[ 'location' ]= document.getElementById( 'locationFilterCheckbox' ).checked;

/*        alert( 'updateFilter' );*/
//         console.log( 'test - %o %o', accomodation_filter, location_filter );
// 





        var filter_ex= this.prepareSubtypeFilterExpression();
        
/*        console.log( type_filter );*/
/*        
        console.log( this.filter[ 'accomodation' ] );
        console.log( this.filter[ 'slope' ] );
        console.log( this.filter[ 'location' ] );*/

        var ex= '(wrong';

        if( !this.filter[ 'accomodation' ] )
            ex+= '|accomodation';

        if( !this.filter[ 'slope' ] )
            ex+= '|slope';

        if( !this.filter[ 'location' ] )
            ex+= '|location';

        ex+= ')\-?[\\d]+';

       // console.log( ex );

        var regex= new RegExp( ex );
        
        for( var key in this.added_points )
        {
           // console.log( 'Filtering' );
           // console.log( this.added_points[ key ] );
           // console.log( key );
            if( key.match( regex ) )
            {
               // console.log( 'Removing' );
                this.added_points[ key ].setMap( null );
                delete this.added_points[ key ];
            }
            else
            {
                if( ( this.added_points[ key ].subtype ) && ( !this.added_points[ key ].subtype.match( filter_ex ) ) )
                {
                   // console.log( 'Removing' );
                    this.added_points[ key ].setMap( null );
                    delete this.added_points[ key ];
                }
//                 else
//                     console.log( 'Not removing' );

            }
/*            this.added_points[ key ]= false;*/
                /*
            console.log( key );
            console.log( this.added_points[ key ] )
            */
        }
        

        
        this.last_req= '';
        this.updateVisibleObjects();
        
    }




    this.handleLeftMapTabClick= function()
    {
   
        if( this.right_map_control_tab.id )
        {
            this.right_map_control_tab.removeAttribute( 'id' );
            this.left_map_control_tab.id= 'activeGoogleMapTab';

            this.right_tab_content.style.display="none";
            this.left_tab_content.style.display="block";
        }

    }





    this.handleRightMapTabClick= function()
    {
        if( this.left_map_control_tab.id )
        {
            this.left_map_control_tab.removeAttribute( 'id' );
            this.right_map_control_tab.id= 'activeGoogleMapTab';

            this.right_tab_content.style.display="block";
            this.left_tab_content.style.display="none";

//             $('#googleMapCityList').jScrollPane();
        }

    }






    this.updateAccomodationFilter= function()
    {
        this.accomodation_filter= new Array();
        
        var layer= document.getElementById( 'accomodationFilterCheckbox' ).parentNode.parentNode;

/*        console.log( "layer %o", layer );*/
        
        var boxes= portableGetElementsByClassName( layer, 'accomodationTypeFilterBox' );

/*        console.log( boxes );*/
        
        for( itr= 0; itr < boxes.length; itr++ )
        {
/*            console.log( boxes[ itr ] );*/
            this.accomodation_filter[ this.accomodation_filter.length ]= parseInt( ( boxes[ itr ].id.split( '_' ) )[ 1 ] );
        }
/*        console.log( this.accomodation_filter );*/
    }





    this.updateLocationFilter= function()
    {
        this.location_filter= new Array();

        var layer= document.getElementById( 'locationFilterCheckbox' ).parentNode.parentNode;

        var boxes= portableGetElementsByClassName( layer, 'locationTypeFilterBox' );


        for( itr= 0; itr < boxes.length; itr++ )
        {
/*            console.log( boxes[ itr ] );*/
            this.location_filter[ this.location_filter.length ]= parseInt( ( boxes[ itr ].id.split( '_' ) )[ 1 ] );
        }
//         console.log( this.location_filter );

    }









    this.addMapObjectTypeControlls= function()
    {
        
        var object_filter= document.createElement( 'div' );
        object_filter.className= 'gmapObjectFilter';
        object_filter.id= 'gmapObjectFilter';
        
        
        
        
        
        var acc_filter= document.createElement( 'div' );
        
        var acc_input= document.createElement( 'input' );
        acc_input.type= "checkbox";
        acc_input.id= "accomodationFilterCheckbox";
        acc_input.checked= this.filter[ 'accomodation' ];
        var acc_box= document.createElement( 'label' );
        acc_box.htmlFor= "accomodationFilterCheckbox";
        acc_box.innerHTML= lang.dict.accomodations;
        acc_filter.appendChild( acc_input );
        acc_filter.appendChild( acc_box );
        
        addPortableEventListener( acc_input, 'change', function(){ _this.switchAccomodationFilter(); _this.updateFilter(); } );
        addPortableEventListener( acc_input, 'click', function(){ _this.switchAccomodationFilter(); _this.updateFilter(); } );

        
        object_filter.appendChild( acc_filter );

/*        console.log( filter_accomodation_types );*/
        
        try
        {
/*            console.log( 'starting here' );*/
            for( itr= 0; itr < filter_accomodation_types.length; itr++ )
            {
/*                console.log( filter_accomodation_types[ itr ] );*/
                
                var item_filter= document.createElement( 'div' );
                

                // HACK #1 BEGINS
                if( filter_accomodation_types[ itr ].key == 6 )
                    item_filter.style.display= 'block';
                else
                    item_filter.style.display= 'none';
                // HACK #1 ENDS


                item_filter.style.marginLeft= '10px';
                var item_input= document.createElement( 'input' );
                item_input.type= "checkbox";
                item_input.id= "accomodationFilter_"+filter_accomodation_types[ itr ].key;
                item_input.checked= this.filter[ 'location' ];
                var item_box= document.createElement( 'label' );
                item_box.type= "checkbox";
                item_box.htmlFor= "accomodationFilter_"+filter_accomodation_types[ itr ].key;
                item_box.innerHTML= filter_accomodation_types[ itr ].value;
/*
                console.log( filter_accomodation_types[ itr ].value );
                
                console.log( lang.dict[ filter_accomodation_types[ itr ].value ] );*/
                item_box.innerHTML= lang.dict[ filter_accomodation_types[ itr ].value ];

                
                item_filter.appendChild( item_input );
                item_filter.appendChild( item_box );
                item_input.className= "accomodationTypeFilterBox";
                addPortableEventListener( item_input, 'change', function(){ _this.updateAccomodationFilter(); _this.updateFilter(); } );
                
                object_filter.appendChild( item_filter );
            }
/*            console.log( 'done' );*/
        }
        catch( e )
        {
            // nothing should be done here, really...
        }







        
        
        
        
        var ski_filter= document.createElement( 'div' );
        
        var ski_input= document.createElement( 'input' );
        ski_input.type= "checkbox";
        ski_input.id= "skiFilterCheckbox";
        ski_input.checked= this.filter[ 'slope' ];
        var ski_box= document.createElement( 'label' );
        ski_box.htmlFor= "skiFilterCheckbox";
        ski_box.innerHTML= lang.dict.skiCenters;
        ski_filter.appendChild( ski_input );
        ski_filter.appendChild( ski_box );
        addPortableEventListener( ski_input, 'change', function(){ _this.updateFilter(); } );
        addPortableEventListener( ski_input, 'click', function(){ _this.updateFilter(); } );

        
        object_filter.appendChild( ski_filter );


        
        var loc_filter= document.createElement( 'div' );
        var loc_input= document.createElement( 'input' );
        loc_input.type= "checkbox";
        loc_input.id= "locationFilterCheckbox";
        loc_input.checked= this.filter[ 'location' ];
        var loc_box= document.createElement( 'label' );
        loc_box.type= "checkbox";
        loc_box.htmlFor= "locationFilterCheckbox";
        loc_box.innerHTML= lang.dict.touristAttractions;
        loc_filter.appendChild( loc_input );
        loc_filter.appendChild( loc_box );
        addPortableEventListener( loc_input, 'change', function(){ _this.switchLocationFilter(); _this.updateFilter(); } );
        addPortableEventListener( loc_input, 'click', function(){ _this.switchLocationFilter(); _this.updateFilter(); } );

        object_filter.appendChild( loc_filter );

        try
        {
            for( itr= 0; itr < filter_location_types.length; itr++ )
            {
                var item_filter= document.createElement( 'div' );
                item_filter.style.marginLeft= '10px';
                var item_input= document.createElement( 'input' );
                item_input.type= "checkbox";
                item_input.id= "locationFilter_"+filter_location_types[ itr ].id;
                item_input.checked= this.filter[ 'location' ];
                var item_box= document.createElement( 'label' );
                item_box.type= "checkbox";
                item_box.htmlFor= "locationFilter_"+filter_location_types[ itr ].id;
                item_box.innerHTML= filter_location_types[ itr ].name;
                item_filter.appendChild( item_input );
                item_filter.appendChild( item_box );
                item_input.className= "locationTypeFilterBox";
                addPortableEventListener( item_input, 'change', function(){ _this.updateLocationFilter(); _this.updateFilter(); } );
                    
                object_filter.appendChild( item_filter );
            }
        }
        catch( e )
        {
            // nothing should be done here, really...
        }

        document.getElementById( 'googleMapCitySelectorLayer' ).appendChild( object_filter );



        $('#gmapObjectFilter').jScrollPane();

        this.right_tab_content= object_filter.parentNode;
        object_filter.parentNode.style.display= "none";
        
        
        
        
    }












    this.switchToMaxMode= function()
    {
        if( _this.step == 0 )
        {
            this.switchView();
            return;
        }
        else
        {
            if( this.max_mode )
            {
                this.map_box.parentNode.insertBefore( this.controller_button_max, this.map_box.parentNode.firstChild );
                this.map_box.parentNode.insertBefore( this.controller_button, this.map_box.parentNode.firstChild );
                this.controller_button_max.style.position= "absolute";
                this.controller_button.style.position= "absolute";



                this.map_box.insertBefore( this.map_layer, this.map_box.firstChild );

                
                var tabber= document.getElementById( 'googleMapCitySelectorLayer' );
                
                this.map_tabber_parent.appendChild( tabber );


                this.map_layer.style.position= "absolute";
                this.map_layer.style.width= "968px";
                this.map_layer.style.height= "236px";
                this.map_layer.style.left= "0px";
                this.map_layer.style.top= "0px";

                google.maps.event.trigger( this.map_layer, 'resize' );
                google.maps.event.trigger( this.google_map, 'resize' );


                

//                 console.log( "Wtf: %o", this.map_tabber_parent );
//                 console.log( "Wtf: %o", tabber );

               // tabber.style.position= 'style';

                tabber.style.left= '969px';

                tabber.style.position= 'absolute';
                tabber.style.top= "40px";
                tabber.style.zIndex= "1000";

                
                this.max_mode= false;

            }
            else
            {
                document.body.insertBefore( this.map_layer,  document.body.firstChild );
                document.body.insertBefore( this.controller_button_max, document.body.firstChild );
                document.body.insertBefore( this.controller_button, document.body.firstChild );
                
                this.controller_button_max.style.position= "fixed";
                this.controller_button.style.position= "fixed";

                // another ugly hack... eh

                
                var tabber= document.getElementById( 'googleMapCitySelectorLayer' );

                this.map_tabber_parent= tabber.parentNode;
                
/*                console.log( tabber );*/
                document.body.insertBefore( tabber,  document.body.firstChild );
                tabber.style.position= "fixed";
                tabber.style.left= "100%";
                tabber.style.marginLeft= "-195px";
                tabber.style.top= "50px";
                tabber.style.zIndex= "1000";
//                 
                
                this.map_layer.style.position= "fixed";
                this.map_layer.style.left= "0px";
                this.map_layer.style.top= "0px";
                
                this.map_layer.style.width= "100%";
                this.map_layer.style.height= "100%";

                this.map_layer.style.zIndex= 9;


//                 var map_tabs= document.getElementById( )




                
                google.maps.event.trigger( this.map_layer, 'resize' );
                google.maps.event.trigger( this.google_map, 'resize' );

                this.max_mode= true;

            }
        }

    }








    this.configureFilter= function()
    {

        try
        {
            for( key in filter_settings )
                document.getElementById( key ).checked= filter_settings[ key ];

            {
            

            }
        }
        catch( exc )
        {
        }
 
        _this.updateFilter();
        _this.switchAccomodationFilter();
        _this.switchLocationFilter();
    }























    
//    box_max_height= parseInt( portableGetComputedStyle( this.map_layer, 'height' ) );
//    box_max_width= parseInt( portableGetComputedStyle( this.map_layer, 'width' ) );



    box_max_height= 236;
    box_max_width= 968;

  
    step_height= box_max_height/this.step_count;
    step_width= box_max_width/this.step_count;

    this.box_heights= new Array();
    this.box_widths= new Array();


    
    for( var itr= 0; itr < step_count; itr++ )
    {
        this.box_heights.push( ""+parseInt( Math.round( itr*step_height ) )+"px" );
        this.box_widths.push( ""+parseInt( Math.round( itr*step_width ) )+"px" );
    }
//     alert( this.box_widths );

    this.box_heights[ step_count ]= ""+box_max_height+"px";
    this.box_widths[ step_count ]= ""+box_max_width+"px";

    this.loop_handler= 0;


    var selected_object_pos= false;

        try
        {
            if( typeof( current_location ) != 'undefined' )
            {
                selected_object_pos= new google.maps.LatLng( current_location.x, current_location.y );
                google_map_params.center= selected_object_pos;
            }
            
        }
        catch( exc )
        {
            // we use try/catch here only 'cause javascript isn't a decent language...
        }



    this.google_map= new google.maps.Map( map_layer, google_map_params );


    if( selected_object_pos )
    {
        
        var tmp_point= new google.maps.Marker({
            position: selected_object_pos,
            map: this.google_map,
            draggable: false,
            icon: this.on_icons[ current_location.type ],
            zIndex: 2000
            });
/*        console.log( tmp_point );*/
    }

    
    this.last_map_zoom= this.google_map.getZoom();
    
//     G_PHYSICAL_MAP.getMinimumResolution = function () { return 1 };
//     G_NORMAL_MAP.getMinimumResolution = function () { return 1 };
//     G_SATELLITE_MAP.getMinimumResolution = function () { return 1 };
//     G_HYBRID_MAP.getMinimumResolution = function () { return 1 };
//     
    this.controller_button= map_button;

//     var austria_bounds= new google.maps.LatLngBounds(
//         new google.maps.LatLng( ),
//         new google.maps.LatLng( )
//         );
//     
//     this.google_map.setBounds();
//     

    google.maps.event.addListener(this.google_map, 'dragend', function()
        {
        _this.updateVisibleObjects();
        });
        
    google.maps.event.addListener(this.google_map, 'resize', function()
        {
        _this.updateVisibleObjects();
        });

    google.maps.event.addListener(this.google_map, 'tilesloaded', function()
        {
        _this.updateVisibleObjects();
        });
        
    google.maps.event.addListener(this.google_map, 'drag', function()
        {
        _this.checkMapBounds();
        });

        
    google.maps.event.addListener(this.google_map, 'zoom_changed', function()
        {
        _this.handleZoomChange();
        });

//     google.maps.event.addListener( this.google_map, 'tilesloaded', function()
//         {
//         _this.fixMapControlsPositions( map_layer );
//         });




    




    
        
    addPortableEventListener( this.controller_button, 'click', function(){ _this.switchView() } );
    addPortableEventListener( this.controller_button_max, 'click', function(){ _this.switchToMaxMode() } );
  

    this.addCityControlLayer();

    
    
    this.controller_button.href= "javascript:void(0)";

    this.controller_button_max.href= "javascript:void(0)";

    
    
    this.addMapObjectTypeControlls();







    // hack...

    var tabber= document.getElementById( 'googleMapCitySelectorLayer' );

    this.left_map_control_tab= portableGetElementsByClassName( tabber, 'googleMapTabLeft' )[ 0 ];
    this.right_map_control_tab= portableGetElementsByClassName( tabber, 'googleMapTabRight' )[ 0 ];



    addPortableEventListener( this.left_map_control_tab, 'click', function(){ _this.handleLeftMapTabClick(); } );
    addPortableEventListener( this.right_map_control_tab, 'click', function(){ _this.handleRightMapTabClick(); } );


    this.updateAccomodationFilter();
    this.updateLocationFilter();

    this.configureFilter();


    
    this.updateVisibleObjects();
    var map_open= getCookieValue( 'googleMapHelper_open', false );
    if( ( map_open ) && ( parseInt( map_open ) === 1 ) )
    {
        this.map_box.style.width= _this.box_widths[ this.step_count ];
        this.map_box.style.height= _this.box_heights[ this.step_count ];

        this.step= this.step_count;

        this.updateVisibleObjects();
    }


    _this.handleRightMapTabClick();

    // document.getElementById( 'googleMapCitySelectorLayer' ).style.display= 'block';
    
    this.init_done= true;
    
    return this;
}















function initMap()
{
    var map_box= document.getElementById( "mapBox" );
    var map_layer= document.getElementById( "mapLayer" );

    var map_button= document.getElementById( "navigateCard" );
    var map_button_max= document.getElementById( "navigateCard_max" );

    
    if( ( map_layer ) && ( map_box ) )
    {
        map_box.style.width= "0px";
        map_box.style.height= "0px";

        var latlng;

        try
        {
            if( typeof( current_location ) != 'undefined' )
                latlng= new google.maps.LatLng( current_location.x, current_location.y );
            else
                latlng = new google.maps.LatLng( 47.74048, 13.5 );
        }
        catch( exc )
        {
            var latlng = new google.maps.LatLng( 47.74048, 13.5 );
        }
    




        
        var map_options=
            {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN,
/*            disableDefaultUI: true,*/
            navigationControl: true,
            mapTypeControlOptions:
                {
                    position: google.maps.ControlPosition.TOP_LEFT

                }
//             navigationControlOptions:
//                 {
//                 style: google.maps.NavigationControlStyle.ZOOM_PAN
//                 }
            };

        inaustria_map= InaustriaMap( map_box, map_layer, map_button, map_options, map_button_max );
        
    }

}



 





runOnLoad( initMap );













