var Shop = {
    warning :true,
    warningMsg :'',

    init : function() {
        $(document).ready( function() {
            Shop.setupShippingForm();
            Shop.createWarning();
        });
    },
    createWarning : function() {
        $(window).bind('beforeunload', function() {
            if (Shop.warning) {
                return Shop.warningMsg;
            }
        });
    },

    setupShippingForm : function() {
        $('#altDelivery').hide();

        $('#address_shipping_same')
            .attr('checked', 'checked')
            .click( function() {
                if ($(this).attr('checked')) {
                    $('#altDelivery').slideUp();
                }
                else {
                    $('#altDelivery').slideDown();
                }
            });
    },

    disableWarning : function() {
        Shop.warning = false;
    }

}

var togglerOn = false;
var activeProduct = 0;

function toggleProduct(id) {
    hideAllProducts();
    if(activeProduct != id) {
        var obj = document.getElementById('productensubmenu' + id);
        if(obj) {
            if(obj.style.display == 'none') {
                obj.style.display = '';
            }
            else {
                obj.style.display = 'none';
            }
        }
        activeProduct = id;
    }
    else {
        activeProduct = 0;
    }
}

function toggleProductInfo(id, show) {
    hideAllProductInfos();
    var obj = document.getElementById('producteninfo' + id);
    var objLink = document.getElementById('producteninfolink' + id);
    var contentObj = document.getElementById('content');
    if(obj && objLink) {
        if(show) {
            togglerOn = true;
            obj.style.display = '';
            objLink.className = 'hover';
            if(contentObj) {
                contentObj.className = 'fade';
            }
        }
        else {
            obj.style.display = 'none';
            objLink.className = '';
            if(contentObj) {
                contentObj.className = '';
            }
        }
    }
}

function adjustFlashHeight(newHeight) {
    getMovie('bestellijstObj').style.height = newHeight + "px";
}

function getMovie(movieName) {
    return swfobject.getObjectById(movieName);
}

function addToCart(productId) {
	$.get(
        '/services/process/cartAdd.xml',
        {product_id:productId},
        function(data) {
            if(getMovie("bestellijstObj")) {
                getMovie("bestellijstObj").updateOrder();
            }
            else {
                window.location.reload();
            }
        }
    );
}

function activateList() {
    getMovie("bestellijstObj").gogo();
}

function updatePrice() {
    var formObj = document.getElementById('product');
    if(formObj) {
        var items = formObj['beleg[]'];
        var totaal = parseFloat(formObj.startprijs.value);
        for(var i = 0; i < items.length; i++) {
            if(items[i].checked == true) {
                totaal += parseFloat(formObj['belegprijs_' + items[i].value].value);
            }
        }
        document.getElementById('totaal_prijs').innerHTML = totaal.toFixed(2).toString().replace(',', '').replace('.', ',');
    }
}

function hideAllProducts() {
    var obj;
    for(var i = 0; i < productensubmenus.length; i++) {
        obj = document.getElementById('productensubmenu' + productensubmenus[i]);
        if(obj) obj.style.display = 'none';
    }
}

function hideAllProductInfos() {
    if(togglerOn) {
        togglerOn = false;
    }
    else {
        var contentObj = document.getElementById('content');
        if(contentObj) contentObj.className = '';
        
        if(typeof productinfos != 'undefined') {
            for(var i = 0; i < productinfos.length; i++) {
                document.getElementById('producteninfo' + productinfos[i]).style.display = 'none';
                document.getElementById('producteninfolink' + productinfos[i]).className = '';
            }
        }
    }
}

document.onclick = function() {
    hideAllProductInfos();
}

function updateQuantity(itemId, quantity) {
    $.get('/services/process/cartUpdate.xml', {id: itemId, aantal: quantity});

    if(quantity == 0) {
        $('#cart_product_' + itemId).remove();
    }
    else {
        var price = $('#product_stuksprijs_' + itemId).html();
        var newPrice = quantity * price;
        $('#product_price_' + itemId).html(newPrice);
    }
}

Shop.init();
