// shop cart slider
var shopCart = Class.create();
shopCart.prototype = {

    initialize : function (element) {
        this.cart = element;
        this.writeEvents();
    },

    writeEvents : function () {
        this.head = $(this.cart).getElementsByClassName('sc_head')[0];

        this.head.observe('click', this.press);
        this.head.myClass = this;

        this.statusOpen = false;

        this.buttonOpen = $('button_open');
        this.buttonClosed = $('button_closed');

        if ($('shopcart').getHeight() > 50) {
            this.statusOpen = true;
        }
        try {
            this.contentHeight = this.cart.getElementsByClassName('shop_details')[0].getHeight() + 26;
        }
        catch (e) {

        }
    },

    press : function () {
        if (this.myClass.statusOpen == false) {
            this.myClass.openCart();
        }
        else {
            this.myClass.closeCart();
        }
    },

    openCartT : function () {
        $('shopcart').setStyle({height: this.contentHeight + 'px'});
        this.buttonClosed.style.display = 'none';
        this.buttonOpen.style.display = 'block';
        this.statusOpen = true;
    },

    openCart : function () {
        ex1 = new Animator({transition: Animator.makeEaseIn(2),duration: 1000});
        ex1.addSubject(new NumericalStyleSubject($(this.cart), 'height', 26, this.contentHeight));
        ex1.play();
        this.buttonClosed.style.display = 'none';
        this.buttonOpen.style.display = 'block';
        this.statusOpen = true;
    },

    closeCart : function () {
        ex1 = new Animator({transition: Animator.makeEaseIn(2),duration: 1000});
        ex1.addSubject(new NumericalStyleSubject($(this.cart), 'height', this.contentHeight, 26));
        ex1.play();
        this.buttonClosed.style.display = 'block';
        this.buttonOpen.style.display = 'none';
        this.statusOpen = false;
    }
};

// shop handling
function addItem(itemId) {
    variables = $('form_' + itemId).serialize();
    url = '/tools/shop/cart.php';

    var size_de = "";
    var color_de = "";
    var size_en = "";
    var color_en = "";
    var canadd = true;

    elements = $('form_' + itemId).getElements();
    for (i = 0; i < elements.length; i++) {
        if (elements[i].name == "size") {
            if (elements[i].getValue() == "") {
                size_de = "Bitte wählen Sie eine Größe aus!\n";
                size_en = "Please select a size!\n";
                canadd = false;
            }
        }
        if (elements[i].name == "color") {
            if (elements[i].getValue() == "") {
                color_de = "Bitte wählen Sie eine Farbe aus!\n\n";
                color_en = "Please select a color!";
                canadd = false;
            }
        }
    }

    if (canadd) {
        new Ajax.Updater('shopcart', url, {method: 'post', asynchronous:false, parameters: variables });
        var shopcart = $('shopcart');
        if (shopcart != null) {
            shopObject = new shopCart(shopcart);
        }
        shopObject.openCartT();
    }
    else {
        alert(size_de + color_de + size_en + color_en);
    }

    return false;
}

function reloadCart() {
    variables = $('cartItems').serialize();
    url = '/tools/shop/cart.php';
    new Ajax.Updater('shopcart', url, {method: 'post', asynchronous:false, parameters: variables });

    var shopcart = $('shopcart');
    if (shopcart != null) {
        shopObject = new shopCart(shopcart);
    }

    shopObject.openCartT();

    return false;
}

function removeItem(itemNr) {
    url = '/tools/shop/cart.php';
    new Ajax.Updater('shopcart', url, {method: 'post', asynchronous:false, parameters: 'action=remove&item=' + itemNr });

    var shopcart = $('shopcart');
    if (shopcart != null) {
        shopObject = new shopCart(shopcart);
    }

    shopObject.openCartT();
    return false;
}

var billlayeropen = false;


/**
 * Dupliziere Lieferadresse auf Rechnungsadresse
 */
function duplicateForm() {
    var tmp = billlayeropen;
    billlayeropen = false;
    extDuplicate();
    billlayeropen = tmp;
}
/**
 * Rechung in Lieferadresse kopieren nur wenn der layer offen ist
 */

function extDuplicate() {

    if (!billlayeropen) {

        var elementID = null;
        var targetFormElement = null;
        var sourceFormElement = null;
        var sourceFormElementType = null;

        var elements = Form.getElements($('orderForm'));
        for (i = 0; i < elements.length; i++) {
            sourceFormElement = elements[i];
            elementID = sourceFormElement.getAttribute("id");
            if (typeof elementID == "string") {
                if (elementID.length > 0) {
                    if (elementID.indexOf("_r") < 0) {
                        targetFormElement = $(elementID + "_r");
                        if (targetFormElement) {
                            sourceFormElementType = sourceFormElement.tagName.toLowerCase();

                            // input
                            if (sourceFormElementType == "input") {
                                targetFormElement.value = sourceFormElement.value;
                            }

                            // select
                            if (sourceFormElementType == "select") {
                                selectedValue = sourceFormElement.getElementsByTagName("option")[sourceFormElement.selectedIndex].value;
                                $A(targetFormElement.getElementsByTagName("option")).each(function (option) {
                                    if (option.value == selectedValue) {
                                        option.selected = true;
                                    }
                                    else {
                                        option.selected = false;
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
    }
}

function openbilllayer() {
    billlayeropen = true;

    ex1 = new Animator({transition: Animator.makeEaseIn(2),duration: 1000});
    ex1.addSubject(new NumericalStyleSubject(document.getElementById("billadress"), 'height', 0, 215));
    ex1.addSubject(new NumericalStyleSubject(document.getElementById("openbilladress"), 'height', 60, 0));
    ex1.play();
}



