﻿Ext.ns("OXX",  "OXX.Accordion", "OXX.Layout", "OXX.Basket");




OXX.Application = function() {

    var products = null;
    var windows = {};
    var contentElements = {};
    var expandId = 'product-bottom'
    var useFlipflop = false;
    var expandHeight = 200;
    var _basket = null;

    function _init() {

        products = new Object();

        _basket = new OXX.Basket('mybasket');

        productHeight = (Ext.fly(expandId) != null) ? Ext.fly(expandId).getHeight() : 0;

        useFlipflop = Ext.isObject(Ext.fly('product-bottom'));
        expandHeight = useFlipflop ? Ext.get('product-bottom').first('.inner-content').getHeight() + 100 : 0;

       
        if (!OXX.Application.security.isAnonymous()) {

            //  Ext.fly('mybox_9079250').remove();
        }

    }



    function _setupEvents() {

        Ext.select('.tipafriend').on('click', function(e, t) {
            var url = 'url=' + document.location.href;

            var nav = document.location.href.split('/');
            var href = '';
            for (var i = 0; i < nav.length - 1; i++) {
                href += nav[i] + '/';
            }
            var aid = OXX.Util.paramvalue('aid');
            href += 'default.aspx?aid=' + aid + '&ivtid=7';

            document.location = href + '&' + url;
        });


        Ext.select('.allprod').on('click', function(e, t) {
            document.location = '?aid=' + OXX.Util.paramvalue('aid');
        }, this);



        Ext.select('.product-link').on('click', function(e, t) {
            var productId = Ext.get(t).dom.id.replace('product_link', '');
            OXX.ProductCollection.redirectToProduct(productId);
        }, this);


        /*
        Hoover of the loovers
        */
        Ext.select('.organisation').on('mouseover', function(e, t) {

            Ext.get(t).parent('.imgbanner').replaceClass('imgbanner', 'imgbanner overhover'); //child('.color').setVisible(true);
            //Ext.get(t).parent('.imgbanner'). //child('.gray').setVisible(false);

        }).on('mouseout', function(e, t) {

            Ext.get(t).parent('.imgbanner').replaceClass('imgbanner overhover', 'imgbanner'); //child('.gray').setVisible(true);
            //Ext.get(t).parent('.imgbanner').child('.color').setVisible(false);
        });




        /*
        The boxlinks are clickable in the whole box
        */
        Ext.select('.boxbanner-full').each(function(el, th, index) {

            el.on('click', function(e, t) {
                var domEl = Ext.fly(t).parent('.boxbanner-full');
                if (Ext.isObject(domEl)) {
                    var linkEl = domEl.first('.link');
                    if (Ext.isObject(linkEl)) {
                        document.location = linkEl.dom.href;
                    }
                }
            }, this);

            el.on('mouseover', function(e, t) {
                Ext.get(t).setStyle('cursor', Ext.isIE ? 'hand' : 'pointer');
            }, this);
        });


        /*
        All elements with class hover will implement this 
        */
        Ext.select('.hover').on('mouseover', function(e, t) {
            Ext.get(t).setStyle('cursor', Ext.isIE ? 'hand' : 'pointer');
        }, this);


        /*
        This is a flip-flop expand-collapse
        */
        if (Ext.fly(expandId) != null) {
            Ext.fly('product-expand').on('click', function(e, t) {
                var el = Ext.get(expandId);
                //  el.setStyle('overflow', 'hidden');
                el.hasClass('large') ? el.setHeight(1, { duration: 0.5, callback: OXX.Application.resizeLayout }) : el.setHeight(expandHeight, { duration: 0.5, callback: OXX.Application.resizeLayout });
                el.toggleClass('large');
            }, this);
        }

        /* Here we catch page events, its now only implemented for the products */

        /* buy */
        Ext.fly('page').on('click', function(e, t) {
            if (Ext.isPrimitive(t.id)) {
                var splits = t.id.split('_');
                _basket.add(splits[2]);
            }
        }, this, { delegate: '.btn_buy' });

        /* info */
        Ext.fly('page').on('click', function(e, t) {
            if (Ext.isPrimitive(t.id)) {
                //var idSplits = t.id.split('_');
                //   _basket.add(splits[2]);
                var name = t.name;
                var splits = name.split(':');
                document.location = '?aid=' + splits[1] + ((splits[2] != splits[1]) ? "&pid=" + splits[2] : "");

            }
        }, this, { delegate: '.btn_info' });




        /*
        Registerstuff
        */

        Ext.select('.mybox_LOGIN').on('click', function(e, t) {

            OXX.Util.redirect(1);

        }, this).on('mouseover', function(e, t) {
            Ext.fly(t).setStyle('cursor', Ext.isIE ? 'hand' : 'pointer');
        }, this);
    }



    function _resizeLayout(add) {

        var lrx = new Array(4);
        lrx[0] = parseInt(Ext.fly('inner-leftbar').getHeight());
        lrx[1] = parseInt(Ext.fly('inner-content').getHeight());
        lrx[2] = parseInt(Ext.fly('inner-rightbar').getHeight());
        lrx[3] = parseInt(Ext.fly('inner-extrabar').getHeight());
        lrx.sort(function(a, b) {
            return a - b;
        });

        var mainEl = Ext.fly('main');
        var minHeight = (lrx[lrx.length - 1] + 40);
        mainEl.setHeight(minHeight);

    }



    return Ext.apply(new Ext.util.Observable, {

        publicProperty: "You can access this",

        init: function() {
            _init();
            _setupEvents();


            _resizeLayout();
        },

        clearBasket: function() {
            _basket.removeAll();
        },

        basketCount: function() {
            _basket.totalize();

            return _basket.totalCount;
        },

        getBasketItems: function() {
            return _basket.items;
        },

        basketIsEmpty: function() {
            return _basket.isEmpty();
        },

        resizeLayout: function() {

            _resizeLayout();
        }
    });


} ();







Ext.onReady(OXX.Application.init);



