$( document ).ready( function() {
	$( 'a' ).bind( 'focus', function() { this.blur(); } );
	
	$( '#forum_index tr.board' ).addClass( 'clickable' ).bind( 'click', function( event ) {
		if( event.target.tagName == 'A' ) return true;
		else
		{
			document.location = $( this ).find( 'h6 a' ).attr( 'href' );
			return false;
		}
	} );
	
	$( 'form h3' ).each( function() {
		var h3 = $( this );
		var dl = h3.next();
		
		h3.addClass( 'imploded' );
		dl.hide();
		
		h3.bind( 'click', function() {
			if( h3.hasClass( 'imploded' ) )
			{
				h3.removeClass( 'imploded' );
				dl.show();
			}
			else
			{
				h3.addClass( 'imploded' );
				dl.hide();
			}
		} );
	} );
	
	$( '#search_results form dl, #search_results form input.submit' ).hide();
	$( '#search_results form h2' ).addClass( 'imploded' ).one( 'click', function() {
		$( this ).removeClass( 'imploded' );
		$( '#search_results form dl' ).slideDown( 'fast' );
		$( '#search_results form input.submit' ).fadeIn( 'slow' );
	} );
	
	$( 'blockquote:visible blockquote:hidden' ).each( function() {
		$( this ).before( '<label for="showquote" class="hiddenquote">quote hidden</label>' );
	} );
	
	$( 'label[for=showquote]' ).click( function() {
		$( this ).next().slideDown().end().slideUp( 'fast' );
	} );
	
	$( 'label.pagination' ).click( function() {
		var url = /(.*)\/pages:(\d+)-(\d+)/.exec( $( this ).attr( 'for' ) );
		var title = $( this ).attr( 'title' );
		var from = parseInt( url[2] );
		var to = parseInt( url[3] );
		url = url[1];
		
		link = function( u, t, i ) {
			return '<a href="' + u + '/page:' + i + '" title="' + t + '">' + i + '</a>';
		}
		
		$( this ).html( link( url, title, from ) );
		for( var i = from+1 ; i <= to ; i++ )
			$( this ).append( ', ' + link( url, title, i ) );
		$( this ).replaceWith( $( this ).html() );
	} );
	
	var suggesting = false;
	$( 'form' ).bind( 'submit', function( e ) {
		return !suggesting;
	} );
	$( 'input.suggest' )
		.attr( 'autocomplete', 'off' )
		.each( function() {
			$( this ).data( 'value', $( this ).val() );
		} )
		.after( '<ul class="suggest"></ul>' )
		.bind( 'keyup', function( e ) {
			if( e.keyCode == 38 )
			{
				var cur = $( this ).next().find( '.selected' );
				if( cur.prev().length )
					cur.removeClass( 'selected' ).prev().addClass( 'selected' );
			}
			else if( e.keyCode == 40 )
			{
				var cur = $( this ).next().find( '.selected' );
				if( cur.next().length )
					cur.removeClass( 'selected' ).next().addClass( 'selected' );
			}
			else if( e.keyCode == 13 )
			{
				var cur = $( this ).next().find( '.selected' ).find( 'label' );
				$( this )
					.val( cur.html() )
					.prev().val( cur.attr( 'for' ) );
				$( this ).blur();
				return;
			}
			else if( $( this ).val() != $( this ).data( 'value' ) )
			{
				$( this ).data( 'value', $( this ).val() );
				$( this ).prev().val( '' );
				var ul = $( this ).next();
				$.get( 'ajax/user_suggest.php', {
					search: $( this ).val()
				}, function( data ) {
					ul.empty();
					data = data.split( '\n' );
					var first = true;
					for( var i in data )
					{
						if( data[i] )
						{
							var result = data[i].split( ':', 2 );
							ul.append( '<li' + (first?' class="selected"':'') + '><label for="' + result[0] + '">' + result[1] + '</label></li>' );
							first = false;
						}
					}
				} );
			}
		} )
		.bind( 'focus', function() {
			suggesting = true;
			$( this ).removeClass( 'err' );
			$( this ).next().slideDown();
		} )
		.bind( 'blur', function() {
			suggesting = false;
			var text = $( this ).val();
			var label = $( this ).next()
				.slideUp( 'fast' )
				.find( 'label' ).filter( function() { return $( this ).html() == text; } )[0];
			
			if( label )
				$( this ).prev().val( $( label ).attr( 'for' ) );
			
			if( $( this ).hasClass( 'strict' ) && !$( this ).prev().val() )
				$( this ).addClass( 'err' );
		} )
		.next().bind( 'click', function( e ) {
			if( e.target.tagName == 'LI' )
				e.target = $( e.target ).find( 'label' )[0];
			if( e.target.tagName == 'LABEL' )
			{
				$( this ).prev().prev().val( $( e.target ).attr( 'for' ) );
				$( this ).prev().val( $( e.target ).html() );
			}
		} );
	;
	
	$( '#report select[name=reason]' ).change( function() {
		if( $( this ).val() == 'other' )
		{
			$( this ).replaceWith( '<input type="text" name="reason" />' );
			$( '#report input[name=reason]' ).focus();
		}
	} );
} );
