
	var GK = (navigator.userAgent.indexOf('Gecko') != -1) ? true : false;
	var OP = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
	var IE = (navigator.userAgent.indexOf('MSIE') != -1) ? true : false;
	
	function MessageBox(options) //options: title, content, width, height, element_selector
	{		
		if (!options.title) options.title = "";
		if (!options.content) options.content = "";
		var element = '<div class="message-box" title="'+ options.title +'">'+ options.content +'</div>';

		if (!options.width) options.width = 400;
		if (!options.height) options.height = 200;
		if (options.element_selector)
			element = options.element_selector;

		$(element).dialog({
			modal: true,
			width: options.width,
			height: options.height,
			close: function(event, ui) {
				$(this).dialog("destroy").remove();
			},
			buttons: {
				Ok: function() {
					$(this).dialog("destroy").remove();
				}
			}
		});
	}
	
	function upgradeForms()
	{
		//var strCookie = "de756ae5850e3cc9a3dd822c0d107052";
		for (i=0;i<document.forms.length;i++) 
		{
			if (document.forms[i].method == "post" && !document.forms[i].hash) 
			{
				var newTextField = document.createElement('input'); 
				newTextField.setAttribute('type','hidden'); 
				newTextField.setAttribute('name','hash'); 
				newTextField.value=global_hash; 
				document.forms[i].appendChild(newTextField);
			} 
		}
	}
	
	function isScrolledIntoView(scrolled_elem, elem)
	{
		var docViewTop = $(scrolled_elem).scrollTop();
		var docViewBottom = docViewTop + $(scrolled_elem).height();
		
		var elemTop = $(elem).position().top;
		var elemBottom = elemTop + $(elem).height();
		
		return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
	}
	
	function NormalizeUrl(url)
	{
		re = new RegExp("[\/]{2,}", "g");
		return url.replace(re, "/"); 
	}

	function radio_open_window()
	{
		window.open("http://citysakh.ru/public/radio.html", "LoveRadioOnline", "width=240, height=175,toolbar=no,scrollbars=no,resizable=no,status=no,left=100,top=100");
	}
	
	//from some forum
	function getXY(o) 
	{
		var c = {x:o.offsetLeft, y:o.offsetTop}, p = o.offsetParent;
		while (p) {
			c.x += p.offsetLeft;
			c.y += p.offsetTop;
			p = p.offsetParent;
		}
		return c;
	}
	//
	/*function getBounds(element)
	{
		var left = element.offsetLeft;
		var top = element.offsetTop;
		for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
		{
			left += parent.offsetLeft - parent.scrollLeft;
			top += parent.offsetTop - parent.scrollTop
		}
		return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
	}*/
	//from some site
	function getBounds(elem) 
	{
	    var box = elem.getBoundingClientRect()
	    var body = document.body
	    var docElem = document.documentElement

	    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
	    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft

	    var clientTop = docElem.clientTop || body.clientTop || 0
	    var clientLeft = docElem.clientLeft || body.clientLeft || 0

	    var top  = box.top +  scrollTop - clientTop
	    var left = box.left + scrollLeft - clientLeft
	 
	    return { top: Math.round(top), left: Math.round(left) }
	}


	function AbuseShowWnd(url_part1, url_part2)
	{
		var url = "";
		if (!url_part1 || url_part1 == "") url_part1 = "http://" + location.host + location.pathname + location.search;
		if (!url_part2) url_part2 = "";
		url = url_part1 + url_part2;
		window.open("/common/abuse.php?url=" + escape(url),
   			'abuseWnd','width=450,height=300,status=no,toolbar=no,menubar=no,resizable=yes');
	}
	function switch_block(id)
	{
		var block = document.getElementById(id);
		if (block.style.display == "block")
		{
			block.style.display = "none";
			document.getElementById(id + "_btn").src = "/common/img/widgets/block_close.png";
		} else {
			block.style.display = "block";
			document.getElementById(id + "_btn").src = "/common/img/widgets/block_open.png";
		}			
	}
	
	//-----------------------drop menu-----------------------------------
	//TODO: опции для различных вариантов выпадания	
	var DropMenu = {
		
		obj_id : "",
		
		SwitchDisplay : function(obj_id, event, obj, x_fix, y_fix)
		{
			this.obj_id = obj_id;
			var menu_items_style = document.getElementById(obj_id + "_items").style;
			var main_menu = document.getElementById(obj_id);
			var target_id = (IE) ? event.srcElement.id : event.target.parentNode.id;
			if (menu_items_style.display == "none") 
			{
				//var main_menu_title = document.getElementById(obj_id + "_title");
				var main_menu_title = document.getElementById(obj.id);
				var pos = getXY(main_menu_title);
				//TODO: избавиться от ИЕ-фиксов
				if (!x_fix) x_fix = 0;
				if (!y_fix) y_fix = 0;
				var pos_x_fix = 5 + x_fix;
				var pos_y_fix = 5 + y_fix;
				if (IE) {				
					pos_x_fix = 45;
					pos_y_fix = pos_y_fix + (-10);
				}				
				menu_items_style.left = (pos.x - pos_x_fix) + "px";
				menu_items_style.top = (pos.y - main_menu_title.offsetHeight - pos_y_fix) + "px";
				menu_items_style.display = "block";
				var o = this;
				document.getElementById(this.obj_id).onmouseout = function() {o.StartTimer();};
				document.getElementById(this.obj_id).onmouseover = function() {o.ClearTimer();};
				//addHandler(main_menu, "mouseout", this.StartTimer);
				//addHandler(main_menu, "mouseover", this.ClearTimer);
				
			}
		},
		
		SwitchDisplay2 : function(obj_id, event, obj, x_fix, y_fix)
		{
			this.obj_id = obj_id;
			var menu_items_style = document.getElementById(obj_id + "_items").style;
			var main_menu = document.getElementById(obj_id);
			var target_id = (IE) ? event.srcElement.id : event.target.parentNode.id;
			if (menu_items_style.display == "none") 
			{
				//var main_menu_title = document.getElementById(obj_id + "_title");
				var main_menu_title = document.getElementById(obj.id);
				var bounds = getBounds(main_menu_title);
				if (!x_fix) x_fix = 0;
				if (!y_fix) y_fix = 0;
				menu_items_style.left = (bounds.left + x_fix) + "px";
				menu_items_style.top = (bounds.top + y_fix) + "px";
				menu_items_style.display = "block";
				var o = this;
				document.getElementById(this.obj_id).onmouseout = function() {o.StartTimer();};
				document.getElementById(this.obj_id).onmouseover = function() {o.ClearTimer();};
				//addHandler(main_menu, "mouseout", this.StartTimer);
				//addHandler(main_menu, "mouseover", this.ClearTimer);
				
			}
		},
		
		StartTimer : function()
		{
			var o = this;
			this.dropMenu_Timeout = setTimeout(function() {o.HideMenu();}, 400);
		},
		
		ClearTimer : function()
		{
			if(typeof this.dropMenu_Timeout == "number") {
				clearTimeout(this.dropMenu_Timeout); 
				delete this.dropMenu_Timeout;
			}
		},
		
		HideMenu : function()
		{
			document.getElementById(this.obj_id + "_items").style.display = "none";
			//removeHandler(document.getElementById(this.obj_id), "mouseout", this.StartTimer);
			//removeHandler(document.getElementById(this.obj_id), "mouseover", this.CleraTimer);
			document.getElementById(this.obj_id).onmouseout = null;
			document.getElementById(this.obj_id).onmouseover = null;
			this.ClearTimer();
		}
		
	}	
	//-----------------------/drop menu-----------------------------------
	
	function GetSymbolsCount(sourceObject, destObject)
	{
		setHtml(destObject, getValue(sourceObject).length);
	}
	
	function GetSymbolsCountRemain(sourceObject, destObject, max_simbols)
	{
		var simbols_remain = max_simbols - getValue(sourceObject).length;
		if (simbols_remain < 0) simbols_remain = 0;
		setHtml(destObject, simbols_remain);
	}
	
	function showLoginForm(obj)
	{
		var login_form = document.getElementById("login_form_inline");
		if (obj) {
			var pos = getXY(obj);
			login_form.style.left = pos.x;
			login_form.style.top = pos.y + obj.offsetHeight;
		}
		login_form.style.display = (login_form.style.display == "none") ? "block" : "none";
		
	}

	function addHandler(object, event, handler) 
	{
		if (typeof object.addEventListener != 'undefined')
			object.addEventListener(event, handler, true);
		else if (typeof object.attachEvent != 'undefined')
			object.attachEvent('on' + event, handler);
	}
	
	function removeHandler(object, event, handler)
	{
		if (typeof object.removeEventListener != 'undefined')
			object.removeEventListener(event, handler, true);
		else if (typeof object.detachEvent != 'undefined')
			object.detachEvent('on' + event, handler);			
	}
	
	//tooltip
	//depends: jquery, jquery.position
	function Tooltip(options) //class_name, default_content, content_callback
	{
		this.popup = false;
		this.content_callback = options.content_callback;
		this.class_name = (options.class_name) ? options.class_name : "tooltip";

		var elem = document.createElement("div");
		elem.style.display = "none";
		elem.style.position = "absolute";
		elem.style.top = "0px";
		elem.style.left = "0px";
		elem.innerHTML = (options.default_content) ? options.default_content : "";
		elem.className = this.class_name;
		document.body.appendChild(elem);
		this.popup = elem;
		
		this.StartShowTimer = function(sender)
		{
			var o = this;
			$(sender).mouseleave(function() {o.ClearShowTimer();});
			this.dropMenu_Timeout = setTimeout(function() {o.Show(sender);}, 300);
		};
		this.ClearShowTimer = function()
		{
			if(typeof this.dropMenu_Timeout == "number") {
				clearTimeout(this.dropMenu_Timeout); 
				delete this.dropMenu_Timeout;
			}
		};
		
		this.Show = function(sender)
		{
			if (this.popup.style.display == "none") 
			{
				$(this.popup).show().position({
					of: $(sender),
					my: "left top",
					at: "left bottom"
				});
				
				//$(this.popup).html(this.content_callback());
				this.content_callback(this.popup);
				
				
				var o = this;
				$(sender).mouseleave(function() {o.StartHideTimer();});
				$(sender).mouseenter(function() {o.ClearHideTimer();});
			}
		};
		
		this.StartHideTimer = function()
		{
			var o = this;
			this.dropMenu_Timeout = setTimeout(function() {o.HideMenu();}, 200);
		};
		this.ClearHideTimer = function()
		{
			if(typeof this.dropMenu_Timeout == "number") {
				clearTimeout(this.dropMenu_Timeout); 
				delete this.dropMenu_Timeout;
			}
		};
		
		this.HideMenu = function()
		{
			$(this.popup).hide();
			$(this.popup).unbind();			
			this.ClearHideTimer();
		};
	}
	//
	
	var UserInfoPopup = {
		
		popup : false,
		users_data : new Array(),
		
		Init : function()
		{
			var elem = document.createElement("div");
			elem.id = "user_info_popup";
			elem.style.display = "none";
			document.body.appendChild(elem);
			this.popup = elem;
		},
		
		StartShowTimer : function(sender, event, login)
		{
			if (!this.popup) this.Init();
			var o = this;
			sender.onmouseout = function() {o.ClearShowTimer()};
			this.dropMenu_Timeout = setTimeout(function() {o.Show(sender, event, login);}, 300);
		},
		ClearShowTimer : function()
		{
			if(typeof this.dropMenu_Timeout == "number") {
				clearTimeout(this.dropMenu_Timeout); 
				delete this.dropMenu_Timeout;
			}
		},
		
		Show : function(sender, event, login)
		{
			if (login == "Citysakh.ru") return false;
			if (this.popup.style.display == "none") 
			{
				var bounds = getBounds(sender);
				x_fix = -10;
				y_fix = -16;
				this.popup.style.left = (bounds.left + x_fix) + "px";
				this.popup.style.top = (bounds.top + y_fix) + "px";
				this.popup.style.display = "block";
				
				this.GetUserInfo(login);
				
				var o = this;
				this.popup.onmouseout = function() {o.StartHideTimer();};
				this.popup.onmouseover = function() {o.ClearHideTimer();};
			}
		},
		
		StartHideTimer : function()
		{
			var o = this;
			this.dropMenu_Timeout = setTimeout(function() {o.HideMenu();}, 200);
		},		
		ClearHideTimer : function()
		{
			if(typeof this.dropMenu_Timeout == "number") {
				clearTimeout(this.dropMenu_Timeout); 
				delete this.dropMenu_Timeout;
			}
		},
		
		HideMenu : function()
		{
			this.popup.style.display = "none";
			this.popup.onmouseout = null;
			this.popup.onmouseover = null;			
			this.ClearHideTimer();
		},
		
		GetUserInfo : function(login)
		{
			//var data_index = this.get_index_by_login(login);
			//if (data_index !== false) {
			if (this.users_data[login] && this.users_data[login].proceed == false) {
				this.popup.innerHTML = this.users_data[login].html;
				//document.getElementById("user_info_popup").innerHTML = this.users_data[data_index].html;
			} else if (!this.users_data[login]) {
				this.popup.innerHTML = "<b>Загрузка...</b>";
				this.users_data[login] = {proceed: true};
				var vars = {
					action: "userinfo",
					login: login
				}
				try {
					var obj = this;
					sendXmlHttpRequest("post", "/common/actions/general.php", xhParseParams(vars), function(jsHttp) {
						eval("var json = " + jsHttp.responseText);
						if (json.Message.Type == "info") 
						{
							if (json.Result == true) 
							{
								var html = UserInfoPopup.FormatPopup(json.user_info);
								obj.users_data[json.user_info.login].html = html;
								obj.popup.innerHTML = html;
								obj.users_data[login].proceed = false;
							}
						} else {
							UserInfoPopup.popup.innerHTML = json.Message.Type + ":<br>" + json.Message.Text;
						}
					});
				} catch (e) {
					UserInfoPopup.popup.innerHTML = e;
				}
			}
		},
		
		FormatPopup : function(data)
		{
			var html = '<div><div style="float: left;"><table>';
			html += '<tr><td colspan="2" style="font-size: 1.1em;"><img src="/common/img/icons/'+data.sex+'2.png"> <b>'+data.login+'</b>'; 
			html += ' (<a class="small_link" href="http://citysakh.ru/Profile?user='+data.login+'">профиль</a>)</td></tr>';
			if (data.online)
				html += '<tr><td colspan="2"><b style="color: gray;">Online</b></td></tr>';
			html += '<tr><td><b>Пол:</b></td><td>'+data.sex_title+'</td></tr>';
			if (data.birthday)
				html += '<tr><td><b>Дата рождения:</b></td><td>'+data.birthday+'</td></tr>';
			if (data.city)
				html += '<tr><td><b>Город:</b></td><td>'+data.city+'</td></tr>';
			html += '<tr><td><b>Дата регистрации:</b></td><td>'+data.signday+'</td></tr>';
			html += '<tr><td colspan="2"><a href="http://citysakh.ru/PM?to='+data.login+'#new">Написать личное сообщение</a></td></tr>';
			html += '</table></div>';
			html += '<div style="float: right; padding-left: 10px;">';
			if (data.counts.forum > 0)
				html += '<a href="http://forum.citysakh.ru/search/?user='+data.login+'">Форум (<b>'+data.counts.forum+'</b>)</a><br>';
			if (data.counts.photos > 0)
				html += '<a href="http://foto.citysakh.ru/users/'+data.login+'">Фотографии (<b>'+data.counts.photos+'</b>)</a><br>';
			if (data.counts.blogs > 0)
				html += '<a href="http://blogs.citysakh.ru/users/'+data.login+'">Блоги (<b>'+data.counts.blogs+'</b>)</a><br>';
			if (data.counts.nlife > 0)
				html += '<a href="http://citysakh.ru/nightlife/fotoreports/?userphotos='+data.login+'">Ночная жизнь (<b>'+data.counts.nlife+'</b>)</a><br>';
			if (data.counts.creativity > 0)
				html += '<a href="http://citysakh.ru/creativity/search/?show=user&login='+data.login+'">Творчество (<b>'+data.counts.creativity+'</b>)</a><br>';
			if (data.counts.video > 0)
				html += '<a href="http://video.citysakh.ru/users/'+data.login+'">Видео (<b>'+data.counts.video+'</b>)</a><br>';
			html += '</div><div style="clear: both;"></div></div>';
			return html;
		}
		
		/*get_index_by_login : function(login)
		{
			for (var i=0;i<this.users_data.length;i++) {
				if (this.users_data[i].login == login) return i;
			}
			return false;
		}*/
	}
	
	var JsUrl = {
		
		jsurl_params : [],
		current_hash : false,
		
		watch_timer : null,		
		onUrlChange : null,
		
		getLocationHash : function()
		{
			return location.hash.substring(1, location.hash.length);
		},
		
		ParseHash : function(address)
		{
			var hash = (address) ? address : this.getLocationHash();
			this.current_hash = hash;
			this.jsurl_params = (hash == "") ? [] : hash.split("/", 10);
		},
		
		GetParam : function(num)
		{
			if (this.current_hash === false || this.current_hash != this.getLocationHash()) this.ParseHash();
			return (this.jsurl_params[num]) ?  this.jsurl_params[num] : "";
		},
		
		StartUrlWatch : function()
		{
			var o = this;
			this.watch_timer = setInterval(function() {o.UrlWatch();}, 200);
		},		
		StopUrlWatch : function()
		{
			if(typeof this.watch_timer == "number") {
				clearInterval(this.watch_timer); 
				delete this.watch_timer;
			}
		},
		UrlWatch : function()
		{
			if (this.current_hash !== this.getLocationHash()) {
				this.ParseHash();
				if (this.onUrlChange != null)
					this.onUrlChange.method.call(this.onUrlChange.object);
			}
		}
	}
	
	function Fader(objId, afterFadeout)
	{
		this.fadeObj = document.getElementById(objId);
		this.interval;
		this.afterFadeout = afterFadeout; //after - "del" - delete element, "hide" - hide element, "" - ничего не делать
		//this.callBack;
		
		this.Opacity;		
		this.curHeight;
		
		this.fadeOut = function(callBack) {
			this.Opacity = 10;
			//this.callBack = callBack;
			if (IE) {
				this.fadeObj.style.filter = "alpha(opacity=100)";
				this.fadeObj.style.height = "100%";
				//this.fadeObj.style.backgroundColor = "";
			} else {
				this.fadeObj.style.opacity = "1";
			}
			var oInstance = this;
			this.interval = setInterval(function() {oInstance.setOpacity(-1);}, 50);
		}

		this.fadeIn = function() {
			this.Opacity = 0;
			if (IE) {
				this.fadeObj.style.filter = "alpha(opacity=0)";
				this.fadeObj.style.height = "100%";
				//this.fadeObj.style.backgroundColor = "";
			} else {
				this.fadeObj.style.opacity = "0";
			}
			var oInstance = this;
			this.interval = setInterval(function() {oInstance.setOpacity(1);}, 50);
		}
		
		this.setOpacity = function(dir)
		{
			this.Opacity = this.Opacity + (dir * 1);
			if ((this.Opacity > 0 && dir == -1) || (this.Opacity < 10 && dir == 1)) {
				if (IE) {
					this.fadeObj.style.filter = "alpha(opacity="+ this.Opacity*10 +")";
				} else {
					this.fadeObj.style.opacity = this.Opacity/10;
				}
			} else {
				clearInterval(this.interval);
				if (this.afterFadeout != "" && dir == -1) {
					this.fadeObj.style.overflow = "hidden";
					this.curHeight = this.fadeObj.offsetHeight;
					
					var oInstance = this;
					this.interval = setInterval(function() {oInstance.setSize();}, 5);
				}// else if (this.callBack) this.callBack();
			}
		}
		
		this.setSize = function()
		{
			this.curHeight = this.curHeight  - 10;
			if (this.curHeight > 0) {
				this.fadeObj.style.height = this.curHeight + "px";
			} else {
				clearInterval(this.interval);
				
				if (this.afterFadeout == "del") {
					this.fadeObj.parentNode.removeChild(this.fadeObj);
				} else if (afterFadeout == "hide") {
					this.fadeObj.style.display = "none";
					
					//!
					this.fadeObj.style.height = "100%";					
				}
				//if (!this.callBack) this.callBack();			
			}
		}

	}
	
	function transformXML(xml, xslUrl, elementId)
	{
		if (!IE) {
			var processor = new XSLTProcessor();
			var xslStylesheet = document.implementation.createDocument("", "", null);
			xslStylesheet.load(xslUrl);
			xslStylesheet.onload = function() {
				processor.importStylesheet(xslStylesheet);
				var fragment = processor.transformToFragment(xml, document);
				var postsList = document.getElementById(elementId);
				postsList.innerHTML = "";
				postsList.appendChild(fragment);
			}
		} else {
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc = xml;

			var xsl = new ActiveXObject("Microsoft.XMLDOM");
			xsl.async = false;
			xsl.load(xslUrl);

			var r = xmlDoc.transformNode(xsl);
			var postsList = document.getElementById(elementId);
			postsList.innerHTML = r;			
		}
	}
	
	function switchDisplay(blockId, display)
	{
		var block = document.getElementById(blockId);
		if ((block.style.display == "block" && !display) || (!block.style.display && !display) || display == false) {
			block.style.display = "none";
			return false;
		}
		else if (block.style.display == "none" || display == true) {
			block.style.display = "block";
			return true;
		}
	}
	
	function getValue(elementId)
	{
		return document.getElementById(elementId).value;
	}
	
	function setValue(elementId, value)
	{
		document.getElementById(elementId).value = value;
	}
	
	function getHtml(elementId)
	{
		return document.getElementById(elementId).innerHTML;
	}
	
	function setHtml(elementId, html)
	{
		document.getElementById(elementId).innerHTML = html;
	}
	
	function get(query)
	{
		switch (query.charAt(0)) {
			case "#":
				return document.getElementById(query.substring(1, query.length));
				break;
			case ".":
				return document.getElementsByClassName(query.substring(1, query.length));
				break;
		}
	}
	
	//cookie
	//взято откуда-то...
	function GetCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return false;
	}

