$(document).ready(function() {
	// page options
	if ($('.glossary.popup').length > 0) {
		$('#nav-left').append('<div class="ex-left" id="page_opt"><h2 class="sectiontitle">Page Options</h2><ul><li><a href="" id="hover_glossary">disable hover glossary</a></li></ul></div>');
	}
	// actions
	$('#page_opt a#hover_glossary').click(function() {
		var text = $(this).text();
		if (text == 'enable hover glossary') {
			$(this).text('disable hover glossary'); // action: enable
			$('a.glossary').addClass('popup');
			$('a.glossary span.hide').toggleClass('hoverdef').toggleClass('hide');
		} else {
			$(this).text('enable hover glossary'); // action: disable
			$('a.glossary').removeClass('popup');			
			$('a.glossary span.hoverdef').toggleClass('hide').toggleClass('hoverdef');
		}
		return false;
	});

	// hover glossary
	$('a.glossary.popup').hover(function() {
		if (!$(this).hasClass('popup')) {
			return false;
		}
			var span = $(this).find('span.hoverdef');
			if (span.text() == '' || span.text() == 'retrieving entry...') {
				span.html('retrieving entry...');
				var html=$(this).attr('href');
				$.get(html+'/ajax',function(data) {
					$('a.glossary').each(function() {
						if ($(this).attr('href') == html) {
							$(this).find('span.hoverdef').html(data);
							if (data.indexOf('Entry too long') != -1) {
								$(this).find('span.hoverdef').addClass('unfound');
							}
						}
					});
				});
			}
	});
	
	$('#pagenav').removeClass('hide');
	$('#pagenav a').click(function() {
		var selection = $(this).attr('rel');
		structurize(selection);
		datafill(selection);			
		
		// button appearance
		$('#pagenav a').removeClass('sel');
		$(this).addClass('sel');
		return false;	
	});
		
	// AJAX: change type
	$('a.changetype').click(function() {
		var href = $(this).attr('href');
		$.post(href);
		$(this).remove();
		return false;	
	});
	
	// expand titles
	$('.mainlevel-drop li').hover(
	function() {
		var abbrev = $(this).find('a.mainlevel-drop span.expanded');
		if ( !abbrev.data('full') ) { // make sure we're not already expanded
			var new_abbrev = expandtitle( $(this).find('a.mainlevel-drop span.expanded').html() );
			abbrev.html(new_abbrev);
			$(this).find('a.sublevel-drop').css({'width':'80px'});
		}
		abbrev.data('full',true);
	},
	function() {
		var abbrev = $(this).find('a.mainlevel-drop span.expanded');
		if ( abbrev.data('full') ) { // make sure we're not already compressed
			var new_abbrev = compresstitle( $(this).find('a.mainlevel-drop span.expanded').html() );
			abbrev.html(new_abbrev);
//				$(this).find('a.sublevel-drop').css({'padding-right':'auto'});				
		}
		abbrev.data('full',false);
	}); 	
	
}); // document

// fill structures with data
function datafill(selection) {
	// put in content
	for (i=0;i<entries.length;i++) {
		$('#left_'+entries[i][selection]+' ul.keyword').append('<li><a href="#'+entries[i]['a_name']+'">'+entries[i]['entry_name']+'</a></li>'); // left nav
		$('#section_'+entries[i][selection]).append(entries[i]['entry']); // body
	}
	
	// expand titles
	$('.mainlevel-drop li').hover(
	function() {
		var abbrev = $(this).find('a.mainlevel-drop span.expanded');
		if ( !abbrev.data('full') ) { // make sure we're not already expanded
			var new_abbrev = expandtitle( $(this).find('a.mainlevel-drop span.expanded').html() );
			abbrev.html(new_abbrev);
			$(this).find('a.sublevel-drop').css({'width':'80px'});
		}
		abbrev.data('full',true);
	},
	function() {
		var abbrev = $(this).find('a.mainlevel-drop span.expanded');
		if ( abbrev.data('full') ) { // make sure we're not already compressed
			var new_abbrev = compresstitle( $(this).find('a.mainlevel-drop span.expanded').html() );
			abbrev.html(new_abbrev);
//				$(this).find('a.sublevel-drop').css({'padding-right':'auto'});				
		}
		abbrev.data('full',false);
	}); 
}
	
function expandtitle(html) {
	if (!html) {
		return;
	}
	if (html.indexOf('Mirage') != -1) {	 // mirage
		html = html.replace(/gun$/,'Gunjou').replace(/ex$/,'Exaudi Nos').replace(/hata$/,'Shinku no Hata wo Hirugaese').replace(/sa$/,'Saiai no Anata e');
		return html.replace(/Mirage/,'Mirage of Blaze');
	} else if (html.indexOf('KKM') != -1) { // kkm
		return html.replace(/KKM/,'Kyou Kara Maou');
	} else {
		return html;
	}
}
function compresstitle(html) {
	if (!html) {
		return;
	}	
	if (html.indexOf('Mirage of Blaze') != -1) {	 // mirage
		html = html.replace(/Gunjou/,'gun').replace(/Exaudi Nos/,'ex').replace(/Shinku no Hata wo Hirugaese/,'hata').replace(/Saiai no Anata e/,'sa');
		return html.replace(/Mirage of Blaze/,'Mirage');
	} else if (html.indexOf('Kyou Kara Maou') != -1) { // kkm
		return html.replace(/Kyou Kara Maou/,'KKM');
	} else {
		return html;
	}
}			