var ll = {
	'en' : {
		'close' : 'close',
		'next' : 'next',
		'previous' : 'previous',
		'current' : '{current} of {total}',
		'quick-search' : 'Quick Search'
	},
	'ru' : {
		'close' : 'закрыть',
		'next' : 'следующая',
		'previous' : 'предыдущая',
		'current' : '{current} из {total}',
		'quick-search' : 'Быстрый поиск'
	}
};

$(document).ready(function(){
	$('#lang-menu a').bind('click', function(){
		var value = $(this).parent().attr('lang');
		setCookie('lang',value,365);
	});
	
	// $('#days-menu').after('<div id="news-list-tools"><label for="quick-search">Quick Search:</label>&nbsp;<input type="text" id="quick-search" class="text" value="" /> <input id="only-top" type="checkbox" class="checkbox" />&nbsp;<label for="only-top">Top only</label> <input id="only-pop" type="checkbox" class="checkbox" />&nbsp;<label for="only-pop">Popular only</label></div>');
	$('#days-menu').after('<div id="news-list-tools"><label for="quick-search">'+ll[lang]['quick-search']+':</label>&nbsp;<input type="text" id="quick-search" class="text" value="" />&nbsp;<img src="fileadmin/templates/imgs/loading.gif" alt="&hellip;" class="preloader-small" /></div>');
	$('.news-list h4 a, .expander').live('click', function(){
		$('.news-list li').removeClass('act');
		$(this).parents('.news-list li').toggleClass('act');
		//return false;
	});

	$('.news-list li.act .expander').live('click', function(){
		$('.news-list li').removeClass('act');
	});

	$('.news-list li').each(function(){
		subheader = $('.content', this).text();
		rating = $('.value', this).text();

		// $('h4', this).favicons({insert: 'insertBefore'});
		
		if (subheader != '') {
			$('.rating', this).after('<div class="expander">&nbsp;</div>');
		}

		if (rating > 1) {
			$(this).addClass('top');
		}
	});

	$('#only-top').change(function(){$('.news-list li:not(.top)').toggleClass('not-top');});
	$('#only-pop').change(function(){$('.news-list li:not(.pop)').toggleClass('not-pop');});

	$('.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);
				$('#news-list-item-'+uid+' .value').html(response.rating*2-response.votes);
				$('.preloader', parent).fadeOut(function(){$(this).remove()});
		},'json');
		
		return false;
	});

	$('input#quick-search').quicksearch('.news-list li:not(.not-top)',{'delay':'300','loader': '.preloader-small','show':function(){
		$('h4 a', this).addClass('cboxElement');
		this.style.display = "";
	},'hide':function(){
		$('.cboxElement', this).removeClass('cboxElement');
		this.style.display = "none";
	}}).val('');

	$('.news-list h4 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(){
			$('#news-single-tools').remove();
			$('#cboxLoadedContent').after('<div id="news-single-tools"></div>');
			var newsId = $.fn.colorbox.element().parents('.news-list li').attr('id').replace(/\D/g, '');
			var newsRating = $('#news-list-item-'+newsId+' .value').text();
			$('#news-list-item-'+newsId+' .rating').clone().appendTo('#news-single-tools');
			$('#news-single-tools .rating .label').after('<a href="#rating-0" id="rating-0" rel="rating-0" class="rating-0"><span>&darr;</span></a>');
			$('#news-single-tools .rating .value').after('<a href="#rating-1" id="rating-1" rel="rating-1" class="rating-1"><span>&uarr;</span></a>');
		},'onCleanup':function(){$('#news-single-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';
}
