/*

KNOWN BUGS:

- CLICK TO ACTIVATE FLASH IN IE6

*/

var Navy = {};

Navy.root = '';

$(document).ready(function(){
	$('body').addClass('js');
	if ($('body').hasClass('page-Main_Page')) {
		//Navy.iniHomePageFlash();
	} else {
		//Navy.iniImages();
		Navy.iniTooltips();
		Navy.iniTables();
		Navy.iniMaps();
	}
});


// CREATES A div
Navy.createDiv = function(id, html, classes, parent) {
	var div = document.createElement('div'); // CLUMSY: US jQuery INSTEAD? $('<div></div>');
	if (id) div.id = id;
	if (html) div.innerHTML = html;
	if (classes) div.className = classes;
	$(parent).append(div);
	return div;
}

// TEMP: LOOPS THROUGH AN OBJECT
testObj = function(o) { str=''; for (i in o) str+=i+': '+o[i]+'\n'; alert(str); }

// LOOPS THROUGH EACH LOCATION
Navy.iniMaps = function() {
	$('.map #australia li, .map #world li').each(function(){
		Navy.createLocation(this);
	});
	$('.jshide').hide();
}

// CREATES THE TOOLTIPS FOR A LOCATION
Navy.createLocation = function(o) {
	var loc = jQuery.trim($(o).text());
	var content = Navy.createTooltip(o, '<ul></ul>');
	var ul = $(content).prepend('<strong>'+loc+'</strong>').children('ul');
	// RETURNS ALL .box span WITH THE TEXT OF loc INSIDE
	$('.box li a span').filter(':contains('+loc+')').each(function(){
		var link = $(this).hide().parent().clone(); // CLONES THE LINK (BECAUSE WE DON'T WANT span.term AS WELL)
		$(ul).append('<li></li>').children('li:last-child').append(link); // CLUMSY: WHY CAN'T I JUST WRAP IT IN AN li?
	});
	$(o).hover(function(){
		$(this).children('.tooltip').show();
		$(o).addClass('aterm');
	}, function(){
		$(this).children('.tooltip').hide();
		$(o).removeClass('aterm');
	});
}

Navy.iniTables = function() {
	smw_sortables_init(); // CLUMSY: ABSORB INTO THIS SCRIPT
	$('.page-Current_Ships .smwtable td:nth-child(2)').each(function(){
		// DO YOU HAVE TO BREAK OUT OF jQuery FOR THIS?
		var html = this.innerHTML;
		$(this).prepend('<span class="smwsortkey">'+html.substr(html.indexOf(' '))+'</span>');
	});
}

Navy.iniHomePageFlash = function() {
	// YOU CAN SET flashvars OF alignX (left OR right) AND alignY (top OR bottom) TO PIN THE EDGES OF THE IMAGE ON RESIZE (DEFAULT CENTRED)
	$('.feature .image').flash(
		{src:'/w/skins/navy/_swf/feature.swf',
			width:'100%',
			height:'100%',
			flashvars:{picURL:$('.feature img').attr('src')}
		}, 
		{version:8}
	);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TOOLTIPS

// SETS UP AJAX HANDLERS
Navy.iniTooltips = function() {
	Navy.iniLinks();
	Navy.iniRanks();
}

// SETS UP LINKS
Navy.iniLinks = function() {
	$('#content a').each(function(){
		if (this.innerHTML.substr(0,4) != 'HMAS' && this.innerHTML.substr(0,4) != 'NHQ') return; // ONLY SHIPS AND BASES
		Navy.addTooltip(this);
	}).removeAttr('title');
}

// SETS UP RANKS
Navy.iniRanks = function() {
	Navy.addTooltip('acronym.rank');
	//$('acronym.rank').removeAttr('title'); // CLUMSY
}

// WRAPS AN ELEMENT IN span.tooltip
Navy.addTooltip = function(o, id) {
	$(o).wrap('<span></span>').parent().addClass('term').hover(
		function() {
			Navy.showTooltip(this, true);
			$(o).parent('.term').addClass('aterm');
		},
		function() {
			Navy.showTooltip(this, false);
			$(o).parent('.term').removeClass('aterm')
		}
	);
}

// DISPLAYS THE TOOLTIP
Navy.showTooltip = function(o, show) {
	var tooltip = $(o).children('.tooltip');
	if (!show) {
		tooltip.hide();
		return;
	}
	if (tooltip.length == 0) {
		Navy.loadTooltip(o); // LOADS THE HTML ON THE FIRST HOVER
	}
	tooltip.show();
}

// LOADS THE CONTENT
Navy.loadTooltip = function(o) {
	var content = Navy.createTooltip(o, '<p>Loading preview...</p>');
	// FIGURES OUT THE id
	if ($(o).children('a').length > 0) {
		// LINK
		var id = $(o).children('a').attr('href');
		id = id.substr(Navy.root.length);
	} else {
		// ACRONYM
		if (!id) id = '/' + $(o).children('acronym').attr('title');
	}
	// AJAX ORDER: START, SEND, SUCCESS/ERROR, [UPDATE HTML?], COMPLETE, STOP, CALLBACK
	// FIX: REPLACE WHITESPACE WITH UNDERSCORES IN id TO AVOID WIKI CONFUSION
	$(content).load(Navy.root+'/Special:ToolTipExport'+id.replace(' ','_'), false, function(response, status, request) {
		// FIX: USED THIS CALLBACK INSTEAD OF ajaxSuccess BECAUSE OF SCOPING PROBLEMS
		if (status == 'success') {
			$(this).hide().animate({opacity:0}, 1).slideDown('slow');
			$(this).animate({opacity:1}, 500);
		} else {
			// DISPLAYS ERROR TEXT, FADES OUT AND REMOVES THE TOOLTIP SO YOU CAN TRY AGAIN
			$(this).html('<p>Sorry, the preview couldn\'t be loaded.</p>').parent().animate({opacity:1}, 2000).fadeOut(function(){
				$(this).parent('.term').removeClass('aterm');
				$(this).remove();
			});
		}
	}); // LOADS THE URL
}

// CREATES THE div.tooltip
Navy.createTooltip = function(o, html) {
	var tooltip = Navy.createDiv(false, false, 'tooltip', o);
	var content = Navy.createDiv(false, html, 'content', tooltip);
	var bottom = Navy.createDiv(false, false, 'bottom', tooltip);
	return content;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// IMAGES

// SETS UP IMAGE EFFECTS
Navy.iniImages = function() {
	Navy.addImageShadows();
	Navy.addImageRollOver();
}

// ADDS DROP SHADOWS TO IMAGES
Navy.addImageShadows = function() {
	imgs = $('.thumb img').wrap('<div></div>'); // WRAPS img IN div
	imgs.parent().addClass('shad'); // ADDS .shad CLASS TO THE div
	imgs.wrap('<div><div></div></div>'); // WRAPS img IN 2 MORE div
}

// ROLLOVER EFFECTS FOR IMAGES
// BUG: DOESN'T CACHE YET
Navy.addImageRollOver = function() {
	imgs = $('.shad').hover(function() {
		Navy.createBlurb(this, $(this).find('img').attr('alt'));
	}, function() {
		$('#blurb').slideUp('fast');
	});
}

// ADDS THE BLURB TO THE IMAGE
Navy.createBlurb = function(o, str) {
	$('#blurb').remove();
	var w = $(o).find('img').width();
	var blurb = Navy.createDiv('blurb', '<p>'+str+'</p><p><strong>More information</strong><p>', false, o);
	$(o).append(blurb);
	$('#blurb').hide().width(w).slideDown();
}