/**
 * JavaScript implementation of commonlibs Image class
 * Usage:
 * $.imageParseImageUrl("http://img.gfx.no/484/484310/ekte3giphone-s6-564x376.300x200!.jpg").imageGetSizeUrl(100, 100);
 * $.imageParseImageUrl("http://img.gfx.no/484/484310/ekte3giphone-s6-564x376.300x200!.jpg").imageGetContainerUrl(200, 300);
 * 
 * Fetch from img/a:
 * $(".foo img").getContainerUrl(200, 300); // Single
 * $(".foo a").getSizeUrl(100, 100); // Single
 *
 * var arr = [];
 * $("img").each(function() {
 *      $.imageParseImageUrl(this.src);
 *      arr.push($.imageGetContainerUrl(200, 300));
 * });
 * 
 * Preloading images:
 * $.imagePreload($.imageGetContainerUrl(200, 300) | [$.imageGetContainerUrl(200, 300), $imageGetContainerUrl(400, 300)]);
 * $(".foo img").each(function() {
 *       $.imageParseImageUrl(this.src);
 *       $.imagePreload($.imageGetContainerUrl(200, 300));
 * });
 * 
 */

(function($) {
    $.extend({
        _imageConfig: {
            id: null,
            height: undefined,
            width: undefined,
            ratio: undefined,
            type: undefined,
            url: undefined
        },
        imageParseImageUrl: function(url) {
            var splitted = url.split("."),
                id = parseInt(url.split("/")[4]),
                width = splitted[splitted.length - 2]
                            .replace("!", "")
                            .split("x")[0],
                height = splitted[splitted.length - 2]
                            .replace("!", "")
                            .split("x")[1],
                regex = new RegExp(',', 'g'),
                url = splitted
                            .slice(0, splitted.length - 2)
                            .toString()
                            .replace(regex, "."),
                type = splitted
                            .slice(splitted.length - 1, splitted.length)
                            .toString(),
                config = {
                    id: id,
                    width: width,
                    height: height,
                    ratio: width / height,
                    url: url,
                    type: type
                };
            // Extend _imageConfig-object
            $.extend($._imageConfig, config);

            return this;
        },
        imagePreload: function(urls) {
            var el = $("<img />");
            if (typeof urls === "string") {
                el.attr('src', urls).remove();
            }
            else {
                $.each(urls, function() {
                    el.attr('src', this).remove();
                });
            }
        },
        imageGetSizeUrl: function(width, height, force) {
            var ratio = $._imageConfig.ratio, url;
            if (!height) {
                if (ratio > 0)
                    height = Math.round(width / ratio);
                else
                    return false;
            }
            if (force)
                height += "!";

            url = $._imageConfig.url + "." 
                + width + "x" + height + 
                "." + $._imageConfig.type;

            return url;
        },
        imageGetContainerUrl: function(width, height) {
            var conRatio = width / height,
            ratio = $._imageConfig.ratio,
            imgWidth, imgHeight;
            if (ratio > conRatio) {
                imgWidth = width;
                imgHeight = Math.round(height / ratio);
            }
            else {
                imgWidth = Math.round(height * ratio);
                imgHeight = height;
            }
            return this.imageGetSizeUrl(imgWidth, imgHeight);
        }
    });

    $.fn.extend({
        getContainerUrl: function(w, h) {
            if (this.is("img, a")) {
                var url = this.attr('src') ? 
                    this.attr('src') : this.attr('href');
            }
            else {
                throw "Not correct tag name, must be 'img' or 'a'";
            }
            
            return $.imageParseImageUrl(url).imageGetContainerUrl(w, h);
        },
        getSizeUrl: function(w, h, f) {
            if (this.is("img, a")) {
                var url = this.attr('src') ? 
                    this.attr('src') : this.attr('href');
            }
            else {
                throw "Not correct tag name, must be 'img' or 'a'";
            }
            
            return $.imageParseImageUrl(url).imageGetSizeUrl(w, h, f);
        }
    });
})(jQuery);
