var ll = {
	'en' : {
		'close' : 'close',
		'next' : 'next',
		'previous' : 'previous',
		'current' : '{current} of {total}',
		'quick-search' : '<mark>Quick search ›</mark> <command>Enter keyword(s) space-separated (examples: "bank", "usd eur")</command>',
		'results' : 'Items'
	},
	'ru' : {
		'close' : 'закрыть',
		'next' : 'следующая',
		'previous' : 'предыдущая',
		'current' : '{current} из {total}',
		'quick-search' : '<mark>Быстрый поиск ›</mark> <command>Введите ключевые слова, разделенные пробелом (пример: "банк", "usd eur")</command>',
		'results' : 'Новостей'
	}
};

$(document).ready(function(){
	// Set values for ordered list in reverse order
	setOLvalues();
	
	// Set class for li with visited links
	$('.news-list li .title a').visited(function(){
		$(this).parent().parent().addClass('visited');
	});
	
	// Set class for latest news
	$('.news-list .time').has('.age').addClass('loud');
	
	// Set cookie for storing language preferences
	$('#page-lang a').bind('click', function(){var value = $(this).parent().attr('lang');setCookie('lang',value,365);});
	
	// Add quick search UI and focus its input field on page load
	$('.news-list').before('<footer id="search" style="display:none"><label for="quick-search">'+ll[lang]['quick-search']+':</label>&nbsp;<input type="text" id="quick-search" class="text" value="" /><div class="preloader"><img src="fileadmin/templates/imgs/loading.gif" alt="&hellip;" /></div></footer><h4 id="results">'+ll[lang]['results']+': <span></span></h4>');
	$('#search').slideDown();
	$('#quick-search').trigger('focus');

	// Add extra classes for li
	$('.news-list li').each(function(){
		// $('.title', this).favicons({insert: 'insertBefore'});
		rating = $('.rating', this).text();
		if (rating > 1) $(this).addClass('top');
	});

	$('.rating a').live('click', function(){
		var value = $(this).attr('rel');
		var parent = $(this).parent();
		var uid = parent.attr('id').replace(/\D/g, '');
		$('.value', parent).append('<div class="preloader"><img src="fileadmin/templates/imgs/loading.gif" alt="&hellip;" /></div>');
		$('.preloader', parent).fadeIn(150);
		$.post('http://' + top.location.host + top.location.pathname + '?eID=lab_ratings&action=rate',{
				pid: pid,
				uid: parent.attr('id'),
				value: value,
				ur: 0,
				up: 1,
				sl: sl
			},function(response){
				$('.value', parent).html(response.rating*2-response.votes);
				$('#li-'+uid+' .value').html(response.rating*2-response.votes);
				$('.preloader', parent).fadeOut(function(){$(this).remove()});
		},'json');
		
		return false;
	});
	
	// General quick search functions
	$('input#quick-search').quicksearch('.news-list li:not(.not-top)',{'delay':'300','loader':'.preloader img','show':function(){
		$('.title a', this).addClass('cboxElement');
		this.style.display = "";
		//setOLvalues();
		setCounter();
	},'hide':function(){
		$('.cboxElement', this).removeClass('cboxElement');
		this.style.display = "none";
		//setOLvalues();
		setCounter();
	}}).val('');
	
	// Quick search helpers
	$('.news-list li small,.preloader').click(function(){
		var text = $(this).text();
		var inputText = $('input#quick-search').val();
		
		if (text == inputText) $('input#quick-search').val('');
		else $('input#quick-search').val(text);
		
		$('input#quick-search').trigger('keyup');
	});
	
	// Colorbox function
	$('.news-list .title a').colorbox({'width':'95%','height':'90%','iframe':'true','rel':'news','close':ll[lang]['close'],'next':ll[lang]['next'],'previous':ll[lang]['previous'],'current':ll[lang]['current'],'onComplete':function(){
			$('#si-tools').remove();
			$('#cboxLoadedContent').after('<div id="si-tools"></div>');
			var newsId = $.fn.colorbox.element().parents('.news-list li').attr('id').replace(/\D/g, '');
			var newsRating = $('#li-'+newsId+' .value').text();
			$('#li-'+newsId+' .rating').clone().appendTo('#si-tools');
			$('#si-tools .rating .label').after('<a href="#rating-0" id="rating-0" rel="rating-0" class="rating-0"><span>&darr;</span></a>');
			$('#si-tools .rating .value').after('<a href="#rating-1" id="rating-1" rel="rating-1" class="rating-1"><span>&uarr;</span></a>');
		},'onCleanup':function(){
			$('#si-tools').remove();
		}
	});
});

function setCookie(cname,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = cname + '=' + escape(value) + ((expiredays==null) ? '' : ';expires=' + exdate.toUTCString()) + ';path=/;domain=.brosment.com';
}

function setOLvalues() {
	var counter = $('ol > li:visible').size();
	$("ol > li:visible").each(function(i) {
		var attrvalue = counter - i;
		$(this).attr("value", attrvalue);
	}); 
}

function setCounter() {
	var counter = $('ol > li:visible').size();
	$("#results span").text(counter); 
}

