﻿function addBookmark(title, url) {
	if (title == undefined)
		title = document.title;
	if (url == undefined)
		url = top.location.href;	
	if (window.sidebar) {// Firefox
		window.sidebar.addPanel(title, url, '');
	} else if (window.opera && window.print) {// Opera
		var t = document.createElement('a');
		t.setAttribute('rel', 'sidebar');
		t.setAttribute('href', url);
		t.setAttribute('title', title);
		t.click();
	} else {	// IE
		window.external.AddFavorite(url, title);	
	}
	return false;
}

///////////////////////////////////
//adv
function initManyHolderClips(title) {
	document.getElementById('manyHolderClips').innerHTML = "<b><a style='color:blue;' target='_blank' href='http://manyclips.net/'>Скачать клип " + title + " бесплатно</a></b>";
}

function addLink(holder, link) {
	var el = document.getElementById(holder);
	if (el != 'undefined' && el != null)
		el.innerHTML = link;
}

function addBannerToMarketGid(bannerHtml) {
	if (bannerHtml == null || bannerHtml == '')
		return;
	var d = document;
	var marketGid = d.getElementById("MarketGid5624");
	if (marketGid == null)
		return;
	var trs = marketGid.getElementsByTagName('TR');
	var tds = marketGid.getElementsByTagName('TD');
	if (trs == null || trs.length == 0)
		return;
	if (tds == null || tds.length == 0)
		return;
	var tr = trs[0];
	var td0 = tds[2];
	
	var td = d.createElement("TD");
	td.align = td0.align;
	td.vAlign = td0.vAlign;
	tr.insertBefore(td, td0);
	td.innerHTML = bannerHtml;
	
	tds = marketGid.getElementsByTagName("TD");
	for (i = 0; i < tds.length; i++) {
		tds[i].width = 100/tds.length +"%";
	}
}

//!adv
///////////////////////////////////


var CompleteTextBox = {
	
	obj: null,
	handlerUrl : "ArtistHandler.ashx",
	searchAllUrl: "",
	searchArtistUrl: "",
	searchSongUrl: "",
	searchAlbumUrl: "",
	searchTextUrl: "",
	searchSoundtrackUrl: "",
	isReady : true,
	
	hide: function(element) {
		if (typeof element == 'string')
			document.getElementById(element).style.display = 'none';
		else
			element.style.display = 'none';
	},

	show: function(element) {
		if (typeof element == 'string')
			document.getElementById(element).style.display = '';
		else
			element.style.display = '';
	},
	
	getXMLHTTPRequest: function () {
		var xRequest=null;
		if (window.XMLHttpRequest)
			xRequest = new XMLHttpRequest();
		else if (typeof ActiveXObject != "undefined")
			xRequest = new ActiveXObject("Microsoft.XMLHTTP");
		return xRequest;
	},
	
	GetDataViaAJAX : function (prefix,WhoGet)	{
		if (!this.isReady)
			return null;
		this.obj = this.getXMLHTTPRequest();
		if (this.obj != null) {
			this.obj.onreadystatechange = function() {	
						CompleteTextBox.ReceiveServerData(WhoGet, this.obj);	
					};
//			if (!document.getElementById(this.soundtrackSelectorClientId).checked)
				this.obj.open("GET", this.handlerUrl + "?prefix="+escape(prefix), true);

//			else
//				this.obj.open("GET", this.handlerUrl + "?soundtrack=true&prefix="+escape(prefix), true);
			this.obj.send(null);
		}
		return false;
	},
	
	SetSearchUrl : function (WhoSend) {
		document.getElementById("all").href = this.searchAllUrl.replace(/pattern/g, WhoSend.value);
		document.getElementById("byArtist").href = this.searchArtistUrl.replace(/pattern/g, WhoSend.value);
		document.getElementById("bySong").href = this.searchSongUrl.replace(/pattern/g, WhoSend.value);
		document.getElementById("byAlbum").href = this.searchAlbumUrl.replace(/pattern/g, WhoSend.value);
		document.getElementById("byText").href = this.searchTextUrl.replace(/pattern/g, WhoSend.value);
		document.getElementById("bySoundtrack").href = this.searchSoundtrackUrl.replace(/pattern/g, WhoSend.value);
	},

	TextKeyDown : function (WhoSend,WhoGet,eventX) {
		if (!this.isReady) {
			this.hide(WhoGet);
			return true;
		}
		if(eventX.keyCode == 13) {
			if (document.getElementById(WhoGet).style.display == "none")
				return true;
			else {
				this.ReturnValueToTextBoxs(document.getElementById(WhoGet), WhoSend);
				return true;
			}
		}
		else if(eventX.keyCode==27) {
			this.hide(WhoGet);
			return false;
		}
		return true;
	},
	
	SendData : function (WhoSend,WhoGet,eventX) {
		this.SetSearchUrl(WhoSend);

		if (!this.isReady)
			return null;

		var objGet=document.getElementById(WhoGet);
		if (WhoSend.value.length < 2) {
			this.hide(WhoGet);
			return false;
		}
		var e = eventX;
		if (e.keyCode==40) //Down arrow click
		{
			if (objGet.selectedIndex<objGet.options.length-1)
				objGet.selectedIndex++;
			else 
				objGet.selectedIndex=0;
			this.ReturnValueToTextBoxsWithoutClose(document.getElementById(WhoGet), WhoSend);
			return false;
		} else if (e.keyCode==38) //Up arrow click
		{
			if (objGet.selectedIndex>0)
				objGet.selectedIndex--;
			else 
				objGet.selectedIndex=objGet.options.length-1;
			this.ReturnValueToTextBoxsWithoutClose(document.getElementById(WhoGet), WhoSend);
			return false;
		} else if (e.keyCode==13) //Enter click
		{
			this.ReturnValueToTextBoxs(objGet,WhoSend)
			return false;
		} else if (e.keyCode==27) //Escape Click
		{
			this.hide(WhoGet);
			return false;
		}

		var offset=this.GetLocation(WhoSend);
		this.GetDataViaAJAX(WhoSend.value, WhoGet);
		
		objGet.style.top =offset.y+WhoSend.offsetHeight+'px';
		objGet.style.left =offset.x+'px';
		objGet.style.width =offset.width+'px';
		//objGet.style.display='block';
	},
	
	GetLocation : function (element) {
		var offsetX = 0;
		var offsetY = 0;
		var widthElt = element.offsetWidth;
		var heightElt = element.offsetHeight;
		var parent;
		for (parent = element; parent; parent = parent.offsetParent) {
			if (parent.offsetLeft)
				offsetX +=parent.offsetLeft;
			if (parent.offsetTop)
				offsetY +=parent.offsetTop;
		}
		return{x:offsetX,y:offsetY,width:widthElt,height:heightElt};
	},

	ReceiveServerData : function (context) {
		
		var args='';
		var select = document.getElementById(context);
		
		//debugger;
		
		if(this.obj.readyState == 4) {
			if(this.obj.status == 200) {
				args = this.obj.responseText;
			}
			else {
				//alert("Error retrieving data!" );
			}
		}
		else
		{
			this.hide(select);
			return;
		}

		if (args == null || args == '' || args.length == 0) {
			this.hide(select);
			return;
		}
		var j=0;
		var mass=args.split("\t");
		for (var i=select.length-1; i>-1; i--)
			select.options[i] = null;
		for (var i = 0; i < mass.length-1; i++) {
			select.options[j] = new Option(mass[i]);
			j++;
		}

		if (j !=0 )
			select.size = j+1;
		else
			select.size = 2;

		this.show(select);

		return;
	},
	
	ReturnValueToTextBoxs : function (SelectObj,TextObj) {
		if (SelectObj.selectedIndex!=-1) {
			TextObj.value=SelectObj.options[SelectObj.selectedIndex].text;
			TextObj.focus();
			this.SetSearchUrl(TextObj);
			this.hide(SelectObj);
		}
	},

	ReturnValueToTextBoxsWithoutClose : function (SelectObj,TextObj) {
		if (SelectObj.selectedIndex!=-1) {
			TextObj.value=SelectObj.options[SelectObj.selectedIndex].text;
			this.SetSearchUrl(TextObj);
		}
	},
	
	SelectItem : function (select,textbox,mouse,EventX) {
		TextObj=document.getElementById(textbox);
		SelectObj=select;
		if (mouse==true)
			this.ReturnValueToTextBoxs(SelectObj,TextObj);
		else {
			var e=EventX;
			if (e.keyCode==13)
				this.ReturnValueToTextBoxs(SelectObj,TextObj);
			else if(e.keyCode==27) {
				TextObj.focus();
				this.hide(SelectObj);
			}
			e.returnValue=false;
		}
	}
	
};


//Socail bookmarks
function NewOdnaknopka2() {
	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.imageButtonSrc = '/Client/Img/Odnaknopka/button.gif';
	this.imageIconsSrc = '/Client/Img/Odnaknopka/icons.gif';

	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);
		switch (system) {
			case 1: return 'http://bobrdobr.ru/addext.html?url='+url+'&title='+title;
			case 2: return 'http://memori.ru/link/?sm=1&u_data[url]='+url+'&u_data[name]='+title;
			case 3: return 'http://www.mister-wong.ru/index.php?action=addurl&bm_url='+url+'&bm_description='+title;
			case 4: return 'http://del.icio.us/post?v=4&noui&jump=close&url='+url+'&title='+title;
			case 5: return 'http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl='+url+'&lname='+title;
			case 6: return 'http://www.google.com/bookmarks/mark?op=add&bkmk='+url+'&title='+title;
			case 7: return 'http://text20.ru/add/?source='+url+'&title='+title+'&text='+this.selection();
			case 8: return 'http://pisali.ru/load_article/';
			case 9: return 'http://news2.ru/add_story.php?url='+url;
			case 10: return 'http://smi2.ru/add/';
			case 11: return 'http://myscoop.ru/add/?URL='+url+'&title='+title;
			case 12: return 'http://moemesto.ru/post.php?url='+url+'&title='+title;
			case 13: return 'http://www.ruspace.ru/index.php?link=bookmark&action=bookmarkNew&bm=1&url='+url+'&title='+title;
			case 14: return 'http://www.100zakladok.ru/save/?bmurl='+url+'&bmtitle='+title;
			case 15: return 'http://rumarkz.ru/bookmarks/?action=add&popup=1&address='+url+'&title='+title;
			case 16: return 'http://www.vaau.ru/submit/?action=step2&url='+url;
		}
	}

	this.hide=function() {
		if (this.timeout) clearTimeout(this.timeout);
		document.getElementById('odnaknopka').style.visibility='hidden';
	}
	
	this.show=function(element) {
		if (this.timeout) clearTimeout(this.timeout);
		var left=0,top=0;
		var style=document.getElementById('odnaknopka').style;
		while (element) {
			left+=element.offsetLeft;
			top+=element.offsetTop;
			element=element.offsetParent;
		}
		style.left=left+'px';
			style.top=(top+16)+'px';
		style.visibility='visible';
	}
	
	this.init=function() {
		var titles=new Array('&#1041;&#1086;&#1073;&#1088;&#1044;&#1086;&#1073;&#1088;','Memori','&#1052;&#1080;&#1089;&#1090;&#1077;&#1088; &#1042;&#1086;&#1085;&#1075;','del.icio.us','&#1071;&#1085;&#1076;&#1077;&#1082;&#1089;.&#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1082;&#1080;','&#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1082;&#1080; Google','&#1058;&#1077;&#1082;&#1089;&#1090; 2.0','&#1055;&#1080;&#1089;a&#1083;&#1080;','News2','&#1057;&#1052;&#1048; 2','AddScoop','&#1052;&#1086;&#1105;&#1052;&#1077;&#1089;&#1090;&#1086;','RuSpace','&#1057;&#1090;&#1086; &#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1086;&#1082;','RUmarkz','&#1042;&#1072;&#1072;&#1091;!');
		if (!document.getElementById('odnaknopka')) {
			var div=document.createElement('div');
			div.id='odnaknopka';
			div.style.position='absolute';
			div.style.visibility='hidden';
			div.style.width='260px';
			div.style.border='1px solid #aaa';
			div.style.background='#fff';
			div.style.margin='0';
			div.style.padding='0';
			div.style.overflow='hidden';
			div.style.zIndex='1000';
			div.style.font='normal 12px arial';
			div.style.lineHeight='20px';
			div.style.color='#666';
			html='';
			html+='<ul style="overflow:hidden;width:130px;float:left;list-style:none;margin:0;padding:0;text-align:left">';
			html+='<li style="overflow:hidden;height:20px;margin:0;padding:0;background:#ddd">&nbsp;&nbsp;&#1044;&#1086;&#1073;&#1072;&#1074;&#1080;&#1090;&#1100; &#1074;:</li>';
			for (var i=0;i<8;i++) {
				html+='<li style="overflow:hidden;height:20px;margin:0 0 '+(/MSIE/.test(navigator.userAgent)&&!window.opera?'-3px':'0')+' 2px;padding:0 0 0 16px;background:url(' + this.imageIconsSrc +') -'+(i*32)+'px 2px no-repeat"><a target="_blank" style="background:#fff;line-height:20px;float:left;width:100%;text-decoration:none;color:#666" href="'+this.url(i+i+1)+'"  onmouseover="this.style.background=\'#f0f0f0\';" onmouseout="this.style.background=\'#fff\';" title="'+titles[i+i]+'">&nbsp;'+titles[i+i]+'</a></li>';
			}
			html+='</ul>';
			html+='<ul style="overflow:hidden;width:130px;float:left;list-style:none;margin:0;padding:0;text-align:left">';
			html+='<li style="overflow:hidden;height:20px;margin:0;padding:0;background:#ddd;text-align:right">&copy;&nbsp;<a target="_blank" href="http://odnaknopka.ru/" style="color:#666;text-decoration:none">&#1054;&#1076;&#1085;&#1072;&#1050;&#1085;&#1086;&#1087;&#1082;&#1072;.&#1056;&#1091;</a>&nbsp;&nbsp;</li>';
			for (var i=0;i<8;i++) {
				html+='<li style="overflow:hidden;height:20px;margin:0 0 '+(/MSIE/.test(navigator.userAgent)&&!window.opera?'-3px':'0')+' 2px;padding:0 0 0 16px;background:url(' + this.imageIconsSrc +') -'+(i*32+16)+'px 2px no-repeat"><a target="_blank" style="background:#fff;line-height:20px;float:left;width:100%;text-decoration:none;color:#666" href="'+this.url(i+i+2)+'" onmouseover="this.style.background=\'#f0f0f0\';" onmouseout="this.style.background=\'#fff\';" title="'+titles[i+i+1]+'">&nbsp;'+titles[i+i+1]+'</a></li>';
			}
			html+='</ul>';
			div.innerHTML=html;
			div.onmouseover=function() {if (odnaknopka2.timeout) clearTimeout(odnaknopka2.timeout)}
			div.onmouseout=function() {odnaknopka2.timeout=setTimeout('odnaknopka2.hide()',500)};
			document.body.insertBefore(div,document.body.firstChild);
		}
		document.write('<img src="' + this.imageButtonSrc + '" width="136" height="16" alt="Социальные закладки" title="Социальные закладки" style="border:0;margin:0;padding:0;cursor:pointer;" onclick="odnaknopka2.show(this);" onmouseout="odnaknopka2.timeout=setTimeout(\'odnaknopka2.hide()\',500);">');
	}
}

//////////////////
//UserQuickMenu
function UserQuickMenu() {

	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.hide=function() {
		if (this.timeout) clearTimeout(this.timeout);
		document.getElementById('userQuickMenu').style.visibility='hidden';
	}
	
	this.show=function(element) {
		if (this.timeout) clearTimeout(this.timeout);
		var left=-50,top=0;
		var style=document.getElementById('userQuickMenu').style;
		while (element) {
			left+=element.offsetLeft;
			top+=element.offsetTop;
			element=element.offsetParent;
		}
		style.left=left+'px';
			style.top=(top+16)+'px';
		style.visibility='visible';
	}
	
	this.init=function() {
		var div = document.getElementById('userQuickMenu');
		div.onmouseover=function() {if (userQuickMenu.timeout) clearTimeout(userQuickMenu.timeout)}
		div.onmouseout=function() {userQuickMenu.timeout=setTimeout('userQuickMenu.hide()',1000)};
	}
	
}




jQuery.iSlideView = {
	build: function(user_options) {
		var defaults = {
			thumbPrefix: "",
			slideBy: 1,
			speed: 350
		};
		return jQuery(this).each(function() {
			var options = jQuery.extend(defaults, user_options);
			var config;

			var x = jQuery(this);
			var list = x.find(".slideView");
			var links = list.find("a");
			var images = list.find("img");

			//Prepare DOM
			x.prepend("<span class='desc'></span><span class='arrowL arrow'><</span><div class='wrap'><ul class='thumbList'></ul></div><span class='arrowR arrow'>></span>");
			var thumbList = x.find(".thumbList");
			var i = 0;
			images.each(function() {
				var l = jQuery(this);
				thumbList.append("<li>" +
					"<a href='#' id='" + (i + 1) + "'></a></li>");
				i++;
			});

			//Force to hide overflow
			x.css("overflow", "hidden");

			//Attach events
			var arrow = x.find(".arrow");
			var back = x.find(".arrowL");
			var next = x.find(".arrowR");
			var desc = x.find(".desc");
			var liW = thumbList.find("li").width();
			var xW = x.width();
			var xH = x.height();
			var thumbW = thumbList.width(); //458px
			var li = thumbList.find("li");
			var tLink = thumbList.find("a");
			var totalW = li.length * liW;
			var xTotalH = li.length * xH;

			back.hide();
			x.find(".wrap").fadeTo("fast", 0.6);

			desc.text(images.eq(0).attr("alt")).fadeTo("fast", 0.6).hover(function() {
				jQuery(this).fadeTo("fast", 0.9);
			}, function() {
				jQuery(this).fadeTo("fast", 0.6);
			});

			list.css("height", xTotalH);
			thumbList.css("width", totalW);

			var Pointer = 0;
			var total = li.length;

			next.click(function() {
				//debugger;
				if (Pointer == images.length - 1)
					Pointer = -1;
				var offsetL = parseInt(thumbList.css("left")) - liW * options.slideBy;

				thumbList.animate({
					left: offsetL + "px"
				}, options.speed, function() {
					if (Pointer == total - 1) {
						next.hide();
					}
					desc.text(images.eq(Pointer).attr("alt"));
					var offsetT = -(xH * Pointer);

					list.animate({
						top: offsetT
					}, options.speed);
				});
				Pointer++;
				if (Pointer != 0) {
					back.show().fadeTo("fast", 0.6);
				}

			});

			back.click(function() {
				if (Pointer == 0)
					Pointer = images.length;

				var offsetL = parseInt(thumbList.css("left")) + liW * options.slideBy;
				thumbList.animate({
					left: offsetL + "px"
				}, options.speed, function() {
					if (Pointer == 0) {
						back.hide();
						next.show().fadeTo("fast", 0.6);
					}
					desc.text(images.eq(Pointer).attr("alt"));
					var offsetT = -(xH * Pointer);
					list.animate({
						top: offsetT
					}, options.speed);

				});

				Pointer--;
			});

			tLink.hover(function() {
				var id = $(this).attr("id") - 1;
				var offsetT = -(xH * id);
				desc.text(images.eq(id).attr("alt"));
				list.animate({
					top: offsetT
				}, options.speed);
			}, function() {
				var offsetT = -(xH * Pointer);
				desc.text(images.eq(Pointer).attr("alt"));
				list.animate({
					top: offsetT
				}, options.speed);
			});

			thumbList.hover(function() {
				x.find(".wrap").fadeTo("fast", 0.8);
			},
			function() {
				x.find(".wrap").fadeTo("fast", 0.6);
			});

			arrow.fadeTo("fast", 0.6).hover(function() {
				jQuery(this).fadeTo("fast", 0.9);
			},
			function() {
				jQuery(this).fadeTo("fast", 0.6);
			}
			);

		});
	}
}

jQuery.fn.slideView = jQuery.iSlideView.build;

//Helping functions

function getThumbName(n, prefix) {
	var arr = n.split("/");
	var tl = n.length;
	var w = arr[arr.length - 1].length;
	return n.substr(0, tl - w - 1) + "/" + prefix + arr[arr.length - 1];
}

function focusTo(pointer, list, height, speed, desc, text) {
	//alert(pointer);
	var id = pointer - 1;
	var offsetT = -(height * id);
	list.animate({
		top: offsetT
	}, speed);
	desc.text(text);
}
