/*
// 2006-03-18
// Copyright (c) Artburo | http://www.artburo.ru/
// Author - Stepan Galkin
*/

/* common vars */
var ua				= navigator.userAgent.toLowerCase();
var isOpera			= (ua.indexOf('opera') != -1);
var isIE			= (ua.indexOf('msie') != -1 && !isOpera && (ua.indexOf('webtv') == -1) );
var errorElm		= null;			// объект формы, где есть ошибка
var currIdx			= null;			// индекс текущей картинки из m_LargePictures
var hasThumbs		= false;		// флаг для окна-превьюшки с большими картинками
var m_LargePictures = new Array();	// пути к большим картинкам
var propsMsg		= new Array();	// здесь храним сообщение об ошибке для каждого свойства


/* item's pictures rollover */
function ShowLargePicture(idx,cIdx)
{
	if (!document.getElementById) return;
	var cIdx = (cIdx ? cIdx : 0);
	var container = document.getElementById('m_LargePicture');
	var newPic = new Image();
	var hasChild = (m_LargePictures[idx].length > 1 ? true : false);
	if (idx == currIdx && !hasChild) return;
	container.className = 'loading';
	container.innerHTML = 'Картинка загружается...';
	newPic.src = m_LargePictures[idx][cIdx];
	if (hasChild)
		newPic.onclick = new Function('ShowLargePicture(' + idx + ',' + (cIdx == m_LargePictures[idx].length - 1 ? 0 : cIdx + 1) + ')');
	else
		newPic.onclick = new Function('ShowLargePicture(' + (idx == m_LargePictures.length - 1 ? 0 : idx + 1) + ',0)');
	if (hasThumbs)
	{
		var prevThumb = document.getElementById('thumb_' + currIdx);
		var currThumb = document.getElementById('thumb_' + idx);
		if (prevThumb) prevThumb.className = '';
		if (currThumb)
		{
			currThumb.className = 'current';
			currThumb.childNodes.item(0).focus();
		}
	}
	container.innerHTML = '';
	container.className = '';
	container.appendChild(newPic);
	currIdx = idx;
	if (document.getElementById('m_PicturesLinks'))
	{
		var _html = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td>Другие фото:</td><td align="right">';
		var _pics = (hasChild ? m_LargePictures[idx] : m_LargePictures);
		for (var i = 0; i < _pics.length; i++)
		{
			if (hasChild)
				_html += '<span onclick="ShowLargePicture(' + idx + ',' + i + '); return false;"' + (i == cIdx ? ' class="current"' : '') +'>' +
						 (i == cIdx ? '' : '<a href="javascript:void(0);">') + (i + 1) + (i == cIdx ? '' : '</a>') + '</span>\n';
			else
				_html += '<span onclick="ShowLargePicture(' + idx + ',0); return false;"' + (i == idx ? ' class="current"' : '') +'>' +
						 (i == idx ? '' : '<a href="javascript:void(0);">') + (i + 1) + (i == idx ? '' : '</a>') + '</span>\n';
		}
		_html += '<span class="arrows">';
		if (hasChild)
			 _html += '<a href="javascript:void(0);"><img src="/images/p-prev.gif" width="10" height="13" alt="Предыдущая страница" onclick="ShowLargePicture(' + idx + ',' + (cIdx == 0 ? m_LargePictures[idx].length - 1 : cIdx - 1) + ');" /></a>' +
					  '<a href="javascript:void(0);"><img src="/images/p-next.gif" width="10" height="13" alt="Следующая страница" onclick="ShowLargePicture(' + idx + ',' + (cIdx == m_LargePictures[idx].length - 1 ? 0 : cIdx + 1) + ');" /></a>';
		else
			 _html += '<a href="javascript:void(0);"><img src="/images/p-prev.gif" width="10" height="13" alt="Предыдущая страница" onclick="ShowLargePicture(' + (idx == 0 ? m_LargePictures.length - 1 : idx - 1) + ');" /></a>' +
					  '<a href="javascript:void(0);"><img src="/images/p-next.gif" width="10" height="13" alt="Следующая страница" onclick="ShowLargePicture(' + (idx == m_LargePictures.length - 1 ? 0 : idx + 1) + ');" /></a>';
		document.getElementById('m_PicturesLinks').innerHTML = _html + '</span></td></tr></table>';
	}
	return false;
}


/* item's sets selector */
function addSet(setId)
{
	if (!document.getElementById) return;
	var _tr		= document.getElementById(setId);
	var _cb		= document.getElementById(setId + 'cb');
	var _props	= document.getElementById(setId + 'props');
	var _block	= document.getElementById(setId + 'hl');
	_props.style.display = _cb.checked ? 'block' : 'none';
	with (_block.style)
	{
		height = _tr.offsetHeight;
		marginLeft = -8;
		marginTop = -5;
		visibility = _cb.checked ? 'visible' : 'hidden';
	}
}


/* item's form validator */
function Add2Cart(form,propsMsg)
{
	if (!propsMsg || typeof(propsMsg) != 'object') return true;
	var error = false;
	var border;
	var error_message;
	with (form.elements)
	{
		clearError();
		for (var i = 0; i < length; i++)
		{
			if (item(i).tagName != 'SELECT' || item(i).parentNode.style.display == 'none') continue;
			else if (item(i).getAttribute('required') == 1 && item(i).value == '0')
			{
				error = true;
				errorElm = item(i);
				error_message = propsMsg[item(i).getAttribute('propId')];
				item(i).onchange = new Function('checkError(this)');
				break;
			}
		}
	}
	if (error && error_message)
	{
		if (!border)
		{
			border = document.createElement('DIV');
			border.id = errorElm.name + 'brd';
			with (border.style)
			{
				position = 'absolute';
				width = errorElm.offsetWidth + (isIE ? 6 : 0);
				height = errorElm.offsetHeight + (isIE ? 6 : 0);
				border = 'solid 3px #FF002E';
				marginLeft = -3;
				marginTop = -3;
				zIndex = 0;
				visibility = 'hidden';
			}
			errorElm.parentNode.insertBefore(border,errorElm);
		} else border = document.getElementById(errorElm.name + 'brd')
		border.style.visibility = 'visible';
		alert(error_message);
		errorElm.focus();
		return false;
	} else return true;
}

function clearError()
{
	if (errorElm != null && document.getElementById(errorElm.name + 'brd'))
			document.getElementById(errorElm.name + 'brd').style.visibility = 'hidden';
}

function checkError(elem)
{
	if (elem.value != 0 && elem == errorElm)
		clearError();
}
