function bookmark(title, url) {
	if (title == undefined)
		title = document.title;
	if (url == undefined)
		url = top.location.href;
	if (window.sidebar) {
	// Firefox
		window.sidebar.addPanel(title, url, '');
	} else if (window.opera && window.print) {
	// Opera
		var t = document.createElement('a');
		t.setAttribute('rel', 'sidebar');
		t.setAttribute('href', url);
		t.setAttribute('title', title);
		t.click();
	} else if(window.external ) {
	// IE
		window.external.AddFavorite(url, title);
	} else {
		alert('Что бы добавить страницу в избранное нажмите CTRL-D.');
	}
	return false;
}

var is_animate = false;

function list(koef, moved_block_id, step, style_name, tag_name, children_block)
{
	if(is_animate)
	{
		return;
	}
	var block = byId(moved_block_id);
	if(!children_block)
	{
		children_block = block;
	}
	var top = block.style[style_name];
	var top_num = Number(top.replace("px", ""));
	if(top_num < 0)
	{
		top_num -= step * koef;
		var moved_child = koef < 0 ? firstChild(children_block, tag_name) : lastChild(children_block, tag_name);
		children_block.removeChild(moved_child);
		block.style[style_name] = top_num + "px";
		koef < 0 ? children_block.appendChild(moved_child) : children_block.insertBefore(moved_child, children_block.firstChild);
	}
	top_num += step * koef;
	animate(moved_block_id, style_name, top_num);
}

function animate(id, style_name, style_value)
{
	var animate_params = {};
	animate_params[style_name] = String(style_value) + "px";
	is_animate = true;
	$("#" + id).animate(animate_params, 500, function (){is_animate = false;});
}

function showAuthRegForm(id, show)
{
	old_id = id; 
	id = "#" + id;
	if(show)
	{
		$(id).show();
		$(id).fadeTo(500, 1);
		document.body.onkeydown = function(event)
		{
			event = event || window.event;
			if(event.keyCode == 27)
			{
				showAuthRegForm(old_id, false);
			}
		}
	}
	else
	{
		$(id).fadeTo(500, 0, function (){$(id).hide();});
		
	}
}

function showAuthForm(show)
{
	if(show)
	{
		showRegForm(false);
		showConfirm(false);
	}
	showAuthRegForm('auth_form_small', show);
	byId('auth_small_mail').focus();
}

function showRegForm(show)
{
	if(show)
	{
		showAuthForm(false);
		showConfirm(false);
	}
	showAuthRegForm('reg_form_small', show);
}

function showConfirm(show)
{
	if(show)
	{
		showAuthForm(false);
		showRegForm(false);
	}
	showAuthRegForm('confirm_form_small', show);
	byId('confirm_small_mail').focus();
}

function submitRegForm(f)
{
	var err = '';
	removeClass(f.name, "input_error");
	if(!check_correct_sing(f.name.value))
	{
		err += "Введите Имя\n";
		addClass(f.name, "input_error");
	}
	removeClass(f.fam, "input_error");
	if(!check_correct_sing(f.fam.value)) 
	{
		err += "Введите Фамилию\n";
		addClass(f.fam, "input_error");
	}
	removeClass(f.mail, "input_error");
	if(!check_mail(f.mail.value)) 
	{
		err += "Введите E-mail\n";
		addClass(f.mail, "input_error");
	}
	removeClass(f.pass1, "input_error");
	removeClass(f.pass2, "input_error");
	if(!check_correct_sing(f.pass1.value)) 
	{
		err += "Введите Пароль\n";
		addClass(f.pass1, "input_error");
		addClass(f.pass2, "input_error");
	}
	if(f.pass1.value != f.pass2.value) 
	{
		err += "Пароли не совпадают\n";
		addClass(f.pass1, "input_error");
		addClass(f.pass2, "input_error");
	}
	if(err)
	{
		alert(err);
	}
	else
	{
		f.submit();
	}
}

function checkNumChars(field_name, countChars)
{
	var field = document.getElementById(field_name);
	if(!field)
	{
		return;
	}
	
	this.printText = function()
	{
		if(this.text)
		{
			this.field.parentNode.removeChild(this.text);
		}
		var str = this.string.replace("NUM", maxChars - this.field.value.length);
		this.text = document.createTextNode(str)
		this.field.parentNode.appendChild(this.text);
	}

	this.maxChars = countChars || 255;
	this.field = field;
	this.string = "Осталось NUM символов";
	this.field.onkeyup = function (foo)
	{
		return function()
		{
			if(foo.field.value.length > foo.maxChars)
			{
				foo.field.value = foo.field.value.substring(0, foo.maxChars);
			}
			foo.printText();
		}
	}(this)
	this.printText();
}
function check_mail(mail){
  mail = (mail != undefined)?mail:'';
  var reg = /[a-z0-9_-]+(?:\.[a-z0-9_-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
  if (!mail.match(reg)) return false;
  
  return true;
}
function check_phone_hard(A){
  A = (A != undefined)?A:'';
  var reg = /^\s?(\+?[\s\d\-\,\(\)]+)$/;
  if (!A.match(reg)) return false;
  
  return true;
}
function check_correct_sing(A){
  A = (A != undefined)?A:'';
  var reg = /([\>\<\?\%]+)/;
  if(A == '') return false;
  if (A.match(reg)) return false;
  
  return true;
}
$(document).ready(function(){
  $(".tumbs .tumb2 img, .tumbs .foto52 img, #tovars_block .opis5 .foto5 img, #table_tovars .foto5 img").each(function(){
    var height = $(this).height();
    var $ofd = $(this);
    if(height < 150){
      var image = new Image();
      image.src = $(this).attr("src");
      image.onload = function(){
        var height = $ofd.height();
         if(height < 150){
             var h = (150 - height)/2;
          h = (h > 0)?h:0;
         $ofd.css({"padding-top": h+"px"});
         }
      }; 
    }
  });
});
