if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

Arboreus = {
    setCookie: function(key, value, force) {
        if (Arboreus.isAllowedCookie || force) {
            jQuery.cookie(key, value);
        }
    },

    updateLoadingBackground: function() {
        // initialization loading backgrounds:
        jQuery('.loading-background').each(function (index, element) {
            var el = jQuery(element);
            var parent = el.parent();

            var widthEl = parent.width();
            var heightEl = parent.height();

            var widthStyles = [
                'left', 'right'
            ];

            var heightStyles = [
                'top', 'bottom'
            ];

            jQuery(widthStyles).each(function (index, styleName) {
                widthEl += parseInt(parent.css('padding-' + styleName).replace('px', ''));
            });

            jQuery(heightStyles).each(function (index, styleName) {
                heightEl += parseInt(parent.css('padding-' + styleName).replace('px', ''));
            });

            el.width(widthEl);
            el.height(heightEl);

            el.css({
                'margin-left': -1 * parseInt(parent.css('padding-left').replace('px', '')),
                'margin-top' : -1 * parseInt(parent.css('padding-top').replace('px', ''))
            });
        });
    },

    hideLoadingBackground: function() {
        // initialization loading backgrounds:
        jQuery('.loading-background').height(0).width(0);
    },

    refreshActions: function () {
        if (typeof Arboreus.tooltips == 'undefined') {
            return;
        }

        Arboreus.tooltips.bindControlls();
    },

    isExist: function() {
        var isExist = true;
        if (arguments.length > 0) {
            jQuery.each(arguments, function (index, arg) {
               if (typeof arg === 'undefined' || arg === null) {
                   isExist = false;
                   return false;
               }
            });
        }

        return isExist;
    },

    length: function(obj) {
        var size = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) size++;
        }
        return size;
    },

    getOrdinalPostfix: function(number) {
        if(number.toString().substr(-2, 2) == 11 || number.toString().substr(-2, 2) == 12 || number.toString().substr(-2, 2) == 13){
            suffix = "th";
        }
        else if (number.toString().substr(-1, 1) == 1){
            suffix = "st";
        }
        else if (number.toString().substr(-1, 1) == 2){
            suffix = "nd";
        }
        else if (number.toString().substr(-1, 1) == 3){
            suffix = "rd";
        }
        else {
            suffix = "th";
        }

        return suffix;
    },

    namespace: function(namespace) {
        var names = namespace.split('.');
        var current = window;

        jQuery.each(names, function (index, name) {
            if (!Arboreus.isExist(current[name])) {
                current[name] = {};
            }

            current = current[name];
        })
    },

    pxToNum: function(px) {
        return parseFloat(px.replace('px', ''));
    },

    getNaturalWidth: function(img) {
        if(typeof img.attr('naturalWidth') == "undefined") {
            var temp_image = new Image();
            temp_image.src = img.attr('src');
            return temp_image.width;
        } else {
            return img.attr('naturalWidth');
        }
    },

    getNaturalHeight: function(img){
        if(typeof img.attr('naturalHeight') == "undefined") {
            var temp_image = new Image();
            temp_image.src = img.attr('src');
            return temp_image.height;
        } else {
            return img.attr('naturalHeight');
        }
    },

    resizeImage: function (img, containerWidth, containerHeight) {
        img = jQuery(img);

        var imageWidth  = Arboreus.getNaturalWidth(img);
        var imageHeight = Arboreus.getNaturalHeight(img);

        img.attr('w', imageWidth);
        img.attr('h', imageHeight);

        var k1 = containerWidth / imageWidth;
        var k2 = containerHeight / imageHeight;

        if (k1 > k2) {
            var k = k2;
        } else {
            var k = k1;
        }

        var w  = Math.round(imageWidth * k);
        var h = Math.round(imageHeight * k);

        if (w < Arboreus.getNaturalWidth(img) && h < Arboreus.getNaturalHeight(img)) {
            imageWidth  = w;
            imageHeight = h;
        }

        img.width(imageWidth);
        img.height(imageHeight);

        return {width: imageWidth, height: imageHeight};
    },

    requestToCart: function (params) {
        var url = oState.baseUrl + 'updateCart2.php';
        if (typeof params === 'string') {
            url = params;
            params = null;
        }

        jQuery.scrollTo('.all', 500, function () {
            if (jQuery('#shopping-cart-container').length > 0) {
                jQuery('#shopping-cart-loading').addClass('hidden');
                jQuery('#shopping-cart-container .loading-background').removeClass('hidden');
            } else {
                jQuery('#shopping-cart-loading').removeClass('hidden');
            }

            Arboreus.updateLoadingBackground();

            jQuery.post(
                url,
                params,
                function (obj) {
                    shoppingCartResponse(obj);
                    Arboreus.refreshActions();
                },
                'json'
            );
        });
    }
}

Arboreus.namespace('ArboreusSource');

