function dumpObject(obj)
{
	s = obj.toString()+': ';
	for (a in obj) s += a+', ';
	alert(s);
}

function showShadow(w, h)
{
	var id = $('idShadow');
	if (!id)
	{
		id = document.createElement("DIV");
		id.className  = 'shadow';
		id.id = 'idShadow';
		document.body.appendChild(id);
		id = Element.extend(id);
	}
	id.setOpacity(0.5);
	id.setStyle({display:'block', height:h+'px', width:w+'px'});
	return id;
}

function hideShadow()
{
	return $('idShadow').hide();
}

/*
function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = document.title;
  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
}
*/
var core = new Object();
/*
core.onloadObjects = new Array();

core.onload = function ()
{
	for (var i=0; i<core.onloadObjects.length; i++)
	{
		core.onloadObjects[i].onload();
	}
}
*/
core.logout = function ()
{
	if (confirm("Вы дейтвительно хотите выйти?"))
	{
		document.location = '?act=logout'
	}
}


function getPos(el)
{
	if (!el || !el.offsetParent) return false;
	var res = new Array()
	res["left"] = el.offsetLeft;
	res["top"] = el.offsetTop;
	var objParent = el.offsetParent;
	while (objParent.tagName.toLowerCase() != "body")
	{
		res["left"] += parseInt(objParent.offsetLeft);
		res["top"] += parseInt(objParent.offsetTop);
		objParent = objParent.offsetParent;
	}
	res["right"] = res["left"] + el.offsetWidth;
	res["bottom"] = res["top"] + el.offsetHeight;
	return res;
}

core.loadOnline = function()
{
	new Ajax.Request('/online/', { 
	    method:'get',
	    onSuccess: function(transport){
			$('idOnlinePanel').update(transport.responseText || "ошибка получения данных");
			//tooltip.refreshTooltips();
			//document.write('<sc'+'ript type="text/javascript">tooltip.refreshTooltips();</'+'sc'+'ript>');
	    }
  	});
}

function register(e)
{
	if (!e) e = window.event;
	if (e.ctrlKey)
	{
		switch(e.keyCode)
		{
			case 37:
				id = $('id_prev');
				if (id) location.href = id.href;
				break;
			case 39:
				id = $('id_next');
				if (id) location.href = id.href;
				break;
		}
	}
}

showed = false;
function showGroup(n)
{
	var g = 'profileGroup'+n;
	var a = 'profileGroup'+n+'a';
	if ((showed != false) && (showed == g))
	{
		new Effect.SlideUp(g);
		$(a).className = 'group';
		showed = false;
		return;
	}
	
	if(showed)
	{
		$(showed+'a').className = 'group';
		new Effect.SlideUp(showed);
	}
	
	new Effect.SlideDown(g);
	$(a).className = 'groupinv';
	showed = 'profileGroup'+n;
}

function response(id, user)
{
	$('id_response_to_val').value = id;
	$('id_response_to').update("<b>Кому: &#171;"+user+"&#187;</b><a href='javascript:response_del()'><img src='http://static.mobime.ru/i/clear.gif' width='16px' height='16px' align='absmiddle' border='0'/></a>");
}

function response_del()
{
	$('id_response_to_val').value = 0;
	$('id_response_to').update('');
}

function view_com(t, id)
{
	var o = $('idqam'+id);
	var d = $('idqamb'+id);

	if ((d.innerHTML != '') && (o.className != 'qam'))
	{
		new Effect.SlideUp(d);
		o.className = 'qam';
	}
	else
	{
		if (d.innerHTML == '')
		{
			d.style.display = 'none';
			o.className = 'qamr';
			//o.innerHTML = 'загрузка';
			new Ajax.Request('/comments/mini/'+t+'.html?id='+id, { 
			method:'get',
		    onSuccess: function(transport){
		    	d.className = 'qamb';
		    	d.update(transport.responseText || "ошибка получения данных");
		    	setTimeout("new Effect.SlideDown($('idqamb"+id+"'));$('idqam"+id+"').className = 'qamb';", 500);
		    }
			});
		}
		else
		{
			d.className = 'qamb';
			o.className = 'qamb';
			new Effect.SlideDown(d);
		}
	}
}

function gorating(n,v,id)
{
	ar = {1:'news',2:'articles',3:'reviews'};
	new Ajax.Request('/'+ar[n]+'/rating.html?v='+v+'&id='+id, { 
    method:'get',
    onSuccess: function(transport){
		$('idRating').update(transport.responseText || "ошибка получения данных");
    }
	});
}

function phoneRating(id, b)
{
	b.disabled = 'on';
	new Ajax.Request('/phones/rating.html', 
	{ 
	parameters:{id:id, r:$F('idRat_r'), b:$F('idRat_b'), g:$F('idRat_g')},
    method:'get',
    onSuccess: function(transport){
		$('idRating').update(transport.responseText || "ошибка получения данных");
    }
	});
}

function loadTopPanels()
{
	$('tpPlace').update('<center>Загрузка...</center>');
	new Ajax.Request('/toppanels/', { 
    method:'get',
    onSuccess: function(transport){
		$('tpPlace').update(transport.responseText);
		var tpi0 = $('tpi0');
		var tpi1 = $('tpi1');
		var tpi2 = $('tpi2');
		tpi0.setOpacity(0).setStyle({display:'block'});
		tpi1.setOpacity(0).setStyle({display:'block'});
		tpi2.setOpacity(0).setStyle({display:'block'});
		
		new Effect.Parallel (
			[
			new Effect.Opacity('tpi0', { sync: true, to: 1.0, from: 0.0 }),
			new Effect.Opacity('tpi1', { sync: true, to: 1.0, from: 0.0 }),
			new Effect.Opacity('tpi2', { sync: true, to: 1.0, from: 0.0 }),
			new Effect.SlideDown('tpt0'),
			new Effect.SlideDown('tpt1'),
			new Effect.SlideDown('tpt2')
			],
			{ duration: 1 }
		);
		
    }
	});
}

function printMini()
{
	imgRambler = new Image();
	imgRambler.src = 'http://counter.rambler.ru/top100.cnt?923016&rn='+Math.random();
	
	imgLI = new Image();
	imgLI.src = 'http://counter.yadro.ru/hit;Mobime?r'+escape(document.referrer)+((typeof(screen)=='undefined')?'':';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+';u'+escape(document.URL)+';'+Math.random();
}

function printCounters()
{
	document.write('<a href="http://www.liveinternet.ru/click;Mobime" target=_blank><img src="http://counter.yadro.ru/logo;Mobime?52.1" border=0 width=88 height=31></a>');
}

Grip = function() 
{
	this.staticOffset = 0;
	this.textarea = '';
}

Grip.prototype = {
	startDrag : function(t, e) 
	{
		Event.observe(document, 'mousemove', grip.performDrag);
		Event.observe(document, 'mouseup', grip.endDrag);
		this.textarea = $(t);
		this.staticOffset = $(t).offsetHeight - Event.pointerY(e);
		return false;
	},
	
	performDrag : function(e) 
	{
		grip.textarea.setStyle({height: Math.max(48, grip.staticOffset + Event.pointerY(e)) + 'px'});
		return false;
	},

	endDrag : function(e) 
	{
		Event.stopObserving(document, 'mousemove', grip.performDrag);
		Event.stopObserving(document, 'mouseup', grip.endDrag);
	}
}

Wiki = function () {this.invokelist = new Array();}

Wiki.prototype = {
	
	getCursor : function(o)
	{
	  var result = { start: 0, end: 0 };
	  if (o.setSelectionRange)
	  {
	    result.start= o.selectionStart;
	    result.end = o.selectionEnd;
	  }
	  else if (!document.selection) { return 0; }
	  else if (document.selection && document.selection.createRange)
	  {
	    var range = document.selection.createRange();
	    var stored_range = range.duplicate();
	    stored_range.moveToElementText(o);
	    stored_range.setEndPoint('EndToEnd', range);
	    result.start = stored_range.text.length - range.text.length;
	    result.end = result.start + range.text.length;
	  }
	  return result;
	},

	setCursor : function(o, start, end)
	{
		if (o.createTextRange)
		{
			var range = o.createTextRange();
			range.move("character", start);
			range.select();
		} 
		else
		if (o.selectionStart) 
		{
			o.setSelectionRange(start, end);
		}
	},

	custom : function(id, ibTagStart, ibTagEnd, val)
	{
		var o = $(id);
		var p = 0;
		o.focus();
		var cur = this.getCursor(o);
		var top = o.scrollTop;
		if (document.selection)
		{
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if (typeof(val)=='undefined')
			{
				v = ibTagStart + rng.text + ibTagEnd;
				p = cur.end+ibTagStart.length;
			}
			else 
			{
				v = rng.text + val;
				p = cur.end+val.length;
			}
			rng.text = v;
		}
		else
		{
			var ss = o.selectionStart;
			var es = o.selectionEnd;
			
			if (es <= 2)
			{
				es = o.textLength;
			}
			
			var start  = (o.value).substring(0, ss);
			var middle = (typeof(val)=='undefined')?(o.value).substring(ss, es):val;
			var end    = (o.value).substring(es, o.textLength);
			
			middle = ibTagStart + middle + ibTagEnd;
			o.value = start + middle + end;
			
			var p = ss + (middle.length) - ibTagEnd.length;
		}
		
		this.setCursor(o, p, p);
		if (top) o.scrollTop = top;
		o.focus();
	},
	
	bold : function (id)
	{
		this.custom(id, '**', '**');
	},
	
	italic : function (id)
	{
		this.custom(id, '//', '//');
	},
	
	stroke : function (id)
	{
		this.custom(id, '<del>', '</del>');
	},
	
	underline : function (id)
	{
		this.custom(id, '__', '__');
	},
	
	user : function (id)
	{
		var name = prompt("Введите имя пользователя", "");
		if (name) this.custom(id, '', '', '@'+name);
	},
	
	video : function (id)
	{
		var url = prompt("Введите URL на видео в youtube или rutube", "http://");
		if (url) this.custom(id, '', '', '{{'+url+'}}');
	},
	
	image : function (id)
	{
		if (typeof(this.invokelist['image']) == 'string') eval(this.invokelist['image']+"('"+id+"')");
	},
	
	link : function (id)
	{
		var o = $(id);
		if (o.setSelectionRange)
		{
			var s = o.selectionStart != o.selectionEnd;
		}
		else
		{
			var range = document.selection.createRange();
			var s = range.text != '';
		}
		if (!s) 
		{
			alert('Выделите текст, который хотите сделать ссылкой');
			return;
		}
		var url = prompt("Введите URL ссылки", "http://");
		if (!/^(http|https|ftp)\:\/\/[0-9A-Za-z][0-9A-Za-z\-\.]*\.[a-zA-Z]{2,4}/.test(url)) 
		{
			alert('Это не URL');
			return;
		}
		this.custom(id, '[['+url+'|', ']]');
	},
	
	getLines : function (o)
	{
		var cur = this.getCursor(o);
		
		if (document.selection)
		{
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
	
			var ss = cur.start;
			var es = cur.end;
		}
		else
		{
			var ss = o.selectionStart;
			var es = o.selectionEnd;
		}
		
		if (o.value.substring(ss-1, ss) != '\n') 
		{
			ss = o.value.substring(0, ss).lastIndexOf('\n')+1;
		}
		if (o.value.substring(es, es+1) != '\n') 
		{
			es = es + o.value.substring(es+1, o.value.length).indexOf('\n')+1;
		}
		return { start: ss, end: es };
	},
	
	quote : function (id)
	{
		var o = $(id);
		o.focus();
		var lines = this.getLines(o);
		ss = lines.start;
		es = lines.end;
		var v = o.value.substring(ss, es);
	
		var start  = (o.value).substring(0, ss);
		var end    = (o.value).substring(es, o.textLength);
		var p = v.split('\n');
		v = '';
		for(i=0; i<p.length; i++) 
		{
			var s = p[i].replace(/(^\s+)|(\s+$)/g, '');
			var f = s.substring(0, 1);
			v += '\n'+((f == '>')?'>':'> ')+s;
		}
		v = v.substring(1);
		o.value = start + v + end;
		var p = ss + v.length;
		this.setCursor(o, p, p);
		o.focus();
	},
	
	list : function(id, t)
	{
		var o = $(id);
		o.focus();
		var lines = this.getLines(o);
			
		ss = lines.start;
		es = lines.end;
		var v = o.value.substring(ss, es);
	
		var start  = (o.value).substring(0, ss);
		var end    = (o.value).substring(es, o.textLength);
		var p = v.split('\n');
		v = '';
		for(i=0; i<p.length; i++) 
		{
			var s = p[i].replace(/(^\s+)|(\s+$)/g, '');
			var f = s.substring(0, 1);
			if (['-', '*'].include(f)) s = s.substring(1);
			v += '\n  '+t+s;
		}
		v = v.substring(1);
		o.value = start + v + end;
		var p = ss + v.length;
		this.setCursor(o, p, p);
		o.focus();
	},
	
	ol : function (id)
	{
		this.list(id, '*');
	},
	
	ul : function (id)
	{
		this.list(id, '-');
	},
	
	linebreak : function (id)
	{
		this.custom(id, '', '', '\n\n-------------------------\n\n');
	}
}

Editor = function()
{
	this.id = null;
}

Editor.prototype = {
	showFileDialog : function (id)
	{
		this.id = id;
		var e = document.documentElement;
		var b = document.body;
		var h = e.clientHeight ? e.clientHeight : b.scrollHeight;
		var w = e.clientWidth ? e.clientWidth : b.scrollWidth;
		idShadow = showShadow(w, h);
		idShadow.observe('mousedown', editor.hideFileDialog);

		$('idImageAlt').value = '';
		$('idImagePosition').value = 1;
		$('idImageSrc').value = 'http://';
		$('idImageBrowse').value = '';
		
		var shift = document.documentElement.scrollTop || document.body.scrollTop;
		h = (((window.innerHeight ? window.innerHeight : b.clientHeight)-400)/2)+shift+'px';
		
		$('imageForm').setStyle({display:'block', top:h, left:((w-300)/2)+'px'});
		if (Prototype.Browser.IE)
		{
			skip = $('idImagePosition');
			$$('SELECT').each(function(el){if (el != skip) el.setStyle({visibility:'hidden'})});
		}

		$$('EMBED').each(function(el){el.setStyle({visibility:'hidden'})});
	},
	
	hideFileDialog : function ()
	{
		idShadow = hideShadow();
		Event.stopObserving(idShadow, 'mousedown', editor.hideFileDialog);
		$('imageForm').hide();

		$$('SELECT').each(function(el){el.setStyle({visibility:''})});
		$$('EMBED').each(function(el){el.setStyle({visibility:''})});
	},
	
	insertImage : function (name)
	{
		var alt = $F('idImageAlt').replace(/(^\s+)|(\s+$)/g, '');
		switch ($F('idImagePosition'))
		{
			case '2': name = name+" "; break;
			case '3': name = " "+name; break;
		}
		if (alt != '') name += '|'+alt;
		wiki.custom(this.id, '', '', '{{'+name+'}}');
		this.hideFileDialog();
	}
}

wiki = new Wiki();
grip = new Grip();
editor = new Editor();