/*	rodw.js | Reis om de Wereld
**	Lodewijk Schutte | low
**	v1.5 | 20101011
**	---------------------------------------------------------------------- */

// Shadowbox.init({overlayOpacity : 0.8});

$(function(){

	$('body').addClass('js-on');
	
	$('a[rel=shadowbox]').fancybox({
		overlayOpacity: 0.6,
		overlayColor: '#000',
		titlePosition: 'inside',
		titleFormat: '<div id="fancybox-title"></div>'
	});
	
	var continents, map, countries;

	if (continents = $('#continents').get(0)) {
		
		var home = $('body').hasClass('home');
		var modifier = home ? 0.66 : 1;

		$(continents).css({
			width: (home ? 390 : 590),
			height: (home ? 219 : 332),
			backgroundImage: (home ? 'url(../images/wereld-390.gif)' : 'url(../images/wereld-590.gif)')
		});
		
		$('a', continents).each(function(){

			var coords = $(this).attr('rel').split(',');
			
			if (coords.length == 4) {
				
				for (var i = 0; i < coords.length; i++) {
					coords[i] = coords[i] * modifier;
				}

				$(this).css({
					width: coords[0]+'px',
					height: coords[1]+'px',
					lineHeight: coords[1]+'px',
					left: coords[2]+'px',
					top: coords[3]+'px'
				});
			}
			
		});
		
	}
	
	
	map = $('#continent-map').get(0);
	countries = $('#countries a');
	
	if (countries.length) {

		if (map) {
			
			
			var setMap = function(el, src, w, h) {
				$(el).css({
					backgroundImage: 'url('+src+')',
					width: w,
					height: h
				});
			}
			
			var img = $('#continent-map img').get(0),
				tmp = new Image();

			tmp.src = img.src;
			
			
			if (tmp.width && tmp.height) {
				setMap(map, tmp.src, tmp.width, tmp.height);
			} else {
				tmp.onload = function() {
					setMap(map, tmp.src, tmp.width, tmp.height)
				}
			}
			
		
			$(img).remove();
		}

		countries.each(function(i){

			var coords = $(this).attr('rel').split(',');
			
			if (coords.length == 4) {
				
				var a = $(this).clone().attr('title',$(this).text()).empty();
				
				$(a).css({
					width: coords[0]+'px',
					height: coords[1]+'px',
					left: coords[2]+'px',
					top: coords[3]+'px'
				});
				
				$(a).appendTo(map);
				
				$(this).hover(function(){
					$(a).addClass('hover');
				}, function(){
					$(a).removeClass('hover');
				});
			}
			
		});
		
	}
	
	// add commas
	$('#countries li:not(:last-child) a').append('<span>,</span>');
	
	// Open external links in new windoe
	$('a[href^=http]').click(function(e){
		if ($(this).attr('href').indexOf(document.domain) == -1) {
			window.open($(this).attr('href'));
			return false;
		}
	});

	
});
