// <![CDATA[

//window.onerror = function()
//{
//	alert(window.onerror.caller);
//}
if (document.documentElement) document.documentElement.className += " script";

/*
_event(window, 'onload', function()
{
	Forms.init();
});*/

/**
 * globals
 */
$$ie = ""+document.documentElement=="[object]";
$$safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1);

q.mainWindow = !(typeof window.opener == 'undefined' || window.opener == null);
if (q.mainWindow)
{
	try {
		q.mainWindow = (typeof window.opener.opener == 'undefined' || window.opener.opener == null) ? window.opener : window.opener.opener;

		if (q.mainWindow.name != 'mainWindow')
			q.mainWindow = false;

	} catch(e) {q.mainWindow = false;}
}

var $oQuery = q.mainWindow ? q.mainWindow.jQuery : jQuery;

//var $$flash = function flashVersion()
//{
//	var p;
//	if (((p = navigator.plugins) && (p = p["Shockwave Flash"]) && (p = p.description)) ||
//		((p = window.ActiveXObject) && (p = new p("ShockwaveFlash.ShockwaveFlash").GetVariable("$version"))))
//			return p.replace(/^\D+/, '').split(/\D+/g);
//	return null;
//}();
//
//if ($$flash && $$flash[0] >= 6)
//{
//	$flash = function flash(src, id, style, vars)
//	{
//		document.write('<object'+ (!$$ie ? ' data="'+ src +'"' : "") + (id ? ' id="'+ id +'"' : "") + (style ? ' style="'+ style +'"' : "") +' type="application/x-shockwave-flash">'
//			+'<param name="movie" value="'+ src +'" />'
//			+'<param name="allowScriptAccess" value="sameDomain" />'
//			+ (vars ? '<param name="flashvars" value="'+ vars +'" />' : "")
//			+'</object>');
//	}
//}
//else $flash = new Function();

function flashVersion()
{
	var p;
	if (((p = navigator.plugins) && (p = p["Shockwave Flash"]) && (p = p.description)) ||
		((p = window.ActiveXObject) && (p = new p("ShockwaveFlash.ShockwaveFlash").GetVariable("$version"))))
			return p.replace(/^\D+/, '').split(/\D+/g);
	return null;
}

var domEl = function(e,c,a,p,x) { if(e||c) { c=(typeof c=='string'||(typeof c=='object'&&!c.length))?[c]:c; e=(!e&&c.length==1)?document.createTextNode(c[0]):e; var n = (typeof e=='string')?document.createElement(e) : !(e&&e===c[0])?e.cloneNode(false):e.cloneNode(true); if(n.nodeType!=3&&n.canHaveChildren) { c[0]===e?c[0]='':''; for(var i=0,j=c.length;i<j;i++) typeof c[i]=='string'? n.appendChild(document.createTextNode(c[i])): n.appendChild(c[i].cloneNode(true)); } if(a){for (var i in a) i=='class'?n.className=a[i]:n.setAttribute(i,a[i]);} } if(!p)return n; p=(typeof p=='object'&&!p.length)?[p]:p; for(var i=(p.length-1);i>=0;i--) { if(x){while(p[i].firstChild)p[i].removeChild(p[i].firstChild); if(!e&&!c&&p[i].parentNode)p[i].parentNode.removeChild(p[i]);} if(n) p[i].appendChild(n.cloneNode(true)); } }

// RegExp escape
String.prototype.regEsc = function(){ return this.replace(eval('/([\\/\\\\\\*\\+\\-\\[\\]\\{\\}\\?\\:\\|])/g'), '\\$1'); }
// undefined
var _u;
// eval object
function _x(string) { try { return new Function("return "+ string)(); } catch(e){ _x._error = e; return null } }
// getElements
function $(objectOrId, tagName)
{
	if (!document.getElementById) return;
	if (typeof(objectOrId) == 'string') objectOrId = document.getElementById(objectOrId);
	if (!tagName) return objectOrId;
	if (tagName && objectOrId.getElementsByTagName) return objectOrId.getElementsByTagName(tagName);
}

// extend object
function _extend(target, object, keep)
{
	if (target instanceof Array) for (var i=0; i<object.length; i++) target.push(object[i]);
	else for (var i in object) if (!keep || target[i] == _u) target[i] = object[i];
	return target;
}
// add event
function _event(target, name, handler)
{
	if (typeof target[name] == "function")
	{
		var old = target[name];
		target[name] = function()
		{
			old.apply(this, arguments);
			return handler.apply(this, arguments);
		};
	}
	else target[name] = handler;
}
// add class
function _addclass(element, className) { element.className += ' ' + className; }
// remove class
function _remclass(element, className) { if (element.className) element.className = element.className.replace(_x("/\\s*"+ className.regEsc() +"/g"), ""); }

/**
 * Compatibility set
 */

if (!Function.prototype.apply) Function.prototype.apply = function(object, args) { var t = '', i=0, u; for (;i<args.length;i++) t += (i ? ', ' : '') + 'args['+ i +']'; object.__apply__ = this; var r = _x('object.__apply__('+t+')'); object.__apply__ = u; return r; };
if (!Array.prototype.push) Array.prototype.push = function() { var a = arguments; for (var i=0; i<a.length; i++) this[this.length] = a[i]; return this.length; };

/**
 * Class construction
 */

// Class prototype methods and properties
Function.prototype.__proto = function(o){ _extend(this.prototype, o); return this; };
// Class static methods and properties
Function.prototype.__static = function(o){ _extend(this, o); return this; };

function __class(extendClass)
{
	var Class = function()
	{
		if (typeof this.__constructor == 'function') this.__constructor.apply(this, arguments);
	}, p;

	if (extendClass)
	{
		p = typeof extendClass == 'function' ? extendClass.prototype : extendClass;
		Class.__proto(p);
		Class.__proto({ constructor : Class, _parent : p });
	}
	Class.__proto({ _static : Class });
	return Class;
};

/**
 * functions uset inside document
 */

function $translation()
{
	var _this = arguments.callee;
	if (!_this.all) _this.all = new Object();


	for (var i=0; i<arguments.length; i+=2)
		_this.all[arguments[i]] = arguments[i+1];
}
$translation.get = function(name)
{
	if (this.all && this.all[name]) return this.all[name];
	else return 'Error, no translation!';
};

$translation.morph = function(number, word)
{
	var postfix = " end";

	switch(q.lang)
	{
		case "en":
			if(number >= 11 && number <= 19)
			{
				word = this.get(word+postfix+"2");
			}
			else
			{
				number %= 10;
				if(number == 1)
				{
					word = this.get(word+postfix+"1");
				}
				else
				{
					word = this.get(word+postfix+"2");
				}
			}
			break;
		case "ru":
			if(number >= 11 && number <= 19)
			{
				word = this.get(word+postfix+"3");
			}
			else
			{
				number %= 10;
				if(number == 1)
				{
					word = this.get(word+postfix+"1");
				}
				else if(number >= 2 && number <= 4)
				{
					word = this.get(word+postfix+"2");
				}
				else if(number >= 5 && number <= 9 || number == 0)
				{
					word = this.get(word+postfix+"3");
				}
			}
			break;
		default:
			word = "";
	}

	return word;
}

function $run(element)
{
	var _this = arguments.callee;
	if (typeof element == "string" && _this.elements["#"+ element]) return _this.elements["#"+ element].apply(_this.elements, arguments);
	if (element && _this.elements[element.id]) return _this.elements[element.id](element);
	else return false;
}

function $form(id)
{
	var a = arguments,
		_this = a.callee,
		i = 1,
		b = new Array();
	
	if (typeof id == "string") var element = $(id);
	if (element && _this.elements[id])
	{
		b[0] = element;
		for (;i<a.length;i++) b[i] = a[i];
		try { return _this.elements[id].apply(null, b); } catch(e) {}
	}
	else return false;
}

/**
 * Forms Base Class
 */

var Forms = {
	init : function()
	{
		if (this.elements)
		{
			for (var i=0; i<document.forms.length; i++)
			{
				if (this.elements[document.forms[i].id]) this.elements[document.forms[i].id](document.forms[i]);
			}
		}
	},
	setBehavior : function(form, options)
	{
		var ele = form.elements;
		options = options || new Object();
		_extend(options, this.behaviors, true);

		for (var i=0; i<ele.length; i++)
		{
			switch (ele[i].type)
			{
				case 'text': case 'password': case 'textarea': case 'select': case 'multiselect':
					if ((""+ document.documentElement) != "[object]" || ele[i].type != 'textarea') _event(ele[i], 'onfocus', focusb);
					_event(ele[i], 'onblur', blurb);
					break;
				case 'submit':
					if (options['submitDisable']) _event(form, 'onvalid', disable(form));
					if (options['submitValue']) _event(form, 'onvalid', wait(ele[i]));
					break;
			}
		}

		function disable(e) { return function() { e.onsubmit = function(){ return false; }; } }
		function wait(e) { return function() { e.value = $translation.get("please wait"); } }
		function focusb() { _addclass(this, "focus"); return true; }
		function blurb() { _remclass(this, "focus"); }
	},

	behaviors : {
		'submitDisable' : true,
		'submitValue' : true
	},

	setValidation : function(form, config)
	{
		new FormValidation(form, config);
	}
};

var FormValidation = __class().__proto({
	__constructor : function(form, config)
	{
		this.form = form;
		this.config = config;
		this.set();
	},

	set : function()
	{
		var _this = this;
		_event(this.form, 'onsubmit', function(){ return _this.validate() });
	},

	validate : function()
	{
		this.errors = new Object();
		this.groups = new Object();
		this.focus = _u;
		this.valid = true;

		for (var i in this.config)
		{
			if (this.form.elements[i])
			{
				if (this.config[i] instanceof Array)
				{
					for (var j=0; j<this.config[i].length; j++)
					{
						if (!this._static.validate(this.form.elements[i], this.config[i][j]))
						{
							this.addError(this.config[i][j].type.toString(), this.form.elements[i], this.config[i][j].message);
							break;
						}
						else _remclass(this.form.elements[i], "error");
					}
				}
				else
				{
					if (!this._static.validate(this.form.elements[i], this.config[i]))
					{
						this.addError(this.config[i].type, this.form.elements[i], this.config[i].message);
					}
					else _remclass(this.form.elements[i], "error");
				}
			}
		}

		for (var i in this.errors)
			this['error_'+ i +'_handler'](this.errors[i]);

		if (this.valid && this.form.onvalid) this.form.onvalid();
		return this.valid;
	},

	addError : function(type, element, message)
	{
		this.valid = false;
		if (!this.errors[type]) this.errors[type] = new Array();
		if (!this.focus) this.focus = element;
		_addclass(element, "error");
		this.errors[type].push($translation.get(message));
	},

	error_alert_handler : function(array)
	{
		var t = "";
		for (var i=0; i<array.length; i++)
			t += array[i] +"\n";

		alert(t);

		if (typeof this.focus.focus == "function") this.focus.focus();
		if (this.focus.select == "function") this.focus.select();
	},

	error_alertGroup_handler : function(array) { alert(array[0]); }

}).__static({
	validate : function(element, rules)
	{
		if (rules.selected && element.selectedIndex == 0)
			return false;

		if (rules.minlength && (rules.minlength > element.value.length))
			return false;

		if (rules.regexp && !_x(rules.regexp).test(element.value))
			return false;

		if (rules.radio)
		{
			for (var i=0; i<element.length; i++)
				if (element[i].checked)
				{
					var checked = true;
					break;
				}
			if (!checked) return false;
		}

		if (rules.func && !rules.func.apply(element)) return false;

		return true;
	}
});



/*
function validateLoginForm(f)
{
	if (f.login.value == "")
	{
		f.login.focus();
		alert("Login field is empty");
		return false;
	}
	else if (f.password.value == "")
	{
		f.password.focus();
		alert("Password field is empty");
		return false;
	}

	f.submit.disabled = true;
	f.submit.value = 'Please wait...';
	return true;
}

window.onload = function focusLogin()
{
	document.getElementById("login").focus();
}
*/
// ]]>

function $dump(input, _indent)
{
	if (typeof(_indent) == 'string')
	{
		var indent = _indent + '    ';
		var paren_indent = _indent + '';
	}
	else
	{
		var indent = '    ';
		var paren_indent = '';
	}

	switch (typeof(input))
	{
		case 'boolean':
			var output = (input ? 'true' : 'false') + "\n";
		break;
		case 'object':
			if (input === null)
			{
				var output = "null\n";
				break;
			}
			
			var output = ((input.reverse) ? 'Array' : 'Object') + " (\n";

			if (input.length)
			{
				for (var i = 0; i < input.length; i++)
				{
					output += indent + "[" + i + "] => " + $dump(input[i], indent);
				}
			}
			else
			{
				for (var i in input)
				{
					output += indent + "[" + i + "] => " + $dump(input[i], indent);
				}
			}
			
			output += paren_indent + ")\n";
		break;
		case 'number':
		case 'string':
		default:
			var output = "" + input + "\n";
		break;
	}
	return output;
}