$(init);

function init()
{
	$('ul.works li').each(function()
	{
		var e = this;
		$('.gallery a', e).lightBox()
		$('a:first', e).click(function(){
			$('.gallery a:first', e).click();
			ping(this.href);
			return false;
		});
	});
	$('#textoverlay').click(function(){
		if ($(this).hasClass('formactive'))
		{
			$(this).removeClass('formactive');
			$('#textoverlay-alpha').css({opacity: 0.8});
			if ($('.textoverlay.active').length > 0)
			{
				return;
			}
		}
		$('.textoverlay').removeClass('active');
		$('#textoverlay .block').removeClass('active');
		$(this).hide()
	});
	$('#textoverlay .block').click(function(){return false;});
	$('#textoverlay .block a[target=_blank]').click(function(){window.open(this.href); return false});
	$('.textoverlay').each(function()
	{
		var e = this;
		var pos = e.href.indexOf('#');
		if (pos == -1) return;
		var href = e.href.substr(pos);
		$(e).click(function()
		{
			ping(href.substr(1));
			$('.textoverlay').removeClass('active');
			$('#textoverlay').removeClass('formactive');
			$(e).blur().addClass('active');
			$('#textoverlay').show();
			$('#textoverlay-alpha').height($('body').height()).show().css({opacity: 0.8});
			$('#textoverlay .block').removeClass('active');
			$(href).addClass('active');
			return false;
		});
	});
	$('.service > .inner').hide();
	$('.service > h4 > a').click(function()
	{
		var pos = this.href.indexOf('#');
		if (pos == -1) return;
		var href = this.href.substr(pos);
		var self = $(href).hasClass('active');
		$('.service .active').removeClass('active').slideUp();
		if (!self)
		{
			$(href).addClass('active').slideDown(function()
			{
				$('#textoverlay-alpha').height($('html')[0].scrollHeight);
			});
		}
	});
	$('a[href=#contact]').click(function()
	{
		ping('contact');
		$('#textoverlay').show().addClass('formactive');
		if ($('.textoverlay.active').length > 0)
		{
			$('#textoverlay-alpha').height($('html')[0].scrollHeight).css({opacity: 0.92});
		}
		else
		{
			$('#textoverlay-alpha').height($('body').height()).show().css({opacity: 0.8});
		}
		$('#contact .ok').hide();
		$('#contact .controls').show();
		var defaultIndex = $(this).hasClass('seo') ? 4 : 0;
		$('#contact select').val($('#contact select option')[defaultIndex].value);
	});
	$('#contact').click(function(){return false;});
	$('#contact').submit(ajaxform_submit);
	$('#contact').bind('reset', ajaxform_reset);
	$('#contact input[type=submit]').click(function(){$(this.form).trigger('submit')});
	$('#contact input[type=reset]').click(function(){$(this.form).trigger('reset')});
}

function ping(href)
{
	$.post(href, {'ping' : 1}, function(d, s){}, 'json');
}

function ajaxform_submit()
{
	var res = {};
	var form = this;
	$(':input', this).each(function()
	{
		if (!this.name) return;
		res[this.name] = $(this).val();
		res['_antispam'] = 'desu';
	});
	$('.status', form).text('');
	$.post(this.action, res, function(d, s){ajaxform_result(d, s, form)}, 'json');
	return false;
}

function ajaxform_reset()
{
	$('.status', this).text('');
	$('.error', this).text('');
	return true;
}

function ajaxform_result(data, status, form)
{
	if (status != 'success' || typeof data['ok'] == 'undefined') return;
	$('.error', form).text('');
	if (data['ok'])
	{
		var msg = data['result'] || '';
		if (msg.indexOf('@Message: ') != -1)
		{
			msg = msg.substr(10);
		}
		$(form).trigger('reset');
		$('.status', form).text(msg);
		$('.controls', form).slideUp(function(){$('.ok', form).show();});
	}
	else if (typeof data['errors'] != 'undefined')
	{
		for (var key in data['errors'])
		{
			var name = 'label.error[for=' + key + ']';
			$(name, form).text(data['errors'][key]);
		}
	}
}

