
if (typeof console == 'undefined' && window.loadFirebugConsole) 
{
    window.console = window.loadFirebugConsole();
}


/**
 * Execute the following lines as soon as the DOM is ready
 */
jQuery(document).ready(function()
{

    /**
     * ------------------------------------------------------------------
     * resizing the map
     * ------------------------------------------------------------------
     */
    NABU.optimizeMapsize();
    jQuery(window).bind('resize', NABU.optimizeMapsize);
    jQuery(window).bind('load', initialize_openLayers);

    //jQuery.blockUI( { message: "<img src='./images/spinner.gif' /><br><span class='header'><h2>Anwendung wird geladen</h2></span><br><span class='smalltext'>Bitte haben Sie einen Moment Geduld</span>" } );
    
    jQuery(window).bind('load', function () {
	    
        jQuery('#page_margins').removeAttr('style');	    
	NABU.optimizeMapsize();
        btnpressImg('get_info');
	//jQuery.unblockUI();
    });

    
    /**
     * -------------------------------------------------------------------
     * Tabs for the legend and the layer navigation
     * -------------------------------------------------------------------
     */
    jQuery('#content > ul').tabs();
    
    /**
     * -------------------------------------------------------------------
     * Mapping components external controls
     * -------------------------------------------------------------------
     */
    jQuery('#nav_main #mapping-functionality li img').hover(function()
    {
        jQuery(this).addClass('hovering');
    }, function()
    {
        jQuery(this).removeClass('hovering');
    });
    
    jQuery('#layer-legend-gaz-container').dialog({
        position: [45, 80],
        title: 'Karten',
        width: 260,
        height: 355,
        autoOpen: true
    });
    
    jQuery('#mapping-functionality-container').dialog({
        position: [450, 110],
        title: 'Navigationswerkzeuge',
        width: 220,
        height: 95,
        autoOpen: true
    });
    
    jQuery('#mapping-permalink-container').dialog({
        position: [600, 110],
        title: 'Kartenlink',
        width: 415,
        height: 120,
        autoOpen: false
    });
    
    jQuery('#mapping-print-container').dialog({
        position: [540, 110],
        title: 'Kartendruck',
        width: 240,
        height: 250,
        autoOpen: false
    });

    /**
     * -------------------------------------------------------------------
     * Contact and imprint control
     * -------------------------------------------------------------------
     */
    jQuery('#contact-imprint').dialog({
        position: [620, 110],
        title: 'Kontakt &amp; Impressum',
        width: 542,
        height: 335,
        autoOpen: false
    });
    
    /**
     * -------------------------------------------------------------------
     * start information
     * -------------------------------------------------------------------
     */
    jQuery('#information-start').dialog({
        position: [620, 100],
        title: 'Besucherinformation',
        width: 550,
        height: 360,
        autoOpen: true
    });
    
    /**
     * -------------------------------------------------------------------
     * external windows to insert point/line/polygon information
     * -------------------------------------------------------------------
     */
    
    jQuery('#feature-info-container').dialog({
        position: [350, 200],
        title: 'Information',
        width: 280,
        height: 300,
        autoOpen: false
    });

        /**
     * -------------------------------------------------------------------
     * accordion functions
     * -------------------------------------------------------------------
     */
    jQuery('#control-layer-legend-gaz').bind('click', function()
    {
        jQuery('#layer-legend-gaz-container').dialog('open');
        return false;
    });
    
    jQuery('#control-mapping-functions').bind('click', function()
    {
        jQuery('#mapping-functionality-container').dialog('open');
        return false;
    });
    
    jQuery('#control-contact-imprint').bind('click', function()
    {
        jQuery('#contact-imprint').dialog('open');
        return false;
    });
    
    
    /**
     * -------------------------------------------------------------------
     * special map functions
     * -------------------------------------------------------------------
     */
    jQuery('#tooltip').bind('click', function()
    {
        $this = jQuery(this);
        NABU.MAPCONTROLSTATES.TOOLTIP.active = !NABU.MAPCONTROLSTATES.TOOLTIP.active;
        if (NABU.MAPCONTROLSTATES.TOOLTIP.active) 
        {
            $this.attr('src', './images/famfamfam/additions/tt_on_26x26.png');
        }
        else 
        {
            $this.attr('src', './images/famfamfam/additions/tt_off_26x26.png');
        }
    });
    
    
    
    /**
     * -------------------------------------------------------------------
     * Mapping function
     * -------------------------------------------------------------------
     */
    jQuery('#mapping-functionality img').not('#toggle_digitizing').bind('click', function()
    {
        var alertMsg = this.alt || this.title;
        
        switch (this.id)
        {
            case 'zoom_out':
                NABU.MAP.zoomOut();
                break;
            case 'zoom_in':
                NABU.MAP.zoomIn();
                break;
            case 'zoom_full':
                NABU.MAP.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
                break;
            case 'zoom_rec':
                NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.ZOOMBOX);
                NABU.setCurrentMouseAction();
                btnpressImg('zoom_rec');
                break;
                
            case 'center':
                NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.SETCENTER);   
                NABU.setCurrentMouseAction();
                break;
                
            case 'pan':
                NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.DEFAULT);
                NABU.setCurrentMouseAction();
                btnpressImg('pan');
                break;
                
            case 'copy_link':
                //NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.PERMALINK);  
                //NABU.setCurrentMouseAction(); 
                NABU.showPermaLink();
                break;
            case 'get_info':
                NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.GETFI);
                NABU.setCurrentMouseAction();
                btnpressImg('get_info');
                break;
  	  case 'measure':	    
		NABU.toggleStatus(NABU.MAPCONTROLSTATES, NABU.MAPCONTROLSTATES.MEASURE);			
		NABU.setCurrentMouseAction();
		btnpressImg('measure');	
		break;
			
            case 'print':
                jQuery('#mapping-print-container').dialog('open');
                NABU.showPrintDialog();
                break;
                
            default:
                // alert( alertMsg );
                info(alertMsg);
                break;
        }
    });
    
    /**
     * sortable layers
     */
    jQuery('#zon ul, #wus ul, #por ul, #lma ul, #fuf ul').sortable({
        axis: 'y',
        stop: handleLayerSortingStop,
        start: handleLayerSortingStart,
        cursor: 's-resize',
        containment: 'parent'
    });
    
           
	
	/**
	 * Printing
	 */
	jQuery('#map-print').bind('click', function () {
		var URLs = [];
        jQuery.each(NABU.MAP.layers, function(i, n)
        {
            // is this layer being displayed?
            if (!!n.visibility) 
            {
                // we need to push WMS URLS
                if (n.CLASS_NAME === 'OpenLayers.Layer.WMS' && typeof n.getURL === 'function' && typeof n.getTilesBounds === 'function') 
                {
                    URLs.push(n.getURL(n.getTilesBounds()));
                }
            }
        });
        var opts = {
		'pdfText': encodeURIComponent(jQuery('#print-comment').val()),
		'pdfActiveLayerURLs[]': URLs
        };
        jQuery('#pdfLinkWrapper').children().remove();
        jQuery.post('./includes/ajax_handlers/ajax_make_pdf.php', opts, function(response)
        {
            var pdfname = jQuery('result', response).text();
            jQuery('#pdfLinkWrapper').append('<p><a class="pdf-link" href="tmp/' + pdfname + '">Ihr PDF...</a></p>');
        });
	});
	
	
    /**
     * -------------------------------------------------------------------
     * Gazetteer function
     * -------------------------------------------------------------------
     */
    // jQuery('#page').show();

}); // end of document ready

