/* In Map Control Class */
function InMapControl(){
    this.position = G_ANCHOR_TOP_LEFT;
    this.positionSize = new GSize(0,0);
    this.zoomCallback = function() { };
	this.panCallback = null;
}

InMapControl.prototype = new GControl();

InMapControl.prototype.setMap = function(Map){
    this.map = Map;
}

InMapControl.prototype.setZoomCallback = function(callback){
    this.zoomCallback = callback;
}

InMapControl.prototype.setPanCallback = function(callback){
    this.panCallback = callback;
}

InMapControl.prototype.setControlTpl = function(controlTpl){
    this.controlTpl = controlTpl;
}

InMapControl.prototype.setPosition = function(position){
    this.position = position;
}

InMapControl.prototype.setPositionSize = function(size){
    this.positionSize = size;
}

InMapControl.prototype.pan = function(d){
	if ('function' == typeof(this.panCallback)){
		this.panCallback(this.map.getMap(), d);
	}else{
		switch(d){
			case 'up':
				dx = 0;
				dy = 1;
				break;
			case 'down':
				dx = 0;
				dy = -1;
				break;
			case 'left':
				dx = -1;
				dy = 0;
				break;
			case 'right':
				dx = 1;
				dy = 0;
				break;
		}
		this.map.getMap().panDirection(dx,dy);
	}
}

InMapControl.prototype.initialize = function(map){
    
    var control = $(this.controlTpl.html);
    var that = this;
    // Set up basic controls
    control.find('#map-up').click(function(){
		that.pan('up');
    });
    
    control.find('#map-down').click(function(){
		that.pan('down');
    });
    
    control.find('#map-left').click(function(){
		that.pan('left');
    });
    
    control.find('#map-right').click(function(){
		that.pan('right');
    });
    
    center = this.map.getMap().getCenter();
    zoom = this.map.getMap().getZoom();
    control.find('#map-centre').click(function(){
        map.setCenter(center, zoom);
    });
    
    // Must append before slider is set up
    $(this.map.getMap().getContainer()).append(control);
    
    // Set up zoom slider control
    var maxZoom = Map.getTypeValue('Default', 'maxZoom') || 20;
    var minZoom = Map.getTypeValue('Default', 'minZoom') || 0;
    control.find('#zoom-in').click(function(){
        if (map.getZoom() >= maxZoom) { return; }
        map.zoomIn();
        that.setSliderZoom(map.getZoom());
        that.zoomCallback();
    });
    
    control.find('#zoom-out').click(function(){
        if (map.getZoom() <= minZoom) { return; }
        map.zoomOut();
        that.setSliderZoom(map.getZoom());
        that.zoomCallback();
    });
    
    control.find('#zoom-toggle').click(function(){
        if (map.getZoom() >= maxZoom) { 
            map.zoomOut(); 
        } else {
            map.zoomIn(); 
        }
        that.setSliderZoom(map.getZoom());
        that.zoomCallback();
    });

    var zoomSteps = maxZoom - minZoom;
    $('#zoom-track').slider({
        handle: '#zoom-pointer',
        axis: 'vertical',
        max: minZoom,
        min: maxZoom,
        startValue: this.map.zoom,
        steps: zoomSteps,
        change: function(e, ui){
            map.setZoom(ui.value);
            that.zoomCallback();
        }
    });
    return control[0];
}

InMapControl.prototype.setSliderZoom = function(zoom){
    $('#zoom-track').slider('moveTo', zoom);
}

InMapControl.prototype.getDefaultPosition = function(){
    return new GControlPosition(this.position, this.positionSize);
}

/* In Map Type Control Class */
function InMapTypeControl(){
    this.controlTpl = null;
    this.container = null;
}

InMapTypeControl.prototype = new InMapControl();

InMapTypeControl.prototype.initialize = function(map){
    if (this.controlTpl == null){ alert('No control template. Please use object.setControlTpl()'); return; }
    var control = $(this.controlTpl.html);
    this.setup(control, map);
    $(this.map.getMap().getContainer()).append(control);
    return control[0];
}

InMapTypeControl.prototype.setup = function(container,map){
    var container = $(container);
    var that = this;
    container.find('.mtButton').each(function(){
        var type = that.resolveTypeFromClass(this);
        map.addMapType(type);
        $(this).click(function(){
            map.setMapType(type);
        });
    });
    return container;
}

InMapTypeControl.prototype.resolveTypeFromClass = function(el){
    if ($(el).hasClass('mtSatellite')){
        return G_SATELLITE_MAP;
    } else if ($(el).hasClass('mtHybrid')) {
        return G_HYBRID_MAP;
    } else if ($(el).hasClass('mtPhysical')) {
        return G_PHYSICAL_MAP;
    } else {
        return G_NORMAL_MAP;
    }
}

