(function($){
    $.fn.walterize = function(userOptions) {

        var options = {
            images : {
                first : {
                    src : 'img/walter.png',
                    coords : [
                      [[48,66],[79,97],'Those rich fucks!<br>This whole fucking thing...'],
                      [[85,83],[100,103],'Huh?<br><i>[blows out a cloud of smoke]</i>'],
                      [[86,105],[104,117],'Shomer shabbos!'],
                      [[74,140],[158,173],'Smokey, this is not \'Nam.<br>This is bowling. There are rules.'],
                      [[160,182],[206,226],'What\'s mine is mine.'],
                      [[3,74],[35,115],'This is what happens when you fuck a stranger in the ass!'],
                      [[67,227],[169,257],'Smokey, my friend, you are entering a world of pain.'],
                      [[67,257],[169,270],'OVER THE LINE!']
                    ], dom:''
                },
                second : {
                    src : 'img/spaceye.png',
                    coords : [
                      [[70,98],[134,138],'M1 Helmet. Nice stuff.'],
                      [[25,7],[217,47],'Look, man. Have you ever heard of Vietnam?'],
                      [[75,148],[93,183],'M16A1.<br>Make no mistake about it.'],
                      [[156,184],[180,210],'1st Cavalry Division.'],
                      [[156,212],[180,228],'Yeah, still PFC.'],
                      [[182,228],[202,284],'Apparently, there\'s only water.<br>No booth, man. '],
                    ]
                }
            },
            indicator : {
                src : 'img/progress.gif'
            },
            coats : {
                src : 'img/drysa.png',
                desc : 'The dude is a local patriot'
            },
            contactImages : {
                twitter : {
                    src : 'img/twitter.png', desc : 'Tweet me..<br> something coherent', href : 'http://twitter.com/spaceye'
                },
                fb : {
                    src : 'img/facebook.png', desc : 'Meet the man<br> on the Friggin\' Book', href : 'http://www.facebook.com/spaceye'
                },
                gdgt : {
                    src : 'img/gdgt.png', desc : 'Check the gadgetry!', href : 'http://gdgt.com/profile/spaceye'
                },
                skype : {
                    src : 'img/skype.png', desc : 'Talk like a man,<br>will you?!', href : 'skype:vad.kakouka?add&displayname=spaceye'
                },
                mail : {
                    src : 'img/email.png', desc : 'Mail the dude', href : 'mailto:' + ["thedude","spaceye.eu"].join("@")
                }
            },
            color : '#000',
            hintPadding : {x:12,y:12},
            delays : {
                fadeIn: 500,
                fadeOut: 500
            },
            opacity : 0.8,

        };

        // add user submitted options
        if ( options ) { $.extend(options, userOptions); }

        return this.each(function() {

            // image jQuery object
            this.obj = $(this);
            this.options = options;

            this.insertImagesIntoDom = function() {

                for ( var image in this.options.images ) {

                    this.obj.append("<div id='" + image + "_image'>");
                    this.options.images[image].dom = $("<img>").attr('src',this.options.images[image].src)
                                                               .css({'display':'none','cursor': ( image == 'first' ) ? 'pointer' : 'default'});
                    this.obj.find("div#" + image + "_image").append(this.options.images[image].dom);
                    $(this.options.images[image].dom).hintMe({coords:this.options.images[image].coords, hintPadding:{x:14,y:14},opacity:0.9,color:'#000'});
                }

                this.obj.append("<div id='contacts'>");

                for ( var image in this.options.contactImages ) {

                    this.options.contactImages[image].dom = $("<img>").attr('src',this.options.contactImages[image].src)
                                                                      .attr('title',this.options.contactImages[image].desc)
                                                                      .css({'display':'none'});
                    this.obj.find("div#contacts").append(this.options.contactImages[image].dom);
                    this.options.contactImages[image].dom.attr('link',this.options.contactImages[image].href);
                    $(this.options.contactImages[image].dom).hintMe({hintPadding:{x:20,y:12},opacity:0.9,color:'#000'});
                    this.options.contactImages[image].dom.bind('click',function(){
                        window.location = $(this).attr('link');
                        $(this).trigger('mouseout');
                    });
                }

                this.obj.append("<div id='coats'>");
                this.options.coats.dom = $("<img>").attr('src',this.options.coats.src)
                                                                  .attr('title',this.options.coats.desc)
                                                                  .css({'display':'none'});
                this.obj.find("div#coats").append(this.options.coats.dom);
                $(this.options.coats.dom).hintMe({opacity :1,color:'#000',opacity:0.9});

            };


            this.placeIndicator = function() {

                this.options.indicator.dom = $("<div id='progress'>").css({'display':'none','left':'50%','top':'50%','width':'64','height':'30','position':'absolute','margin-left':'-32px','margin-top':'-15px','text-align':'center','vertical-align':'middle','font-fmaily':'arial','font-size':'10pt'}).html("<img style='margin-bottom:18px;' width='16px' height='16px' src='" + this.options.indicator.src+ "'><br><span></span>");
                this.obj.append(this.options.indicator.dom);
                this.options.indicator.dom.fadeIn(1500);
            }

            this.removeIndicator = function() {

                var self = this;
                this.options.indicator.dom.fadeOut(500, function(){
                    $(self.options.indicator.dom).remove();
                    self.positionImages();
                });

            }

            this.runIndicator = function() {

                var self = this;

                if ( !this.allImagesLoaded() ) {
                    setTimeout(function(){
                        self.runIndicator();
                        },1000);
                }
                else {
                    this.removeIndicator();
                }
            };

            this.allImagesLoaded = function() {

                if ( this.options.indicator.dom[0].naturalWidth == 0 ) {
                    return false;
                }

                else {

                    this.options.indicator.dom.find('span').html('Loading..');

                    for ( var image in this.options.images ) {
                        if ( this.options.images[image].dom[0].naturalWidth == 0 ) return false;
                    }
                    for ( var image in this.options.contactImages ) {
                        if ( this.options.contactImages[image].dom[0].naturalWidth == 0 ) return false;
                    }

                    return ( this.options.coats.dom[0].naturalWidth != 0 ) ? true : false;
                }
            }

            this.positionImages = function() {

                var self = this;

                for ( var image in this.options.images ) {
                    this.options.images[image].dom.css({'left':Math.round($(window).width()/2) - Math.round(this.options.images[image].dom.width()/2),'position':'absolute','top':Math.round($(window).height()/12)});
                }

                this.options.images.first.dom.bind('click',function(){
                    self.options.images.first.dom.trigger('mouseout');
                    self.options.images.first.dom.stop().fadeOut(500, function(){
                        self.options.images.second.dom.stop().fadeIn(500, function(){
                            self.dropContacts();
                        });
                    });
                });

                this.options.images.second.dom.bind('click',function(){
                    self.options.images.second.dom.trigger('mouseout');
                    $("div#contacts, div#coats").find("img").stop().fadeOut(600, function(){
                        self.options.images.second.dom.stop().fadeOut(500, function(){
                            self.options.images.first.dom.stop().fadeIn(500);
                        });
                    });
                });

                this.options.images.first.dom.stop().fadeIn(100);
            };

            this.dropContacts = function(){

                var i = 1;

                for ( var image in this.options.contactImages ) {

                    var start = {x:0,y:0};
                    var stop = {x:0,y:0};

                    start.x = this.options.images.second.dom.offset().left + this.options.images.second.dom.width() - this.options.contactImages[image].dom.width() - 12;
                    start.y = this.options.images.second.dom.offset().top + 54;
                    stop.x = start.x;
                    stop.y = this.options.images.second.dom.offset().top + this.options.images.second.dom.height()/2 + 90 - this.options.contactImages[image].dom.height()*1.5*i;
                    i++;

                    var self = this;
                    this.options.contactImages[image].dom.css({'top':start.y,'left':start.x,'position':'absolute','display':'block','cursor':'pointer','opacity':0.1});
                    this.options.contactImages[image].dom.stop().animate({top:stop.y,opacity:1},550,'linear');
                    this.showCoat();
                }

            };

            this.showCoat = function(){

                var self = this;

                for ( var image in this.options.contactImages ) {
                    if ( parseInt(this.options.contactImages[image].dom.css('opacity')) < 1 ) {

                        setTimeout(function(){self.showCoat();}, 700);
                        return false;
                    }
                }

                var start = {x:0,y:0};

                start.x = this.options.images.second.dom.offset().left + this.options.images.second.dom.width() + this.options.coats.dom.width() - 30;
                start.y = this.options.images.second.dom.offset().top + 66;

                this.options.coats.dom.css({'top':start.y,'left':start.x,'opacity':1,'display':'none','position':'absolute'});
                this.options.coats.dom.stop().fadeIn(1000);
            };

            this.insertImagesIntoDom();
            this.placeIndicator();
            this.runIndicator();

        });
    };
})(jQuery)

