function img404(e,size) {
	e.src='/i/f/no_user_pic_'+size+'.gif';
}

$(function(){
	
	// === Show and hide long part of user profile === //
    
        var $userinfo = $('div.forums-user-info,div.forums-user-about','#forums').wrapAll('<div></div>').parent();
        
        if ($userinfo.length) {   
	        var $profileimg = $userinfo.find('div.forums-user-col1 img')
	        var imgsrc = $profileimg.attr('src').replace(/\/profile\//,"/40/").replace(/no_user_pic_profile/,'no_user_pic_40');
	        var profilename = $('#username_text', $userinfo).text();
	        var $profiletoggle = $('<div class="forums-user-info" style="padding:5px;cursor:pointer;position:relative;"><img src="'+imgsrc+'" width="40" height="40" style="vertical-align:middle;margin-right:10px;" />Click to show more about <strong>'+profilename+'</strong><img src="/i/arrow-se.png" width="16" height="16" style="position:absolute;bottom:0;right:0;"/></div>').click(function(){
	        	$(this).hide();
	        	$userinfo.show();
	        }).insertAfter($userinfo);
	        
	        if ($userinfo.parents('#forums.collapse-profile').length) {
	        	$userinfo.hide();
	        } else {
	        	$profiletoggle.hide();
	        }
	        
	        $userinfo.find('div.forums-user-info').css('position','relative').append($('<img src="/i/arrow-nw.png" width="16" height="16" style="position:absolute;bottom:0;right:0;" title="Minimize profile information"/>').click(function(){
	        	$userinfo.hide();
	        	$profiletoggle.show();
	        }));
	    }
    
		var profile_ps = $('div.forums-user-about p');
		if (profile_ps.length > 1) {
			profile_ps.not(':first').wrapAll('<div class="forums-user-more"></div>');
			$('<span class="forums-user-more-link">&nbsp;<a href="#">Show&nbsp;more...</a></span>')
				.appendTo(profile_ps.filter(':first')).click(function(){
				$('div.forums-user-more').show();
				$(this).hide();
				return false;
			});
		}
	
	
	// === Report post button === //
	
		$('a.report-button').click(function(){
			return confirm("Are you sure you'd like to report this post to the forum moderator?");
		});
	
	
	// === Forums search === //
	
		// Make a copy of the search box element, then delete it
		var searchbox = $('#forum-search-box').clone().click(function(e) {
			// Stop click propagation for detecting click outside the box
			e.stopPropagation();
		});
		$('#forum-search-box').remove();
		
		
		// Add the search box to the span containing its link, and bind click events
		$('#forum-search-link').append(searchbox).children('a').click(function(e){
			e.preventDefault();
			e.stopPropagation();
			var link = $(this);
			var searchbox = link.siblings('#forum-search-box');
			
			if ( searchbox.is(':hidden') ) {
				// Highlight the link
				link.addClass('active');
				// Fade the box in quickly, focus and select text in the search field
				searchbox.show().find('input[name=q]').select();
				// Detect click outside the search box
				$(document).click(function(){
					// Trigger the search close link
					link.removeClass('active');
					searchbox.hide();
					// Remove trigger from document
					$(document).unbind('click');
				});
			} else {
				link.removeClass('active');
				searchbox.hide();
			}
		});
		
		$('#forum-search-focus a').click(function(e){
			e.preventDefault();
			$('input[name=q]').select();
		});
		
		$('#forum-search-hilite').click(function(e){
			e.preventDefault();
			var speed = 300;
			$('#forum-search-link').fadeOut(speed, function(){
				$(this).fadeIn(speed, function(){
					$(this).fadeOut(speed, function(){
						$(this).fadeIn(speed, function(){
							$(this).fadeOut(speed, function(){
								$(this).fadeIn(speed);
							});
						});
					});
				});
			});
		});
});
