var searchPanel = {
	path_cat: "/search.html",
	path_goo: "/search-results.html",
	form: null,
	panel: null,
	field: null,

	init: function (form, panel, field)
	{
		var t = this;

		this.form = document.getElementById(form);
		this.panel = document.getElementById(panel);
		this.field = document.getElementById(field);

		var onf = this.field.onfocus;
		this.field.onfocus = function (e) {
			t.showPanel();
			onf();
		};
	},

	showPanel: function ()
	{
		var m = document.getElementById("MainMenu");
		var p = getAbsoluteOffset(m);

		this.panel.style.left = (p[0]+m.offsetWidth-215)+"px";
		this.panel.style.top = (p[1]+35)+"px";
		this.panel.style.display = 'block';

		var t = this;
		document.onmousedown = function (e) {
			t.checkPanel(e);
		};
	},

	hidePanel: function ()
	{
		this.panel.style.display = 'none';
		document.onmousedown = null;
	},

	checkPanel: function (e)
	{
		if (!e) var e = window.event;
		var s = (e.target)?e.target:e.srcElement;
		while (s.tagName != 'BODY')
		{
			if ((s.id == "SearchPanel") || (s.className == "edit"))
			{
				return true;
			}
			s = s.offsetParent;
		}

		this.hidePanel();
	},

	setAction: function (n)
	{
		this.form.action = (n == 0)?this.path_cat:this.path_goo;
	}
}

function getAbsoluteOffset(eSrc)
{
	var ret = [0, 0];

	while (eSrc != null)
	{
		ret[0] += eSrc.offsetLeft;
		ret[1] += eSrc.offsetTop;
		eSrc = eSrc.offsetParent;
	}

	return ret;
}

var searchTitle = {
	title: "ПОИСК",
	field: null,

	set: function (field)
	{
		this.field = document.getElementById(field);
		var t = this;
		this.field.onfocus = function (e) {t.focus_in(this)};
		this.field.onblur = function (e) {t.focus_out(this)};
		this.focus_out(this.field);
	},

	focus_in: function (el)
	{
		with (this.field)
		{
			if (style.textAlign != "center") return;
			style.textAlign = "";
			style.color = "";
			value = "";
		}
	},

	focus_out: function (el)
	{
		with (this.field)
		{
			if (value != "") return;
			style.textAlign = "center";
			style.color = "#145388";
			value = this.title;
		}
	}
}

var formChecker  = {
	formObj: '',
	clCorr: 'edit edit_correct',
	clWrong: 'edit edit_wrong',

	setForm: function (f)
	{
		if (typeof f != "object") f = document.getElementById(f)
		this.formObj = f;
	},

	add: function (fieldName, rule)
	{
		var t = this;
		var o = this.formObj[fieldName];
		o.fillRule = rule;
		addEvent(o, 'change', function (e) {t.checkField(o);}, false);
		addEvent(o, 'keyup', function (e) {t.checkField(o);}, false);
	},

	check: function (f)
	{
		if (!f)
		{
			f = this.formObj;
			var l = f.length;
			for (var i = 0; i < l; i++)
			{
				if ((!f[i].fillRule) && (f[i].name.toString().match(/_$/)))
				{
					this.add(f[i].name, '.+');
				}
			}
			return false;
		}

		var l = f.length;
		var r = true;
		for (var i = 0; i < l; i++)
		{
			if (f[i].fillRule)
			{
				if (!this.checkField(f[i]))
				{
					r = false;
				}
			}
		}

		if (!r) alert("Заполните, пожалуйста, все необходимые поля!");

		return r;
	},

	checkField: function (el)
	{
		if (el.value.match(el.fillRule))
		{
			el.className = this.clCorr;
			return true;
		} else {
			el.className = this.clWrong;
		}

		return false;
	}
}

function checkForm(a)
{
	l = a.length;
	r = true;
	for (i = 0; i < l; i++)
	{
		n = a[i].name.toString().substr(a[i].name.toString().length-1, 1);
		if (n == "_")
		{
			if (a[i].value == "")
			{
				a[i].style.backgroundColor = "#552222";
				r = false;
			} else {
				a[i].style.backgroundColor = "";
			}
		}
	}

	if (!r) alert("Заполните, пожалуйста, все необходимые поля!");

	return r;
}

function addEvent(elm, evType, fn, useCapture)
{
	if (elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

function validateImageReload(im)
{
	var params = "";

	var src = im.src.split('?');
	if (src[1])
	{
		var tmp = src[1].split(/0\.\d/);
		tmp = tmp[0].replace(/^(.*)(\&)$/, '$1');
		params = tmp;
	}
	src = src[0]+'?'+params+'&'+Math.random();
	im.src = src;
}


var slideMenu = {
	m: "",
	fnc_mi: null,
	last: null,
	to: 0,

	init: function(p)
	{
		var t = this;
		var d;
		this.m = $(p)[0];
		if (!this.m) return false;

		if (!this.fnc_mi)
		{
			this.fnc_mi = function (e) {t.mouseOver(e, this);};
		}

		$(this.m).children("li").each(function(i, item) {
			$(item).find('div').bind('mouseover', t.fnc_mi);
			if (d = $(item).children('ul'))
			{
				d.css('display', 'none');
			}
		});
	},

	mouseOver: function (evt, e)
	{

		var t = this;
		this.to = setTimeout(
			function (ee) { t.slideOpen(e); },
			300
		);

		$(e).bind('mouseout', function(ee) { clearTimeout(t.to); } );


/*
		n.slideDown(150);

		if (this.last)
		{
			$(this.last).bind('mouseover', this.fnc_mi);
			$(this.last).next('ul').slideUp(50);
		}

		this.last = e;
		$(e).unbind('mouseover', this.fnc_mi);
*/
	},

	slideOpen: function (e)
	{
		var n = $(e).next('ul');
		if (!n) return false;

		n.slideDown(150);

		if (this.last)
		{
			$(this.last).bind('mouseover', this.fnc_mi);
			$(this.last).next('ul').slideUp(50);
		}

		this.last = e;
		$(e).unbind('mouseover', this.fnc_mi);
	}
}

$(function(){
	slideMenu.init(".SubDomains");
});