
function initRollovers() {
	$('.rollover').hover(function() {
			var img = $(this).find('img:first');
			newsrc = img.attr('src').replace(/_off./,'_on.');
			img.attr({src:newsrc});
		},
		function() {
			var img = $(this).find('img:first');
			img.attr({src:img.attr('src').replace(/_on./,'_off.')});
	});
}

var gallery_objects = [];
var current_gallery_item = new Object();

function loadGallery(xml){
	
	
	// reset gallery items
	gallery_objects = [];
	gallery_gallery_item = null;
	
	var items = $(xml).find('item');
	
	// load current gallery html
	var idx = 0;
	items.each(function() {
		
		var img = $(this).find('image:first');
		var flash = $(this).find('flash:first');
		
		var html = '';
		var width = 0;
		var height = 0;
		
		if(img.length == 1) {
			 
 			html = '<img src="galleries/' + img.attr('src') + '" alt="" />';
			width = img.attr('width');
			height = img.attr('height');
		}
		
		if(flash.length == 1) {
			var flashvars = {};
			var params = {
		
				// very common params
				src: 'galleries/' + flash.attr('src'),
				width: flash.attr('width'),
				height: flash.attr('height'),		
		
				// flashembed specific options
				version:null,
				onFail:null,
				expressInstall:null,  
				debug: false,
		
				// flashembed defaults
				bgcolor: '#FFFFFF',
				allowfullscreen: false,
				allowscriptaccess: 'always',
				quality: 'high',
				type: 'application/x-shockwave-flash',
				pluginspage: 'http://www.adobe.com/go/getflashplayer'
			};
			
			width = flash.attr('width');
			height = flash.attr('height');
			
			var html = '<div style="width:' + width + 'px;height:' + height + 'px">' + window.flashembed.getHTML(params,flashvars) + '</div>';
		
		}
		
		var linktext = $(this).attr('linktext') != null ? $(this).attr('linktext') : 'Launch Site';
		
		var obj = new Object();
		obj.html = html;
		obj.title = $(this).attr('title');
		obj.link = $(this).attr('href') == null?'':'| <a href="' + $(this).attr('href') + '" target="_blank" class="right-arrow">' + linktext + '</a>';
		obj.width = width;
		obj.height = height;
		obj.id = idx;
		
		gallery_objects[idx] = obj;
		idx++;
	});
	
	current_gallery_item = gallery_objects[0];
	
	
	// load the modal for the gallery
	$('#Gallery').modal({
		opacity:40,
		position:['50px','50%'],
		onOpen:function(dialog) {
			
			$('.city-hover').addClass('nohover');
			dialog.overlay.fadeIn(100, function() {
				dialog.container.fadeIn(100,
					function() {
						
						// set gallery to first item
						UpdateGallery();
					
						// keep our window in order
						dialog.data.fadeIn(100, function() {
							dialog.data.css({
								marginLeft: Math.round(dialog.data.width() / 2 * -1)
							});
						});
					}
				);
			});
		},
		onClose:function(dialog) {
			$.modal.close();
			$('.city-hover').removeClass('nohover');
		}
	});
}

function updatePosition() {
	$('.simplemodal-data').css({marginLeft: Math.round($('.simplemodal-data').width() / 2 * -1)});
}

function UpdateGallery() {
	
	$('#simplemodal-container').fadeOut(100, function() {

		$('#GalleryImage').html(current_gallery_item.html);
		$('#CurrentIndex').html(current_gallery_item.id + 1);
		$('#GalleryLength').html(gallery_objects.length);
		$('#GalleryTitle').html(current_gallery_item.title);
		$('#GalleryLink').html(current_gallery_item.link);
		$('#simplemodal-container').fadeIn(100, function() {	
			$(this).everyTime(100, 'modalpos', updatePosition);
		});
		
	});
}

function galleryPrev() {
	
	var newidx = current_gallery_item.id == 0 ? (gallery_objects.length - 1) : (current_gallery_item.id - 1);
	current_gallery_item = gallery_objects[newidx];

	UpdateGallery();
}

function galleryNext() {
	
	current_gallery_item = gallery_objects[(current_gallery_item.id + 1) % gallery_objects.length];

	UpdateGallery();
}

var cache = [];

function galleryHandler(e) {
	e.preventDefault();
	var link = $(this).attr('href');
	if (!cache[link]) {
		$.ajax({
	    	type: "GET",
	        url: link,
	        dataType: "xml",
	        success: function(xml){
				cache[link] = xml;
				loadGallery(xml);
			 }
		});
	} else {
		loadGallery(cache[link]);
	}
	
}
	
function initGalleryLinks(){
	
	$('a.gallery').click(galleryHandler);
}

$(document).ready(function() {

	var imgidx = Math.round(Math.random() * 5) + 1;
	$('#CreatureContainer').html('<img src="images/creature_' + imgidx + '.gif" alt="" />'); 
	
	$('.feature').hover(function() {
		
		// rollover
		var img = $(this).find('img:first');
		newsrc = img.attr('src').replace(/_off.jpg/,'_on.jpg');
		img.attr({src:newsrc});
		
		// hover popup
		var content = $('#' + $(this).attr('id') + '-content').html();
		
		var popcontainer = $(this).find('.feature-hover:first');
		popcontainer.css({left:$(this).position().left + 90, top:$(this).position().top - 15})
		popcontainer.show();
		
	},
	function() {
		var img = $(this).find('img:first');
		img.attr({src:img.attr('src').replace(/_on.jpg/,'_off.jpg')});
		
		var popcontainer = $(this).find('.feature-hover:first');
		popcontainer.hide();
		
		
	});
	
	$('.city-hover').hover(function() {
		if(!$(this).hasClass('nohover'))
			$(this).find('img:first').fadeIn();
	},function() {
		if(!$(this).hasClass('nohover'))
			$(this).find('img:first').fadeOut();
	});
	
	$('#DownloadContainer a').click(function() {
		
	});
	
	$('a.positioned-modal').click(function(e) {
		$('.nohover').removeClass('nohover');
		button = $(this).parent();
		button.addClass('nohover');
		button.find('img:first').show();
		e.preventDefault();
		$.get($(this).attr('href'), function(data){
			// create a modal dialog with the data
			$('#PositionedModal').html(data);
			
			$('<div></div>').modal({
				opacity:40,
				onOpen:function(dialog) {
					$('.city-hover').addClass('nohover');
					dialog.overlay.fadeIn(100, function() {
						dialog.container.fadeIn(100,
							function() {
								$('#PositionedModal').fadeIn(100);
							}
						);
					});
				},
				onClose:function() {
					$('.city-hover').removeClass('nohover');
					$('.city-hover img').hide();
					$('#PositionedModal').fadeOut(100);
					$.modal.close();
					button.find('img:first').hide();
				}
			});
			
		});
	});
	
	$('a.standard-modal').click(function(e) {
		e.preventDefault();
		$('.nohover').removeClass('nohover');
		e.preventDefault();
		$.get($(this).attr('href'), function(data){
			// create a modal dialog with the data
			$('#StandardModal').html(data);
			$('#StandardModal').modal({
				opacity:40,
				position:[100,'50%'],
				onOpen: function(dialog) {
					$('.city-hover').addClass('nohover');
					dialog.overlay.fadeIn(100, function() {
						dialog.container.fadeIn(100,
							function() {
								
								// keep our window in order
								$(this).everyTime(100, 'modalpos', updatePosition);
			
								dialog.data.fadeIn(100);
							}
						);
					});
				},
				onClose: function(dialog) {
					$.modal.close();
					$('.city-hover').removeClass('nohover');
					$('.city-hover img').hide();
					$('#StandardModal').hide();
				}
			});
		});
	})
	
				
	initRollovers();
	initGalleryLinks();
	
	$('#GalleryLeft').click(galleryPrev);
	$('#GalleryRight').click(galleryNext);
	
});
