function img404(e,size) {
	e.src='http://d3f650ayx9w00n.cloudfront.net/lib/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;
			});
		}
	
	
	// === 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);
							});
						});
					});
				});
			});
		});
		
	// === Simpleweb === //
		
	// === Display the time left to edit your posts === //
	window.setInterval(updateEditTime, 1000);
	var serverTime = $(".serverTime").html();
	var curTime = Math.round(new Date().getTime() / 1000);
	var serverDiff = curTime - serverTime;
	var timeOut = 5*60;
	
	function updateEditTime() 
	{ 
		$(".forums-post").each(function()
		{
			var time = Math.round(new Date().getTime() / 1000);
			var now = time - serverDiff;
			var secondsLeft = ($("span.unixTime",this).html()) - (now - timeOut);
			if (secondsLeft > timeOut) { secondsLeft = timeOut; }
			var str = '';
			if (secondsLeft < 1) {
				if ($("a.edit-post-user",this).is(":visible")) {
					$("#cancelButton",this).click();
					$("a.edit-post-user",this).hide();
				}
				return true;
			} else if (secondsLeft < 60) {
				str = secondsLeft + " seconds";
			} else {
				str = Math.ceil(secondsLeft/60) + " minutes";
			} 
			$("span.timeLeft",this).html(str);
		});
	}
	updateEditTime();

});

$(document).ready(function(){
	if ($.fn.autocomplete) {
		$("#pname").autocomplete({
			source: "/toybox/",
			select:function(event,ui) {
				$("#pid").val(ui.item.id);
				$("#pname").val(ui.item.name);
			}
		});
	}
});

