// JavaScript Document
var toOut=null;
function moutSNB (obj) {
  if( !obj) return;
  toOut=window.setTimeout(moutDo, 500);
}

function moutDo() {
  obj.className='link';
}

function prepareSongs(str){
	
	
	
	matches = str.match(/\/songs2\/#?[0-9]+\/$/);
	
	if (matches && matches.length > 0) {
		str = str.replace(matches[0], '/songs2/');
	}
	
	//return str;
	
	matches = str.match(/\/songs\/[0-9]{1,2}\/$/);
	
	if (matches && matches.length > 0) {
		str = str.replace(matches[0], '/songs/');
	}
	
	matches = str.match(/\?.*$/);
	
	if (matches && matches.length > 0) {
		str = str.replace(matches[0], '');
	}
	
	matches = str.match(/\/[^\/]+$/);
	
	if (matches && matches.length > 0) {
		str = str.replace(matches[0], '/');
	}
	
	
	
	return str;
	
}

if (!window.AgregatorSNB) {
	function AgregatorSNB() {
		if (AgregatorSNB.arguments.length) for (var p in AgregatorSNB.arguments[0]) this[p] = AgregatorSNB.arguments[0][p];
		this.str = "<div class='link' onmouseover=\"this.className='link hover'; \" onmouseout=\"this.className='link';\"><b>Поделиться с друзьями:</b>";

		//if (!this.href) this.href = encodeURIComponent(location.href + ((location.href.indexOf("?") == -1) ? "?from=" : "&from="));
		//else this.href = encodeURIComponent(this.href + ((this.href.indexOf("?") == -1) ? "?from=" : "&from="));
		
		if (!this.href) this.href = encodeURIComponent(prepareSongs(location.href));
		else this.href = encodeURIComponent(prepareSongs(this.href));

		//if (!this.title) this.title = encodeURIComponent((navigator.userAgent.indexOf("MSIE") != -1) ? document.getElementsByTagName("title")[0].innerText : $("title").text());
		if (!this.title) this.title = encodeURIComponent(document.title);
		else this.title = encodeURIComponent(this.title);

		if (!this.description) this.description = "description"; else this.description = encodeURIComponent(this.description);

		/*if (!this.image && $("img[name=picture]")) {
			if ($("img[name=picture]").attr("src")) {
				var imageLinkPattern = /(\w+)\.jpg/;
				var iconImageLink = $("img[name=picture]").attr("src");
				this.image = "&image=" + encodeURIComponent("http://www.ozon.ru/" + iconImageLink.replace(imageLinkPattern, "small/$1.gif"));
			}
			else this.image = "";
		}
		else this.image = "";*/
	};
}

if (window.AgregatorSNB) {
	AgregatorSNB.prototype.formLink = function (url, picture, onclick, title, resource) {
		var id = "";
		var arr = /\/context\/detail\/id\/(\d+)[^d]*/.exec(document.location.href);
		if (arr != null && arr.length > 1) {
			id = arr[1];
		}
		var onclickFunction = "SetOmnitureCode('shareThis', null, 'shareThis', 'shareThis', 'event25', ';" + id + "', '" + resource + "', '', '', '', '');";
		//if (onclick) onclickFunction += "return AgregatorSNB.description(this);";

		//return "<a href='" + url + "\" target='_blank' onclick=\"" + onclickFunction + "\" title=\"" + title + "\"><img src='" + picture + "' alt='' /></a>&nbsp;";
		return "<a href='" + url + "' target='_blank' title=\"" + title + "\" onclick=\"new Ajax.Request('/', {method: 'get',  parameters: {save_stats: '1', url: '"+url+"'} });\"><img src='" + picture + "' alt='' /></a>";
	};
	AgregatorSNB.prototype.display = function (n) {
		this.str += "<div class=\"ico2soc\" >";
		this.str += this.formLink("http://connect.mail.ru/share?share_url=" + this.href + "&title="+encodeURIComponent(n), "/images/social/moi-mir.gif", false, "Мой Мир","moimir");
		this.str += this.formLink("http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl=" + this.href, "/images/social/odklsmall.gif", false, "Одноклассники","odnoklassniki");
		this.str += this.formLink("http://vkontakte.ru/share.php?url=" + this.href + "&title=" + encodeURIComponent(n) + "&noparse=true", "/images/social/vkontakte.gif", false, "В контакте", "vkontakte");
		this.str += this.formLink("http://www.livejournal.com/update.bml?subject=" + encodeURIComponent(n) + "&event=" + encodeURIComponent("<a href=\"") + this.href + encodeURIComponent("\">") + encodeURIComponent(n) + encodeURIComponent("</a>"), "/images/social/livejournal.gif", false, "LiveJournal", "livejournal");
		this.str += this.formLink("http://twitter.com/share?url=" + this.href + "&text=" + encodeURIComponent(n), "/images/social/twitter.gif", false, "Twitter", "twitter");
		this.str += this.formLink("http://www.facebook.com/sharer.php?u=" + this.href + "&t=" + n, "/images/social/facebook.gif", false, "Facebook", "facebook");
		this.str += this.formLink("https://plusone.google.com/_/+1/confirm?hl=en&url=" + this.href, "/images/social/gp.png", false, "Google Plus", "googleplus");
		
		this.str += "</div></div>";
		document.write(this.str);
	};
	
	AgregatorSNB.prototype.display2 = function (url_add, n) {
		this.str += "<div class=\"ico2soc\" onmouseover=\"this.parentNode.className='link hover'; \">";
		
		var sl = encodeURIComponent("/");
		
		this.str += this.formLink("http://connect.mail.ru/share?share_url=" + this.href +url_add + sl + "&title="+encodeURIComponent(n), "/images/social/moi-mir.gif", false, "Мой Мир","moimir");
		this.str += this.formLink("http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl=" + this.href +url_add + sl, "/images/social/odklsmall.gif", false, "Одноклассники","odnoklassniki");
		this.str += this.formLink("http://vkontakte.ru/share.php?url=" + this.href  +url_add + sl + "&title=" + encodeURIComponent(n) + "&noparse=true", "/images/social/vkontakte.gif", false, "В контакте", "vkontakte");
		this.str += this.formLink("http://www.livejournal.com/update.bml?subject=" + encodeURIComponent(n) + "&event=" + encodeURIComponent("<a href=\"") + this.href +url_add + sl +encodeURIComponent("\">") + encodeURIComponent(n) + encodeURIComponent("</a>"), "/images/social/livejournal.gif", false, "LiveJournal", "livejournal");
		this.str += this.formLink("http://twitter.com/share?url=" + this.href +url_add + sl + "&text=" + encodeURIComponent(n), "/images/social/twitter.gif", false, "Twitter", "twitter");
		this.str += this.formLink("http://www.facebook.com/sharer.php?u=" + this.href +url_add + sl + "&t=" + encodeURIComponent(n), "/images/social/facebook.gif", false, "Facebook", "facebook");
		this.str += this.formLink("https://plusone.google.com/_/+1/confirm?hl=en&url=" + this.href, "/images/social/gp.png", false, "Google Plus", "googleplus");
		
		this.str += "</div><i>...</i></div>";
		document.write(this.str);
	};
	
	AgregatorSNB.description = function (lnk) {
		var description = "";
		var txt;
		var lnkFound = false;
		try {
			var thisLink = $(lnk);
			$("table.detail").find("td.detail_leftpadd").each(function () {
				if ((($(this).text().indexOf('�� OZON.ru') != -1) || ($(this).text().indexOf('�� ��������') != -1) || ($(this).text().indexOf('�� �������������') != -1)) && !lnkFound) {
					txt = $(this).parents().next().find("td.detail_centralcell").text().split(".");
					for (var i in txt) {
						if (description.length < 100) description += txt[i] + ".";
						else break;
					}
					thisLink.attr("href", thisLink.attr("href") + "&description=" + encodeURIComponent(description));
					lnkFound = true;
				}
			});
		} catch (e) { };

		return true;
	}
};
