
$(
	function () {
		$( "button:not(.parsed)" ).each(
			function () {
				$( this ).addClass( 'parsed' );
				if ( $( this ).children( 'img' ).size() )
					$( this ).html( '<b>' + $( this ).html() + '</b>' );
//				alert( "w:" + $( this ).width() + " h:" + $( this ).height() );
				var wraperElem1, wraperElem2;
				$( this ).before( wraperElem1 = $( '<div class="b1" style="width:'+ ( $( this ).width() + 28 + 32 ) +'px;" />' ) );
				//.append( wraperElem1 = $( '<div class="b3" />' ) ) );
				wraperElem1
					.append( '<div class="b2"><div>&nbsp;</div></div>' )
					.append( $( '<div class="b5"></div>' ) )
					.append( $( '<div class="b3"></div>' ) );
				wraperElem1.append( this ).append( '<div class="b4"><div>&nbsp;</div></div>' );
				
			}
		);
		
		$( "table.auto-fix-zebra tr:even" ).each(
			function () {
				$( this ).addClass( 'odd-row' );
			}
		);
		
		$( "textarea" ).autoResize();
		
		$( "input[type='checkbox'].bulk-check-action," ).each(
			function () {
				$( this ).click(
					function () {
						$( "input[name='" + $( this ).attr( "value" ) + "\[\]'],input[value='" + $( this ).attr( "value" ) + "'].bulk-check-action" ).attr( "checked", $( this ).attr( "checked" ) );
					}
				);
			}
		);
//		var rowHighlightFunc = function () {
//			$( this ).parent().parent().find( 'tr' ).removeClass( 'hovered' );
//			$( this ).parent().addClass( 'hovered' );
//		}
//		$( "table.topics-list td" ).mouseover( rowHighlightFunc );
	}
);

var recentUpdater = {
		lastMessageId : 0,
		tickTime : 30000,
		wasRemovedInPrevTick : 0, 
		timer : null,
		container : null,
		init : function () {
			clearTimeout( recentUpdater.timer );
			recentUpdater.container = $( "#newMessages > .slider" );
			recentUpdater.timer = setTimeout( recentUpdater.tick, recentUpdater.tickTime );
		},
		tick : function () {
			clearTimeout( recentUpdater.timer );
			
			var
				sliderWidth = recentUpdater.container.innerWidth(),
				itemsCount = recentUpdater.container.find( ">div" ).size();
			
			$.ajax(
				'/index.php?action=quickrecent;json;msg_id=' + recentUpdater.lastMessageId + ';sliderWidth=' + sliderWidth + ';itemsCount=' + itemsCount + ';wasRemovedInPrevTick=' + recentUpdater.wasRemovedInPrevTick,
				{
					"type" : "get",
						"complete" : function ( jqXHR, textStatus ) {
							if ( textStatus == 'success' )
								return;
							recentUpdater.timer = setTimeout( recentUpdater.tick, recentUpdater.tickTime );  
						},
						"success" : function ( value ) {
							var n = $( value.posts ).css( { 'opacity' : 0, 'width' : '0px' } );
							n.insertBefore( recentUpdater.container.find( '> :first-child' ) );
							n.each( function() {
								$( this ).animate(
									{ 'width' : $( this ).find( ">div" ).innerWidth() + 'px' },
									'slow',
									function () {
										n.animate( { 'opacity' : 1 }, 'slow' );
									}
								);
							} ).removeClass( 'new' );
							var containerWidth = recentUpdater.container.parent().parent().innerWidth();
							var num = 0;
							recentUpdater.wasRemovedInPrevTick = 0;
							recentUpdater.container.find( ">div" ).each(
								function () {
									num++;
									if ( num > 7 || $( this ).offset().left > containerWidth ) {
										recentUpdater.wasRemovedInPrevTick++;
										$( this ).remove();
									}
								}
							);
							if ( value.msgId )
								recentUpdater.lastMessageId = value.msgId;
							recentUpdater.timer = setTimeout( recentUpdater.tick, recentUpdater.tickTime );
						},
						dataType : "json"
					}
				);
		}
}





