/*
 * RBFF map buttons control
 */
function RBFFButtonControl() {}

RBFFButtonControl.prototype = new GControl();

RBFFButtonControl.prototype._container = null;

RBFFButtonControl.prototype.initialize = function(map) {
    var _messenger       = window;
    var _templates       = {};
    var _parser          = TrimPath.parseDOMTemplate;

    _templates['gm_button_control'] = _parser('gm_button_control_tpl');
    
    var html   = _templates['gm_button_control'].process({
        profile_name : settings['profile_display_name'],
        base_url     : settings['base_url']
    });
    _container = $(html)[0];

    map.getContainer().appendChild(_container);


    /* Controller */
    $('#show_labels:input', _container).livequery('click', function() {
        var v = false;

        if ($(this).attr('checked')) {
            v = true;
        }

        $(_messenger).trigger('mp::labels::toggle::changed', [v]);

        /* Don't return false: what can I say... IE */
        //return false;
    });
       
    $('#show_navaid_markers:input', _container).livequery('click', function() {
        var v = false;
    
        if ($(this).attr('checked')) {
            v = true;
        }
    
        $(_messenger).trigger('mp::gwmarkers::toggle::changed', [v]);
    
        /* Don't return false: what can I say... IE */
        //return false;
    });
    
    $('#noaa_charts_list', _container).livequery('change', function() {
        var v = false;
    
        if ($(this).val() != -1) {
            v = true;
        }
    
        $(_messenger).trigger('mp::gwcharts::toggle::changed', [v]);
    
        /* Don't return false: what can I say... IE */
        //return false;
    });
       
    $('#show_noaa_charts:input', _container).livequery('click', function() {
        var v = false;
    
        if ($(this).attr('checked')) {
            v = true;
        }
    
        $(_messenger).trigger('mp::gwcharts::toggle::changed', [v]);
    
        /* Don't return false: what can I say... IE */
        //return false;
    });
       
    $('*[@rel=show_key]', _container).livequery('click', function() {
        alert('map key clicked');

        return false;
    });
    $('div[@rel=show_help]', _container).livequery('click', function() {
        $(_messenger).trigger('all::open::clicked', ['help']);
            
        return false;
    });
	
	$("#show_navaid_markers:input", _container).attr("checked",false); 
    $("#show_noaa_charts:input", _container).attr("checked",false);
    
    return _container;
}

RBFFButtonControl.prototype.toggleLabels = function(show) {
    $('#show_labels:input', this._container).attr('checked', true);
    
    if (!show) {
        $('#show_labels:input', this._container).attr('checked', false);
    }
}

RBFFButtonControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}

RBFFButtonControl.prototype.toggleGWControls = function(show) {
    if (!show) {
        $('.map_control_items_geowake', this._container).hide();
    }else{
        $('.map_control_items_geowake', this._container).show();  
    }
}

/*
 * Help screen control
 */
function RBFFHelpControl() {}

RBFFHelpControl.prototype = new GControl();

RBFFHelpControl.prototype._container = null;

RBFFHelpControl.prototype.initialize = function(map) {
    var _messenger        = window;
    var _templates        = {};
    var _parser           = TrimPath.parseDOMTemplate;

    var show_hide_checked = false;
	if(readCookie('rbff_show_hide')) {
	    show_hide_checked = true;
	}

    _templates['gm_help_control'] = _parser('gm_help_control_tpl');

    var html   = _templates['gm_help_control'].process({
        show_hide_checked: show_hide_checked
    });
    _container = $(html)[0];

    map.getContainer().appendChild(_container);

    /* Controller */
    $('a[@rel=get_started]', _container).livequery('click', function() {
        $(_messenger).trigger('all::close::clicked', []);

        return false;
    });

	$('#showhide', _container).livequery('click', function() {
		var hide = false;
		
        if ($(this).attr('checked')){
            hide = true;
        }

    	if(hide) {
    		createCookie('rbff_show_hide', false, 3);
    		$('#showhide').attr('checked', true);
    	}
    	else {
    		eraseCookie('rbff_show_hide');
    		$('#showhide').removeAttr('checked'); 		
    	}
	});

    return _container;
}

RBFFHelpControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}

/*
 * Hotspot screen control
 */
function RBFFHotspotControl(options) {
    _view = 'start';

    var view = (options || {}).view;
    
    if (view == 'finish') {
        _view    = view;
        _options = options;
    }
}

RBFFHotspotControl.prototype = new GControl();

RBFFHotspotControl.prototype._container = null;
RBFFHotspotControl.prototype._view      = null;
RBFFHotspotControl.prototype._options   = null;

RBFFHotspotControl.prototype.initialize = function(map) {
    var _messenger        = window;
    var _templates        = {};
    var _parser           = TrimPath.parseDOMTemplate;
    
    _templates['gm_hotspot_control_box']    = _parser('gm_hotspot_control_box_tpl');
    _templates['gm_hotspot_control_start']  = _parser('gm_hotspot_control_start_tpl');
    _templates['gm_hotspot_control_finish'] = _parser('gm_hotspot_control_finish_tpl');
    _templates['gm_hotspot_control_error']  = _parser('gm_hotspot_control_error_tpl');

    /* Generate control content */
    var content = '';
    if (_view == 'start') {
        // if map zoom is too far out, prevent new spot
        if(map.getZoom() < 14) {
             $(_messenger).trigger('all::hotspot::zoom::change', [14]);
        }
        
        content = _templates['gm_hotspot_control_start'].process({
            base_url: settings['base_url']
        });
    }
    else if (_view == 'finish') {
        //_options['base_url'] = settings['base_url'];
        content = _templates['gm_hotspot_control_finish'].process(_options.hotspot);
    }

    /* Generate control wrapper and insert content */
    var html = _templates['gm_hotspot_control_box'].process({
        content: content
    });
    _container = $(html)[0];
    map.getContainer().appendChild(_container);

    /* Controller */
    $('a[@rel=cancel]', _container).livequery('click', function() {
        $(_messenger).trigger('all::close::clicked', []);

        return false;
    });

    $('a[@rel=drop]', _container).livequery('click', function() {
        $(_messenger).trigger('all::close::clicked', []);

        return false;
    });
    $('a[@rel=continue]', _container).livequery('click', function() {
        $('form', _container).submit();

        return false;
    });

	$('a.begin_add_hotspot', _container).livequery('click', function() {
		var type = $(this).attr('rel');

        if ($('#terms_agree:checked').length != 1) {
            alert('You have to agree to the terms and conditions.');

            return false;
        }

		if (type != 'facility' && type != 'water' && type != 'hotspot') {
		    return false;
		}
		
		$(_messenger).trigger('all::hotspot::add::clicked', [type]);
		$(_messenger).trigger('all::close::clicked', []);

		return false;
	});
    
    return _container;
}

RBFFHotspotControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}

/*
 * Nautical Button Control
 */
function RBFFNauticalControl() {}

RBFFNauticalControl.prototype = new GControl();

RBFFNauticalControl.prototype._container = null;

RBFFNauticalControl.prototype.initialize = function(map) {
    var _messenger        = window;
    var _templates        = {};
    var _parser           = TrimPath.parseDOMTemplate;

    _templates['gm_nautical_control'] = _parser('gm_nautical_control_tpl');

    var html   = _templates['gm_nautical_control'].process();
    _container = $(html)[0];

    map.getContainer().appendChild(_container);
    
    /** Controller **/
    $('a[@rel=nautical_info]', _container).livequery('click', function() {
        $(_messenger).trigger('mp::nautical::toggle::changed', []);

        return false;
    });
    
    return _container;
}

RBFFNauticalControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}