/*Города*/
function showCities(id)
{
	var obj = $('#'+id);
	var parent = obj.parent();
	obj.css('display', 'block');
	parent.bind('mouseover', function(){
								var timer = obj.attr('timer');
								if(timer)
								{
									clearTimeout(timer);
									obj.removeAttr('timer');
								}
							});
	parent.bind('mouseout', function(){
								var timer = setTimeout(function(){
																	obj.css('display', 'none');
																	parent.unbind();
																}, 500);
								obj.attr('timer', timer);
							});
}
/*/Города*/

/*Проверка числового поля*/
function checkDigitalField(ev)
{
	var Code = (ev.keyCode || ev.which);

	//Фильтрация запрещенных символов
	if ((Code < 48 || Code > 57) && (Code < 37 || Code > 40 ) && (Code != 8 && Code != 13 && Code !=46)) return false;

	return true;
}
/*/Проверка числового поля*/

/*Формат числа*/
function number_format(number, decimals, dec_point, thousands_sep) 
{
    number = (number + "").replace(/[^0-9+\-Ee.]/g, "");
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),        
		sep = (typeof thousands_sep === "undefined") ? "," : thousands_sep,
        dec = (typeof dec_point === "undefined") ? "." : dec_point,
        s = "",
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);            
			return "" + Math.round(n * k) / k;
        };

    s = (prec ? toFixedFix(n, prec) : "" + Math.round(n)).split(".");
    if(s[0].length > 3){        
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || "").length < prec) {
        s[1] = s[1] || "";
        s[1] += new Array(prec - s[1].length + 1).join("0");    
	}
    return s.join(dec);
}
/*/Формат числа*/

/*Расширяем возможности массивов*/
if(typeof(Array.prototype.inArray) == "undefined" ){
    Array.prototype.inArray = function(str){
        for(var k in this){
            if(this[k] == str){
                return true;
            }
        }
        return false;
    };
}

if(typeof(Array.prototype.sortNumeric) == "undefined" ){
    Array.prototype.sortNumeric = function(str){
        var fail = true;
		
		while(fail)
		{
			fail = false;
			var prev = false;
			for(var k in this){
				if(prev && parseFloat(this[prev])>parseFloat(this[k]))
				{
					tmp = this[k];
					this[k] = this[prev];
					this[prev] = tmp;
					fail = true;
				}
				prev = k;
			}
			
		}
		
        return true;
    };
}
/*/Расширяем возможности массивов*/

/*Смена вкладки*/
function changeTabs(link, className)
{
	if($(link).hasClass('active'))
	{
		var parent = link;
		while(parent.className!=className)
		{
			parent = parent.parentNode;
		}
		
		$('.titles a', parent).each(function(){
			if($(this).hasClass('active'))
			{
				$(this).removeClass('active');
			}
			else
			{
				$(this).addClass('active');
			}
		});
		
		$('.inner', parent).each(function(){
			if(this.style.display=='none')
			{
				this.style.display = 'block';
			}
			else
			{
				this.style.display = 'none';
			}
		});
	}
	link.blur();
}
/*/Смена вкладки*/

/*position: fixed*/
function fixed(e, pos)
{	
    if (/MSIE ([\d\.]+).+Win/.test(navigator.userAgent))
    {
        e.style.position = 'absolute';
        var top = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
        return (top + pos) + 'px';
    }
    else e.style.top = pos + 'px';
}
/*/position: fixed*/

/*Закрыть попап*/
function closePopup()
{
	var shade = document.getElementById('bodyShade');
	if(shade)
	{
		shade.parentNode.removeChild(shade);
	}
	var popup = document.getElementById('popup');
	if(popup)
	{
		popup.parentNode.removeChild(popup);
	}
}

$(document).keyup(function(event){
    if (event.keyCode == 27) {
        closePopup();
    }
});
/*/Закрыть попап*/

/*Няшки*/
$(document).ready(function () {
    $(".toggle").each(function(){
		var parent = this.parentNode;
		var child = parent.childNodes;
		var i = 0;
		while(child[i]!=this && i<child.length && ++i){}
		while(++i && !child[i].tagName && i<child.length){}
		var object = child[i];
		object.style.display = 'none';
		$(this).bind('click', function(){$(object).toggle(); return false;});
	});
});
/*/Няшки*/

/*Открытие ссылки в попапе*/
function openPopup(link)
{
	if(link.target != 'hidden_popup')
	{
		link.target = 'hidden_popup';
		link.href += 'popup.php';
	}
}

function openPopupLocal(obj)
{
	obj = document.getElementById(obj);
	/*Создаем затенение*/
	var div = document.createElement('a');
	div.id = "bodyShade";
	div.href = 'javascript:closePopup()';
	document.body.appendChild(div);
	/* /Создаем затенение*/

	/*Выводим форму*/
	var div = document.createElement('div');
	div.id = "popup";
	div.className = 'form_popup';
	div.innerHTML = '<a href="javascript:closePopup()" class="close_btn"></a><div class="inner" id="formContainer">' + obj.innerHTML + '</div>';
	document.body.appendChild(div);

	var top = Math.max(Math.round((window.parent.document.body.clientHeight - div.offsetHeight)/2), 0);
	div.style.marginTop = top + 'px';
	if(div.offsetHeight < window.parent.document.body.clientHeight)
	{
		div.className += ' popup_fixed';
	}
	/* /Выводим форму*/
}
/*/Открытие ссылки в попапе*/
