new Namespace("eu.mckinley.products.teambox");
eu.mckinley.products.teambox = Class.create({
    ajaxurl : "/?controller=teambox&language=",
    elem : null,
    info : false,

    initialize : function (elem) {
        this.ajaxurl = this.ajaxurl + websiteconfig["language"],this.elem = elem;
        try {
            $('teamboxmessage').setOpacity(0);
            $('teamboxmessage').setStyle({display:'block'});
        }
        catch (e) {

        }
    },

    add : function (id) {
        var color = false;
        try {
            color = $('productcolors').value
        }
        catch (e) {
        }
        var size = false;
        try {
            size = $('productsize').value
        }
        catch (e) {
        }


        var ani = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 500 });
        ani.addSubject(new NumericalStyleSubject($('teamboxmessage'), 'opacity', 0, 1));
        ani.play();
        var teamboxmsg = window.setInterval(function () {

            var ani = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 300 });
            ani.addSubject(new NumericalStyleSubject($('teamboxmessage'), 'opacity', 1, 0));
            ani.play();

            window.clearInterval(teamboxmsg);
        }, 1500);


        new Ajax.Request(this.ajaxurl + "&action=ajaxadd", {
            method: 'get',
            parameters: 'id=' + id + "&color=" + color + "&size=" + size,
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');

                if (data.count > 0) {
                    this.elem.addClassName("enable");
                }
                var childs = this.elem.childElements();
                childs[1].update("[ " + data.count + " ] ");
            }.bind(this),
            onFailure: function(resp) {

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    updateCount : function (inputfield) {

        new Ajax.Request(this.ajaxurl + "&action=updatecount", {
            method: 'get',
            parameters: 'value=' + inputfield.value + '&name=' + inputfield.name   ,
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');

            }.bind(this),
            onFailure: function(resp) {

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    updateColor : function (inputfield) {

        new Ajax.Request(this.ajaxurl + "&action=updatecolor", {
            method: 'get',
            parameters: 'value=' + inputfield.value + '&name=' + inputfield.name   ,
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');

            }.bind(this),
            onFailure: function(resp) {

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    updateSize : function (inputfield) {

        new Ajax.Request(this.ajaxurl + "&action=updatesize", {
            method: 'get',
            parameters: 'value=' + inputfield.value + '&name=' + inputfield.name   ,
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');

            }.bind(this),
            onFailure: function(resp) {

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    checkForm : function (form) {
        new Ajax.Request(this.ajaxurl + "&action=checkform", {
            method: 'get',
            parameters: $(form).serialize(),
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');
                var blocks = $$('#content form.personal p');
                for (var i = 0; i < blocks.length; i ++) {
                    $(blocks [i]).removeClassName("error");
                }

                if (data.error.length > 0) {
                    $$('#content form.personal .requiredinfo')[0].setStyle({right: "144px"});
                    for (var i = 0; i < data.error.length; i ++) {
                        var search = "#content form.personal p." + data.error[i];
                        $$(search)[0].addClassName("error");
                    }
                }
                else {
                    $$('#content form.personal .requiredinfo')[0].setStyle({right: "-300px"});

                    $$('#content form.personal')[0].submit();
                }

                return false;

            }.bind(this),
            onFailure: function(resp) {
                return false;

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    /**
     * Removes a Product from the Teambox
     * @param interger productid Product ObjectID
     * @param integer index Index in Sliding
     */
    removeProduct : function (productid, index) {
        if (typeof index == 'number') {
            try {
                teamboxSlider.removeIndex(index);
            }
            catch (e) {

            }
        }
        new Ajax.Request(this.ajaxurl + "&action=removeproduct", {
            method: 'get',
            parameters: "productid=" + productid ,
            onComplete: function (resp) {

                var response = resp.responseText;

                var data = eval('(' + response + ')');

                if (data.reload == true) {
                    location.reload();
                }

                if ($('product_' + productid)) {
                    $('product_' + productid).remove();
                }
                try {
                }
                catch (e) {
                }


                var childs = myteambox.elem.childElements();
                childs[1].update("[ " + data.count + " ] ");
            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    },
    removeShop : function (shopid) {
        new Ajax.Request(this.ajaxurl + "&action=removeshop", {
            method: 'get',
            parameters: "shopid=" + shopid ,
            onComplete: function (resp) {

                var response = resp.responseText;

                var data = eval('(' + response + ')');

                if (data.shopcounts == 0) {
                    $('stores').hide();
                    $('errorlayer').show();
                    try {
                        $('submitteambox').remove();
                    }
                    catch(e) {

                    }
                }

                var shopelement = $('store_' + shopid);
                shopelement.toggle();

            },
            onException: function(resp, exception) {
                throw exception;
            }
        })
    }
});

