$(document).ready(function() {
	
	$('#qtrans_language_chooser').attr('id', 'lang');
	$('#lang li.active a').addClass('sel');
	$('.post-container, #blog-header, .icon-bar, .tabs, .paging, .participate').addClass('clear');
	$('#feed').prependTo('#lang').wrap('<li class="rss-icon"></li>');
	$('.widget a img:odd').addClass('last');	
	$('.tabbed').tabs();

	if ( current_language == 'de' ) { $('.widget h4:contains(Archive)').text('Archiv'); }


	// $('.clear').append('<div style="clear:both;"></div>');

	//--------------------------------------------------------

	// Fix the de language search
	if ( current_language == 'de' )
	{
		var action = $('#searchform').attr('action');
		var new_action = action + current_language + '/';
		$('#searchform').attr('action', new_action);
	}
	
	//--------------------------------------------------------
	
	// Lightbox effect

	$.superbox.settings = {
		boxWidth: "640", // Default width of the box
		boxHeight: "480", // Default height of the box
		closeTxt: "x",
		loadTxt: "",
		nextTxt: "&rarr;",
		prevTxt: "&larr;",
		overlayOpacity: .3
	};
	$.superbox();


	
	//--------------------------------------------------------

	// Make photo caption flickr URLs into links
	var caption = $('.caption').text();
	
	if ( caption.match("flickr") )
	{
		$('.caption').html('<a href="' + caption + '">Credit</a>');		
	}
	
	//--------------------------------------------------------

	// Add classes for downloads			
	$('a[href$=".pdf"]').addClass('pdf');
	
	
	//--------------------------------------------------------
	
	// Convert mp3 link to flash player
	$('a[href$=".mp3"]').each( function(i) {
		
		$(this).attr('id', 'audioplayer-' + i);
		
		var file = $(this).attr('href');
		
		// console.log(file);
		
    AudioPlayer.embed("audioplayer-" + i, {soundFile: file});  

		// return false;

	});
	
	
	//--------------------------------------------------------

	// Add classes for input types (for non CSS3 browsers)			
	$('input[type="hidden"]').addClass('hidden');
	$('input[type="submit"]').addClass('submit');
	
	
	//--------------------------------------------------------
	
	// Add flags to comments
	if ( typeof(json_commentsgeo) !== 'undefined' )
	{
		for ( i in json_commentsgeo )
		{
			var comment_id = '#comment-' + json_commentsgeo[i]['comment_ID'];	
			var country_code = json_commentsgeo[i]['comment_author_country_code'].toUpperCase();
			(country_code == "") ? country_code = "Unknown" : country_code = country_code;			
			var flag_path = json_commentsgeo[i]['comment_author_country_flag_path'];
			var flag_img = '<img class="comment-flag" title="' + country_code + '" src="' + template_url + '/' + flag_path + '" />';

			$(comment_id + ' .fn').after( flag_img );
		}
	}

	//--------------------------------------------------------

	// Show/hide the subscribe by email form

	var email_form = $('.widget_subscribe form'); 
	var email_submit = $('.widget_subscribe .submit');
	var email_link = $('.widget_subscribe .email');

	// Hide the form when the page loads
	email_form.hide();
	
	// Show the form when the email link is clicked
	email_link.click( function () {
		email_link.toggle();
		email_form.toggle();
		return false;
	});
	
	// Hide the form when the form submit is clicked
	email_submit.click( function () {
		email_link.toggle();
		email_form.toggle();
	});
		
	// Clear/reset the email input on focus/blur	
	$('.widget_subscribe input[name="email"]').focus( function () {
		$(this).val("");
	})
		.blur( function () {
		if ($(this).val() == "") {
			$(this).val($(this)[0].defaultValue);
		};
	});


	//--------------------------------------------------------

	// schedule.php

	$('.results tr.day:first').addClass('first');

	$('.results .description').hide();
	
	$('.results .day-description:first').show();
	
	
	$('.results .toggle-description').click(function(event) {
		
		$(this).parent().parent().toggleClass('active');
		// console.log( $(this).parent().parent().next('tr.description').text() )
		
		$(this).parent().parent().next('tr.description').toggle();

		return false;

	});

		
	$('.schedule #datepicker').datepicker({ 
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		minDate: new Date(2009, 5, 17),
		maxDate: new Date(2009, 8, 8),
		onSelect: function(date, instance) 
		{
			 window.location.href = "?date_range=" + date;
		}
	});
	
	if ( current_language == 'de')
	{
		$('.schedule #datepicker').datepicker('option', 'dayNamesMin', ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa']);	
		$('.schedule #datepicker').datepicker('option', 'monthNames', ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']);		
	}
	
	
	$('.schedule #datepicker').hide();
	
	$('.schedule #show-datepicker').click(function(event) {
		$('.schedule #datepicker').toggle();
		return false;
	});
	
	$('a.print').click( function() { window.print(); });
	

});