google.load("maps", "2");
google.initializeMaps = function() {
	google.maps.__customControl = function($selector, posV, posH) {
		this.printable = function() {
			return true;
		} 
		this.selectable = function() {
			return true;
		}
		this.initialize = function() {
			return $selector[0];
		}
		this.getDefaultPosition = function() {
			if (posH === 'left') {
				var offsetH = $selector.css('left').replace('px', '');
			} else {
				var offsetH = $selector.css('right').replace('px', '');
			}
			if (posV === 'top') {
				var offsetV = $selector.css('top').replace('px', '');
			} else {
				var offsetV = $selector.css('bottom').replace('px', '');
			}
			
			var anchor = G_ANCHOR_TOP_RIGHT;
			if (posH === 'left' && posV === 'top') anchor = G_ANCHOR_TOP_LEFT; 
			else if (posH === 'right' && posV === 'bottom') anchor = G_ANCHOR_BOTTOM_RIGHT; 
			else if (posH === 'left' && posV === 'bottom') anchor = G_ANCHOR_BOTTOM_LEFT; 
			
			var offset = new google.maps.Size(offsetH, offsetV);
			return new google.maps.ControlPosition(anchor, offset);
		}
	}
	google.maps.__customControl.prototype = new google.maps.Control();
}

$.fn.extend({
    serial_fade: function(o) {
        if(!o.speed || o.speed == undefined || o.speed == null) { o.speed = 'slow'; }
        if(!o.fade || o.fade == undefined || o.fade == null)    { o.fade = 'in'; }
        if(!o.index || o.index == undefined || o.index == null) { o.index = 0; }
        var s = this.selector;
        if(o.fade.toLowerCase() == 'in') {
            return this.eq(o.index).fadeIn(o.speed, function() {
                o.index++;
                if($(s).eq(o.index).length > 0) {
                    $(s).serial_fade({speed:o.speed,fade:o.fade,index:o.index});
                }
            });
        } else {
            return this.eq(o.index).fadeOut(o.speed, function() {
                o.index++;
                if($(s).eq(o.index).length > 0) {
                    $(s).serial_fade({speed:o.speed,fade:o.fade,index:o.index});
                }
            });
        }
    }
});

$(document).ready(function(){
	// add rounded borders
	$(".enclosed").each(function(){
		var content = $(this).html();
		$(this).html('<div class="top"></div><div class="content">'+ content +'</div><div class="bottom"></div>');
	});
	
	$('#contextMain .tabs .tab').bind('click', function(){
		$(this).addClass('active');
		$(this).siblings('.tab').removeClass('active');
	});
	
	$('.resetMap').bind('click', function() {
		$('#municipalitySelector').val(0);
		$('#departmentSelector').val(0);
		IBD.map.setZoom(12);
		IBD.map.panTo(IBD.basePoint);
		IBD.initialize();
		return false;
	});
	
	$('#cancelLogin').bind('click', function(){
		window.location = '/';
		return false;
	});
	
	google.initializeMaps();
	IBD.initialize('map', 51.81808, 4.706933, 12);
	IBD.setFiltersAndSelections(true);	
});
