/* Compiled JS */

/* i18n.js */

var i18n = i18n ? i18n : (function()
{
	var format = function(input, replacements)
	{
		var output = new String(input);
		for (var i = 1; i <= replacements.length; i++)
		{
			output = output.replace(new RegExp('(\\$' + i + ')', 'g'), '' + replacements[i - 1]);
		};
		return output;
	}
	
	return {
		text: {},
		
		gettext : function(raw) 
		{
			message = new String(raw);
			message.format = function()
			{
				return format(raw, arguments);
			}
			return message;
		},
		
		ngettext : function(message1, messagen) 
		{
			message = new String(message1);
			message.format = function()
			{
				if (arguments.length == 0 || arguments[0] == 1)
				{
					return format(message1, arguments);		
				}
				else
				{
					return format(messagen, arguments);		
				}
			};
			return message;
		}
	}
})();


/* tabs.js */

$(document).ready(function(){			
	$('.tabs').each(function()
	{
		var container = $(this);
		var tabs = [];
		
		var showTab = function(name)
		{	
			//Switching to actual tab?
			var valid = false;
			for (var i = 0; i < tabs.length; i++)
			{
				if (tabs[i] == name)
				{
					valid = true;
				}
			}
			
			if (valid)
			{
				var tab = container.find('a[href=#' + name + ']');
				var panel = container.find('#' + name + 'Panel');
					
				//Is state actually changing?
				if (!tab.hasClass('current'))
				{
					var content = container.find('.content');
					var oldHeight = content.height();
					
					container.find('.content .current').hide();
					container.find('.options .current').removeClass('current');
										
					tab.addClass('current');
					panel.addClass('current').show();
					var newHeight = content.height();
					
					content.
						css('height', oldHeight + 'px').
						css('overflow', 'hidden');
					
					content.
						animate(
							{height: newHeight},
							'medium', 
							function() {
								content.
									css('overflow', 'auto').
									css('overflow-x', 'hidden').
									css('height', 'auto');
							});
					
				}	
			}
		};
		
		//Get all tabs, store names and apply events.
		container.find('.options a').each(function(){
			var name = this.href.substr(this.href.indexOf('#') + 1);
			tabs.push(name);
			$(this).click(function(){
				showTab(name);
			});
		});
		
		if (document.location.hash != '')
		{
			showTab(document.location.hash.substr(1));
		}
		
		$(document).bind('hashchange', function(){
			var name = document.location.hash != '' ? document.location.hash.substr(1) : '';
			showTab(name);
		});
	});
});

/* captcha.js */

$(document).ready(function(){
	
	var hideAudio = function()
	{
		var audio = $(this).parent().parent().find('.captchaAudio');
		if ($.browser.msie)
		{
			audio.find('object').each(function() {
				this.controls.stop();
			});
		}
		audio.remove();
	}
	
	$('.captchaReload').click(function()
	{
		hideAudio.call(this);
		$(this).parent().parent().
			find('.captchaImage').
			show().
			attr('src', this.href + '?t=' + new Date().valueOf());
		return false;
	});
	
	$('.captchaPlay').click(function()
	{
		hideAudio.call(this);
		
		var code = [];
		if ($.browser.msie)
		{
			code = [
				'<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" height="45" src="', this.href, '" type="application/x-oleobject">',
					'<param name="url" value="', this.href, '">',
					'<param name="autostart" value="true">',
					'<param name="uimode" value="mini">', 
				'</object>'];
		}
		else
		{
			code = ['<embed height="45" autostart="true" bgcolor="white" src="', this.href, '" type="audio/x-wav" />'];
		}
		
		var base = $(this).parent().parent();
		base.find('.captchaAudio').remove();
		base.find('.captchaImage').
			hide().
			after('<div class="captchaAudio">' + code.join("") + '</div>');
			
		if ($.browser.msie)
		{
			//Dynamically inserted objects get their size reset on play state change.
			var obj = $('.captchaAudio object').get(0);
			window.setInterval(function(){
				obj.uiMode = 'mini';
				obj.style.height = '45px';
				obj.style.width = '182px';
			}, 20);
		}
		
		return false;
	});
});

/* site.js */

$(document).ready(function()
{
	//Attach global events herer (only if necessary);
	window.FileFactory = {}
		
	$('#beta a[href=#hide]').click(function(){
		alert(i18n.text.beta.hide);
		$('#beta').slideUp();
		$.cookie('beta', 2, {
			expires: 60, 
			path: '/', 
			domain: '.' + document.location.host.split('.').slice(-2).join('.')
		});
		return false;
	});
	
	//Make error and flash messages fade in to grab attention
	var wobble = function(direction)
	{
		direction = direction == -1 ? -1 : 1;
		$(this).
			css('position', 'relative').
			css('z-index', '-1').
			animate({left: (direction * 50)  + 'px'}, 200).
			animate({left: (direction * -20) + 'px', opacity: 1}, 300).
			animate({left: (direction * 10)  + 'px'}, 150).
			animate({left: '0px'}, 70, 'swing', function(){
				$(this).css('position', 'static');
			});
	}
		
	if ($('#content').children().length > 0)
	{
		$('#errorMessage, #loginMessage, #flashMessage').
			css('opacity', '0').
			css('left', '-50px').
			each(function(){
				$(this).
					css('opacity', null).
					css('filter', null).
					css('position', 'static');
				
				try {
					$(this).css('z-index', 'auto');
				}
				catch(e)
				{
					
				}
				
				var elem = this;
				var enabled, mouseX, mouseY;
				wobble.call(this);
				var message = $(this);
				
				message.
					mousedown(function(e) {
						enabled = e.target.tagName.toLowerCase() != 'a';
						mouseX = e.clientX;
						mouseY = e.clientY;
					}).
					mouseup(function(e) {
						if (enabled && (Math.abs(e.clientX - mouseX) + Math.abs(e.clientY - mouseY)) > 200)
						{
							wobble.call(elem, e.clientX > mouseX ? 1 : -1);
						}
					}).
					find('.message').
						prepend($('<a href="#close" class="hideMessage"></a>').
							click(function(){
								message.slideUp();
							})
						);
			});
	}

	//Make form elements show their active state
	$('form.fancy > ul > li.active *')
		.live('blur', function(e){ 
			$('.fancy .active').removeClass('active');
			return false;
		});
		
	var formTags = {textarea:1, input:1, select:1, button:1};
		
	$('form.fancy li *')
		.live('focus', 
			function(e){
				if (true || e.target.tagName.toLowerCase() in formTags)
				{
					$('.fancy .active')
						.removeClass('active');
						
					var ancestor = null;
					var parents = $(e.target)
						.parents()
						.each(function(elem){
							if ($(this).is('li'))
							{
								ancestor = this;
							}
						})
				
					if (ancestor)
					{
						$(ancestor).addClass('active');		
					}
				}
			});
			
	//Navigation login
	var email = $('#quicklogin input[name=email]');
	var password = $('#quicklogin input[name=password]');		
	
	if (email.length && password.length)
	{	
		var emailHint = email.attr('title');
		var passwordHint = password.attr('title');
		
		//Show and hide hints in email and password fields of login
		var emailBlur = function(){
			if (email.val() == '' || email.val() == emailHint) 
			{
				email.addClass('hinting').val(emailHint);
			}
			else
			{
				email.removeClass('Hinting');
			}
		};	
		var emailFocus = function(){
			if (email.val() == emailHint) email.val('');
			email.removeClass('hinting');
		};
		
		email.change(emailBlur).focus(emailFocus).blur(emailBlur);
		emailBlur();
		
		var passwordBlur = function(){
			if (password.val() == '' || password.val() == passwordHint)
			{
				password.val('');
				password.addClass('hinting');
				var passwordType = password.get(0).type;
				if (passwordType) passwordType = 'text';
				password.val(passwordHint);
			}
			else
			{
				password.removeClass('Hinting');
			}
		};	
		var passwordFocus = function(){
			if (password.val() == passwordHint) password.val('');
			password.removeClass('hinting');
			password.get(0).type = 'password';
		};
		
		password.change(passwordBlur).focus(passwordFocus).blur(passwordBlur);
		passwordBlur();
	}
		
	
	//Make "login" link a submit button
	$('#quicksubmit').click(function(){
		emailFocus();
		passwordFocus();		
		$('#quicklogin form').get(0).submit();
		return false;
	});

	// Fix login w/ enter behaviour in IE
	$('#quicklogin input').live("keydown", function(e){
		if (e.keyCode == 13) {
			e.preventDefault();
			$("#quicklogin").find("form").submit();
		}
	});
	
	//Markdown preview areas
	$("#previewBody").click(function() {
		$("#previewContainer").hide();
		$("#editContainer").show();
		$("#editContainer textarea").focus();
	});
	
	$("a[href=#preview]").click(function() {
		// Show preview
		if ($("#editContainer").css("display") != 'none')
		{
			var value = $("#editContainer textarea").val();
			
			$.ajax({
				type: "POST",
				url: "/markdown.json",
				data: "text=" + (value ? value : ''),
				dataType: "json",
				success: function(json){
					$("#editContainer").hide();
					$("#previewBody").html(json.preview);
					$("#previewContainer").show();
				},
				error: function(){
					alert(il8n.text.shared.cannotPreview);
				}
			});
		}
		// Show textarea
		else
		{
			$("#previewContainer").hide();
			$("#editContainer").show();
		}
		return false;
	});
	
	$('#navigation .sub').each(function(){
		var sub = $(this);
		var parent = $(this).parent();	
		var timeout = null;
		
		//Switch from using 'visibility' for toggling to 'display' 
		sub.css('display', sub.css('visibility') == 'visible' ? 'block' : 'none').
			css('visibility', 'visible');
		
		//Add a container inlined to keep the right alignment'
		var container = $('<div></div>').
			css('display', 'inline').
			css('position', 'absolute');
		
		//Add container and move sub inside it.
		parent.append(container);
		sub.remove();
		container.append(sub);
				
		parent.
			bind('mouseenter', function(e){				
				window.clearTimeout(timeout);
				sub.slideDown('fast');
			}).
			bind('mouseleave', function(e){
				window.clearTimeout(timeout);
				timeout = window.setTimeout(function(){
					sub.slideUp('fast');
				}, 500);
			});
	});
});


/* magic.js */

$(document).ready(function()
{
	/*
	 * Magic classes can be used anywhere on the site:
	 *
	 * toggle 			Toggle the target (href). E.g. <a href="#options" class="toggle"> will toggle element <div id="options">
	 * autoselect		Select all input text on click. E.g. <textarea class="autoselect" />
	 * autofocus		Focus on field on page load. <input class="autofocus">
	 */
	 
			
	//Assign focus to any field that has class "autofocus"
	var autofocus = $('.autofocus').get(0);
	if (autofocus)
	{
		autofocus.focus();
	}
	
	//Auto-select on click
	$('.autoselect').
		focus(function(){
			this.select();
		});
	
	
	//Take any link with class 'toggle' and toggle it's target.
	$('.toggle').click(function(){
		if (this.href && this.href.indexOf('#'))
		{
			var id = this.href.substr(this.href.indexOf('#'));
			if (id)
			{
				$(id).slideToggle();
			}
		}
		return false;
	});
});

