
pannier = new Pannier(jQuery);
jQuery(document).ready(pannier.init);

log = pannier.log;

function Pannier($) {
	verboseLog = false;
	
	this.init = init;
	function init() {
		$('body').addClass('js-enabled');
		setupHeadings();
		setupNav();
		setupTranslate();
	}
	
	this.log = log;
	function log(msg) {
		if (typeof console == 'undefined' || typeof console.log == 'undefined') {
			if (verboseLog)
				alert(msg);
		} else {
			console.log(msg);
		}
	}
	
	this.pseudoHover = pseudoHover;
	function pseudoHover(els, cl) {
		var $els = $(els);
		cl = (typeof cl == 'undefined') ? 'hover' : cl;
		$els.each(function(i, el) {
			var $el = $(el);
			$el.hover(function() {
				$el.addClass(cl);
			}, function() {
				$el.removeClass(cl);
			});
		});
	}
	
	function setupHeadings() {
		if (!$('body').hasClass('home')) {
			$('#main-content h1').each(function(i, el) {
				var $el = $(el);
				$el.html('<span class="i">' + $el.html() + '</span>');
			});
		}
	}
	
	function setupNav() {
		var d = 500; // duration of effects (ms)
		pseudoHover('#nav > li');
		$('#nav .subnav').hide();
		$('#nav > li').hover(function () {
			$(this).find('.subnav').addClass('active').stop().removeAttr('style').hide().slideDown(d);
		}, function() {
			$(this).find('.subnav').removeClass('active').stop().slideUp(d, function() {
				$(this).hide().removeAttr('style');
			});
		});
	}
	
	function setupTranslate() {
		$translate = $('#translate');
		$panel = $translate.find('div.panel');
		$translate.find('a.trigger').click(function(e) {
			e.preventDefault();
			if ($panel.hasClass('hide')) {
				// panel is hidden, so show it
				$panel.hide().removeClass('hide').slideDown(150);
			} else {
				// panel is showing, so hide it
				$panel.slideUp(150, function() {
					$panel.addClass('hide').show();
				});
			}
			$(this).blur();
		});
		
		// clicking on the document also hides the panel
		$(document).click(function(e) {
			if (!$panel.hasClass('hide')) {
				// panel is showing, so hide it
				$panel.slideUp(150, function() {
					$panel.addClass('hide').show();
				});
			}
		});
		
		// clicking on the panel shouldn't hide it at the document level (above)
		$translate.click(function(e) {
			e.stopPropagation();
		});
		
	}
	
}






