
// by EVIL without good know JS =))

var userAgent = navigator.userAgent.toLowerCase();
var is_ie  = (userAgent.indexOf('msie') != -1);
var is_moz = (navigator.product == 'Gecko');

String.prototype.prolength = function()
{
	// И не только ie...
	return (this.indexOf('\n') != -1) ? this.replace(/\r?\n/g, '_').length : this.length;
}

// js: cookies

function in_array(what, where)
{
	var needle = new String(what);
	for (var w in where)
		if (where[w] == needle)
			return w;
	return null;
}

function add_cookie(name, value, expires)
{
	stand = new Date(); // expires... O_o
	stand.setTime(stand.getTime() + expires);
	document.cookie = name + '=' + escape(value) + '; path=/' + '; expires=' + stand.toGMTString();
}

function delete_cookie(name)
{
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

function get_cookie(name)
{
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1)
				value_end = cookie_length;
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0)
			break;
	}
	return null; // false...
}

function sw(id)
{
	var con = new Array('today', 'tomorrow');

	var e = document.getElementById(id);
	var v = e.style.display;

	if (v == 'none')
	{
		e.style.display = '';

		if (in_array(id, con)) 
			delete_cookie(id);
		else
			add_cookie(id, '1', 900000);
	}
	else
	{
		e.style.display = 'none';

		if (in_array(id, con)) 
			add_cookie(id, '1', 900000);
		else
			delete_cookie(id);
	}

	return false;
}

function switcher()
{
	var els = new Array('article', 'download', 'allsynet', 'today', 'tomorrow');

	for (var e in els)
	{
		get = get_cookie(els[e]);
		if (get) sw(els[e]);
	}
}

// Текст.

var s = "";
var myform = "mainform";
var myarea = "mainarea";
var myhead = "mainhead";

function g()
{
	msgfield = document.forms[myform][myarea];
	// if (typeof(msgfield.selectionStart) != 'undefined')
	if (msgfield.selectionStart !== msgfield.selectionEnd)
		s = msgfield.value.substr(msgfield.selectionStart, msgfield.selectionEnd - msgfield.selectionStart);
	else if (document.selection && document.selection.createRange)
		s = document.selection.createRange().text;
	else if (window.getSelection)
		s = window.getSelection() + '';
	else
		s = "";
	return false;
}

function i(text, movestart, moveend)
{
	msgfield = document.forms[myform][myarea];
	msgfield.focus();

	if (typeof(msgfield.selectionStart) != 'undefined')
	{
		// Удаление лишних переносов (!ie).
		text = text.replace(/(\r*\n)+/g, '\n');

		var start = msgfield.selectionStart + 0;
		msgfield.value = msgfield.value.substr(0, msgfield.selectionStart) 
		+ text + msgfield.value.substr(msgfield.selectionEnd);

		if (typeof movestart != 'undefined')
		{
			msgfield.selectionStart = start + movestart;
			msgfield.selectionEnd = start + text.prolength() - moveend;
		}
		else
		{
			msgfield.selectionStart = start;
			msgfield.selectionEnd = start + text.prolength();
		}
	}
	else if (document.selection && document.selection.createRange)
	{
		var sel = document.selection.createRange();
		sel.text = text; // text.replace(/\r?\n/g, '\n');

		if (typeof movestart != 'undefined')
		{
			sel.moveStart('character', -text.prolength() + movestart);
			sel.moveEnd('character', -moveend);
		}
		else
			sel.moveStart('character', -text.prolength());
		sel.select();
	}
	else
		msgfield.value += text;
}

function sc(el) // storeCaret
{ 
	if (el.createTextRange)
		el.caretPos = document.selection.createRange().duplicate();
}

function tag(open, close)
{
	if (s)
		i(open+s+close, open.length, close.length);
	else
		i(open+close, open.length, close.length);
	return false;
}


function n(name, extra)
{
	if (extra == 1)
	{
		var e = document.getElementById('hidden');
		e.style.display = '';
	}

	if (s)
	{
		suffix = "[b]"+name+"[/b]:\n[quote]";
		prefix = "[/quote]";
	}
	else
	{
		suffix = "[b]"+name+"[/b], ";
		prefix = "";
	}

	i(suffix + s + prefix, suffix.length, prefix.length);
	return false;
}

function check(needHead)
{
	len = document.forms[myform][myarea].value.length;
	min = 1; max = 20000;
	errors = "";

	if (len < min)
		errors = "Минимальная длина сообщения: " + min + " символ.";

	if (len > max)
		errors = "Максимальная длина сообщения: " + max + " символов. Сейчас: " + len + ".";

	if (needHead > 0)
		if (document.forms[myform][myhead].value.length < needHead)
			errors = "Слишком короткий заголовок! Минимум: " + needHead + " символа.";

	if (errors != "")
	{
		alert(errors);
		return false;
	}
	else
	{
		document.forms[myform].submit.disabled = true;
		return true;
	}
}

function link(id) // [!] no standart...
{
	var url = "http://www.synet.ru/entry/"+id;
	prompt('Постоянная ссылка на этот пост:', url);
	return false;
}

// для alt, title...

window.onerror = null;
var tooltip_attr_name = "tooltip";
var tooltip_blank_text = "Открывается в новом окне.";
var tooltip_newline_entity = " ";
var tooltip_max_width = "190";

window.onload = function(e){
	if (document.createElement) tooltip.d();
	switcher();
}

tooltip = {

	t: document.createElement("DIV"),
	c: null,
	g: false,

	m: function(e){
		if (tooltip.g){
			oCanvas = document.getElementsByTagName(
			(document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY"
			)[0];
			x = window.event ? event.clientX + oCanvas.scrollLeft : e.pageX;
			y = window.event ? event.clientY + oCanvas.scrollTop : e.pageY;
			tooltip.a(x, y);
		}
	},

	d: function(){
		tooltip.t.setAttribute("id", "tooltip");

		document.body.appendChild(tooltip.t);
		a = document.all ? document.all : document.getElementsByTagName("*");
		aLength = a.length;
		for (var i = 0; i < aLength; i++){
			tooltip_title = a[i].getAttribute("title");
			tooltip_alt = a[i].getAttribute("alt");
			tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip_blank_text;
			if (tooltip_title || tooltip_blank){
				a[i].setAttribute(tooltip_attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip_blank_text : tooltip_blank_text) : tooltip_title);
				if (a[i].getAttribute(tooltip_attr_name)){
					a[i].removeAttribute("title");
					if (tooltip_alt && a[i].complete) a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}else if (tooltip_alt && a[i].complete){
				a[i].setAttribute(tooltip_attr_name, tooltip_alt);
				if (a[i].getAttribute(tooltip_attr_name)){
					a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}
			if (!a[i].getAttribute(tooltip_attr_name) && tooltip_blank){
				// пусто =)
			}
		}
		document.onmousemove = tooltip.m;
		window.onscroll = tooltip.h;
	},

	s: function(e){
		d = window.event ? window.event.srcElement : e.currentTarget;
if (!d.getAttribute(tooltip_attr_name)) return;
if (tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
tooltip.t.appendChild(document.createTextNode(d.getAttribute(tooltip_attr_name)));

   r = d.getAttribute(tooltip_attr_name);
   re = /  /ig;
   tooltip.t.innerHTML = r.replace(re, "<br />");
	
	tooltip.c = setTimeout("tooltip.t.style.visibility = 'visible';", 1);
	tooltip.g = true;

	},

	h: function(e){
		tooltip.t.style.visibility = "hidden";
		if (!tooltip_newline_entity && tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
		clearTimeout(tooltip.c);
		tooltip.g = false;
		tooltip.a(-99, -99);
	},

	l: function(o, e, a){
		if (o.addEventListener) o.addEventListener(e, a, false); // was true--Opera7b workaround!
		else if (o.attachEvent) o.attachEvent("on" + e, a);
			else return null;
	},

	a: function(x, y){
		oCanvas = document.getElementsByTagName(
		(document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY"
		)[0];

		w_width = window.innerWidth ? window.innerWidth + window.pageXOffset : oCanvas.clientWidth + oCanvas.scrollLeft;
		w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : oCanvas.clientHeight + oCanvas.scrollTop;

		tooltip.t.style.width = "auto";

		t_width = window.event ? tooltip.t.clientWidth : tooltip.t.offsetWidth;
		t_height = window.event ? tooltip.t.clientHeight : tooltip.t.offsetHeight;

		if (t_width > tooltip_max_width){
			tooltip.t.style.width = tooltip_max_width + "px";
			t_width = window.event ? tooltip.t.clientWidth : tooltip.t.offsetWidth;
		}

		t_extra_width = 7;
		t_extra_height = 5;

		tooltip.t.style.left = x + 8 + "px";
		tooltip.t.style.top = y + 8 + "px";

		while (x + t_width + t_extra_width > w_width){
			--x;
			tooltip.t.style.left = x + "px";
			t_width = window.event ? tooltip.t.clientWidth : tooltip.t.offsetWidth;
		}

		while (y + t_height + t_extra_height > w_height){
			--y;
			tooltip.t.style.top = y + "px";
			t_height = window.event ? tooltip.t.clientHeight : tooltip.t.offsetHeight;
		}
	}
}
