var totalsumm = 0;
var totaltmp = '';
var totaltmp2 = '';
var pricesavestr = '';
$(document).ready(loadPrice);
function addPrice()
{
	work = $('#work').val();
	count = parseInt($('#priceCount').val());	
	works[work]['count'] = count;
	drawWorks();
	$('#priceCount').val('');
	$('#totla_price').html('');
}

function setTotalPrice()
{
	work = $('#work').val();
	if (count = parseInt($('#priceCount').val()))
	{	
		$('#totla_price').html('стоимость ' + works[work]['price']*count+'р.' );
	}
}

function drawWorks()
{
	totalsumm = 0;
	totaltmp = '<table width="100%">';
	totaltmp2 = '';
	$.each(works,drawWork);
	totaltmp+='<tr><td colspan="5" align="right"><b>Итого</b> '+totalsumm+'р.</td></tr>';	
	totaltmp+='<tr><td colspan="5" align="right"><a href="javascript:process()">оформить online</a></td></tr>';	
	totaltmp+'</table>';
	$('#order_list').html(totaltmp);
	if ($('#orderText').get(0))
	$('#orderText').val(totaltmp2);
	
	savePrice();
}

function delPrice(id)
{
	works[id]['count'] = 0;
	drawWorks();
	savePrice();
}

function drawWork(i,val)
{
 	val = this; 	
	if ((val['count']>0)&&val['name'])
	{
	totaltmp += '<tr><td >'+val['name']+'</td><td>'+val['price']+'р. '+val['size']+'</td><td>'+val['count']+' '+val['size']+'</td><td>'+(val['price']*val['count'])+'р.</td><td><a href="javascript:delPrice('+i+')"><img src="/images/priceDel.png" border="0"></a></td></tr>';
	totaltmp2 += val['name']+'-'+val['count']+' '+val['size']+'-'+(val['price']*val['count'])+'р.\n';
	totalsumm += val['price']*val['count'];
	}
	
}

function loadPrice()
{
	if (str = getCookie('priceSave')){
	
	tmp = str.split(";");
	$.each(tmp,loadOne);
	drawWorks();
	}
}

function loadOne(i,val)
{
	tmp = this.split(":");
	if (tmp[0])
	works[tmp[0]]['count'] = tmp[1];
}



function process()
{
	savePrice();
	window.location = '/page_order.html';
}
function savePrice()
{
	$.each(works,saveOne);
	setCookie ('priceSave', pricesavestr);
}
function saveOne(i,val)
{
 	val = this; 	
	if ((val['count']>0)&&val['name'])
	{
		pricesavestr += i+':'+val['count']+';';	
	}
	
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
        var cookie = " " + document.cookie;
        var search = " " + name + "=";
        var setStr = null;
        var offset = 0;
        var end = 0;
        if (cookie.length > 0) {
                offset = cookie.indexOf(search);
                if (offset != -1) {
                        offset += search.length;
                        end = cookie.indexOf(";", offset)
                        if (end == -1) {
                                end = cookie.length;
                        }
                        setStr = unescape(cookie.substring(offset, end));
                }
        }
        return(setStr);
}
function showPrice(k)
{	
	
	
	if ($('#table'+k).css('display') == 'none')
	{
		$('#table'+k).css('display','block');
	}
	else
	{
		$('.price_table').css('display','none');
		$('#table'+k).css('display','none');	
	}
}

function calculate_summ()
{
	tmp_total_summ = 0;
	tmp_total_table = '<table width="383" border="1" class="price_table" style="display:block">';
	print_total_table = '<table width="383" border="0" class="price_table" style="display:block">';
	$('.price_element').each(function (){		
		if (this.value)
		{
			tmp_total_table += '<tr><td >'+works[this.name]['name']+'</td><td>'+works[this.name]['price']+'р. '+works[this.name]['size']+'</td><td>'+parseInt(this.value)+' '+works[this.name]['size']+'</td><td>'+(works[this.name]['price']*parseInt(this.value))+'р.</td><td><a href="javascript:deltotalPrice('+this.name+')"><img src="/images/priceDel.png" border="0"></a></td></tr>';
			print_total_table += '<tr><td >'+works[this.name]['name']+'</td><td>'+works[this.name]['price']+'р. '+works[this.name]['size']+'</td><td>'+parseInt(this.value)+' '+works[this.name]['size']+'</td><td>'+(works[this.name]['price']*parseInt(this.value))+'р.</td></tr>';
			tmp_total_summ+=this.id*parseInt(this.value);
		}
	});
	
	tmp_total_table+='<tr><td colspan="5" align="right"><a href="javascript:totlalClear()">Очистить</a></td></tr>';	
	tmp_total_table+='<tr><td colspan="5" align="right"><a href="javascript:totlalPrint()">Печать</a></td></tr>';	
	tmp_total_table+='</table>';
	
	print_total_table+='<tr><td colspan="4"><b>Итого: '+tmp_total_summ+' р.</b></td></tr>';
	print_total_table+='</table>';
	
	$('#tablePlace').html(tmp_total_table);
	
	$('#total_summ').html(' '+tmp_total_summ+' р.');
}

function deltotalPrice(id)
{
	
	$('.price_element[@name='+id+']').val('');
	calculate_summ();
}

function totlalPrint()
{
	win=window.open('/empty.htm');
	setCookie ('print_total_table', print_total_table);
}

function totlalClear()
{
	$('.price_element').val('');
	calculate_summ();
}

function checkForm()
{
	form = $('.linkMapper').get(0);
	
	err = '';
	
	if (!form.name1.value)err += 'имя\n';
	if (!form.email.value)err += 'e-mail\n';
	
	if (err)alert('Вы не заполнили поля:\n'+err);
	else LinkMapper_go(form);
	
	
}

var order_str = '';

function processOrder()
{
	order_str = '';
	$('.price_str').each(function (){if ($('#'+this.id+' .price_element').val())order_str+= $('#'+this.id+' .name').html()+' - '+$('#'+this.id+' .size_price').html()+' - '+$('#'+this.id+' .price_element').val()+'\n';});
	order_str += '\n\n Итого:'+tmp_total_summ+' р.';
	setCookie ('order_str', order_str);
	
	window.location = '/page_order.html';
	
}

$(window).load(function ()
{
	$('.hideOnClick').click(hideOnClick_click);
	$('.hideOnClick').blur(hideOnClick_blur);
	$('.level1').click(showLevel2);
	$('.carFillter a').click(fillterGoodsByVendor)
	
	
});
var fillterId = 0;
function fillterGoodsByVendor()
{
	fillterId = $(this).attr('dbid');
	$('.carFillter a').css('font-weight','100');
	$(this).css('font-weight','bold');
	
		
	if (!fillterId)
		$('.goodLink').css('display','block');
	else
	$('.goodLink').each(function ()
	{
		if ($(this).attr('mods') && $(this).attr('mods').indexOf(','+fillterId) == -1)
		{
			$(this).css('display','none');
		}
		else	
			$(this).css('display','block');
	});
	
	
}

function showLevel2()
{
	if ($('.parent'+this.id).css('display') == 'none')
	{
		$('.level2').css('display','none');
		$('.parent'+this.id).css('display','block');		
	}
	else
		$('.parent'+this.id).css('display', 'none');
		
	if ($('.parent'+this.id).length == 0)
		window.location = this.href;
		
	return false;
}


function fillterGoods(msg)
{
	var goods = msg;
	
	$('.goods .good').each(function () 
	{
		if (goods.indexOf('| '+this.id) > 0)
			$(this).find('.alfa').css('display','none');			
		else
			$(this).find('.alfa').css('display','block');			
		
	}
	)
}

function hideOnClick_click()
{
	if ($(this).attr('rel') == $(this).attr('value'))
		$(this).attr('value','');
}
function hideOnClick_blur()
{
	if ($(this).attr('value') == '')
		$(this).attr('value',$(this).attr('rel'));
}



var totalsumm = 0;
var totaltmp = '';
var totaltmp2 = '';
var pricesavestr = '';
$(document).ready(loadPrice);
function addPrice()
{
	work = $('#work').val();
	count = parseInt($('#priceCount').val());	
	works[work]['count'] = count;
	drawWorks();
	$('#priceCount').val('');
	$('#totla_price').html('');
}

function setTotalPrice()
{
	work = $('#work').val();
	if (count = parseInt($('#priceCount').val()))
	{	
		$('#totla_price').html('стоимость ' + works[work]['price']*count+'р.' );
	}
}

function drawWorks()
{
	totalsumm = 0;
	totaltmp = '<table width="100%">';
	totaltmp2 = '';
	$.each(works,drawWork);
	totaltmp+='<tr><td colspan="5" align="right"><b>Итого</b> '+totalsumm+'р.</td></tr>';	
	totaltmp+='<tr><td colspan="5" align="right"><a href="javascript:process()">оформить online</a></td></tr>';	
	totaltmp+'</table>';
	$('#order_list').html(totaltmp);
	if ($('#orderText').get(0))
	$('#orderText').val(totaltmp2);
	
	savePrice();
}

function delPrice(id)
{
	works[id]['count'] = 0;
	drawWorks();
	savePrice();
}

function drawWork(i,val)
{
 	val = this; 	
	if ((val['count']>0)&&val['name'])
	{
	totaltmp += '<tr><td >'+val['name']+'</td><td>'+val['price']+'р. '+val['size']+'</td><td>'+val['count']+' '+val['size']+'</td><td>'+(val['price']*val['count'])+'р.</td><td><a href="javascript:delPrice('+i+')"><img src="/images/priceDel.png" border="0"></a></td></tr>';
	totaltmp2 += val['name']+'-'+val['count']+' '+val['size']+'-'+(val['price']*val['count'])+'р.\n';
	totalsumm += val['price']*val['count'];
	}
	
}

function loadPrice()
{
	if (str = getCookie('priceSave')){
	
	tmp = str.split(";");
	$.each(tmp,loadOne);
	drawWorks();
	}
}

function loadOne(i,val)
{
	tmp = this.split(":");
	if (tmp[0])
	works[tmp[0]]['count'] = tmp[1];
}



function process()
{
	savePrice();
	window.location = '/page_order.html';
}
function savePrice()
{
	$.each(works,saveOne);
	setCookie ('priceSave', pricesavestr);
}
function saveOne(i,val)
{
 	val = this; 	
	if ((val['count']>0)&&val['name'])
	{
		pricesavestr += i+':'+val['count']+';';	
	}
	
}


