jQuery(document).ready(function(){
	var labels = jQuery('body').hasClass('nl')?
		{	more: 'Meer informatie',
			reset: 'Reset kaart',
			farms: 'Onze referenties',
			search: 'Zoeken',
			refUrl: '/nl/referenties/'
		}:
		{	more: 'Read more',
			reset: 'Reset map',
			farms: 'Our farms',
			search: 'Search',
			refUrl: '/en/references/'
		};

	if(document.all){ // IE... hate it
		jQuery('#navigation > li').hover(
			function(){jQuery(this).addClass('hover').parent().find('li.selected').removeClass('selected').addClass('selected-hover');}, 
			function(){jQuery(this).removeClass('hover').parent().find('li.selected-hover').removeClass('selected-hover').addClass('selected');}
		).find('ul').css({top: '30px'});
	}
	
	function setupMap(reset){
		jQuery('div.maps, div.maps p.address').hide();
		map.clearOverlays();
		map.setUIToDefault();
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(31.653381,17.226563), 1);
		if(!home && !reset){
			if(window.location.href.indexOf('#')){
				var divId = window.location.href.substring(window.location.href.indexOf('#')+1);
				jQuery('div#'+divId).show();
			}
		}
		
		jQuery('div.maps').each(function(){
			var t = jQuery(this)
				name = t.find('h2').text(),
				address = t.find('p.address');
				img =  address.find('img').attr('src');
				latlng = address.find('em').text(),
				addr = jQuery(address.clone()).find('em, img').remove().end().html();
			addMarker(name, addr, latlng, img, divId);
		});
	}
	function addMarker(name, addr, latlng, img, divId){
		icon.image = img;
		var glatlng = GLatLng.fromUrlValue(latlng), marker = new GMarker(glatlng, {icon: icon, zIndexProcess: getZIndex}), pretty = name.prettyfy();
		map.addOverlay(marker);
		GEvent.addListener(marker,'click', function() {
			var html = '<h3>'+name+'</h3><p>'+addr+'</p>';
			if(ref){
				jQuery('div.maps').hide();
				jQuery('div#'+pretty).show();
			}else if(home){
				html += '<p><a href="'+labels.refUrl+'#'+pretty+'">'+labels.more+' &raquo;</a></p>';
			}
			marker.openInfoWindowHtml(html);
			if(map.getZoom() < 10) map.setZoom(10);
			map.panTo(marker.getLatLng());
		});
		if(divId == pretty){
			GEvent.trigger(marker, 'click'); 
		}
	}
	function getZIndex(marker, i){
		return GOverlay.getZIndex(marker.getPoint().lat()) + marker.getIcon().image.indexOf('spotlight')*1000000;
	}
	var b = jQuery('body'), home = b.hasClass('home'), ref = b.hasClass('references');
	if(home || ref){
		jQuery('#contents').prepend('<h2>'+labels.farms+' <small>[ <a href="#">'+labels.reset+'</a> ]</small></h2><div id="map-references"></div>');
		jQuery('#contents h2 small a').click(function(){this.blur();setupMap(true);return false;});
		if(home){
			jQuery('#right-column').prepend('<div style="border: 1px solid #000;"><embed type="application/x-shockwave-flash" wmode="transparent" src="/includes/monoslideshow/monoslideshow.swf" style="" id="slideshow" name="slideshow" bgcolor="#ffffff" quality="high" flashvars="showLogo=false&amp;showVersionInfo=false&showRegistration=false" base="/includes/monoslideshow/" width="458" height="330"/></div>');
		}
	}
	if(jQuery('#map-references')[0]){
		var el = jQuery('#map-references'),	div = el.get(0), map = new GMap2(div), icon, divId;
		var icon = new GIcon();
		icon.iconSize = new GSize(32, 30);
		icon.iconAnchor = new GPoint(0, 30);
		icon.infoWindowAnchor = new GPoint(27, 8);
		setupMap(false);
	}
	if(jQuery('div#accordion')[0]){
		jQuery('div#accordion').accordion({autoHeight: false, collapsible: true, active: false});
	}
	jQuery('input#q').val(labels.search).focus(function(){if(this.value == labels.search) this.value = '';}).blur(function(){if(this.value == '') this.value = labels.search;});
});
