var Toyota = {
	language: null,
	
	initLanguage: function()
	{
		var $matches = /^\/(en|fr)\//.exec(window.location.pathname),
			$url = window.location.toString()
			;
			
		if ($matches && $matches.length)
		{
			Toyota.language = $matches[1].toLowerCase();
			
			// manually configure language toggle link to point to the same page
			$("#language_toggle a").attr("href", $url.replace(
				'/' + Toyota.language,
				'/' + (Toyota.language == 'en' ? 'fr' : 'en')
				));
		}
	},
	
	initVotes: function()
	{
		$(".votes").each(function()
		{
			Toyota.setVotesHtml(this);
		});
	},
	
	setVotesHtml: function(target, bump)
	{
		target = $(target);
		
		if (target.length > 1)
		{
			target.each(function() { Toyota.setVotesHtml(this, bump); });
			return;
		}
		
		var count = target.find("span").text(),
			label = target.find("strong").text(),
			html = ''
			;
		
		if (bump === true)
			count = (parseInt(count) + 1).toString();
			
		$(count.split('')).each(function()
		{
			html += '<span class="digit">' + this + '</span>';
		});
		
		if (label)
			html += '<strong class="label">' + label + '</strong>';
		
		target.html(html);
	},
	
	initShinyButtons: function()
	{
		$(".button_shiny").each(function()
		{
			$(this)
				.wrapInner('<span class="label"><span></span></span>')
				.append('<span class="right"></span>')
				;
		});
	},
	
	initLayout: function()
	{
		Toyota.initShinyButtons();
		
		$(".button_shiny.vote").bind("click", function(e)
		{
			e.preventDefault();
			
			var a = $(this);
			
			// loading class prevents rapid double clicking
			if (a.hasClass('voted') || a.hasClass('loading'))
				return;
			
			var id = a.attr('href').substr(1);
			
			a.addClass('loading');
			
			$.ajax({
				url: Toyota.url('/contest/entries/vote/' + id),
				success: function(response)
				{
					var parent = a.parent(),
						replacement = $('<a href="#" class="button_shiny brown checkbox vote voted">' + response + '</a>')
						;
					
					// replace the anchor
					a.replaceWith(replacement);
					// set dummy click handler
					replacement.bind("click", function(e)
					{
						e.preventDefault();
					});
					// update shiny button to have proper markup
					Toyota.initShinyButtons();
					// fix IE6 png if available
					if (parent.pngFix)
						parent.pngFix();
						
					// update the count on the screen
					Toyota.setVotesHtml($('.entry' + id + ' .votes'), true /* will bump count by one */);
				}
			})
		});
	},
	
	url: function(url)
	{
		return '/' + Toyota.language + url;
	},
	
	redirect: function(url)
	{
		window.location = Toyota.url(url);
	},
	
	autoTabIndex: function(form)
	{
		$(form)
			.find('input, select, textarea')
			.each(function(index, element)
			{
				$(element).attr('tabindex', index + 1); // zero tabindex takes to the browser controls in FF
			});
	},
	
	initNotice: function()
	{
		var notice = $("#notice");
		
		notice.find(".close").click(function(e)
		{
			e.preventDefault();
			notice.slideUp();
			$.get('/contest/entries/close_notice');
		});
	}
};

$(document).ready(function()
{
	Toyota.initLanguage();
	Toyota.initVotes();
	Toyota.initLayout();
});
