var lenses;
var cases;
var cartItem;
var productColors;


$(document).ready(function() 
{
	lenses = eval(lensesJson);
	cases = eval(casesJson);
	cartItem = eval(cartItemJson);

	productColors = eval(productColorsJson);

        $('.boxes .box, #lenses .box').hide();

        // napoveda pro na dalku, na blizko, ...
        $('input[name=glasses_type]').change(function() {
          updateGlasses();
        });

        updateGlasses();

        // vybere prvni radio z novych moznosti po zmene typu
	$(':radio[name^=lensProductType]').click(function() {
		var type = $(this).val();
		$(':radio[name^=lensProductNumber]').attr('disabled', true);                
		$('#box_' + type + ' :radio').attr('disabled', false);
                $('#box_' + type + ' :radio:eq(0)').attr('checked', true);
	});

        // aktualizace ceny a zobrazeni aktualni napovedy
        $('input[name=lensProductType], input[name=lensProductNumber]').change(function() {
		bind();
		updateDetailInfo();
	});

        // dioptrie a cylindry
        $('#right_sphere').change(function() {
            bind();
            actualizeSphereAndCylinder('right', $(this).val());
        });
        $('#left_sphere').change(function() {
            bind();
            actualizeSphereAndCylinder('left', $(this).val());
        });

        $('#lenses :radio, #lenses-type :radio').change(function() {
            actualizeSphereAndCylinder('both', null)
        });

        $('#right_cylinder').change(function() {
		bind();
		updateDetailInfo();
	});
        $('#left_cylinder').change(function() {
		bind();
		updateDetailInfo();
	});

        if ($.browser.msie) {
          $("#lenses-type input").click(function() {
            this.blur();
            this.focus();
          });
        }

	updateDetailInfo();
});

function updateGlasses() {
    var glasses = $('input[name=glasses_type]:checked');
    if(glasses.val() == 'NONDIOPTRIC') {
        $('#dioptric-data').hide();
    } else {
        $('#dioptric-data').show();
    }
    glasses.parent().parent().children('li').removeClass('active');
    glasses.parent().addClass('active');
    $('#glasses_types .box').hide();
    $('#glasses_types #box_' + glasses.attr('id')).show();
}


function actualizeSphereAndCylinder(type, val) {
    if(type == 'both') {
        $.get(
                "/shoppingcart/ajax-sphere/lens_id/" + cartItem.lensId,
                {},
                function(data) {
                    $('#right_sphere').html(data);
                    $('#left_sphere').html(data);
                }
        );
        $('#right_cylinder').html('<option value="0">žádný</option>');
        $('#left_cylinder').html('<option value="0">žádný</option>');
    } else if(type == 'right') {
        $.get(
                "/shoppingcart/ajax-cylinder/lens_id/" + cartItem.lensId + "/sphere/" + val,
                {},
                function(data) {
                    $('#right_cylinder').html(data);
                    bind();
                    updateDetailInfo();
                }
        );
    } else if(type == 'left') {
        $.get(
                "/shoppingcart/ajax-cylinder/lens_id/" + cartItem.lensId + "/sphere/" + val,
                {},
                function(data) {
                    $('#left_cylinder').html(data);
                    bind();
                    updateDetailInfo();
                }
        );
    }
}

function bind() 
{
	// color
	//cartItem.productColorId = $(':radio[id^=color-]:checked').val();

	// lens
	var lensProductNumber = $(':radio[name^=lensProductNumber]:checked').val();

	if(lenses[lensProductNumber] != null) {
		cartItem.lensProductNumber = lensProductNumber;
                cartItem.lensId = lenses[lensProductNumber].id;
		cartItem.lensPriceAmount = lenses[lensProductNumber].price_amount;
		cartItem.lensPriceCurrency = lenses[lensProductNumber].price_currency;
	}

	// case
	cartItem.caseProductNumber = $(':radio[id^=gcaseProductNumber-]:checked').val();
}


function resetColorIndex()
{
	var colorSelect = $('select[name=lensColor]').get(0);
	colorSelect.selectedIndex = 0;
}


function updateDetailInfo() 
{

        var type = $('input[name=lensProductType]:checked').val();

        $('#lenses .box').hide();

        $('#lenses #box_' + type).show();
        $('#' + type).parent().parent().children('li').removeClass('active');
        $('#' + type).parent().addClass('active');

        var subtype = $('input[name=lensProductNumber]:checked').val();
        $('#lenses #box_' + type + '-' + subtype).show();

        var out = '<table>';

	// lens	
	out += '<tr><th>Čočky</th><td>' + lenses[cartItem.lensProductNumber].name + '</td></tr>';

	var lensPriceId = cartItem.lensProductNumber;
	var totalPrice = parseFloat(cartItem.productPriceAmount) + parseFloat(lenses[lensPriceId].price_amount);

        var rightCylinder = $('#right_cylinder option:selected').text();
        var leftCylinder = $('#left_cylinder option:selected').text();
        var rightPos = rightCylinder.indexOf('(');
        var leftPos = leftCylinder.indexOf('(');
        if(rightPos != -1) {
            totalPrice += parseFloat(rightCylinder.substr(rightPos + 1, rightCylinder.lastIndexOf(',') - rightPos - 1));
        }
        if(leftPos != -1) {
            totalPrice += parseFloat(leftCylinder.substr(leftPos + 1, leftCylinder.lastIndexOf(',') - leftPos - 1));
        }

	// color
	var productColorId = $('input[name=productColorId]').val();
	for(var i in productColors) {
		if(productColors[i].id == productColorId) {
			out += '<tr><th>Barva obrub</th><td>' + productColors[i].color + '</td></tr>';
			break;
		}
	}

	// case
	if(cases[cartItem.caseProductNumber] != null) out += '<tr><th>Pouzdro</th><td>' + cases[cartItem.caseProductNumber].name + '</td></tr>';

	out += '<tr><th class="total-price last"><strong>Cena celkem</strong></th><td class="total-price orange">' + totalPrice + ' Kč</td></tr>';
	out += '</table>';

	$('#cartItemDetailInfo').html(out);
	
}
