var objDM = {
    arrResult : [],
    arrCart : [],
    intCount : 0, 

    // Calls the fancybox
    // @param       String      HTML ID of the content
    dialog : function (strID)
    {
        $('.downloadbutton').css('display', 'none');
        $.fancybox($('#'+ strID).html(), {
            'width' : 400,
            'height' : 220,
            'autoDimensions' : false,
            'padding' : 20,
            'overlayColor' : '#fff',
            'showCloseButton' : false, 
            // Reset the cart array when the fancybox gets closed without saving
            'onClosed' : function() {
                objDM.arrCart = [];
                // We have to count the the entries this way cause arr.length doesn't work here...
                objDM.intCount = 0;
            }
        });
    },
    
    // Adds or removes an element from/to the cart
    // @param       string      URL to do the cart action
    addRemove : function (objCheck, strURL)
    {
        // Is the item already in the cart?
        if (objDM.arrCart[objCheck.id])
        {
            // We have to count the the entries this way cause arr.length doesn't work here...
            objDM.intCount--;
            // Remove it
            objDM.arrCart[objCheck.id] = "";
        }
        else
        {
            // We have to count the the entries this way cause arr.length doesn't work here...
            objDM.intCount++;
            // Add it
            objDM.arrCart[objCheck.id] = strURL;
        }
        if (objDM.intCount > 0)
        {
            $('.downloadbutton').css('display', 'block');
        }
        else
        {
            $('.downloadbutton').css('display', 'none');
        }
    },
    
    // Saves the cart
    saveCart : function ()
    {
        if (objDM.intCount == 0)
        {
            return false;
        }
        // Send ajax requests
        var strURL = "";
        // To be shur that the site will reload, add a delay
        for (var strID in objDM.arrCart)
        {
            strURL = objDM.arrCart[strID];
            if (strURL !== "")
            {
                $.ajax(strURL, {
                    complete : function(objXHR, strStatus)
                    {
                        if (strStatus == "success")
                        {
                            objDM.arrResult.push(true);
                        }
                        else
                        {
                            // Something went wrong
                            alert("Es gab einen Fehler bei der Übertragung");
                            objDM.arrResult.push(false);
                        }

                        // Have all items been submitted
                        if (objDM.arrResult.length == objDM.intCount)
                        {
                            location.reload();
                        }
                    }
                });
            }
        }
    }
};
