var IE6 = ($.browser.msie && $.browser.version.substr(0,1) < 7) ? true : false;

var tiles = {
	'tile1': { 
		'css': { 'background-image': 'url(landla/images/tile1.png)', 'background-position': '217px 259px' },
		'centre': [306, 286],
		'popup': [162, -44],
		'label': 'right',
		'animation': [ { 'direction': 'up', 'distance': 20 }, { 'direction': 'left', 'distance': 10 } ]
	},
	'tile2': { 
		'css': { 'background-image': 'url(landla/images/tile2.png)', 'background-position': '294px 185px' },
		'centre': [224, 372],
		'popup': [31, 42],
		'label': 'below',
		'animation': [ { 'direction': 'up', 'distance': 30 }, { 'direction': 'left', 'distance': 10 } ]
	},
	'tile3': { 
		'css': { 'background-image': 'url(landla/images/tile3.png)', 'background-position': '382px 76px' },
		'centre': [140, 457],
		'popup': [-2, 245],
		'label': 'right',
		'animation': [ { 'direction': 'left', 'distance': 50 }, { 'direction': 'up', 'distance': 20 }, { 'direction': 'right', 'distance': 10 } ]
	},
	'tile4': { 
		'css': { 'background-image': 'url(landla/images/tile4.png)', 'background-position': '288px 264px' },
		'centre': [332, 383],
		'popup': [330, 0],
		'label': 'right',
		'animation': [ { 'direction': 'down', 'distance': 8 }, { 'direction': 'left', 'distance': 20 } ]
	},
	'tile6': { 
		'css': { 'background-image': 'url(landla/images/tile6.png)', 'background-position': '468px 134px' },
		'centre': [154, 545],
		'popup': [11, 469],
		'label': 'right',
		'animation': [ { 'direction': 'left', 'distance': 20 }, { 'direction': 'up', 'distance': 20 }, { 'direction': 'right', 'distance': 7 } ]
	},
	'tile7': { 
		'css': { 'background-image': 'url(landla/images/tile7.png)', 'background-position': '372px 293px' },
		'centre': [396, 467],
		'popup': [153, 506],
		'label': 'left',
		'animation': [ { 'direction': 'right', 'distance': 5 }, { 'direction': 'up', 'distance': 40 }, { 'direction': 'right', 'distance': 5 } ]
	},
	'tile8': { 
		'css': { 'background-image': 'url(landla/images/tile8.png)', 'background-position': '481px 186px' },
		'centre': [286, 564],
		'popup': [83, 623],
		'label': 'left',
		'animation': [ { 'direction': 'right', 'distance': 7 }, { 'direction': 'up', 'distance': 32 }, { 'direction': 'right', 'distance': 7 } ]
	},
	'tile9': { 
		'css': { 'background-image': 'url(landla/images/tile9.png)', 'background-position': '562px 150px' },
		'centre': [192, 642],
		'popup': [9, 711],
		'label': 'left',
		'animation': [ { 'direction': 'right', 'distance': 8 }, { 'direction': 'up', 'distance': 28 }, { 'direction': 'right', 'distance': 8 } ]
	}
};

if (IE6) {
	jQuery.each(tiles, function(i, val){
		tiles[i].css['background-image'] = tiles[i].css['background-image'].replace('.png', '.gif');
	});
}

var queue = [];	
var timer;
var currentTile = null;
var animate = function() {
	var animation = queue.pop();
	if (animation) {
		if (animation.dot) {
			var dotImg = IE6 ? $('<img src="landla/images/dot.gif" class="dot" />') : $('<img src="landla/images/dot.png" class="dot" />');
			dotImg.prependTo('#tiles').css({
				'top': animation.dot[0] + 'px',
				'left': animation.dot[1] + 'px',
				'display': 'none'
			}).show();
		} else {
			$('dd.' + animation.popup).show();
		}
		timer = setTimeout(animate, 1);
  }
}
	
$(document).ready(function() {
	$('area').hover(function() {
		
		if (currentTile == $(this).attr('id') || !tiles[$(this).attr('id')]) return;
		
		currentTile = $(this).attr('id');
		var tile = tiles[$(this).attr('id')];
		var top = tile.centre[0];
		var left = tile.centre[1];
		var $label = $('dt.' + $(this).attr('id'));
		var $popup = $('dd.' + $(this).attr('id'));
		
		clearTimeout(timer);
		$('.dot').remove();
		
		if (jQuery.support.opacity) {
			$('#tiles dt, #tiles dd').fadeOut('normal');
		}
		else {
			$('#tiles dt, #tiles dd').hide();
		}
			
		$('#tiles').css(tile.css);
		$('#point').css( { 'top': top + 'px', 'left': left + 'px', 'display': 'block' } );
		$popup.css( { 'top': tile.popup[0] + 'px', 'left': tile.popup[1] + 'px' } );
		
		switch (tile.label) {
			case 'below':
				$label.show().css( { 'top': (top + 12) + 'px', 'left': (left + 5 - $label.width() / 2) + 'px' } ); break;
			case 'right':
				$label.show().css( { 'top': (top - 5) + 'px', 'left': (left + 16) + 'px' } ); break;
			case 'left':
				$label.show().css( { 'top': (top - 3) + 'px', 'left': (left - 6 - $label.width()) + 'px' } ); break;
		}
			
		queue = [];
		left += 4;
		top += 4;
		
		if (tile.animation) {
			for (var i = 0; i < tile.animation.length; i++) {
				for (var j = 0; j < tile.animation[i].distance; j++) {
					switch (tile.animation[i].direction) {
						case 'up':
							top -= 5;
							break;
						case 'down':
							top += 5;
							break;
						case 'right':
							left += 5;
							break;
						case 'left':
							left -= 5;
							break;
					}
					queue.push( { 'dot': [top, left] } );
				}
			}
			queue.push( { 'popup': $(this).attr('id') } );
			queue.reverse();
			animate();
		}
		
		return;
	});
	
	
	$('ul').each(function() {
		$(this).find('li').each(function(i) {
			$(this).addClass(i % 2 ? 'even' : 'odd');
		})
	});
	
	$('#tabs').tabs();
	
	$('.PageGalleryComponent a').each(function() {
		$.preloadImage($(this).attr('href'));
		$(this).click(function() {
			$(this).parents('.PageGalleryComponent').find('.current').attr('src', $(this).attr('href'));
			return false;
		})
	});
	
	$('html').click(function(event) {
	  var $target = $(event.target);
		if ($target.parents('.PopupComponent').size() == 0 && !$target.is('area, .dot')) {
			if (jQuery.support.opacity) {
				$('.dot').fadeOut();
				$('#tiles dt, #tiles dd').fadeOut();
				$('#point').fadeOut();
				$('#tiles').css('background-image', 'none');
			} else {
				$('.dot').hide();
				$('#tiles dt, #tiles dd').hide();
				$('#point').hide();
				$('#tiles').css('background-image', 'url(landla/images/transparent.gif)');
			}
			
			clearTimeout(timer);
		}
	});
});



jQuery.preloadImage = function() {
	jQuery("<img>").attr("src", arguments[0]);
} 
