/*!
 * (c) 2010 SysIQ, Inc. All rights reserved.
 *
 * This product is protected by United States laws,
 * international copyright treaties and all other applicable
 * national or international laws.
 *
 * This software may not, in whole or in part, be copied,
 * photocopied, translated, modified, or reduced to any
 * electronic medium or machine readable form
 * without the prior written consent of SysIQ, Inc.
 *
 * Filename: jquery.shutter.js
 * Author:   Alexander Goldobin
 */ 


(function($) {

var defaults = {
    delay: 750,
    image: "loadAddressProgress.jpg",
    minDims: {
        width: 150,
        height: 150
    }
};

$.fn.extend({
    showShutter: function(fn) {
        return this.trigger("showShutter", fn);
    },
    hideShutter: function(fn) {
        return this.trigger("hideShutter", fn);
    },
    updateShutter: function() {
        return this.trigger("updateShutter");
    },
    shutter: function(opts) {
        var settings = $.extend({}, defaults, opts);

        var self = this;

        var img = $(new Image())
            .css("position", "absolute")
            .attr("src", settings.image)
            .load(function() {
                self.updateShutter();
            });

        var shut = $("<div>")
            .css({
                left: 0,
                top: 0,
                position: "relative"
            })
            .addClass("shutter")
            .append(img)
            .hide(0);

        this.append(shut);

        var update = function() {

            var dims = {
                width: (self.innerWidth() > settings.minDims.width ? self.innerWidth() : settings.minDims.width),
                height: (self.innerHeight() > settings.minDims.height ? self.innerHeight() : settings.minDims.height)
            };

            shut.css ({
                width: dims.width,
                height: dims.height 
            });

            img.css({
                left: (dims.width/2 - img.width()/2)+"px",
                top: (dims.height/2 - img.height()/2)+"px"
            });
        };

        img.load(update).attr("src", settings.contentUrl);

        var isShowed = false;
        var needFadeIn = false;

        this.bind("showShutter", function(e, fn) {

            update();
            needFadeIn = true;
            setTimeout(function() {
                if (needFadeIn) {
                    isShowed = true;
                    shut.fadeIn("fast", fn);
                    update();
                }
            }, settings.delay);

            return true;
        });

        this.bind("hideShutter", function(e, fn) {
            isShowed = false;
            needFadeIn = false;
            shut.hide(0, fn);
            return true;
        });

        this.bind("updateShutter", function() {
            if (isShowed) {
                update();
            }
            return true;
        });

        return shut;
    }
});

})(jQuery);

