var tinyurl = '';

function TweetThis(bigurl,title)
{
    $.getJSON(
      "http://json-tinyurl.appspot.com/?&callback=?",
      {url: bigurl},
      function(data){
    	tinyurl = data.tinyurl;
    	var real_href = 'http://twitter.com/home?status=SiteUA.org:%20'+title+':%20'+data.tinyurl;
   	  	$('#zak_11').attr('href',real_href);
      }
    );

}

function ShowZakl() {
	this.domain=location.href+'/';
	this.domain=this.domain.substr(this.domain.indexOf('://')+3);
	this.domain=this.domain.substr(0,this.domain.indexOf('/'));
	this.location=false;
	this.selection=function() {
		var sel;
		if (window.getSelection) sel=window.getSelection();
		else if (document.selection) sel=document.selection.createRange();
		else sel='';
		if (sel.text) sel=sel.text;
		return encodeURIComponent(sel);
	}
	this.url=function(system) {
		var title=encodeURIComponent(document.title);
		var url=encodeURIComponent(location.href);
		var r_url = location.pathname;
		var arr_url = r_url.split('/');
		var mod_url = 'http://'+this.domain+'/system_category/'+arr_url[2];
		
		TweetThis(location.href,title);
		
		switch (system) {
			case 1: return 'http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl='+url+'&lname='+title;
			case 2: return 'http://www.google.com/bookmarks/mark?op=add&bkmk='+url+'&title='+title;
			case 3: return 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url='+url+'&title='+title+'&top=1';
			case 4: return 'http://www.livejournal.com/update.bml?subject='+title+'&event='+url;
			case 5: return 'http://news2.ru/add_story.php?url='+url;
			case 6: return 'http://digg.com/submit?phase=2&url='+url+'&title='+title;
			case 7: return 'http://bobrdobr.ru/addext.html?url='+url+'&title='+title;
			case 8: return 'http://moemesto.ru/post.php?url='+url+'&title='+title;
			case 9: return 'http://memori.ru/link/?sm=1&u_data[url]='+url+'&u_data[name]='+title;
			case 10: return 'http://connect.mail.ru/share?share_url='+url;
			case 11: return 'http://twitter.com/home?status=SiteUA.org:%20'+title+':%20'+tinyurl;
		}
	}
	this.redirect=function() {
		if (this.location) location.href=this.location;
			this.location=false;
		}
		this.go=function(i) {
		this.location=this.url(i);
		setTimeout('zakl.redirect()',2000);
		var scr=document.createElement('script'); 
		scr.type='text/javascript'; 
		document.body.appendChild(scr);
	}
	this.init=function() {
		var titles=new Array('Закладки Yandex.ru','Закладки Google','Windows Live','LiveJournal','News2.ru','Digg','BobrDobr.ru','MoeMesto.ru','Memori.ru','Mail.ru','Twitter');
		var title = document.title+' (SiteUa.org)';
		var description = $('.news_header').html();
		title = title.replace(new RegExp('"', 'g'),"'");
		description = description.replace(new RegExp('"', 'g'),"'");
		var html='<div style="float:left">';
		for (i=0;i<11;i++) {
			html+='<a id="zak_'+(i+1)+'" href="'+this.url(i+1)+'" onclick="zakl.go('+(i+1)+');return false"><img src="http://odnaknopka.ru/images/blank.gif" width="30" height="16" alt=" #" title="'+titles[i]+'" style="border:0;padding:0;margin:0 4px 0 0;background:url(/tpl2/images/bookmarks.gif) -'+(i*30)+'px"/></a>';
		}
		html += '</div>';
		html += '<div class="vkontakte_div" style="float:left;position:relative;top:-4px;left:1px;"><script type="text/javascript" src="http://vkontakte.ru/js/api/share.js?8" charset="windows-1251"></script><script type="text/javascript">document.write(VK.Share.button({noparse: true,title: "'+title+'",description: "'+description+'" },{type: "round_nocount",text: "Поделиться"}));</script></div>';
		document.write(html);
	}
}
zakl = new ShowZakl();
zakl.init();