
$(document).ready(function () {

    $('.select_variant').change(function () {
        $('body').data('VariantID', '');
        $('.select_variant').each(function () {
            var val = $(this).val();
            var data = $('body').data('VariantID');
            if (data) {
                data = data + val + '.';
            } else {
                data = val + '.';
            };
            $('body').data('VariantID', data);
        });
        $('.form_variant .selected_variants').val($('body').data('VariantID'));
        $('.form_variant').submit();
    });


    createColorSelectors();

    $('.colors.dynamic .color-item').live('click', function () {
        var isProductPage = $(this).parents('#single-product-color').length;

        if ($(this).hasClass('color-reset')) {
            $('.colors.dynamic input').removeAttr('checked');
            $('.colors.dynamic .color-item').removeClass('checked');
        } else {
            var id = $(this).attr('rel');
            $(this).toggleClass('checked');
            if ($(id).attr('checked') == "checked") {
                $(id).removeAttr('checked');
            } else {
                $(id).attr('checked', 'checked');
            };
        }

        if (isProductPage) {
            var colors = '';
            $('#single-product-color input').each(function () {
                if ($(this).is(':checked')) {
                    colors = colors + $(this).val() + ',';
                }
            });
            $.ajax({
                data: 'colors=' + colors,
                beforeSend: function (xhr) { ajaxloader.start(xhr); },
                error: function (xhr, ajaxOptions, thrownError) {
                    ajaxloader.stop();
                    ajax_send_error();
                },
                success: function (data) {
                    $(".same-category").html($(data).find(".same-category").html());
                    selectBoxes_init();
                    page_height();
                    product_view.current();
                    title_view.generate();
                    title_view.apply_short();
                    product_images.init();
                    Compare.init();
                    ajaxloader.stop();
                }
            });
        }

    });

});

function createColorSelectors() {
    $('.colors input:checkbox').each(function (i) {
        var id = 'color-item' + i;
        var checked = '';
        var iswhite = '';
        var color = $(this).attr('style');
        if ($(this).is(':checked')) {
            checked = "checked";
        };

        //if color = white
        var white = $(this).attr('value');
        if (white == "FFFFFF" || white == "FFFF00") {
            iswhite = "color-item-white";
        };

        $(this).addClass(id);
        var design_input = '<span rel=".' + id + '" class="color-item ' + iswhite + ' ' + checked + '"><span class="color-checkbox" style="' + color + '"></span></span>';
        $(this).parent().append(design_input);
    });


    var color_reset_btn = '<span class="color-item color-reset"><span class="color-checkbox"></span></span>';
    $('.colors.dynamic').append(color_reset_btn);
}
