/*
 * Functionality around the mini cart.
 */
var MiniCart = {
    // flag, whether cart is open or not
    state: 0,
    
    // during page loading, the Demandware URL is stored here
    url: '',

    // timer for automatic close of cart item view
    timer: null,

    cartRefresh: function(carturl)
    {
        // get the data of the form as serialized string
        MiniCart.url = carturl;
        
        var refreshHandlerFunc = function(req)
        {
        
            // Get the position before the innerHTML is set. After
            // the set the elements have no coordinates. A display
            // refresh from the browser is necessary.
            var minicarttotal = $('minicarttotal');
            var pos = Position.cumulativeOffset(minicarttotal);
            var dim = Element.getDimensions(minicarttotal);

            // replace the content
            var minicart = $('minicart');
            minicart.innerHTML = req.responseText;

            // show the item
            // this display is optimized for Firefox. IE6/7 display the 
            // sliding down cart with an offset of -1
            var slidedowncart = $('slidedowncart');

            slidedowncart.style.left = pos[0] + 0 + 'px';
            slidedowncart.style.top = pos[1] + dim.height + 1 + 'px';
            slidedowncart.style.width = dim.width - 2 + 'px';

            new Effect.SlideDown('slidedowncart');

            // after a time out automatically close it
            MiniCart.timer = setTimeout( 'MiniCart.cartClose()', 6000 );
        }

        var refreshErrFunc = function(req)
        {
            // hide progress indicator
        }
        // cloes a previous mini cart
        MiniCart.cartClose();
        
        // add the product
        new Ajax.Request( MiniCart.url, {method:'post', postBody:'', onSuccess:refreshHandlerFunc, onFailure:refreshErrFunc});
    },

    cartReplace: function(carturl, form, progressImageSrc)
    {
        // get the data of the form as serialized string
        var postdata = Form.serialize(form);
        MiniCart.url = carturl;
    
        // get button reference
        var addButtons = Form.getInputs(form, 'image', 'add');
    
        // the button to update
        var addButton = null;
        
        // disable form
        Form.disable(form);

        // it is an array of buttons, but we need only one all
        // other combinations are strange so far
        if (addButtons.length == 1)
        {
            addButton = addButtons[0];  
        }
    
        var previousImageSrc = null;
    
        // show progress indicator
        if (addButton != null)
        {
            previousImageSrc = addButton.src;
            addButton.src = progressImageSrc;
        }
    
        var handlerFunc = function(req)
        {
            // hide progress indicator
            if (addButton != null)
            {
                addButton.src = previousImageSrc;
            }
            Form.enable(form);
        
            // Get the position before the innerHTML is set. After
            // the set the elements have no coordinates. A display
            // refresh from the browser is necessary.
            var minicarttotal = $('minicarttotal');
            var pos = Position.cumulativeOffset(minicarttotal);
            var dim = Element.getDimensions(minicarttotal);

            // replace the content
            var minicart = $('minicart');
            minicart.innerHTML = req.responseText;

            // show the item
            // this display is optimized for Firefox. IE6/7 display the 
            // sliding down cart with an offset of -1
            var slidedowncart = $('slidedowncart');

            slidedowncart.style.left = pos[0] + 0 + 'px';
            slidedowncart.style.top = pos[1] + dim.height + 1 + 'px';
            slidedowncart.style.width = dim.width - 2 + 'px';

            new Effect.SlideDown('slidedowncart');

			
            // after a time out automatically close it
            MiniCart.timer = setTimeout( 'MiniCart.cartClose()', 6000 );
        }

        var errFunc = function(req)
        {
            // hide progress indicator
            if (addButton != null)
            {
                addButton.src = previousImageSrc;
            }
            Form.enable(form);
        }

        // close a product QuickView
        // if ( QuickView ) QuickView.closeQuickView();

        // cloes a previous mini cart
        MiniCart.cartClose();

        // add the product
        new Ajax.Request( MiniCart.url, {method:'post', postBody:postdata, onSuccess:handlerFunc, onFailure:errFunc});
    },
    
    cartAdd: function( form, progressImageSrc )
    {
        // get the data of the form as serialized string
        var postdata = Form.serialize(form);
    
        // get button reference
        var addButtons = Form.getInputs(form, 'image', 'add');
    
        // the button to update
        var addButton = null;
        
        // disable form
        Form.disable(form);

        // it is an array of buttons, but we need only one all
        // other combinations are strange so far
        if (addButtons.length == 1)
        {
            addButton = addButtons[0];  
        }
    
        var previousImageSrc = null;
    
        // show progress indicator
        if (addButton != null)
        {
            previousImageSrc = addButton.src;
            addButton.src = progressImageSrc;
        }
    
        var handlerFunc = function(req)
        {
            // hide progress indicator
            if (addButton != null)
            {
                addButton.src = previousImageSrc;
            }
            Form.enable(form);
        
            // Get the position before the innerHTML is set. After
            // the set the elements have no coordinates. A display
            // refresh from the browser is necessary.
            var minicarttotal = $('minicarttotal');
            var pos = Position.cumulativeOffset(minicarttotal);
            var dim = Element.getDimensions(minicarttotal);

            // replace the content
            var minicart = $('minicart');
            minicart.innerHTML = req.responseText;

            // show the item
            // this display is optimized for Firefox. IE6/7 display the 
            // sliding down cart with an offset of -1
            var slidedowncart = $('slidedowncart');

            slidedowncart.style.left = pos[0] + 0 + 'px';
            slidedowncart.style.top = pos[1] + dim.height + 1 + 'px';
            slidedowncart.style.width = dim.width - 2 + 'px';

            new Effect.SlideDown('slidedowncart');


            // after a time out automatically close it
            MiniCart.timer = setTimeout( 'MiniCart.cartClose()', 6000 );
        }

        var errFunc = function(req)
        {
            // hide progress indicator
            if (addButton != null)
            {
                addButton.src = previousImageSrc;
            }
            Form.enable(form);
        }

        // close a product QuickView
        // if ( QuickView ) QuickView.closeQuickView();

        // cloes a previous mini cart
        MiniCart.cartClose();

        // add the product
        new Ajax.Request( MiniCart.url, {method:'post', postBody:postdata, onSuccess:handlerFunc, onFailure:errFunc});
    },

    cartClose: function()
    {
        if ( MiniCart.timer != null )
        {
            clearTimeout( MiniCart.timer );
            MiniCart.timer = null;
            Effect.SlideUp('slidedowncart');

        }
    }
}