function trim(s)
{
  return rtrim(ltrim(s));
}

function ltrim(s)
{
  return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
  return s.replace(/\s+$/, ''); 
}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
};
	
function Menu() {
	
	this.current = null;

	this.init = function() {
		var self = this;
		$('.menu td').hover(function() {
			self.showDropDown($(this).children('a'));
		}, function(){
			self.hideDropDown();
		});
	};

	this.showDropDown = function(jqElem) {
		var tgt = '#'+jqElem.attr('tgt');

		this.current = tgt;
		var x = jqElem.offset().left - 10 + 'px';
		var y = jqElem.offset().top + 25 + 'px';

		var dd = $(tgt);
		dd.css({'left':x, 'top':y});
		dd.wait(500).slideDown(500);

		return false;
	};

	this.hideDropDown = function() {
		if (this.current) {
			var dd = $(this.current);
			dd.wait(500).slideUp(500);
		}

		return false;
	};

	this.init();
}


$(document).ready(function(){
	Menu = new Menu();
	testGuestBookForm(true);
});

function testGuestBookForm(init)
{
		
	$("#error_message").hide();
	$("#error_email").hide();

	if(!init)
		{
			var flag = 0;
	
			var re = /\S+/;
			if(!($("#message").val().match(re)))
				{
					$("#error_message").show();
					flag = 1;
				}	
			/*if($("#needemail").attr("checked"))
				{*/
					if(!($("#author_email").val().match(re)))
						{
							$("#error_email").show();
							flag = 1;
						}
				/*}*/
			if(!flag)	
				{
					$("#guest_form").submit();
				}
		}
}

function killBlock(bid)
{
   document.getElementById(bid).style.display='none';
}

function hideBlock(id) {
		document.getElementById(id).style.display='none';
}

function viewBlock(id) {
		document.getElementById(id).style.display='inline';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
	{
	  setTimeout("closereload()", 200);
	}
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
	for(j=0; j<shorts.length; j++)
	  {
		d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
		if(d)
		  {
			d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

			if(d.checked)
			  {
				d1.style.display = '';
			  }
			else
			  {
				d1.style.display = 'none';
			  }
		  }
	  }
  }
}

function chechInquiry(source)
{
  d = document.getElementById(source);
  d.checked = true;
}

function checkboxes(do_check, elem)
{
  var elts = document.forms['search'].elements[elem];
  if(elts[0])
	{
	  for(var i = 0; i < elts.length; i++)
		elts[i].checked = do_check;
	}
  else
	  elts.checked = do_check;
}

function checkstring(form, string, boxes)
{
  d  = document.getElementById(form);
  s  = document.getElementById(string);

  var cond1 = '';

  var elts = document.forms[form].elements[boxes];
  if(elts[0])
	{
	  for(var i = 0; i < elts.length; i++)
		{
		  if(elts[i].checked)
			cond1 = true;
		}
	}
  else
	{
	  if(elts.checked)
		cond1 = true;
	}

  if(!(s.value))
	alert("Вы не ввели строку для поиска");
  else if(!cond1)
	alert("Вы не выбрали направление поиска");
  else
	d.submit();
}

function showhide(what,what2)
{
  if (what.style.display=='none')
	{
	  what.style.display='';
	}
  else
	{
	  what.style.display='none'
	}
}













var myWidth=0;
function init_gallery() {
		setInterval('startLine()', 1);
}

function startLine() {
		if (document.body.clientWidth != myWidth) {
				myWidth = document.body.clientWidth;
				inLine();
		}
}
function inLine() {
		var td = document.getElementById('photo-parent').getElementsByTagName('td');
		var table = document.getElementById('photo-parent').getElementsByTagName('table');
		var count = 0;
		var row = false;

		for (var i = 1; (table.item(i)); i++) {
				if ((row == false) && (table.item(i).offsetTop == table.item(i-1).offsetTop)) count++; else row = true;
				td.item(i).style.height = 'auto';
		}
		td.item(0).style.height = 'auto';
		count++;

		for (var i = 0; (td.item(i)); i=i+count) {
				myHeight = 0;
				for (var o = 0; (td.item(i+o) && o < count); o++) if (td.item(i+o).offsetHeight > myHeight) myHeight = td.item(i+o).offsetHeight;
				for (var o = 0; (td.item(i+o) && o < count); o++) td.item(i+o).style.height = myHeight + 'px';
		}
		document.getElementById('photo-parent').style.visibility = 'visible';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
	{
	  setTimeout("closereload()", 200);
	}
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
	for(j=0; j<shorts.length; j++)
	  {
		d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
		if(d)
		  {
			d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

			if(d.checked)
			  {
				d1.style.display = '';
			  }
			else
			  {
				d1.style.display = 'none';
			  }
		  }
	  }
  }
}



function setMenuOver(elem) {
   elem.className = 'over';
}

function setMenuOut(elem) {
   elem.className = 'default';
}
