$(document).ready(function()
{
	BasketMiniUpdate();
});
function BasketGet()
{
	var basket = {};
	var cookie = getCookie('basket');
	if (cookie)
		basket = eval("("+cookie+")");
	return basket;
}
function BasketSetWarranty(tovar_id, warranty)
{
	var basket = BasketGet();
	
	basket[tovar_id][2] = warranty;
	
	var data_string = JSON.stringify(basket);
	setCookie('basket', data_string, 1, '/');
	
	BasketMiniUpdate();
}
function BasketAdd(tovar_id, price)
{
	var basket = BasketGet();
	
	var warranty = $('input[name=warranty]:checked').val() == undefined ? 0 : $('input[name=warranty]:checked').val();
	
	if (!basket[tovar_id])
	{
		basket[tovar_id] = [1, price, warranty];
	}
	else
	{
		basket[tovar_id] = [++basket[tovar_id][0], price, warranty];
	}
	var data_string = JSON.stringify(basket);
	setCookie('basket', data_string, 1, '/');
	
	var count = 0, sum = 0;
	for (var i in basket)
	{
		count += basket[i][0];
		sum += basket[i][0] * (basket[i][1] / 1 + warranty_all[basket[i][2]] / 1);
	}
	
	BasketMiniUpdate();
	
	$.fancybox
	(
		'<div style="width: 495px; height: 270px; padding: 15px;"><div style="padding-left: 10px;"><img src="/res/public/images/discounts.jpg"></div><br>'+
		'<div style="padding:10px; height: 30px;"><div style="float: left; width: 145px; padding-left: 25px;"><img style="cursor: pointer;" onclick="$.fancybox.close();" alt="Continue shopping" src="/res/themes/default/images/b_contshop.gif"></div><div style="float: left; width: 160px; padding-left: 130px;"><a href="'+site_url+router_basket+'/"><img alt="Checkout" src="/res/themes/default/images/b_checkout.gif"></a></div></div>'
	);
}
function BasketGift(tovar_id)
{
	setCookie('discount_watches_gift', tovar_id, 1, '/');
	document.location.href = site_url+router_basket+'/';
}
function BasketSet(tovar_id, count)
{
	var basket = BasketGet();
	
	basket[tovar_id][0] = count;
	
	var data_string = JSON.stringify(basket);
	setCookie('basket', data_string, 1, '/');
	
	BasketMiniUpdate();
}
function BasketMiniUpdate()
{
	var basket = BasketGet();
	var count = 0, sum = 0;
	for (var i in basket)
	{
		count += basket[i][0];
		sum += basket[i][0] * (basket[i][1] / 1 + warranty_all[basket[i][2]] / 1);
	}
	if (count)
	{
		$('#basket_mini_text').html('Items: <b>'+count+'</b><br />Total: <b>$ '+number_format(sum, 2, '.', ',')+'</b>');
		//Discounts
		if (count > 2)
		{
			$('#discount0').hide();
			$('#discount1').html('<div style="margin: 0 0 5px 0;">Please select:</div><input type="radio" name="discount1" value="1" id="w1" style="margin-right:3px;margin-bottom:3px;  vertical-align: middle;" onClick="SetDiscount1(\'1\');" '+(getCookie('discount1') == '1' ? 'checked="checked"' : '')+' /> <label for="w1"><b>Free Shipping</b></label><br /><input type="radio" name="discount1" value="2" id="w2" style="margin-right:3px;margin-bottom:3px;  vertical-align: middle;" onClick="SetDiscount1(\'2\');" '+(getCookie('discount1') == '2' ? 'checked="checked"' : '')+' /> <label for="w2"><b>Free Lifetime Warranty</b> for one item</label>');
		}
		else if (count > 1)
		{
			$('#discount0').hide();
			$('#discount1').html('<div style="margin: 0 0 5px 0;">Please select:</div><input type="radio" name="discount1" value="3" id="w1" style="margin-right:3px;margin-bottom:3px;   vertical-align: middle;" onClick="SetDiscount1(\'3\');" '+(getCookie('discount1') == '3' ? 'checked="checked"' : '')+' /> <label for="w1"><b>20% off to Shipping</b></label><br /><input type="radio" name="discount1" value="4" id="w2" style="margin-right:3px;margin-bottom:3px; vertical-align: middle;" onClick="SetDiscount1(\'4\');" '+(getCookie('discount1') == '4' ? 'checked="checked"' : '')+' /> <label for="w2"><b>Free 1 year warranty</b> for one item</label>');
		}
		else
		{
			$('#discount0').show();
			$('#discount1').empty();
		}
	}
}
function SetDiscount1(id)
{
	setCookie('discount1', id, 1, '/');
}
function number_format(number, decimals, dec_point, thousands_sep)
{
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
function BasketUpdateTable()
{
	var i = 0;
	$('span.count').each(function(index)
	{
		i+=$(this).html()/1;
	});
	$('#total_count').html(i);
	i = 0;
	$('td.price_one').each(function(index)
	{
		i+=$(this).attr('price')/1;
	});
	$('#total_price_one').html('');
	i = 0;
	$('tr.tovar_line').each(function(index)
	{
		$('.price_all', this).html('$'+number_format($('span.count', this).html() * ($('.price_one', this).attr('price') / 1 + warranty_all[$('select', this).val()] / 1), '2', '.', ''));
		$('.price_all', this).attr('price', number_format($('span.count', this).html() * ($('.price_one', this).attr('price') / 1 + warranty_all[$('select', this).val()] / 1), '2', '.', ''));
	});
	i = 0;
	$('td.price_all').each(function(index)
	{
		i+=$(this).attr('price')/1;
	});
	$('#total_price_all').html('$' + number_format(i, '2', '.', ''));
	$('#total_price_all').attr('price', number_format(i, '2', '.', ''));
}
function ReduceCount(tovar_id)
{
	var count = $('#tovar'+tovar_id+' span.count').html();
	if (count == 1)
		return;
	$('#tovar'+tovar_id+' span.count').html(count - 1);
	BasketSet(tovar_id, count - 1);
	BasketUpdateTable();
}
function IncreaseCount(tovar_id)
{
	var count = $('#tovar'+tovar_id+' span.count').html();
	$('#tovar'+tovar_id+' span.count').html(count/1 + 1);
	BasketSet(tovar_id, count/1 + 1);
	BasketUpdateTable();
}
function BasketRemoveTovar(tovar_id)
{
	var basket = BasketGet();
	delete basket[tovar_id];
	var data_string = JSON.stringify(basket);
	setCookie('basket', data_string, 1, '/');

	$('#tovar'+tovar_id).remove();
	BasketMiniUpdate();
	
	var count = 0;
	for (var i in basket)
	{
		count += basket[i][0];
	}
	if (count == 0)
	{
		document.location.reload();
		return;
	}
	BasketUpdateTable();
}
function BasketRemoveGift()
{
	setCookie('discount_watches_gift', 0, 1, '/');
	$('#tovar_discount').remove();
	BasketMiniUpdate();
	BasketUpdateTable();
}
function BasketTruncate()
{
	deleteCookie('basket', '/');
	document.location.reload();
}

