function closeW() {
	tb_remove();
}

var j=0;
var Buttons = new Object();
function showVk(url, title, description, image) {
	var vk_link = "http://vkontakte.ru/share.php?image="+image+"&amp;url="+url+"&amp;title="+title+"&amp;description="+description;
	return vk_link;
}

function showFb(url, title, description, image) {
	var fb_link = "http://www.facebook.com/sharer.php?s=100&amp;p[title]="+title+"&amp;p[summary]="+description+"&amp;p[url]="+url+"&amp;p[images][0]="+image;
	return fb_link;
}

function showTwi(url, tweet) {
	var twi_link = "http://twitter.com/share?url="+url+"&amp;text="+tweet;
	return twi_link;
}

function getLink(id, url, www_url, title, description, image, tweet) {
	id = encodeURIComponent(id);
	//url = encodeURIComponent(url);
	//www_url = encodeURIComponent(www_url);
    title = encodeURIComponent(title);
    description = encodeURIComponent(description);
    image = encodeURIComponent(image);
    tweet = encodeURIComponent(tweet);

    var twi_href = showTwi(url, tweet);
    var fb_href = showFb(url, title, description, image);
    var vk_href = showVk(url, title, description, image);

    header_popup = '<div class="ConntentBlock"><div class="TitleBlock"><div class="Title">Проголосуй с помощью аккаунта в социальной сети</div><div class="Close"><a href="#" onclick="closeW(); return false;"></a></div><div class="B"></div></div><div class="CenterConntent"><div class="LeftConntent">';
	footer_popup = '<div class="B"></div></div><div class="RightConntent"><span>0</span><div class="B"></div></div><div class="B"></div></div><div class="Footer"><div class="TextFooter">Если ссылки не открываются - скопируйте URL и откройте в новом окне.</div><div class="B"></div></div><div class="B"></div></div>';
	fb_link = '<a class="BlockGolos FBBtn" href="'+fb_href+'" target="_blank"  onclick="window.open'+"(this.href,'...','toolbar=0,status=0,width=626,height=436')"+'; return false;"><div class="GolosGde"><img src="/images/facebook.gif" width="32" height="32" alt="Facebook" title="Facebook" /></div><div class="GolosButton"><p>0</p><span></span></div><div class="B"></div></a>';
	vk_link = '<a class="BlockGolos VKBtn" href="'+vk_href+'" target="_blank" onclick="window.open'+"(this.href,'...','toolbar=0,status=0,width=626,height=436')"+'; return false;"><div class="GolosGde"><img src="/images/vk.gif" width="32" height="32" alt="Вконтакте" title="Вконтакте" /></div><div class="GolosButton"><p id="VKcount_'+(j*1+1)+'">0</p><span></span></div><div class="B"></div></a>';
	tw_link = '<a class="BlockGolos TwBtn" href="'+twi_href+'" target="_blank" onclick="window.open'+"(this.href,'...','toolbar=0,status=0,width=626,height=436')"+'; return false;"><div class="GolosGde"><img src="/images/twitter.png" width="33" height="33" alt="Twitter" title="Twitter" /></div><div class="GolosButton"><p>0</p><span></span></div><div class="B"></div></a>';
	odn_link = '<a class="BlockGolos OdnBtn" href="http://www.odnoklassniki.ru/dk?st.cmd=addShare&st.s=1&st._surl='+www_url+'" target="_blank" class="odkl-klass-stat" onclick="window.open'+"(this.href,'...','toolbar=0,status=0,width=626,height=436')"+'; return false;"><div class="GolosGde GolosOdn"><img src="/images/class.png" width=53" height="43" alt="Одноклассники" title="Одноклассники" /></div><div class="GolosButton"><p>0</p><span></span></div><div class="B"></div></a>';

	$('#'+id+'_box').append(header_popup + fb_link + vk_link + tw_link + odn_link + footer_popup);
	my_showCount(url, www_url, id);
}

function Odnkl(id, url) {
	var f="http://www.odnoklassniki.ru/dk?st.cmd=extOneClickLike&uid=";
	var k=$('#'+id+"_box .OdnBtn");
	var e=k[0].getElementsByTagName("p");
	var l=f+id+"&ref="+encodeURIComponent(url);
	var m=document.createElement("script");
	m.src=l;
	e[0].appendChild(m);
}

ODKL={};

ODKL.updateCountOC = function(a,b,c,d) {
	$('#'+a+'_box').find('.OdnBtn p').text(b);
	current_count = ($("#"+a).html())*1;
	current_count += b*1;
	$("#"+a+'_box').find(".RightConntent span").html(current_count);
	if ($("#"+a+'_box').html() != '') $("#"+a).html(current_count);
}

ODKL.updateCountForVoted = function(a,b,c) {
	current_count = ($("#"+a).html())*1;
	current_count += b*1;
	$("#"+a).html(current_count);
}

function my_showCount(url, www_url, id) {
	j++;
	var i=0;
	Buttons[j] = id;
	var URL = url;
	var FullCount;
	var twitter = 'http://urls.api.twitter.com/1/urls/count.json?url=' + URL + '&callback=?';
    var facebook = 'https://api.facebook.com/method/fql.query?query=select total_count from link_stat where url="' + URL + '"&format=json&callback=?';
    var vkontakte = 'http://vkontakte.ru/share.php?act=count&index=' + (j)*1 + '&url=' + url + '&callback=?';
	VK = new Object();
	VK.Share = {};
	VK.Share.count = function(index, count) {
		i++;
		//current_count = ($("#vs"+index).html())*1;
		current_count = ($("#"+id).html())*1;
		$("#VKcount_"+index).html(count);
		current_count += count;
		$("#"+id).html(current_count);
		$("#"+id+'_box').find(".RightConntent span").html(current_count);
	}

	$.getJSON(vkontakte, function(data) {});

	var rr=1;
	$.getJSON(twitter, function(data) {
		rr--;
		if (rr == 0) {
			$("#"+id+'_box').find(".TwBtn p").html(data.count);
			current_count = ($("#"+id).html())*1;
			current_count += data.count;
			$("#"+id).html(current_count);
			$("#"+id+'_box').find(".RightConntent span").html(current_count);
		}
	});

	var mm=0;
	$.getJSON(facebook, function(data) {
		if (mm == 0) {
			$("#"+id+'_box').find(".FBBtn p").html(data[0].total_count);
			current_count = ($("#"+id).html())*1;
			current_count += data[0].total_count;
			$("#"+id+'_box').find(".RightConntent span").html(current_count);
			$("#"+id).html(current_count);
		}
	});
	
	Odnkl(id, www_url);
}
