cmnAdd_event( window, "load", AddListen );
//cmnAdd_event( window, "load", ProcessPNG );
//cmnAdd_event( window, "load", ShowCart );
searchstring = 'поиск по сайту';
var pass1, pass2;
var GalleryArray;

function $(id)
	{
	return document.getElementById(id);
	}
function AddListen()
	{
	GalleryArray = new Array();
	for (var i = 0; (oInput = document.getElementsByTagName('*')[i]); i++)
		{
		//переход по ссылкам посредством аякса
		if( (cmnMatch_class(oInput, 'a_mainmenu') || cmnMatch_class(oInput, 'a_childmenu') || cmnMatch_class(oInput, 'ajax')) )
			{
			cmnAdd_event(oInput, 'click', ProcessLink);
			}
		
		//автоматическое раскрытие больших фоток
		if ( oInput.tagName == 'IMG' && ( cmnMatch_class(oInput, 'popup_image') || oInput.src.indexOf("popup_image")!=-1 ) )
			{
			cmnAdd_event(oInput, 'click', LoadImg);
			oInput.style.cursor = "pointer";
			im = new Image();
			im.src = getLargeImage(oInput.src);
			GalleryArray[GalleryArray.length] = im;
			}
		
		//скрыть попап, если кликнули вне его
		cmnAdd_event(oInput, 'click', TryToCloseEnlarged);
		
		//скрыть попап по клику на крест
		if(cmnMatch_class(oInput, 'div_close') || cmnMatch_class(oInput, 'div_close-hover'))
			{
			cmnAdd_event(oInput, 'click', CloseGallery);
			}
		
		//показать следующую картинку-попап
		if(cmnMatch_class(oInput, 'div_next') || cmnMatch_class(oInput, 'div_next-hover'))
			{
			cmnAdd_event(oInput, 'click', NextGallery);
			}
		
		//показать предыдущую картинку-попап
		if(cmnMatch_class(oInput, 'div_prev') || cmnMatch_class(oInput, 'div_prev-hover'))
			{
			cmnAdd_event(oInput, 'click', PrevGallery);
			}
		
		//автоматическая проверка заполненности формы
		if(cmnMatch_class(oInput, 'password_1'))
			{
			pass1 = oInput;
			cmnAdd_event(oInput, 'keyup', CheckStrict);
			cmnAdd_event(oInput, 'change', CheckStrict);
			}
		if(cmnMatch_class(oInput, 'password_2'))
			{
			pass2 = oInput;
			cmnAdd_event(oInput, 'keyup', CheckStrict);
			cmnAdd_event(oInput, 'change', CheckStrict);
			}
		if(oInput.tagName=='FORM' && oInput.className == 'strictform')
			{
			cmnAdd_event(oInput, 'submit', CheckStrictForm);
			}
		if (cmnMatch_class(oInput, 'strict'))
			{
			check_(oInput);
			cmnAdd_event(oInput, 'keyup', CheckStrict);
			cmnAdd_event(oInput, 'change', CheckStrict);
			}
		if (cmnMatch_class(oInput, 'a_services'))
			{
			cmnAdd_event(oInput, 'mouseover', ProcessHover);
			cmnAdd_event(oInput, 'mouseout', ProcessUnhover);
			}
		if (cmnMatch_class(oInput, 'mm'))
			{
			cmnAdd_event(oInput, 'mouseover', ShowHider);
			cmnAdd_event(oInput, 'mouseout', HideHider);
			}
			
		}
	if(oInput = $('div_submenu'))
		{
		cmnAdd_event(oInput, 'mouseover', CancelUnhover);
		cmnAdd_event(oInput, 'mouseout', ProcessUnhover);
		}
	//строка поиска - добавляем серым цветом "поиск по сайту", а когда Фокусируемся, убираем.
	if(oInput = document.getElementById('searchstring'))
		{
		if(!oInput.value || oInput.value == searchstring)
			{
			oInput.value = searchstring;
			oInput.style.color = 'gray';
			}
		cmnAdd_event(oInput, 'focus', SearchFocus);
		cmnAdd_event(oInput, 'blur', SearchBlur);
		}
	}
var hr = new Array();
function ProcessLink(event)
	{
	el = getElement(event);
	while( el.tagName != 'A' )
		{
		el = el.parentNode;
		}
	Prevent(event);
	hr = xajax_xLoadPage(el.href);
	pageScroll(0, document.getElementsByTagName('body')[0].scrollTop, 20);
	}
function FillPage()
	{
	ControlFlash('/images/vesta-flash2');
	document.getElementsByTagName('h1')[0].innerHTML = hr['h1_title'];
	$('div_content').innerHTML = hr['content'];
	if(hr['childmenu'])
		{
		$('div_childmenu').style.display='block';
		$('div_childmenu').innerHTML = hr['childmenu'];
		}
	else
		$('div_childmenu').style.display='none';
		
	$('div_mainmenu').innerHTML = hr['mainmenu'];
	document.title = hr['page_title'];
	
	divs = new Array('span_mainpage', 'div_logo');
	for(i=0;i<divs.length;i++)
		{
	linkmain = $(divs[i]);
	a = linkmain.getElementsByTagName('a')[0];
	if( a && (hr['action'] == 'index') )
		{
		linkmain.innerHTML = a.innerHTML;
		}
	else if( !a && (hr['action'] != 'index') )
		{
		linkmain.innerHTML = '<a href="/" class="ajax">'+linkmain.innerHTML+'</a>';
		}
		}
	AddListen();
	}

function SubmitAjaxForm(oForm)
	{
	values = new Array();
	for(i = 0; i < oForm.elements.length; el = oForm.elements[i++])
		{
		if((((el.type=='radio' || el.type=='checkbox') && el.checked) || !(el.type=='radio' || el.type=='checkbox')) && el.name)
			values[el.name] = el.value;
		}
	hr['content'] = xajax_xSubmitForm(values);
	pageScroll(0, document.getElementsByTagName('body')[0].scrollTop, 20);
	}

function CheckStrictForm(event)
	{
	oForm = getElement(event);
	Prevent(event);
	for(var i=0; oElement = oForm.elements[i]; i++)
		{
		if( cmnMatch_class(oElement, 'strict') )
			{
			if( (state = check_(oElement)) !== true)
				{
				alert(state+' '+oElement.title);
				oElement.focus();
				return false;
				}
			}
		}
	if(pass1 && pass2)
		{
		if( (state = check_passwords()) !== true)
			{
			alert(state);
			pass1.focus();
			return false;
			}
		}
	if(cmnMatch_class(oForm, 'ajaxform'))
		{
		SubmitAjaxForm(oForm);
		return false;
		}
	oForm.submit();
	return false;
	}
function CheckStrict(event)
	{
	oInput = getElement(event);
	if(oInput == pass1 || oInput == pass2)
		{
		return check_passwords();
		}
	return check_(oInput);
	}
function check_passwords()
	{
		if(pass1.value==pass2.value)
			{
			pass1.style.background = '#F4FFF2';
			pass2.style.background = '#F4FFF2';
			return true;
			}
		else if(pass1.value && !pass2.value)
			{
			pass1.style.background = '#FFFBF2';
			pass2.style.background = '#FFF2F2';
			return "Введите новый пароль (повторно)";
			}
		else if(pass2.value && !pass1.value)
			{
			pass2.style.background = '#FFFBF2';
			pass1.style.background = '#FFF2F2';
			return "Введите новый пароль";
			}
		else if(pass2.value != pass1.value)
			{
			pass2.style.background = '#FFFBF2';
			pass1.style.background = '#FFFBF2';
			return "Введите совпадающие пароли";
			}
	}
function check_(oInput)
	{
	if( oInput.value=='' )
		{
		oInput.style.background = '#FFF2F2';
		return "Введите";
		}
	else if( oInput.name=='email' && !CorrectEmailAddress(oInput.value) )
		{
		oInput.style.background = '#FFFBF2';
		return "Введите правильный";
		}
	else
		{
		oInput.style.background = '#F4FFF2';
		return true;
		}
	}
function SearchFocus(event)
	{
	el = getElement(event);
	if(el.value==searchstring)
		{
		el.value = '';
		el.style.color = 'black';
		}
	}
function SearchBlur(event)
	{
	el = getElement(event);
	if(!el.value)
		{
		el.value = searchstring;
		el.style.color = 'gray';
		}
	}
function ProcessPNG()//обработка картинок PNG 
	{
	
	for (var i = 0; (oImg = document.getElementsByTagName('img')[i]); i++)
		{
		if(oImg.src.substring(oImg.src.length-3, oImg.src.length) == 'png' && navigator.appName.indexOf('icrosoft') != -1)
			{
			w = oImg.scrollWidth;
			h = oImg.scrollHeight;
			oImg.style.height = h;
			oImg.style.width = w;
			oImg.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+oImg.src+'", sizingMethod="scale")';
			oImg.src = "images/spacer.gif";
			}
		}
	}

Img = new Image();
function getLargeImage(oUrl)
	{
	var what=oUrl.split("/");
	var wh="";
	for(i=0;i<what.length-1;i++)
		wh+=what[i]+'/';
	wh+='large/'+what[i];
	return wh;
	}
function LoadImg(event)
	{
	el = getElement(event);
	
	oUrl=el.src;
	wh = getLargeImage(oUrl);
		
	if( !document.getElementById('div_shadow') )
		{
		extrahtml = xajax_xLoadShadow();
		document.body.innerHTML += extrahtml;
		AddListen();
		}
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i].src == wh)
			{
			Img = GalleryArray[i];
			setTimeout('_fill()',200);
			return;
			}
		}
	}
function _fill()
	{
		if(!isImageOk(Img))
			{
			setTimeout('_fill()',10);
			return;
			}
		shadow = document.getElementById('div_shadow');
		container = document.getElementById('div_enlarged');
		control = document.getElementById('div_control_gallery');
		ofade = document.getElementById('fade');
		
		container.innerHTML = '';
		container.appendChild(Img);
		setTimeout('shadow.style.display = "block";'+
			'y = document.body.scrollTop + (document.body.clientHeight - Img.scrollHeight > 0 ? (document.body.clientHeight - Img.scrollHeight)/2 : 0);'+
			'x = document.body.scrollLeft + (document.body.clientWidth - Img.scrollWidth > 0 ? (document.body.clientWidth - Img.scrollWidth)/2 : 0);'+
			'shadow.style.top = y;'+
			'shadow.style.left = x;'+
			'control.style.left = shadow.scrollLeft + shadow.scrollWidth - 130;control.style.top = shadow.scrollTop + 10;'+
			'shadow.style.visibility="visible";'+
			'ofade.style.width = document.body.scrollWidth;'+
			'ofade.style.height = document.body.scrollHeight;'+
			'ofade.style.display = "block";', 20);
	}
function isImageOk(img) {
    // During the onload event, IE correctly identifies any images
    // that weren't downloaded as not complete. Others should too.
    // Gecko-based browsers act like NS4 in that they report this
    // incorrectly: they always return true.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth
    // and naturalHeight. These give the true size of the image. If
    // it failed to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}
function NextGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == GalleryArray.length - 1 )
				{
				i = -1;
				}
			Img = GalleryArray[i + 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function PrevGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == 0 )
				{
				i = GalleryArray.length;
				}
			Img = GalleryArray[i - 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function CloseGallery(event)
	{
	if(shadow = document.getElementById('div_shadow'))
		{
		shadow.style.visibility = 'hidden';
		shadow.style.display = 'none';
		document.getElementById('fade').style.display = 'none';
		}
	}
function TryToCloseEnlarged(event)
	{
	if(shadow = document.getElementById('div_shadow'))
		{
		el = getElement(event);
		while(el = el.parentNode)
			{
			if( el.id == 'div_shadow' )
				return;
			}
		if(shadow.style.display != 'none')
			{
			shadow.style.visibility = 'hidden';
			shadow.style.display = 'none';
			document.getElementById('fade').style.display = 'none';
			}
		}
	}
function run_movie(arg, w, h) 
	{ 
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+arg+'" ALIGN=""><PARAM NAME=movie VALUE="'+arg+'.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <EMBED src="'+arg+'.swf"  quality=high wmode=transparent  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+arg+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>'); 
	}

function AddToCart( id )
	{
	//var win=window.open("/shop/addtocart/?id="+id+"&amount=1"/*+document.getElementById('qua_'+id).value*/,"_blank","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=400,height=200");
	xajax_xAddToCart( id, 1 );
	el = document.getElementById('i_'+id);
	el.innerHTML = 'Заказали!';
	el.className = 'ordered';
	var idt = setTimeout("el.className = '';el.innerHTML = 'Заказать';",2000);
	}
function ShowCart()
	{
	xajax_xShowCart();
	}
function CorrectEmailAddress(email)
	{
	if(email.length > 0 && ( email.indexOf("@",0) == -1 || email.indexOf(".",0) == -1 ) )
		return false;
	return true;
	}
function getElement(event)
	{
	// Internet Explorer
	if (event.srcElement)
		{
		el = event.srcElement;
		}
	// Netscape and Firefox
	else if (event.target)
		{
		el = event.target;
		}
	return el;
	}
function Prevent(event)
	{
	if (event.cancelable)
		{
		event.preventDefault();
		}
	else
		{
		event.returnValue=false;
		}
	}
function ProcessHover(event)
	{
	el = getElement(event);
	el_pos = getElementPosition(el);
	hider = $('div_hider');
	submenu = $('div_submenu');
	hider.style.top = el_pos.top + el_pos.height;
	hider.style.left = el_pos.left;
	hider.style.width = el_pos.width;
	hider.style.display = 'block';
	
	submenu.style.top = el_pos.top + el_pos.height + hider.scrollHeight + 3;
	submenu.style.left = el_pos.left;
	submenu.style.width = Math.max(170, el_pos.width * 1.3);
	submenu.style.display = 'block';
	}
var tmout;
var tmout_hider;
function ProcessUnhover(event)
	{
	tmout = setTimeout("$('div_submenu').style.display='none';", 200);
	tmout_hider = setTimeout("$('div_hider').style.display='none';", 200);
	}
function CancelUnhover(event)
	{
	clearTimeout(tmout);
	clearTimeout(tmout_hider);
	}

function ShowHider(event)
	{
	clearTimeout(tmout_hider);
	el = getElement(event);
	el_pos = getElementPosition(el);
	hider = $('div_hider');
	hider.style.top = el_pos.top + el_pos.height;
	hider.style.left = el_pos.left;
	hider.style.width = el_pos.width;
	hider.style.display = 'block';
	}
function HideHider(event)
	{
	el = getElement(event);
	if(!cmnMatch_class(el, 'a_services'))
		$('div_hider').style.display='none';
	}

function getElementPosition(elem)
	{
	var w = elem.offsetWidth;
	var h = elem.offsetHeight;
	var l = 0;
	var t = 0;
	while (elem)
		{
		l += elem.offsetLeft;
		t += elem.offsetTop;
		elem = elem.offsetParent;
		}
	return {"left":l, "top":t, "width": w, "height":h};
	}