﻿function debug(x) {
    try {
        console.debug(x);
    } catch (e) {
        alert(x);
    }
}
$.fn.equals = function (compareTo) {
    if (!compareTo || this.length != compareTo.length) {
        return false;
    }
    for (var i = 0; i < this.length; ++i) {
        if (this[i] !== compareTo[i]) {
            return false;
        }
    }
    return true;
};
$.fn.center = function () {
    this.css("position", "relative");
    //this.css("top", ((this.parent().height() - this.outerHeight()) / 2) + this.parent().scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
};
// Home Hero
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active',
                textwidth:75
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var obj = $(this);
                obj.data('options', options);
                var objchildren = obj.children("img");
                var objlinks = obj.children("a");
                var wrapper = $('<div class="hero-wrapper" />').appendTo(obj);
                var linkwrapper = $('<div class="hero-links" />').prependTo(obj).append(objlinks);
                var totalwidth = 0;
                
                objlinks.hover(function(){
                    var elm = $(this);
                    elm.animate({width: '+=' + options.textwidth},200);
                },function(){
                    var elm = $(this);
                    elm.animate({width: '-=' + options.textwidth},200);
                });
                objchildren.imagesLoaded(function () {
                    $(this).each(function(){
                        totalwidth += $(this).width();
                    });
                                        
                    wrapper.width(totalwidth).append(objchildren).center();
                    $(window).resize(function(){
                        if($(window).width() > 994){
                            wrapper.center();
                        }
                    });
                });
                

            });

        }
    };
    $.fn.homehero = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

/*!
* jQuery imagesLoaded plugin v1.0.4
* http://github.com/desandro/imagesloaded
*
* MIT License. by Paul Irish et al.
*/

(function ($, undefined) {

    // $('#my-container').imagesLoaded(myFunction)
    // or
    // $('img').imagesLoaded(myFunction)

    // execute a callback when all images have loaded.
    // needed because .load() doesn't work on cached images

    // callback function gets image collection as argument
    //  `this` is the container

    $.fn.imagesLoaded = function (callback) {
        var $this = this,
        $images = $this.find('img').add($this.filter('img')),
        len = $images.length,
        blank = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

        function triggerCallback() {
            callback.call($this, $images);
        }

        function imgLoaded(event) {
            if (--len <= 0 && event.target.src !== blank) {
                setTimeout(triggerCallback);
                $images.unbind('load error', imgLoaded);
            }
        }

        if (!len) {
            triggerCallback();
        }

        $images.bind('load error', imgLoaded).each(function () {
            // cached images don't fire load sometimes, so we reset src.
            if (this.complete || typeof this.complete === "undefined") {
                var src = this.src;
                // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
                // data uri bypasses webkit log warning (thx doug jones)
                this.src = blank;
                this.src = src;
            }
        });

        return $this;
    };
})(jQuery);
