$(function() {
	$("#top7-pane").easyTabs();
	$("#likes-pane").easyTabs();
	
	if ($('#day-topics-slider li').size() > 6) {
		$('#day-topics-slider').css({margin: 0}).anythingSlider({
			theme           : 'day-topics',
			width           : 870,
			height			: 200,
			autoPlay        : false,
			easing          : 'easeIn',
			buildNavigation : false,
			buildArrows     : true,
			buildStartStop  : false,
			resizeContents  : false
			,showMultiple: 6
			,appendFowardTo : '.day-topics-wrap'
			,appendBackTo   : '.day-topics-wrap'
		});
	}
	galleryCfg = {
		theme           : 'post-gallery',
		width           : 350,
		autoPlay        : false,
		easing          : 'easeIn',
		buildNavigation : false,
		buildStartStop  : false,
		resizeContents  : false,
		enableKeyboard  : true
	};
	// Dynamic gallery build
	$(function(){
		$('img.gallery_plugin').each(function(){
			var img = $(this),
			img_maxw = 350,
			rel = img.attr('rel'),
			id,
			wrapper;
			rel = rel.split("-");
			id = parseInt( rel[1] );
			if (id) {
				$.getJSON(
					'/ajax/gallery/get_images.json',
					{
						id: id
					},
					function(response) {
						if (response.success == true && response.total > 0) {
							img.wrap('<div class="gallery-wrap" />');
							wrapper = img.parent('.gallery-wrap');
							wrapper.before('<br style="clear:both">');
							var cnt = '<ul id="gallery-'+id+'">';
							$.each(response.data.gallery.items, function(k,v){
								var _w = v.width, _h = v.height, _p;
								if (_w > img_maxw) {
									_p = _w/img_maxw;
									_w = img_maxw;
									_h = Math.ceil(_h/_p);
								}
								cnt += '<li>';
								cnt += '<img class="gallery-image" src="'+v.file+'" width="'+_w+'" height="'+_h+'" alt="'+v.title+'" />';
								cnt += '<span class="gallery-img-title">'+v.title+'</span>';
								if (v.description.length > 0)
									cnt += '<span class="gallery-img-desc">'+v.description+'</span>';
								cnt += '</li>';
							});
							cnt += '</ul>';
							wrapper.html(cnt);
							$('#gallery-'+id).anythingSlider(galleryCfg);
						}
					}
				);
			}
		});
	});
});
function loadVKWidget(fn) {
	var delay = 300;
	if (typeof VK === 'undefined') {
		setTimeout( function() { loadVKWidget.call(this, fn); }, delay );
	} else {
		fn.apply(this);
	}
}
function getCommentsForFbUrl(url) {
	FB.api({
		method: 'fql.query',
		query: 'SELECT commentsbox_count FROM link_stat WHERE url="'+url+'"'
	},
	function(response) {
		setCommentsQty(response[0].commentsbox_count, 'fb');
	});
}
function setCommentsQtyVK(qty,l) {
	setCommentsQty(qty, 'vk');
}
function setCommentsQty(qty, p) {
	var article_id = $("#article").data("article-id");
	if (!article_id) return false;
	$.post('/ajax/comment/set_quantity.json', {id: article_id, provider: p, qty: qty});
}

