/* * * * * * * * * * * * * * * * * * * * * *
 * Blinky, das springende etwas
 * 
 * @author: Dustin Breuer <breuer@lemm.de>
 * 
 * @nutzung:
 *      var Blinkey = new Blinky("#daSollBlinkyHin", "Bild/zu/Blinky.jpg", function(){
 *           alert("Das passiert, wenn man Blinky anklickt");
 *      });
 *      
 *      Blinkey.init();
 * 
 * 
 * * * * * * * * * * * * * * * * * * * * */

function Blinky(whereBlinky, picOfBlinky) {
    // Darf Blinky spielen?
        this.action = true;
    // Wo ist Blinky?
        this.blinky = whereBlinky;
    // Und wie sieht er aus? (: Hast du mal ein Foto von ihm?
        this.picOfBlinky = picOfBlinky;
    
    this.blinkyW = 47;
    this.blinkyH = 45;
    
    this.timeout;
    
    this.hoverText = "Klick mich!";
    
    // Was soll passieren, wenn man Blinky "kitzelt"?
    this.tickle = "";

    var me = this;

    this.init = function(){
        if($(">img",me.blinky).is(":hidden")){
            $(this.blinky).show(0);
        } else {
        
            $(this.blinky).css({overflow: 'visible'}).html(
                '<img src="' + this.picOfBlinky + '" alt="Blinky" />');
            // Blinky sagen, was er zu tun hat, wenn man ihn kitzelt
            $(" img", this.blinky).click(this.tickle).css({cursor: "pointer", marginTop: 10, top: -7, left: 9}).attr("title", me.hoverText).simpletooltip();
            
            var width = $(" img", this.blinky).width();
            var height = $(" img", this.blinky).height();
       } 
        $(me.blinky).toggleClass("background", true);
        if(arguments[0] === true){
            this.timeout = window.setTimeout(function(){
                $(">img", me.blinky).animate({top: -150, width: "150%", height: "150%", left: -12}, 450)
                                    .delay(250)
                                    .animate({top: -7, width: me.blinkyW, height: me.blinkyH, left: 9}, Math.random() * 250 + 500, function(){

                $(me.blinky).removeClass("background");
                this.timeout = window.setTimeout(function(){ me.jump(); }, Math.random() * 0 + 5000);
                });
            }, 250);
        } else {
            this.timeout = window.setTimeout(function(){ me.jump(); }, Math.random() * 0 + 5000);
        }
    }

    this.jump = function(){
        if(this.action === true){
            
            $(me.blinky).addClass("background");
            
            $(" img", me.blinky).animate({top: -(Math.random() * 125 + 25)}, Math.random() * 500 + 150)
                                  .delay(Math.random() * 250 + 200)
                                  .animate({top: -10}, Math.random() * 1000 + 500, function(){
                                      $(me.blinky).removeClass("background");
                                  });
            me.timeout = window.setTimeout(function(){ me.jump(); }, Math.random() * 15000 + 10000);

        }
    }
    
    this.hide = function(){
        $(this.blinky).hide(0);
        me.stopper();
    }
    
    this.show = function(){
        $(this.blinky).show(0);
        me.stopper();
    }
    
    this.stopper = function(){
        window.clearTimeout(this.timeout);
        this.action = false;
    }
}
