/*
 *
 * Copyright (c) 2008 George Bonnes (george@olm1.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 1.0.1
 *
 * $LastChangedDate$
 * $Rev$
 *
 */

function expand_shopping_cart_contents() {
    $('#div_cart_contents_wrapper').BlindDown(300);
    $('#div_cart_contents_toggle').css('display', 'none');
}


var active_options = new Object();
function activate_product_option_selection(group_id, option_id) {
    if (!option_id) {
        deactivate_options(group_id);
    }
    var sel = document.getElementById('ProductOptionGroup' + group_id);
    if (!sel) {
        return false;
    }
    //sel.value = option_id;
    
    var optlink = document.getElementById('ProductOption' + group_id + '-' + option_id);
    if (optlink == null) {
        deactivate_options(group_id);
        return false;
    }
    //alert('optlink.className ' + optlink.className);
    //alert("optlink: " + optlink);
    if (optlink.className == 'option_active') {
        optlink.className = '';   
        deactivate_options(group_id);
    } else {
        deactivate_options(group_id);
        active_options[group_id] = option_id;
        optlink.className = 'option_active';   
        sel.value = option_id;
    }
    //alert("active: " + active_options);
    product_details_recalculate();
}

function deactivate_options(group_id) {
    document.getElementById('ProductOptionGroup' + group_id).value = '';
    for (var i in active_options) {
        //alert('i ' + i + ' \noption_id ' + active_options[i]);
        if (i == group_id) {
            document.getElementById('ProductOption' + i + '-' + active_options[i]).className = '';
        }
    }
    
    delete active_options[group_id];
    e_subtotal = document.getElementById('product_subtotal').innerHTML = parseFloat(document.getElementById('product_base_price').innerHTML);
}

var available_option_groups = new Object();

function product_details_recalculate() {
    if (available_option_groups.length == 0) { 
        return false;
    }
    if (!document.getElementById('product_base_price')) {
        return false;
    }
    var base_price = parseFloat(document.getElementById('product_base_price').innerHTML);
    var e_subtotal = document.getElementById('product_subtotal');
    var new_subtotal = base_price;
    //alert(e_subtotal.value);
    //for (var i in active_options) {
    for (var i in available_option_groups) {
        var sel = document.getElementById('ProductOptionGroup' + i);
        group_active_option = sel.value;
        if (group_active_option == "") {
            group_active_option_price_type = 'adds';
            group_active_option_price = 0.00;
        } else {
            group_active_option_price_type = eval("product_option_group_" + i + "_prices[" + group_active_option + "]['type']");
            group_active_option_price = eval("product_option_group_" + i + "_prices[" + group_active_option + "]['price']");
        }
        //alert('base_price: ' + base_price + '\ntype: ' + group_active_option_price_type + '\nprice: ' + group_active_option_price)
        if (group_active_option_price_type == 'adds') {
            new_subtotal += group_active_option_price;
        } else if (group_active_option_type_type == 'subtracts') {
            new_subtotal -= group_active_option_price;
        } else if (group_active_option_type_type == 'is') {
            is_price_difference = group_active_option_price - base_price;
            new_subtotal += is_price_difference;
        }
    }
    e_subtotal.innerHTML = parseInt(new_subtotal * 100) / 100;
}

//addEvent(window, 'load', function() {
//    product_details_recalculate();
//});
$(document).ready(function() { 
    product_details_recalculate();
});


