var iE6 = $.browser.msie && $.browser.version == 6;

function Placeholder(I){H='placeholder';if(!I || (I.placeholder && H in document.createElement('input'))) return;o='';var P=I.getAttribute(H);I=$(I).bind('focus submit',function(){if(I.val()==P){I.val(o);}}).bind('blur',function(){if(I.val()===o){I.val(P);}});if(I.val()===o || I.val()==P){I.val(P);}}


//-------------- convert String value to Number -
$.fn.valInt = function(C){	return parseInt($(this).val());}
//--------------- если аттрибут отсутствует то создаем его с некоторым значением
$.fn.attrExist = function(attrName,newVal){	var T = $(this); if(!T.attr(attrName)){ T.attr(attrName,newVal); } return T; }
//-------- Всё нах, только числа. ---------------------------------------
$.fn.toRealInt = function(){
		var reg = /^[0-9]+$/;
		var T = $(this);
		var V = parseInt(T.attr('maxdigit'));

		if (!reg.test(T.val())) { T.val(0); }
		if (T.valInt() > V) { T.val(V); }
		
		return T;
}
//---------- Туда \ сюда -------------------------
$.fn.digitChange = function(C){
		
		var T = $(this);
		var i = T.toRealInt().valInt();
	
		if (C && i < parseInt(T.attr('maxdigit'))) { T.val(i+1); } //up
		if (!C && i > 0) { T.val(i-1); } //down
		
		return C;
}
//-----------------------------------------------
$.fn.digitInput = function(){
	
	var T = $(this);
	var V = parseInt(T.attr('maxdigit'));
	
	var UP =   $('<a class="edit_send_point up" href="#">&nbsp;</a>').insertBefore(T);
	var DOWN = $('<a class="edit_send_point down" href="#">&nbsp;</a>').insertAfter(T);
		UP.add(DOWN).click(function(){ T.digitChange($(this).hasClass('up')); return false;});
	
	
	return T.attrExist('maxlength',T.attr('maxdigit').length).attrExist('size',T.attr('maxdigit').length).
	blur(function(){ T.toRealInt(); }).
	keydown(function(e){
	
	var k = e.keyCode;
		isDigits1 	= (k >=  48 && k <=  57); 
		isDigits2 	= (k >=  96 && k <= 105); //Калькулятор
		isDigits 	= isDigits1 || isDigits2;
		isArrows 	= (k >=  37 && k <= 39);
		isBackSpace =  k == 8;
		isDel 		=  k == 46;
		
		_1 = T.val().length == 1;
		CHAR = String.fromCharCode(k);
		
		if(k == 38){ T.digitChange(true); }
		if(k == 40){ T.digitChange(false); }
		
		if((isBackSpace || k == isDel) && _1) {
			return T.digitChange(false);
			//return false;
		}
		
		if(isDigits1 || isDigits2 || isArrows || isBackSpace){
			//если в форме уже есть ноль, то меняем его на новое значение.
			if(_1 && T.val() == '0' && isDigits){T.val('');}
			return true;
		} 
				
		return false;
	
	}).keyup(function(){ if(parseInt($(this).val()) > V){$(this).val(V);} });

};


$(document).ready(function(){

Placeholder(document.getElementById('loginmain'));
Placeholder(document.getElementById('paswmain'));


__SELECTED = 'sel'
var tabu = $('.tabu > li.sub');
var mc = $('#mainContent');

//pgtabs :-)
if(iE6){

	//show
	$('.tabu > li.sub > a').bind('mouseover',function(e){
	
	var t = $(e.target);
		
		if(t.hasClass(__SELECTED)) return;
		
		tabu.find('a.'+__SELECTED).removeClass(__SELECTED).next().removeClass('db');
		t.addClass(__SELECTED).next().addClass('db');  	
	
	});
	//hide
	mc.bind('mouseover',function(){ tabu.find('a.'+__SELECTED).removeClass(__SELECTED).next().removeClass('db'); });
	
} else {

	tabu.bind('mouseenter mouseleave',function(e){
		
		var over = e.type == 'mouseenter';
		var CUR = 0;
		
		if($.browser.msie){
			if(e.target.tagName == "A"){
				CUR = $(e.target).parents('li.sub');
			} else {
				CUR = $(e.target);
			}	
			
			} else {
				CUR = e.currentTarget;
		}
		
		sub = $('.sub',CUR);
		if(over){ sub.addClass('db').prev().addClass(__SELECTED); } else { sub.removeClass('db').prev().removeClass(__SELECTED); }
	});

}

//eof pgtabs

				
				var divrea = $('.realmselect');
				
				$("#mainContent").mousemove(function(){
					divrea.hide();	
				});
				
				$(".olga").click(function(){
				
				var rea = $("#"+$(this).attr('rel'));
				
				
				divrea.not(rea).hide();
				
					if(rea.is(':hidden')){
						rea.show();
					} else {
					   rea.hide();
					}
					return false;

				});

	
	$('.button_130, .button_85').hover(function(){
		  $("#s" + this.id).css({ display: "block", zIndex: "2" });
		  $("#w" + this.id).css({ zIndex: "2" });
	},
	function(){
		  $("#s" + this.id).css({ display: "none", zIndex: "1" });
		  $("#w" + this.id).css({ zIndex: "1" });
	});



//new menu
var menutop = $('#menutop');
var cur1 = 'cur1';
var mto;

clearmto = function(){clearTimeout(mto);}

menutopover = function(){
	clearmto();
	var t = $(this).parent('li');
	if(t.hasClass(cur1)) return;
	var c = $('li.cur1',menutop);
		
	mto = setTimeout(function(){
		$('li.cur1',menutop).removeClass(cur1);
		t.addClass(cur1);
		//t.add(c).toggleClass(cur1);
    }, 400);
}
	
menutop.find('ul > li > a').mouseover(clearmto);
$('#menutop > li > span').mouseover(menutopover);




var Helper = {};
	Helper.hi = $('#hint');

if(Helper.hi.size() > 0) {

	Helper.all =  $('div.hint-body',Helper.hi);
	Helper.mini = $('div.hint-hide',Helper.hi);
	Helper.ul =   $('.hint-ul',Helper.all);
	
	Helper.show = 	 $('.hint-show',Helper.mini);
	Helper.disable = $('.hint-disable',Helper.mini);

	Helper.prev = $('.hint-prev',Helper.all);
	Helper.next = $('.hint-next',Helper.all);
	Helper.hide = $('a.hint-hide',Helper.all);
	
	
	Helper.prevnext = function(){
						
						var t = $(this);
						var isNext = t.hasClass('hint-next');	
							
							li = Helper.ul.find('li.active');
							
							newli = isNext ? li.next() : li.prev();
							
							newli = newli.size() == 1 ? newli : isNext ? Helper.ul.find(':first') : Helper.ul.find(':last');
							
							li.add(newli).toggleClass('active');
							
						return false; 	
					};
	
	Helper.remove = function(){
						if (confirm(TXT_HINT_USER_ENABLE)){ 
							Helper.hi.stop().animate({width: 0});
							 $.post(hide_hints_url);
						}
						return false; 	
					};

	Helper.toggle = function(){
						var t = $(this);
						var tp = t.parent();
						var is1 = t.hasClass('hint-show');
						
						Helper.hi.stop().animate({width: 0}, is1 ? 200 : 500, function() {

							if(is1){
								
								var o = Helper.all;
									o.add(tp).toggle();
							
							} else {
							
								var o = Helper.mini.removeAttr('style');
									tp.hide();
							}
							
							Helper.hi.animate({width: o.width()}, is1 ? 500 : 200);
						
						});

						return false;
					}
					
	Helper.show.add(Helper.hide).click(Helper.toggle);
	Helper.disable.click(Helper.remove);
	Helper.prev.add(Helper.next).click(Helper.prevnext);
	
	$(window).keydown(function(e){
		
		e = e.keyCode || e.which;
			if (e == 27) { 
				if(Helper.all.is(':visible')){
					Helper.hide.click();
				}

			};
	});
	
}

});


var widget130 = function (iId, sImg, sHref, sName, sDes, sType, recomend) 
{
	document.write('<div id="w'+ iId +'" class="widget_130" onclick="document.location=\''+ sHref +'\'">' +
	'<div id="'+ iId +'" class="button_130 button_130_'+ sType +'"><img src="'+ sImg +'" /><div>'+ sName +'</div></div>'+
	'<div id="s'+ iId +'" class="slider_130"><h4>'+ sName +'</h4>'+ sDes +'</div> ' + (recomend ? '<div class="bantik">&nbsp;</div>' : '') + '</div>');
};

var widget85 = function (iId, sImg, sHref, sName, sDes, sType, sText) 
{
	document.write('<div id="w'+ iId +'" class="widget_85" onclick="$(\'#param_'+ iId +'\').removeAttr(\'disabled\'); document.forms[0].submit();">'+
	'<div id="'+ iId +'" class="button_85 button_85_'+ sType +'"><span class="text_top">' + sText + '</span><br /><img src="'+ sImg +'" /><div>'+ sName +'</div></div>'+
	'<div id="s'+ iId +'" class="slider_85"><h4>'+ sName +'</h4>'+ sDes +'</div><input type="hidden" id="param_'+ iId +'" name="'+ sHref +'" value="1" disabled="1" /></div>');
};





var _popup;
function popup(url, W, H)
{
	W = W ? W : 760;
	H = H ? H : 450;
	_popup = window.open(url, "_popup", "width=" + W + ", height=" + H + ", top=20, left=20, scrollbars=1, resizable=1");
	_popup.focus();
	return _popup;
}

function doWindow(url, W, H)
{
	popup(url, W, H);
	return false;
}

function closePopup()
{
	if (opener)
	{
		window.close();
		return;
	}

	if(_popup)
	{
		_popup.close();
		return;
	}
}

function closeWindow(){	closePopup();}
function openerReload(){
	if (opener){ opener.location.href = opener.location.href; }
}


function Go(sUrl, target)
{
	if (sUrl)
	{
		switch (target)
		{
			default:
				document.location.href = sUrl;
			break;
		}
	}
}

gObjectList = new Array();
function checkFirstTrRadio(oTr)
{
	nodeWolk(oTr);
	for (index in gObjectList) {
		if (gObjectList[index].type && !gObjectList[index].disabled) {
			gObjectList[index].checked = true;
		}
	}
}

function checkFirstTrCheckbox(oTr)
{
	nodeWolk(oTr);
	for (index in gObjectList)
	{
		if (gObjectList[index].type)
		{
			gObjectList[index].checked = !gObjectList[index].checked;
		}
	}
}

function nodeWolk(oParent)
{
	for (index in oParent.childNodes)
	{
		if (typeof(oParent.childNodes[index]) == 'object' && oParent.childNodes[index].nodeName != '#text')
		{
			gObjectList[gObjectList.length] = oParent.childNodes[index];
		}
		if (oParent.childNodes[index].childNodes)
		{
			nodeWolk(oParent.childNodes[index]);
		}
	}
}

gaControlCheckbox = new Array();
function checkUncheckFormCheckbox(oForm, status, oCheckbox, bNotRedefineOnclick)
{
	bSetOnclick = true;
	if (oCheckbox && oCheckbox.id && oForm.name && !gaControlCheckbox[oForm.name])
	{
		gaControlCheckbox[oForm.name] = oCheckbox.id;
	}
	else
	{
		bSetOnclick = false;
	}

	if (!gaControlCheckbox[oForm.name])
	{
		bSetOnclick = false;
	}

	iFormLength = oForm.length;
	for (i = 0; i < iFormLength; i++)
	{
		if (oForm[i].type == "checkbox")
		{
			oForm[i].checked = status;
		
			if (bSetOnclick && oForm[i].id != gaControlCheckbox[oForm.name] && !bNotRedefineOnclick)
			{
				oForm[i].onclick = function()
				{
					checkUncheckFormControlCheckbox(this);
				}
			}
		}
	}
}

function checkUncheckFormControlCheckbox(oCheckbox)
{
	oForm = oCheckbox.form;
	sIndex = oForm.name;
	if (gaControlCheckbox[sIndex] && document.getElementById(gaControlCheckbox[sIndex]))
	{
		iFormLength = oForm.elements.length;
		status = true;
		for (i = 0; i < iFormLength; i++)
		{
			if (oForm.elements[i].type == 'checkbox' && oForm.elements[i].id != gaControlCheckbox[sIndex] && !oForm.elements[i].checked)
			{
				status = false;
				break;
			}
		}
		document.getElementById(gaControlCheckbox[sIndex]).checked = status;
	}
}

function sayNumber(num) 
{
	if (num < 0)
	{
		return "-" + sayMoney(-num);
	}
	
	if (Math.round(num * 100) / 100 - Math.round(num))
	{
		num = Math.round(num * 100) / 100;
	}
	else
	{
		num  =Math.round(num);
	}
	
	s = num.toString();
	s1 = "";
	l = s.length;
	p = s.indexOf(".");
	if (p > -1)
	{
		s1 = s.substr(p);
		l = p;
	}
	else 
	{
		p = s.indexOf(",");
		if (p > -1) 
		{
			s1 = s.substr(p);
			l = p;
		}
	}
	p = l - 3;
	while (p >= 0) 
	{
		s1 = ' '+ s.substr(p, 3) + s1;
		p -= 3;
	}
	if (p > -3)
	{
		s1 = s.substr(0, 3 + p) + s1;
	}
	if (s1.substr(0, 1) == " ")
	{
		s1 = s1.substr(1);
	}
	return s1;
}

function sayMoney(num, symbol)
{
	result = sayNumber(num);
	if (symbol)
	{
		result += ' '+ symbol;
	}
	return result;
}

function sayPercent(num)
{
	result = sayNumber(num) + '%';
	return result;
}


function progressStaticBarOverflowBlink(sObjectId)
{
	if (document.getElementById(sObjectId))
	{
		if (document.getElementById(sObjectId).style.display != 'none')
		{
			document.getElementById(sObjectId).style.display = 'none';
		}
		else
		{
			document.getElementById(sObjectId).style.display = 'block';
		}
	}
	setTimeout("progressStaticBarOverflowBlink('"+ sObjectId +"')", 1000);
}


function buttonDisabled(oButton)
{
	if (!oButton.disabled)
	{
		sNewCssClassName = oButton.className.replace(/[^a-z]/ig, '') +'Disabled'+ oButton.className.replace(/[a-z]/ig, '');
		
		oButton.disabled = true;
		oButton.className = sNewCssClassName;
	}
}
function buttonEnabled(oButton)
{
	if (oButton.disabled)
	{
		oButton.disabled = false;
		oButton.className = oButton.className.replace(/disabled/ig, '');
	}
}


function fieldDisabled(oField)
{
	oField.disabled = true;
}
function fieldEnabled(oField)
{
	oField.disabled = false;
}


function getFirstChildByNodeName(oNode, sNodeName)
{
	if (oNode.childNodes != 'undefined')
	{
		index = 0;
		while (oNode.childNodes[index].nodeName != sNodeName)
		{
			index++;
		}
		
		return oNode.childNodes[index];
	}
	
	return null;
}


function pointsNeedToNextLevel(iLevel)
{
	return Math.min (Math.floor(parseInt(iLevel) / 10) + 1, 10);
}


function equipmentQualityColorGet(quality, max_quality)
{
	var numcol = 30;
	var color = parseInt (numcol * quality / max_quality);

	var int = 128;
	var gamma = parseInt (int / numcol);
	var correct = int - gamma;

	var r = parseInt ((numcol - color) * gamma) + correct;
	var g = parseInt ((numcol - Math.abs (numcol / 2 - color)) * gamma) + correct;
	var b = parseInt (color * gamma) + correct;

	color = 'rgb(' + r + ',' + g + ',' + b + ')';
	
	return color;
}


function formHiddenFieldValueSet(oForm, sFieldName, mFieldValue)
{
	if (typeof(oForm) == 'undefined')
	{
		return -1;
	}
	
	if (typeof(oForm['sFieldName']) == 'undefined')
	{
		oField = document.createElement('input');
		oField.setAttribute('name', sFieldName);
		oField.setAttribute('type', 'hidden');
		oForm.appendChild(oField);
	}
	else
	{
		oField = oForm['sFieldName'];
	}
	oField.setAttribute('value', mFieldValue);
}


function setCookie(name,value,days)
{
	createCookie(name,value,days);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
	{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name)
{
	readCookie(name)
}

function readCookie(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 null;
}

function eraseCookie(name){
	createCookie(name, "", -1);
}

function round(fVal, i)
{
	i = i ? i : 10;
	return Math.round(fVal / i) * i;
}

function imposeMaxLength(Object, MaxLen, Event)
{
  return ( Object.value.length <= MaxLen ? true : (Event.keyCode == 8 || Event.keyCode == 46 ? true : false) );
}

function messageRecipient(iUserId, sUserTitle)
{
	if (!iUserId)
	{
		return false;
	}
	if (sUserTitle)
	{
		return 	'"'+ sUserTitle +'"' + ' <#'+ iUserId +'>';
	}
	return '#'+ iUserId;
}