(function($) {
	$.fn.showSeller = function(dir) {
		var margin = 115, maxmargin = - $('.sellerslogos .body a').size() * margin + margin;
		$(this).click(function(e){
			if(($('.sellerslogos .body a:first').css('margin-top') != maxmargin +'px' && dir == '-')||( $('.sellerslogos .body a:first').css('margin-top') != '0px' && dir == '+') ) {
			
				$('.sellerslogos .body a:first').animate({'margin-top': dir+'=115px'}, 300);
			}
			
		});
	}
	$.fn.flyout = function() {
		this.click(function(e) {
			$(this).toggleClass('active');
			$(this).siblings('ul').toggleClass('hidden');
		});
	};
	$.fn.addlink = function() {
		return this.click(function(e) {
			if (e.target.tagName != 'A') {
				window.location.href = $('a:first', this).attr('href');
			}
		});
	};
	
	$(function() {
	/*	$('a[href^="http://"]').blank(); */
		$('.citys .arr').flyout();
		$('.selected:first').selected();
		$('.hoverable').hovers();
		$('.clickable', this).addlink();
		$('a').blurry(); // FIXME use * :focus { outline: 0; } instead
		$('.sellerslogos .up').showSeller('+');
		$('.sellerslogos .down').showSeller('-');
		$('#map.overview').each(function() {
			
			
			
			var anchors = {};

			var $$ = $(this).google().roadmap(51.165691, 10.451526, {
				zoom: 13,
				disableDefaultUI: true,
				//draggable: true,
				scrollwheel: true
			}).bind({
				'google.click.marker': function(e, map, marker) {
					var anchor = anchors[ marker.getPosition() ];
					self.location = anchor.href;
				}
			});

			var count = $('.polaroid h3').google().geocode(function(result) {
				var position = result.geometry.location, H3 = $(this);
				// console.log($(this).context);
				anchors[ position ] = H3.parents('.polaroid').find('a:first').get(0);
				$$.marker(position, {
					icon: 'images/mapmark.png',
					title: H3.text()
				});

				// center on last marker
				// FIXME center on median of longitudes and latitudes
				// FIXME set zoom so bouning box includes all markers
				if (--count == 0) { $$.center(position); }
			}).length;
			
			// console.log($('.polaroid h3').length);
			
		});
	});
})(jQuery);
