function wo(url) {window.open(url, '', '');}

function create_elements()
{
	var bodyEl = $('expert');
	var shadow = new Element('div', {id: 'shadow'});
	var popup = new Element('div', {id: 'popup'});
	bodyEl.adopt(shadow, popup);

	if ($$('a.artbox') != '')
	{
		var imagepopup = new Element('div', {id: 'imagepopup'});
		var imagepopupinner = new Element('div', {id: 'imagepopupinner'});
		bodyEl.adopt(imagepopup);
		imagepopup.grab(imagepopupinner);

		images = $$('a.artbox');
		images.each(function(element, index) {
			element.addEvent('click', function(event) {
				event = new Event(event);
				event.preventDefault();
				var ihref = element.getAttribute('href');
				var title = element.getAttribute('title');
				show_images(ihref, title, index, 1);
			});
			imagelink(element);
		});
	}

	new Asset.images(['/img/bgpopup.gif', '/img/b_ok.gif', '/artbox/wait.gif', '/artbox/prev.gif', '/artbox/close.gif', '/artbox/next.gif', '/artbox/zoom.gif']);
}

var personText   = 'Представьтесь, пожалуйста.';
var mailText     = 'Пожалуйста, укажите ваш адрес E-mail.';
var w_mailText   = 'Указан некорректный адрес E-mail.<br>Пожалуйста, исправьте ошибку.';
var subjectText  = 'Пожалуйста, укажите тему письма.';
var messageText  = 'Пожалуйста, введите ваше сообщение.';
var phoneText    = 'Пожалуйста, укажите ваш телефон.';
var interestText = 'Пожалуйста, уточните, по каким вопросам<br>вы хотели бы получить консультацию.';

function showpopup(sendF, scrollF, text)
{ 
	var outerHeight = $('outer').offsetHeight;
	var html = '<p>' + text + '</p>';
	html += '<p class="buttons"><span id="goback">ОК</span></p>';
	$('popup').innerHTML = html;
	$('shadow').setStyles({'height': outerHeight, 'opacity': 0.7, 'display': 'block'});
	$('popup').setStyle('display', 'block');
	$('goback').addEvent('click', function() {
		$('popup').setStyle('display', 'none');
		$('shadow').setStyle('display', 'none');
		var sendF_effect = new Fx.Morph(sendF, {duration: 1500, wait: false});
		new Fx.Scroll(window).toElement(scrollF).chain(function() {
			sendF_effect.start({'background-color': ['#C24A94', '#FFFFFF'], 'border-color': ['#A90B60', '#7C5093']}).chain(function() {
				$(sendF).focus();
			});
		});
	});
}

function sendform()
{
	if ($('person').value == '')  {var sendF = $('person'); var scrollF = $('mainform'); var text = personText; showpopup(sendF, scrollF, text); return false;}
	if ($('mail').value == '')    {var sendF = $('mail'); var scrollF = $('mainform'); var text = mailText; showpopup(sendF, scrollF, text); return false;}
	if ($('mail').value != '')    {var rightmail = /^[\w\.-]+@[a-z0-9\.-]+\.[a-z]{2,6}$/i; if (!rightmail.test($('mail').value)) {var sendF = $('mail'); var scrollF = $('mainform'); var text = w_mailText; showpopup(sendF, scrollF, text); return false;}}
	if ($('subject').value == '') {var sendF = $('subject'); var scrollF = $('mainform'); var text = subjectText; showpopup(sendF, scrollF, text); return false;}
	if ($('message').value == '') {var sendF = $('message'); var scrollF = $('mainform'); var text = messageText; showpopup(sendF, scrollF, text); return false;}
	return true;
}

function makeorder()
{
	if ($('person').value == '')   {var sendF = $('person'); var scrollF = $('orderform'); var text = personText; showpopup(sendF, scrollF, text); return false;}
	if ($('phone').value == '')    {var sendF = $('phone'); var scrollF = $('orderform'); var text = phoneText; showpopup(sendF, scrollF, text); return false;}
	if ($('interest').value == '') {var sendF = $('interest'); var scrollF = $('orderform'); var text = interestText; showpopup(sendF, scrollF, text); return false;}
	return true;
}

function show_images(image, title, index, start)
{
	images = $$('a.artbox');
	var imagesLength = images.length - 1;

	if (index != 0)
	{
		prev = index - 1;
		var prevtitle = images[prev].getAttribute('title');
		if (!prevtitle) prevtitle = '';
		prevlink = '<span class="prev" title="Предыдущее фото" onclick="show_images(\'' + images[prev].getAttribute('href') + '\', \'' + prevtitle.replace(/'/g, "\\'") + '\', ' + prev + ');">Предыдущее фото</span>';
	}
	else {prevlink = '';}

	if (index < imagesLength)
	{
		next = index + 1;
		var nexttitle = images[next].getAttribute('title');
		if (!nexttitle) nexttitle = '';
		nextlink = '<span class="next" title="Следующее фото" onclick="show_images(\'' + images[next].getAttribute('href') + '\', \'' + nexttitle.replace(/'/g, "\\'") + '\', ' + next + ');">Следующее фото</span>';
	}
	else {nextlink = '';}

	$('imagepopupinner').empty();
	var outerHeight = $('outer').offsetHeight;
	var html = '<p class="control">' + prevlink + '<span class="close" title="Закрыть окно" onclick="closeimagepopup();">Закрыть окно</span>' + nextlink + '</p>';
	if (title) html += '<p class="title">' + title + '</p>';
	html += '<p class="count">Фото ' + (index + 1) + ' из ' + (imagesLength + 1) + '</p>';

	fx1 = new Fx.Tween($('shadow'), {duration: 200});
	fx2 = new Fx.Tween($('imagepopup'), {duration: 200});

	if (start)
	{
		$('shadow').setStyles({'height': outerHeight, 'opacity': 0, 'display': 'block'});
		$('imagepopup').setStyles({'opacity': 0, 'display': 'block'});
		fx1.start('opacity', 0.7).chain(function() {
			fx2.start('opacity', 1).chain(function() {
				$('imagepopupinner').setStyle('background-position', 'center center');
				(function(){img = new Asset.image(image, {onload: function() {
					$('imagepopupinner').setStyle('background-position', 'center -9000px');
					img.setStyle('opacity', 0);
					img.inject('imagepopupinner');
					fx3 = new Fx.Tween(img, {duration: 1000});
					fx3.start('opacity', 1).chain(function() {
						$('imagepopupinner').innerHTML += html;
					});
				}});}).delay(1000);
			});
		});
	}
	else
	{
		$('imagepopupinner').setStyle('background-position', 'center center');
		(function(){img = new Asset.image(image, {onload: function() {
			$('imagepopupinner').setStyle('background-position', 'center -9000px');
			img.setStyle('opacity', 0);
			img.inject('imagepopupinner');
			fx3 = new Fx.Tween(img, {duration: 1000});
			fx3.start('opacity', 1).chain(function() {
				$('imagepopupinner').innerHTML += html;
			});
		}});}).delay(1000);
	}
}

function closeimagepopup()
{
	new Fx.Tween($('imagepopup'), {duration: 200}).start('opacity', 0).chain(function() {
		$('imagepopup').setStyle('display', 'none');
		$('imagepopupinner').empty();
		new Fx.Tween($('shadow'), {duration: 200}).start('opacity', 0).chain(function() {
			$('shadow').setStyle('display', 'none');
		});
	});
}

function imagelink(element)
{
	var span = new Element('span');
	span.setStyle('opacity', 0.6);
	element.grab(span);
	element.addEvent('mouseenter', function() {span.setStyle('background-position', 'center center');});
	element.addEvent('mouseleave', function() {span.setStyle('background-position', 'center -9000px');});
}

function showhide()
{
	if ($('ref').getStyle('display') != 'block')
	{
		$('ref').setStyle('display', 'block');
		$('refcontrol').setStyle('background-position', '-1491px center');
	}
	else
	{
		$('ref').setStyle('display', 'none');
		$('refcontrol').setStyle('background-position', '0px center');
	}
}

function init()
{
	create_elements();
}
