$(function() {
    $( 'body' ).addClass( 'js-active' );
    $( 'ul.radio li' )
        .bind('click', function () {
            option_select(this)
    })
    $( 'ul.radio li' )
        .bind('click', function () {
            option_select(this)
    })
    $( '#options input.submit' )
        .bind('click', function () {
            return check_options(this)
    })
});

$('document').ready(function() {$( 'ul.radio li:has(input:checked)' ).addClass( 'selected' );option_set_invalid();option_reset()})

function check_options(current) {
    option_groups = $('#options ul.radio').get()
    for (i in option_groups){
        if($('input:checked', option_groups[i]).length == 0){
            alert("Please select all options before continuing");
            return false;
        }
    }
    return true
     
    /*
    status = true
    $( '#options ul.radio' )
        .each(function (i){
            if($('input:checked', this).length == 0){
                alert("Please select all options before continuing");
                status = false
                return false;
            }
        })
    return status
    */
}

function option_select(current) { 
    // Set IDs
    this_group_id = $(current).children('input').attr('name')
    other_group_id = $('ul.radio li input[name!='+this_group_id+']:first').attr('name')
    this_selection = $(current).children('input').attr('value')
    this_group_current_selection = $('ul#optiongroup-'+this_group_id+' input:checked').attr('value')
    other_group_current_selection = $('ul#optiongroup-'+other_group_id+' input:checked').attr('value')

    // Set current option to selected
    $('ul#optiongroup-'+this_group_id+' li.selected').removeClass( 'selected' )
    $(current).addClass( 'selected' );
    $(current).children('input')[0].checked = true
    
    // If an option in the other category is already selected
    if(other_group_current_selection){
        // Hovering over an invalid option
        if(!option_is_valid(this_group_id, this_selection, other_group_current_selection)){
            
            $('ul#optiongroup-'+other_group_id+' li.selected').removeClass('selected')
            $('ul#optiongroup-'+other_group_id+' input:checked').attr('checked', false)
        }
    }
    option_reset()
}


function option_is_valid(option_id, value1, value2) {
    product_variation_id = ''
    if(satchmo.option_ids[0] == option_id){
        product_variation_id = value1+"::"+value2
    }
    else {
        product_variation_id = value2+"::"+value1
    }
    
    // Invalid combination
    if (!satchmo.variations[product_variation_id]){
        return false
    }
    
    // Out of stock
    if (satchmo.variations[product_variation_id]["QTY"] < 1){
        return false
    }
    
    // Valid combination
    return true
}


function option_set_invalid(current) {
    if (current){
        // Set IDs
        this_selection = $(current).children('input').attr('value')
        this_group_id = $(current).children('input').attr('name')
        
        // Set invalid options
        $( 'ul.radio li' ).removeClass('invalid')
        $( 'ul.radio li' ).filter(function(index){
            return $(this).children("input").attr("name") != this_group_id
            }).filter(function(index){
                return !option_is_valid(this_group_id, this_selection, $(this).children('input').attr('value'))
                }).addClass('invalid')
    }
    else {
        $( 'ul.radio li' ).removeClass('invalid')
        $( 'ul.radio' ).each(function(){
            this_group_id = $('input:first', this).attr('name')
            this_selection = $('input:checked', this).attr('value')
            if (!this_selection) return true
            $( 'ul.radio li' ).filter(function(index){
                return $(this).children("input").attr("name") != this_group_id
                }).filter(function(index){

                    return !option_is_valid(this_group_id, this_selection, $(this).children('input').attr('value'))
                    }).addClass('invalid')
            })
    }
}


function option_hover_in(current) {
    // Hovering over a selected option
    if($(current).children('input').attr('checked')){
        return
    }
    
    // Set IDs
    this_group_id = $(current).children('input').attr('name')
    //alert($(current))
    other_group_id = $('ul.radio li input[name!='+this_group_id+']:first').attr('name')
    this_selection = $(current).children('input').attr('value')
    this_group_current_selection = $('ul#optiongroup-'+this_group_id+' input:checked').attr('value')
    other_group_current_selection = $('ul#optiongroup-'+other_group_id+' input:checked').attr('value')
    
    $('#image-for-option-'+this_selection.replace(' ','-').toLowerCase()+' a').trigger('mouseover')
    valid = true
    
    // Check if the proposed combination is valid and instock
    if(other_group_current_selection && !option_is_valid(this_group_id, this_selection, other_group_current_selection)) {
        valid = false
    }

    if (valid) {
        $(current).addClass('hover-valid');
    }
    else {
        $(current).addClass('hover-invalid')
    }
    option_set_invalid(current)
    $('#optiongroup-'+this_group_id+'-selection').html($('label', current).html())
}

function option_reset(){
    $('ul.radio li.hover-valid').removeClass('hover-valid');
    $('ul.radio li.hover-invalid').removeClass('hover-invalid');
    $('div.option-group h4 span').html('')
    $( 'ul.radio:has(:checked)' ).each(function(){
        group_id = $('input:first', this).attr('name')
        $('#optiongroup-'+group_id+'-selection').html($('li:has(:checked) label', this).html())
    })
    option_set_invalid()
}


$(document).ready(function() {
    $( 'ul.radio li' ).hover(
        function(){option_hover_in(this)},
        function(){option_reset()}
        )
    });

