var calc =
{	cat: function(el)
	{		el = $(el);
		var v = el.val();
		el = el.parent().next().children().hide().end();
		if (v)			el.children('[name='+v+']').show().children().removeAttr('selected');
	},

	item: function(el)
	{		el = $(el);
		var v = el.val();
		el = el.parent().next().children('button');
		(v) ? el.show() : el.hide();
	},

	add: function(el)
	{		el = $(el).parent();
		var cat = el.prev().prev().children('select'),
			item = el.prev().children('select[name='+cat.val()+']'),
			ex = $('#calc input[name='+item.val()+']');
		if (ex.length > 0) {			ex.val(parseInt(ex.val())+1);		} else {
			var prot = $('#calc_prot');
			prot.clone().insertBefore(prot).removeAttr('id').show().addClass('calc')
				.children(':first').html(cat.children('[value='+cat.val()+']').text())
				.next().html(item.children('[value='+item.val()+']').attr('title'))
				.next().html(item.children('[value='+item.val()+']').attr('price'))
				.next().children('input').attr('name', item.val()).val(1).end()
				.next().html(item.children('[value='+item.val()+']').attr('price'));
		}
		this.sum();
	},

	sum: function()
	{
		var s = 0, e, p, f = false;
		$('#calc .calc').each(function()
		{			f = true;
			e = $(this);
			s += parseInt(e.children(':last').text() || 0) * parseInt(e.find('input').val());
		});
		var c = $('#calc');
		if (f) {			c.find('.disc,.total').show();
			c.find('.none').hide();
		} else {			c.find('.disc,.total').hide();
			c.find('.none').show();
		}
		s *= (100 - parseInt($('#calc_disc').val())) / 100;
		$('#calc .total > td:last').text(s);
		$('#calc_confirm').attr('disabled', s == 0);
	},

	quant: function(el)
	{
		el = $(el);
		var v = parseInt(el.val() || 0) * parseInt(el.parent().prev().text() || 0);
		el.parent().next().text(v);
		this.sum();
	},

	confirm: function()
	{
		var s = [], e;
		$('#calc input').each(function()
		{
			e = $(this);
			if (e.val() > 0)
				s.push(e.attr('name')+'='+e.val());
		});
		window.location.href = 'calculator/confirm/'+s.join(';')+'/'+$('#calc_disc').val();
		return false;
	},

	generate: function()
	{
		$('#calc').attr('action', 'calculator/generate').attr('target', '_blank');
	},

	send: function()
	{
		$('#calc').attr('action', 'calculator/send').removeAttr('target');
	}};

function go(url)
{
	window.location.href = url;
	return false;
}

function init()
{	var f = $('.flash'),//.bind('contextmenu', function() { return false; }),
		logos = f.children('img'), i = logos.length-1, n, cur, next;
	//$(logos[i]).load(function()
	//{
		$(logos[i]).show();
		if (logos.length > 1)
			setInterval(function()
			{
				n = (i+1)%logos.length;
				cur = $(logos[i]);
				next = $(logos[n]);
				next.insertBefore(cur).show();
				cur.fadeOut(1000);
				setTimeout(function() { cur.prependTo(cur.parent()); }, 1000);
				i = n;
			}, 5000);
	//});
	var s = $('.special div'), sl = s.length, si = 0;
	if (sl > 0) {
		$(s[si]).show();
		setInterval(function()
		{
			$(s[si]).fadeOut(300);
			si = ++si%sl;
			setTimeout(function() { $(s[si]).fadeIn(300); }, 300);
		}, 2000);
	}
	$('marquee').marquee()
		.mouseover(function () { $(this).trigger('stop'); })
		.mouseout(function () { $(this).trigger('start'); });
}

function add_upload(el)
{
	$(el).before('<div><input type="file" name="imgs[]" /><br />\
				  <select name="imgs_wm[]">\
				  <option value="1">Добавить копирайт</option>\
				  <option value="0">Не добавлять копирайт</option>\
				  </select>\
				  <a href="#" onclick="return del_upload(this);">[Удалить]</a></div>');
	return false;
}

function del_upload(el)
{
	$(el).parent().remove();
	return false;
}

function add_simular()
{	var s = $('#simular');
	s.clone().insertAfter(s.parent().find('div:last')).removeAttr('id').show().children('select').attr('name', 'simular[]');
	return false;}

function del_simular(el)
{	$(el).parent().remove();
	return false;}

function dd(el)
{
	$(el).prev().toggleClass('dd_down').end().next().toggle();
	return false;
}
