/**
 * Stub for Vessel interaction.
 * @author Guy Whitfield
 */
rn.PanelMapSettings = function () {
    var that = this;
    this.settings = {};
    this.mapState = false;


    this.init = function () {
        this.parent.init.call(this);
        this.domNode = $('#map-settings');
        this.setDomElements();
        this.customInput(this.domNode.find('input'));

        $(document).keyup(function (e) {
            if (e.which == 27) {
                if (rn.panelMapSettings.mapState) {
                    rn.panelMapSettings.toggleFullscreen();
                }
            }
        });
    };

    this.toggleFullscreen = function () {

        if (!rn.panelMapSettings.mapState) {
            rn.home.pauseVideos();
            $('#map-settings').appendTo('body').css({ 'position': 'absolute', 'bottom': '0', 'left': '0', 'height': '37px', 'width': '100%', 'z-index': '2770' });
            $('body').addClass('hideOverflow');
            rn.Events.fireGlobalEvent(rn.Events.registry.EVENT_MAP_SIZE_TOGGLE, {});
            rn.panelMapSettings.mapState = true;

        } else {
            $('#map-settings').appendTo($('#map-container')).css({ 'position': 'relative', 'margin-top': '-37px' });
            $('body').removeClass('hideOverflow');
            rn.home.playVideos();
            rn.Events.fireGlobalEvent(rn.Events.registry.EVENT_MAP_SIZE_TOGGLE, {});
            rn.panelMapSettings.mapState = false;
        }
    }

    this.customInput = function (inputgroup) {
        return inputgroup.each(function () {
            if ($(this).is('[type=checkbox]')) {
                var cb = $(this);
                var label = $('label[for=' + cb.attr('id') + ']');

                label.hover(
				function () { $(this).addClass('hover'); },
				function () { $(this).removeClass('hover'); }
			);

                cb.bind('setState', function (e) {
                    var layerType = e.target.id;
                    cb.is(':checked') ? label.addClass('checked') : label.removeClass('checked');
                    var layer = {
                        air: { entity: rn.map.layerAir, relationship: rn.map.layerAirRelationships, type: layerType },
                        land: { entity: rn.map.layerLand, relationship: rn.map.layerLandRelationships, type: layerType },
                        sea: { entity: rn.map.layerSea, relationship: rn.map.layerSeaRelationships, type: layerType }
                    };
                    layer = layer[layerType];

                    if ($(this).is(':checked')) {
                        rn.map.showLayer(layer.entity, layer.relationship, layer.type);
                    } else {
                        rn.map.hideLayer(layer.entity, layer.relationship, layer.type);
                    }

                })
			.trigger('setState')
			.click(function () {
			    $('input[name=' + $(this).attr('name') + ']').trigger('setState');
			})
            }
        });
    }

    this.setDomElements = function () {
        //Click handlers
        $('.linkGlobalDeployment').click(function () {
            rn.Events.fireGlobalEvent(rn.Events.registry.EVENT_GLOBAL_DEPLOYMENT, {});
        });
        $('.linkIncreaseZoom').click(function () {
            rn.map.zoomIn();
        });
        $('.linkDecreaseZoom').click(function () {
            rn.map.zoomOut();
        });
        $('.linkViewVessel').click(function () {
            rn.Events.fireGlobalEvent(rn.Events.registry.EVENT_VIEW_VESSEL, {});
        });
        $('.linkMaximiseMap').click(function () {
            rn.panelMapSettings.toggleFullscreen();
        });
    }


    /**
    * EVENT LISTENERS
    */

    this.onShowVesselOnMap = function () {
        $('#map-settings input').each(function () { if (!$(this).is(':checked')) { $(this).trigger('click'); $(this).attr('checked', true); $("label[for=" + $(this).attr('id') + "]").addClass('checked'); } });

        for (var i = 0, j = rn.map.entitiesLayer.getLength(); i < j; i++) {
            if (!rn.map.entitiesLayer.get(i).getVisible()) {
                rn.map.entitiesLayer.get(i).setOptions({ 'visible': true });
            }
        }
    }

    return this;
}

rn.PanelMapSettings.prototype = new rn.Panel();
rn.PanelMapSettings.constructor = rn.PanelMapSettings;
rn.panelMapSettings = new rn.PanelMapSettings();
rn.panelMapSettings.parent = rn.PanelMapSettings.prototype;

