$(function(){
	
		var handleNearestRoutesTab = function(){}
	
		/* Top routes tabs */
		;(function(){
			var 
				
				/** @type {jQuery} */
				$header = $('#top-routes-header'),
				
				/** @type {jQuery} */
				$modeSwitchers = $('#top-routes-header-switch A'),
				
				mode = 'all',
				
				/** @type {jQuery} */
				$linksList = $('#top-routes-links'),
				/** @type {jQuery} */
				$linksListItems = $linksList.find('>li'),
				/** @type {jQuery} */
				$links = $linksListItems.find('>a'),
				$bodiesWrap = $('#top-routes-tabs-wrap'),
				$bodies = $bodiesWrap.find('>div.top-routes-tab'),
				applyMode = function(newMode){
					if (newMode != undefined){
						mode = newMode;
						}
					$bodiesWrap
						.find('.top-routes-tab-mode').hide()
						.filter('.top-routes-tab-mode-' + mode).show();
					},
				handlers = {
					'nearest-routes':function(/* jQuery */$tab){
						var
							handler = arguments.callee,
							tick = function(){
								if ($tab && $tab.is(':visible')){
									$tab.find('>div.top-routes-tab-mode:visible>div.page:visible>table>tbody>tr>td.route-departure:not(.stop-set-time)')
										.each(function(){
											var 
												/** @type {jQuery} */
												$td = $(this),
												/** @type {Date} */
												dp = new Date(parseInt($(this).searchInClass('closest-departure-')) * 1000),
												diff = dp.diff();
												if ((diff.h == 0 && diff.m <= 0 && diff.s == 0)){
													$td
														.addClass('stop-set-time')
														.html('<strong>Маршрут отправлен</strong>')
													return;
													}
												$td.html(
													'<div class="route-departure-diff">' + 
														timeStr(diff, (diff.s % 2 ? ':' : ' ')) + 
													'</div>');
											});
									}
								};
						if (!handler.interval){
							$.request({
								url: '/routes/nearest',
								dataType: 'html',
								type: 'GET',
								quite: true,
								success:function(res){
									$tab.html(res);
									handler.interval = setInterval(tick, 1000);
									tick();
									}
								});
							}
						else{
							tick();
							}
						}
					};
			$links.click(function(){
				var 
					/** @type {jQuery} */
					$link = $(this),
					header = $link.html(),
					/** @type {jQuery} */
					$listItem = $link.closest('li'),
					id = $link.attr('href').toString().substr(1),
					/** @type {jQuery} */
					$body = $bodies.filter('#' + id);
				if ($listItem.hasClass('active')){
					return false;
					}
				$bodies.hide();
				$body.show();
				$linksListItems.removeClass('active');
				$listItem.addClass('active');
				$header.animate({
					top:'35px'
					},
					'fast',
					undefined,
					function(){
						$header
							.css({top: '-35px'})
							.html(header)
							.animate({
								top: 0
								},
								'fast',
								undefined,
								function(){
									$header.css('top','auto');
									}
								);
						});
				if (handlers[id]){
					handlers[id]($body);
					}
				return false;
				});
			$links.eq(0).click();
			
			$bodiesWrap.click(function(/* Event */e){
				var 
					/** @type {jQuery} */
					$target = $(e.target);
				if ($target.is('ul.pager a')){
					
					;(function(){
						var 
							page = $target.text(),
							/** @type {jQuery} */
							$pager = $target.closest('.pager'),
							/** @type {jQuery} */
							$li = $target.closest('li'),
							/** @type {jQuery} */
							$tab = $target.closest('.top-routes-tab'),
							tabId = $tab.attr('id'),
							/** @type {jQuery} */
							$pages = $tab.find('>div.top-routes-tab-mode:visible>div.page');
						$pages.hide().filter('.page-num-' + page).show();
						$pager.find('strong').each(function(){
							var 
								p = $(this).text();
							$(this).replaceWith('<a href="#page' + p + '">' + p + '</a>');
							});
						$li.html('<strong>' + page + '</strong>');
						if (tabId && handlers[tabId]){
							handlers[tabId]($tab);
							}
						})();
					return false;
					}
				});
			
			$modeSwitchers.click(function(){
				var 
					/** @type {jQuery} */
					$a = $(this);
				if ($a.hasClass('active')){
					return false;
					}
				$modeSwitchers.removeClass('active');
				$a.addClass('active')
				applyMode($a.attr('id').toString().split('-').pop());
				return false;
				});	
			})();
	
			
		/* Screaming */
		;(function(){
			var
				container=$id('top-screams'),
				CHECKING_NEW_SCREAMS_INTERVAL=30000,
				maxCheckCount=1000,
				checkCount=0,
				_lastDt=(new Date()).toDT();
	
			var displayScream=function(scream){
				//container.innerHTML='<li></li>'+container.innerHTML;
				var items=$tag('LI',container);
				var itemContent=''
							+	'<a class="thumb'+(scream.userOnline?' user-online':'')+'" href="'+PATH_USERS+scream.userLogin+'">'
							+	'<img src="'+PATH_USERS+scream.userLogin+'/photo/thumb" alt="'+scream.userLogin+'" />'
							+	'<span></span>'
							+	'</a>'
			        		+	'<h3>'
			        		+		'<a href="/users/'+scream.userLogin+'/">'
			        		+			scream.userLogin+(scream.userCityName?', '+scream.userCityName:'')
			        		+		'</a>'
			        		+		(scream.dt_publishing?'<em>'+scream.dt_publishing.dateReadableSimpleStr()+'</em>':'')
			        		+	'</h3>'
			        		+	'<p>'+scream.msg+'</p>'
			        		+	'<span class="clear0"></span>';
				setChildExt(
					{
						parent:container,
						tag:'LI',
						className:'new',
						setType:'prepend',
						content:itemContent,
						handlerFunction:function(el){
							slideDown(
								el,
								{
									speed:50,
									oncomplete:function(){
										unsetChild(items[items.length-1]);
										}
									}
								)
							}
						}
					);
				};
			//displayScream({msg:'test'});return;
			var checkingInterval=setInterval(
				function(){
					if (++checkCount>maxCheckCount){
						clearInterval(checkingInterval);
						return false;
						}
					makeAjaxExt(
						{
							params:{
								todo:'checkNewScreams',
								dt:_lastDt
								},
							onload:function(res){
								if (res && res.length){
									for (var i=res.length-1; i>=0; i--){
										var scream=res[i];
										if (scream.id){
											displayScream(scream);
											}
										}
									_lastDt=(new Date()).toDT();
									return false;
									}
								}
							}
						);
					}
				,CHECKING_NEW_SCREAMS_INTERVAL
				);
			})();
	
			
		/* New users list */
		;(function(){
			$('#new-users-pix-list a.thumb').each(function(){
				var 
					$a = $(this),
					$img = $a.find('>img');
				$img.attr('title', $img.attr('title') + ' (' + $a.next(':first').text() + ')');
				});
			})();
	});
