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

function create_elements()
{
	if ($$('a.artbox') != '')
	{
		var bodyEl = $('sherwood');
		var shadow = new Element('div', {id: 'shadow'});
		var imagepopup = new Element('div', {id: 'imagepopup'});
		var imagepopupinner = new Element('div', {id: 'imagepopupinner'});
		bodyEl.adopt(shadow, 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');
				show_images(ihref, index, 1);
			});
			imagelink(element);
		});
	}

	if ($$('a.play') != '')
	{
		var player = new Element('div', {id: 'player'});
		var playerinner = new Element('div', {id: 'playerinner'});
		player.inject($('songs'), 'after');
		player.grab(playerinner);

		var songs = $$('a.play');
		songs.each(function(element) {
			element.addEvent('click', function(event) {
				event = new Event(event);
				event.preventDefault();
				var shref  = element.getAttribute('href');
				var stime  = element.getPrevious().getPrevious().getChildren('abbr').get('text');
				var stime  = /(\d{2}):(\d{2})/.exec(stime);
				var stime  = stime[1].toInt() * 60 + stime[2].toInt();
				$$('strong.fn').setStyle('color', '#E6BC64');
				element.getParent().getChildren('strong').setStyle('color', '#765D3E');
				play_song(shref, stime);
			});
		});
	}

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

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

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

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

	$('imagepopupinner').empty();
	var outerHeight = $('outer').offsetHeight;
	var html = '<p class="control">' + prevlink + '<a class="close" href="#" title="Закрыть окно" onclick="closepopup(); return false;">Закрыть</a>' + nextlink + '</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.5).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 closepopup()
{
	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.5);
	element.grab(span);
	element.addEvent('mouseenter', function() {span.setStyle('background-position', 'center center');});
	element.addEvent('mouseleave', function() {span.setStyle('background-position', 'center -9000px');});
}

function play_song(shref, stime)
{
	$('playerinner').empty();
	$('playerinner').set('html', '<p>Для прослушивания композиции вам необходимо<br>установить flash-плеер: <a href="http://www.adobe.com/go/getflashplayer"><img src="/images/getflashplayer.gif" alt="Установить flash-плеер" width="138" height="12"</p>');
	var flashvars = {file: shref, duration: stime, provider: 'sound', backcolor: '#E6BC64', frontcolor: '#000000', lightcolor: '#58442B', autostart: true};
	var params = {wmode: 'transparent'};
	swfobject.embedSWF('/images/player.swf', 'playerinner', '315', '24', '9', false, flashvars, params);
}

function showpopup(sendF, text)
{
	if (!$('warn'))
	{
		var warn = new Element('p', {id: 'warn'});
		warn.inject($('sidebar'), 'top');
	}
	$('warn').set('text', text);
	var sendF_effect = new Fx.Morph(sendF, {duration: 1500, wait: false});
	sendF_effect.start({'background-color': ['#FFFF00', '#E6BC64'], 'border-color': ['#FFFFFF', '#58442B']}).chain(function() {
		$(sendF).focus();
	});
}

var contactText  = 'Представьтесь, пожалуйста:';
var mailText     = 'Пожалуйста, укажите адрес E-mail:';
var w_mailText   = 'Указан некорректный E-mail:';
var subjectText  = 'Пожалуйста, укажите тему письма:';
var messageText  = 'Пожалуйста, введите сообщение:';

function sendform()
{
	if ($('person').value == '')  {var sendF = $('person'); var text = contactText; showpopup(sendF, text); return false;}
	if ($('mail').value == '')    {var sendF = $('mail'); var text = mailText; showpopup(sendF, 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 text = w_mailText; showpopup(sendF, text); return false;}}
	if ($('subject').value == '') {var sendF = $('subject'); var text = subjectText; showpopup(sendF, text); return false;}
	if ($('message').value == '') {var sendF = $('message'); var text = messageText; showpopup(sendF, text); return false;}
	var expdate = new Date();
	var mFromNow = expdate.getTime() + (365*24*60*60*1000);
	expdate.setTime(mFromNow);
	SetCookie('person', $('person').value, expdate, '/');
	SetCookie('mail', $('mail').value, expdate, '/');
	return true;
}

function loadvalues_mail()
{
	var f1 = GetCookie('person');
	var f2 = GetCookie('mail');
	if (f1 != null && $('person').value == '') {$('person').value = f1;}
	if (f2 != null && $('mail').value == '') {$('mail').value = f2;}
}

function addform()
{
	if ($('firstname').value == '') {var sendF = $('firstname'); var text = contactText; showpopup(sendF, text); return false;}
	if ($('message').value == '') {var sendF = $('message'); var text = messageText; showpopup(sendF, text); return false;}
	var expdate = new Date();
	var mFromNow = expdate.getTime() + (365*24*60*60*1000);
	expdate.setTime(mFromNow);
	SetCookie('firstname', $('firstname').value, expdate, '/');
	if ($('city').value != '') SetCookie('city', $('city').value, expdate, '/');
	return true;
}

function loadvalues_guest()
{
	var f1 = GetCookie('firstname');
	var f2 = GetCookie('city');
	if (f1 != null && $('firstname').value == '') {$('firstname').value = f1;}
	if (f2 != null && $('city').value == '') {$('city').value = f2;}
}

function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	var arg  = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie (name, value)
{
	var argv    = SetCookie.arguments;
	var argc    = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path    = (argc > 3) ? argv[3] : null;
	var domain  = (argc > 4) ? argv[4] : null;
	var secure  = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path    == null) ? "" : ("; path=" + path)) +
	((domain  == null) ? "" : ("; domain=" + domain)) +
	((secure  == true) ? "; secure" : "");
}

function effects()
{
	if ($('songs'))
	{
		var myAccordion = new Accordion('a.lyrics', 'p.lyrics', {
			display: -1,
			alwaysHide: true
		});
	}
}

function init()
{
	create_elements();
	effects();
}
