var showImageVariations = 1;

$('.selected_attribute').livequery(function() {

	$(this).click(function() {
		selectProductImage();
	});

	$(this).change(function() {
		selectProductImage();
	});

});

function selectProductImage() {

    if (showImageVariations == 1) {

        var selectedValues = [];

        $('#attributes').find('.selected_attribute:checked').each(function() {

            if ($(this).val() != '' && $(this).val() != undefined && $(this).val() != null) {
                selectedValues[selectedValues.length] = $(this).val();
            }

        });

        $('#attributes').find('select.selected_attribute').each(function() {

            if ($(this).val() != '' && $(this).val() != undefined && $(this).val() != null) {
                selectedValues[selectedValues.length] = $(this).val();
            }

        });

        if (selectedValues.length) {
            $.ajax({type: 'post', url: '?', data: {ajax: true, find_variation_image: true, variation: JSON.stringify(selectedValues)}, dataType: 'json', success: function(response) {

                if (response != '' && response[0] != undefined) {
                    /**
                     * update zoom images
                     */
                    $('#zoom1 img, .MagicThumb-image, .MagicThumb-image-zoomed').each(function() {
                        var srcParts = $(this).attr('src').split('/');
                        srcParts[srcParts.length-1] = response[0];
                        $(this).attr('src', srcParts.join('/'));
                    });
    
                    var hrefParts = $('#zoom1').attr('href').split('/');

                    hrefParts[hrefParts.length-1] = response[0];
                    $('#zoom1').attr('href', hrefParts.join('/'));

                    /**
                     * update product thumbnails
                     */
                    MagicZoom_stopZooms();

                    var liTemplate = $('.productthumbs li:first').clone();
                    $('.productthumbs').empty();

                    $.each(response, function(i, item) {
                        var newLI = liTemplate.clone();
                        var hrefParts = newLI.find('a').attr('href').split('/');
                        hrefParts[hrefParts.length-1] = item;
                        newLI.find('a').attr('href', hrefParts.join('/'));

                        var revParts = newLI.find('a').attr('rev').split('/');
                        revParts[revParts.length-1] = item;
                        newLI.find('a').attr('rev', revParts.join('/'));

                        var srcParts = newLI.find('img').attr('src').split('/');
                        srcParts[srcParts.length-1] = item;
                        newLI.find('img').attr('src', srcParts.join('/'));

                        newLI.appendTo('.productthumbs');
                    });

                    MagicZoom_findZooms();
                    MagicThumb.refresh();
                }

            }});
        }
    }
}

