﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="jQ/jquery-latest-vsdoc.js" />

(function ($) {

    $.extend({
        metadata: {
            defaults: {
                type: 'attr',
                name: 'data',
                cre: /({.*})/,
                single: 'metadata'
            },
            setType: function (type, name) {
                this.defaults.type = type;
                this.defaults.name = name;
            },
            get: function (elem, opts) {
                var df = this.defaults;
                var settings = $.extend({}, this.defaults, opts);
                // check for empty string in single property
                if (!settings.single.length) settings.single = 'metadata';

                var data = $.data(elem, settings.single);
                // returned cached data if it already exists
                if (data) return data;

                data = "{}";

                if (settings.type == "class") {
                    var m = settings.cre.exec(elem.className);
                    if (m)
                        data = m[1];
                } else if (settings.type == "elem") {
                    if (!elem.getElementsByTagName)
                        return undefined;
                    var e = elem.getElementsByTagName(settings.name);
                    if (e.length)
                        data = $.trim(e[0].innerHTML);
                } else if (elem.getAttribute != undefined) {
                    var attr = elem.getAttribute(settings.name);
                    if (attr)
                        data = attr;
                }

                if (data.indexOf('{') < 0)
                    data = "{" + data + "}";

                data = eval("(" + data + ")");

                $.data(elem, settings.single, data);
                return data;
            }
        }
    });
    $.fn.metadata = function (opts) {
        return $.metadata.get(this[0], opts);
    };

})(jQuery);

/// image loader start old
jQuery.fn.onImagesLoaded = function (_cb) {
    return this.each(function () {

        var $imgs = (this.tagName.toLowerCase() === 'img') ? $(this) : $('img', this),
                _cont = this,
                i = 0,
                _done = function () {
                    if (typeof _cb === 'function') _cb(_cont);
                };

        if ($imgs.length) {
            $imgs.each(function () {
                var _img = this,
                    _checki = function (e) {
                        if ((_img.complete) || (_img.readyState == 'complete' && e.type == 'readystatechange')) {
                            if (++i === $imgs.length) _done();
                        }
                        else if (_img.readyState === undefined) // dont for IE
                        {
                            $(_img).attr('src', $(_img).attr('src')); // re-fire load event
                        }
                    }; // _checki \\

                $(_img).bind('load readystatechange', function (e) { _checki(e); });
                _checki({ type: 'readystatechange' }); // bind to 'load' event...
            });
        } else _done();
    });
};
/// image loader end old

/// image loader start new
(function ($) {
    $.fn.onImagesLoad = function (options) {
        var self = this;
        self.opts = $.extend({}, $.fn.onImagesLoad.defaults, options);

        self.bindEvents = function ($imgs, container, callback) {
            if ($imgs.length === 0) { //no images were in selection. callback based on options
                if (self.opts.callbackIfNoImagesExist && callback) { callback(container); }
            }
            else {
                var loadedImages = [];
                if (!$imgs.jquery) { $imgs = $($imgs); }
                $imgs.each(function (i) {
                    //webkit fix inspiration thanks to bmsterling: http://plugins.jquery.com/node/10312
                    var orgSrc = this.src;
                    if (!$.browser.msie) {
                        this.src = ""; //ie will do funky things if this is here (show the image as an X, only show half of the image, etc)
                    }
                    $(this).bind('load', function () {
                        if (jQuery.inArray(i, loadedImages) < 0) { //don't double count images
                            loadedImages.push(i); //keep a record of images we've seen
                            if (loadedImages.length == $imgs.length) {
                                if (callback) { callback.call(container, container); }
                            }
                        }
                    });
                    if (!$.browser.msie) {
                        this.src = orgSrc; //needed for potential cached images
                    }
                    else if (this.complete || this.complete === undefined) { this.src = orgSrc; }
                });
            }
        };

        var imgAry = []; //only used if self.opts.selectorCallback exists
        self.each(function () {
            if (self.opts.itemCallback) {
                var $imgs;
                if (this.tagName == "IMG") { $imgs = this; } //is an image
                else { $imgs = $('img', this); } //contains image(s)
                self.bindEvents($imgs, this, self.opts.itemCallback);
            }
            if (self.opts.selectorCallback) {
                if (this.tagName == "IMG") { imgAry.push(this); } //is an image
                else { //contains image(s)
                    $('img', this).each(function () { imgAry.push(this); });
                }
            }
        });
        if (self.opts.selectorCallback) { self.bindEvents(imgAry, this, self.opts.selectorCallback); }

        return self.each(function () { }); //dont break the chain
    };

    //DEFAULT OPTOINS
    $.fn.onImagesLoad.defaults = {
        selectorCallback: null,        //the function to invoke when all images that $(yourSelector) encapsultaes have loaded (invoked only once per selector. see documentation)
        itemCallback: null,            //the function to invoke when each item that $(yourSelector) encapsultaes has loaded (invoked one or more times depending on selector. see documentation)
        callbackIfNoImagesExist: false //if true, the callbacks will be invoked even if no images exist within $(yourSelector).
        //if false, the callbacks will not be invoked if no images exist within $(yourSelector).
    };
})(jQuery);
/// image loader end new