jQuery(function($) { 
	
	
	$(document).ready(function() {

		
		
		
		//$('#container .box .box-middle').ready(function() 
		$(window).load(function() {
			$('#container .box .resize-me').each(function(n, item) 
			{
				var $item = $(item);
				fix_height($item);
				//setTimeout(function() { fix_height($item); }, 1000)
			});
		});
		function fix_height($box)
		{
			$box.css('height', 'auto');
			var height = Math.ceil($box.height() / 10) * 10;
			$box.height(height);
		}
		
		
		//$().pngFix();
		
		$("#s").clearingInput({text: 'Sök'});
		$("#sidebar #user_login").clearingInput({text: 'Användarnamn'});
		$("#user_pass").clearingInput({text: 'Lösenord'});
		//$("div.registration-form.unsubmitted input.input").clearingInput();
		
	
		$(".comment_author_input").clearingInput({text: 'Namn'});
		$(".comment_email_input").clearingInput({text: 'E-post (visas ej)'});
		$(".comment_text_input").clearingInput({text: 'Kommentar'});
		
		$('div.employees ul.letter-menu li a').click(function() {
			$('div.employees ul.letter-menu li a').removeClass('active');
			$(this).addClass('active');
		});
		
		$(".posten_most_commented_display_options .posten_widget_menu_item").click(function() {
			var menu = $(this);
			$('.posten_most_commented_display_options li').removeClass('active');
			menu.parent().addClass('active');
			if(menu.hasClass('posten_most_commented_menu_today')) {
				$('.posten_most_commented_today').show();
				$('.posten_most_commented_week').hide();
			} else {
				$('.posten_most_commented_today').hide();
				$('.posten_most_commented_week').show();
			}
			return false;
		});
		
		$(".posten_author_display_options .posten_widget_menu_item").click(function() {
			var menu = $(this);
			$('.posten_author_display_options li').removeClass('active');
			menu.parent().addClass('active');
			if(menu.hasClass('posten_author_menu_latest')) {
				$('.posten_author_latest').show();
				$('.posten_author_most').hide();
			} else {
				$('.posten_author_latest').hide();
				$('.posten_author_most').show();
			}
			return false;
		});
		
		$("div.comments-link a").click(function() {
			var id = get_id(this.id);
			var $container = $("#comment-" + id);
			
			var $text = $container.find('span.comment-text:first');
			var temp = $text.text();
			$text.text($text.attr('alt'));
			$text.attr('alt', temp);
			
			$entry = $("#entry-" + id);
			
			var $content = $container.parents('.content.entry').eq(0);
			if($container.hasClass('closed')) {
				
				$container.parents('.entry').css('height', 'auto');
				$entry.addClass("ajax");
				$entry.find(".ajaxcomment .content").load(template_dir + "/ajax-comments.php", {id: id}, function() {
					$container.removeClass('closed').addClass('opened');
					$entry.find(".comment_author_input").clearingInput({text: 'Namn'});
					$entry.find(".comment_email_input").clearingInput({text: 'E-post (visas ej)'});
					$entry.find(".comment_text_input").clearingInput({text: 'Kommentar'});
					fix_height($content);
				});
				
			} else {
				
				$entry.removeClass("ajax");
				$entry.find(".ajaxcomment .content").empty().html(ajaxloader());
				$container.removeClass('opened').addClass('closed');
				fix_height($content);
				
			}
		
			
			
			return false;

		});
		
		
		
	});
	
});


function post_comment(form) {
		
	$ = jQuery;
	var data = $(form).serialize();
	$(form).find("#comment,#author,#email").val("");
	var loader = ajaxloader();
	$('.commentlist').append(loader);
	
	$.ajax({
		url: template_dir + '/ajax-post-form.php', 
		type: 'POST',
		data: data, 
		success: function(response) {
			// wp-workaround
			if(response.indexOf('Hittade') != -1)
				alert(response);
			else {
				$('.commentlist .loader').remove();
				$('.commentlist').append(response);
			}
		},
		error: function(response) {
			alert(response.responseText);
		}
	});
}

function ajaxloader() {
	return '<div class="loader"></div>'	;
}

function get_id(input) {
	return input.split("-").pop();
}