//const
var l2_const_adena = 57;

var l2_const_dcry = 1458;
var l2_const_ccry = 1459;
//

function l2_response(text){
	var lines	= text.split('\n');
	switch(lines[0]){
	case 'got_set_items':
		if(lines.length < 3)
			break;
		if($('set_items' + lines[1]))
			break;
		var div = new Element('div', {'id': 'set_items' + lines[1]}).set('html', '<br />');
		var table = new Element('table', {'class': 'forum', 'cellpadding': 0, 'cellspacing': 0}).inject(div, 'bottom');
		new Element('col', {'width': 40}).inject(table, 'bottom');
		new Element('col').inject(table, 'bottom');
		new Element('col', {'width': 80}).inject(table, 'bottom');
		new Element('col', {'width': 80}).inject(table, 'bottom');
		var th = '<th class="icon"></th><th class="l">' + l2name + '</th><th class="c">P. Def.</th><th class="right c">MP</th>';
		new Element('tr', {'class': 'topic'}).set('html', th).inject(table, 'bottom');
		for(var i = 2; i < lines.length; i++){
			var line = lines[i].split('|');
			var td = '<td class="icon"><img src="' + line[2] + '" /></td>'
					+'<td><a href="/item/' + line[0].replace(/\s/g, '+') + '">' + line[1] + '</a></td>'
					+'<td class="c">' + line[3] + '</td>'
					+'<td class="right c">' + line[4] + '</td>';
			new Element('tr', {'class': 'topic'}).set('html', td).inject(table, 'bottom');
		}
		new Element('tr', {'class': 'btm'}).set('html', '<td colspan="4">&nbsp;</td>').inject(table, 'bottom');
		div.inject($('set' + lines[1]), 'bottom');
		div.getParent('tr').getElement('img').addClass('hidden');
		div.set('slide', {duration: 1000, transition: Fx.Transitions.linear});
		break;
	default:
		if(text)
			alert(text);
	}
}

function addItemInList(item){
	var a	= new Element('a', {'href': '/item/' + item.ttl, 'title': item.name});
	new Element('img', {'src': item.image}).inject(a, 'top');
	new Element('span').set('html', item.name).inject(a, 'bottom');
	a.inject($('found_items'), 'bottom');
}

function adena(cost){
	cost = cost.toString();
	cstr = '';
	dpos = cost.indexOf('.');
	if(dpos !== -1){
		cstr = cost.substr(dpos, cost.length - dpos);
		cost = cost.substr(0, dpos);
	}
	while(cost.length){
		cstr = (cost.length > 3 ? '\'' + cost.substr(-3) : cost) + cstr;
		cost = cost.length > 3 ? cost.substr(0, cost.length - 3) : '';
	}
	if(dpos !== -1)
		cstr = '~' + cstr;
	return cstr;
}

function miniSearcher(){
	var str = this.value;
	if(str.length < 1){
		$('found_items').addClass('no_display');
		return;
	}
	var first = str.substr(0,1);
	if(!alphabet.match(eval('/' + first + '/i'))){
		$('found_items').addClass('no_display');
		return;
	}
	if(typeof items[first] !== 'undefined'){
		var itms = items[first];
		var cnt = 0;
		$('found_items').empty();
		for(var i = 0; i < itms.length; i++){
			if(itms[i].name.match(eval('/^' + str + '/i'))){
				$('found_items').removeClass('no_display');
				addItemInList(itms[i]);
				cnt++;
			}
		}
		if(cnt > 10){
			$('found_items').setStyle('height', '340px');
		}else
			$('found_items').setStyle('height', (34 * cnt) + 'px');
		if(!cnt)
			$('found_items').addClass('no_display');
	}else{
		new Request({
			url: '/modules/l2/' + items_type + '/' + first + '.txt',
			method: 'get',
			onSuccess: function (text){
				items[first] = new Array();
				var lines	= text.split('\n');
				var cnt = 0;
				$('found_items').empty();
				for(var i = 0; i < lines.length; i++){
					var item = lines[i].split('|');
					items[first][i] = {'ttl': item[0], 'name': item[1], 'image': item[2]};
					if(item[1].indexOf(str) !== -1){
						$('found_items').removeClass('no_display');
						addItemInList(items[first][i]);
						cnt++;
					}
				}
				if(cnt > 10){
					$('found_items').setStyle('height', '340px');
				}else
					$('found_items').setStyle('height', (34 * cnt) + 'px');
				if(!cnt)
					$('found_items').addClass('no_display');
			}
		}).send();
	}
}

function countNodePrice(node_tr){
	if(!node_tr.id || !node_tr.id.match(/^sub/))
		return;
	var parent = $(node_tr.id.replace(/^sub/, 'ingr'));
	var cost = 0;
	var tr = node_tr.getElement('tr');
	do {
		cost += parseFloat(tr.getElement('input._price').value) * parseInt(tr.getElement('b').get('html'));
	} while ((tr = tr.getNext()) !== null);
	parent.getElement('input._price').value =
		Math.round(10000 * cost / parseFloat(parent.getElement('input._output').value)) / 10000;
	var parent = parent.getParent('tr');
	if(parent)
		countNodePrice(parent);
}

window.addEvent('domready', function(){
	$$('.grouper img').each(function(img){
		img.addEvent('click', function(e){
			var th = img.getParent('th');
			var id = th.id.replace(/\D/g, '');
			if(this.src.match(/minus\.gif/)){
				th.getElements('img').each(function(img){
					img.src = img.src.replace(/minus\.gif/, 'plus.gif');
				});
				$('grp' + id).addClass('no_display');
			}else{
				th.getElements('img').each(function(img){
					img.src = img.src.replace(/plus\.gif/, 'minus.gif');
				});
				$('grp' + id).removeClass('no_display');
			}
		});
	});
	$$('._price').each(function(input){
		input.addEvent('keyup', function (e){
			var this_id	= this.id;
			var recipe = this_id.replace(/(mat|fee)(\d+)\_[\-\d]+/, '$2');
			var value	= this.value;
			var item_id	= $(this).getParent('tr').getElement('img').id.replace(/item(\d+)/, '$1');
			$$('._priceof' + item_id).each(function(inp){
				countNodePrice(inp.getParent('tr').getParent('tr'));
				if(inp.id !== this_id)
					inp.value = value;
			});
			var cost = 0;
			$$('._mat' + recipe).each(function(pricer){
				if(!pricer.hasClass('_level0'))
					return;
				var mat = pricer.id.replace(/(mat|fee)\d+\_([\-\d]+)/, '$2');
				var price = parseInt(pricer.value);
				if(parseInt(mat) >= 0){
					if(!isNaN(price))
						cost += price * parseInt($('q' + recipe + '_' + mat).get('html'));
				}else
					if(!isNaN(price))
						cost += price;
			});
			var output = parseInt($('output' + recipe).value);
			if(output > 1)
				cost = Math.round(cost / output * 10000) / 10000;
			$('cost' + recipe).set('html', adena(cost));
		});
	});
	$$('._dprice').each(function(input){
		input.addEvent('keyup', function(e){
			var dual_id = this.id.replace(/(dual)(\d+)\_\d+/, '$2');
			var cost = 0;
			$$('._dual' + dual_id).each(function(pricer){
				var mat = pricer.id.replace(/(dual)\d+\_(\d+)/, '$2');
				var price = parseInt(pricer.value);
				if(!isNaN(price))
					cost += price * parseInt(parseInt(mat) ? $('b' + dual_id + '_' + mat).get('html') : 1);
			});
			$('crycost' + dual_id).set('html', '~' + (Math.round(100 * cost / $('cry_count' + dual_id).value) / 100));
			$('dcost' + dual_id).set('html', adena(cost));
		});
	});
	$$('._tax').each(function(input){
		var recipe = input.id.replace(/tax(\d+)/, '$1');
		var d_cry = $('b' + recipe + '_1458');
		var c_cry = $('b' + recipe + '_1459');
		var sop = $('b' + recipe + '_1875');
		var adena = $('b' + recipe + '_57');
		if(d_cry){
			var d_cnt = parseInt(d_cry.get('html'));
		}else
			var d_cnt = 0;
		if(c_cry){
			var c_cnt = parseInt(c_cry.get('html'));
		}else
			var c_cnt = 0;
		if(sop){
			var sop_cnt = parseInt(sop.get('html'));
		}else
			var sop_cnt = 0;
		input.addEvent('change', function(e){
			if(this.checked){
				if(d_cry){
					var cnt = Math.round(d_cnt + d_cnt / 9);
					d_cry.set('html', cnt);
					adena.set('html', Math.round(cnt * 97.5));
					$('adena' + recipe).removeClass('no_display');
				}
				if(c_cry){
					var cnt = Math.round(c_cnt + c_cnt / 9);
					c_cry.set('html', cnt);
					adena.set('html', Math.round(cnt * 450));
					$('adena' + recipe).removeClass('no_display');
				}
				if(sop)
					sop.set('html', Math.round(sop_cnt + sop_cnt / 9));
			}else{
				if(d_cry){
					d_cry.set('html', d_cnt);
					adena.set('html', 0);
					$('adena' + recipe).addClass('no_display');
				}
				if(c_cry){
					c_cry.set('html', c_cnt);
					adena.set('html', 0);
					$('adena' + recipe).addClass('no_display');
				}
				if(sop)
					sop.set('html', sop_cnt);
			}
			$('dual' + recipe + '_57').fireEvent('keyup');
		});
	});
	$$('.node').each(function(img){
		img.addEvent('click', function(e){
			if(this.src.match(/plus\.gif/ )){
				this.src = this.src.replace(/plus\.gif$/, 'minus.gif');
				this.getParent('tr').getElement('input._price').disabled = true;
				this.getParent('tr').getElement('input._price').addClass('disabled');
				countNodePrice($(this.getParent('tr').id.replace(/^ingr/, 'sub')));
			}else{
				this.src = this.src.replace(/minus\.gif$/, 'plus.gif');
				this.getParent('tr').getElement('input._price').disabled = false;
				this.getParent('tr').getElement('input._price').removeClass('disabled');
			}
			$(this.getParent('tr').id.replace(/^ingr/, 'sub')).toggleClass('hidden');
		});
	});
	$$('._frm_prices').each(function(form){
		var recipe = form.id.replace(/prices(\d+)/, '$1');
		form.setAjaxSubmit('l2', function(){
			var prices	= '';
			var first	= true;
			$$('._mat' + recipe).each(function(pricer){
				if(pricer.getParent('.hidden'))
					return;
				var mat = pricer.get('name');
				if(mat)
					prices += (first ? '' : '|') + mat + '=' + parseInt(pricer.value);
				first = false;
			});
			if(!prices)
				return false;
			form.prices.value = prices;
			return true;
		});
	});
	
	// item
	if($('item_code_show')){
		$('item_code_show').addEvent('click', function(e){
			new Event(e).stop();
			if($('item_code')){
				$('item_code').showUndergrounded();
				return;
			}
			var div		= new Element('div', {'id': 'item_code', 'class': 'l2_default'});
			var table	= new Element('table', {'cellpadding': 2}).inject(div, 'top');
			var tbody	= new Element('tbody').inject(table, 'top');
			var trh		= new Element('tr').inject(tbody, 'top');
			var th		= new Element('th', {'colspan': 2}).inject(trh, 'top').set('html', 'Код для сайта');
			new Element('img', {'src': '/themes/clanhall_net/img/l2/close.gif'}).inject(th, 'top').addEvent('click', function(e){
				e.target = $('forms_back');
				hideUndergrounded(e);
			});
			
			var tr1		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr1, 'bottom').set('html', 'Код для сайта (рисунок)');
			var code1	= '<a href="' + l2item_url + '" title="' + l2item_title + '"><img src="' + l2item_image + '" alt="' + l2item_alt + '" /></a>';
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr1, 'bottom').set('html', '<textarea cols="60" rows="4">' + code1 + '</textarea>');
			var tr11	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr11, 'bottom').set('html', code1);
			
			var tr2		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr2, 'bottom').set('html', 'Код для сайта');
			var code2	= '<a href="' + l2item_url + '" title="' + l2item_title + '"><img src="' + l2item_image + '" alt="' + l2item_alt + '" /></a> <a href="' + l2item_url + '" title="' + l2item_title + '">' + l2item_name + '</a>';
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr2, 'bottom').set('html', '<textarea cols="60" rows="4">' + code2 + '</textarea>');
			var tr21	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr21, 'bottom').set('html', code2);
			
			var codeImg		= '<a href="' + l2item_url + '"><img src="' + l2item_image + '" /></a>';
			var codeBoth	= '<a href="' + l2item_url + '"><img src="' + l2item_image + '" /></a> <a href="' + l2item_url + '">' + l2item_name + '</a>';
			var bbCodeImg	= '[URL=' + l2item_url + '][IMG=' + l2item_image + '][/URL]';
			var bbCodeBoth	= '[URL=' + l2item_url + '][IMG=' + l2item_image + '][/URL] [URL=' + l2item_url + ']' + l2item_name + '[/URL]';
	
			var tr3		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr3, 'bottom').set('html', 'Код для форума (рисунок), 1-й вариант');
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr3, 'bottom')
				.set('html', '<textarea cols="60" rows="4">[URL=' + l2item_url + '][IMG=' + l2item_image + '][/URL]</textarea>');
			var tr31	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr31, 'bottom').set('html', codeImg);
			
			var tr4		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr4, 'bottom').set('html', 'Код для форума (рисунок), 2-й вариант');
			var code4	= '<a href="' + l2item_url + '" title="' + l2item_title + '"><img src="' + l2item_image + '" alt="' + l2item_alt + '" /></a>';
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr4, 'bottom')
				.set('html', '<textarea cols="60" rows="4">[URL=' + l2item_url + '][IMG]' + l2item_image + '[/IMG][/URL]</textarea>');
			var tr41	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr41, 'bottom').set('html', codeImg);
			
			var tr5		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr5, 'bottom').set('html', 'Код для форума, 1-й вариант');
			var code5	= '<a href="' + l2item_url + '" title="' + l2item_title + '"><img src="' + l2item_image + '" alt="' + l2item_alt + '" /></a>';
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr5, 'bottom')
				.set('html', '<textarea cols="60" rows="4">[URL=' + l2item_url + '][IMG=' + l2item_image + '][/URL] [URL=' + l2item_url + ']' + l2item_name + '[/URL]</textarea>');
			var tr51	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr51, 'bottom').set('html', codeBoth);
			
			var tr6		= new Element('tr').inject(tbody, 'bottom');
			new Element('td').inject(tr6, 'bottom').set('html', 'Код для форума, 2-й вариант');
			var code6	= '<a href="' + l2item_url + '" title="' + l2item_title + '"><img src="' + l2item_image + '" alt="' + l2item_alt + '" /></a>';
			new Element('td', {'rowspan': 2, 'class': 'r'}).inject(tr6, 'bottom')
				.set('html', '<textarea cols="60" rows="4">[URL=' + l2item_url + '][IMG]' + l2item_image + '[/IMG][/URL] [URL=' + l2item_url + ']' + l2item_name + '[/URL]</textarea>');
			var tr61	= new Element('tr').inject(tbody, 'bottom');
			new Element('td', {'class': 'l'}).inject(tr61, 'bottom').set('html', codeBoth);

			div.getElements('textarea').each(function(txtarea){
				txtarea.addEvent('click', function(e){
					this.selectionStart = 0;
					this.selectionEnd = this.value.length;
				});
			});
			div.showUndergrounded();
		});
	}
});
