/*
        __author__ = "john avila"
        __copyright__ = "Copyright 2013,"
        __license__ = "Affero GPL"
        __version__ = "1.0.1"
        __maintainer__ = "john"
	__by__ = "asikersoft"
	
        __email__ = "avilajohn@asikersoft.com"
        __status__ = "Production"

        */

var parpadeo=0;
var checkpoint_array = new Array(),
    checkpointx_array = new Array(),
    checkpointy_array = new Array(),
    teleport_array = new Array(),
    teleportx_array = new Array(),
    teleporty_array = new Array(),
    speed = false,
    invisibility = false;

var g_ressources = [{
    name: "tileset",
    type: "image",
    src: "data/tileset/tileset.png"
}, {
    name: "level01",
    type: "tmx",
    src: "data/level01.tmx"
}, {
    name: "level02",
    type: "tmx",
    src: "data/level02.tmx"
}, {
    name: "level03",
    type: "tmx",
    src: "data/level03.tmx"
}, {
    name: "level04",
    type: "tmx",
    src: "data/level04.tmx"
}, {
    name: "level05",
    type: "tmx",
    src: "data/level05.tmx"
}, {
    name: "level06",
    type: "tmx",
    src: "data/level06.tmx"
}, {
    name: "level07",
    type: "tmx",
    src: "data/level07.tmx"
}, {
    name: "level08",
    type: "tmx",
    src: "data/level08.tmx"
}, {
    name: "level09",
    type: "tmx",
    src: "data/level09.tmx"
}, {
    name: "level10",
    type: "tmx",
    src: "data/level10.tmx"
}, {
    name: "level11",
    type: "tmx",
    src: "data/level11.tmx"
}, {
    name: "level12",
    type: "tmx",
    src: "data/level12.tmx"
}, {
    name: "level13",
    type: "tmx",
    src: "data/level13.tmx"
}, {
    name: "level14",
    type: "tmx",
    src: "data/level14.tmx"
}, {
    name: "level15",
    type: "tmx",
    src: "data/level15.tmx"
}, {
    name: "level16",
    type: "tmx",
    src: "data/level16.tmx"
}, {
    name: "gripe_run_right",
    type: "image",
    src: "data/sprite/gripe_run_right_" + color + ".png"
}, {
    name: "jump",
    type: "image",
    src: "data/sprite/jump.png"
}, {
    name: "jump_up",
    type: "image",
    src: "data/sprite/jump_up.png"
}, {
    name: "parallax",
    type: "image",
    src: "data/parallax/parallax2.png"
}, {
    name: "collect",
    type: "image",
    src: "data/sprite/collect.png"
}, {
    name: "32x32_font",
    type: "image",
    src: "data/sprite/32x32_font2.png"
}, {
    name: "none",
    type: "image",
    src: "data/sprite/none.png"
}, {
    name: "saw",
    type: "image",
    src: "data/sprite/spin.png"
}, {
    name: "saw2",
    type: "image",
    src: "data/sprite/spin2.png"
}, {
    name: "move62",
    type: "image",
    src: "data/sprite/move62.png"
}, {
    name: "move42",
    type: "image",
    src: "data/sprite/move42.png"
}, {
    name: "move41",
    type: "image",
    src: "data/sprite/move41.png"
}, {
    name: "speed_right",
    type: "image",
    src: "data/sprite/speed_right_32.png"
}, {
    name: "speed_left",
    type: "image",
    src: "data/sprite/speed_left_32.png"
}, {
    name: "teleport_img",
    type: "image",
    src: "data/sprite/teleport_img.png"
}, {
    name: "spike",
    type: "image",
    src: "data/sprite/spike.png"
}, {
    name: "check",
    type: "image",
    src: "data/sprite/check.png"
}, {
    name: "visible",
    type: "image",
    src: "data/sprite/visible.png"
}, {
    name: "invisible",
    type: "image",
    src: "data/sprite/invisible.png"
}, {
    name: "portal",
    type: "image",
    src: "data/sprite/portal.png"
}, {
    name: "portal2",
    type: "image",
    src: "data/sprite/portal2.png"
}, {
    name: "soundtrack7",//background_music
    type: "audio",
    src: "./data/audio/",
    channel: 2
}, {
    name: "die",
    type: "audio",
    src: "./data/audio/",
    channel: 2
}, {
    name: "collect",
    type: "audio",
    src: "./data/audio/",
    channel: 2
}, {
    name: "jump",
    type: "audio",
    src: "./data/audio/",
    channel: 2
}, {
    name: "checkpoint",
    type: "audio",
    src: "./data/audio/",
    channel: 2
}, {
    name: "wrong",
    type: "audio",
    src: "./data/audio/",
    channel: 2
}];

var jsApp = {
    version: "1.1",
    objRef: null,
    onload: function() {
      
      var intFrameWidth = screen.width;//window.innerWidth;
      var intFrameHeight = screen.height;//window.innerHeight;
	
    //	alert(" good look!" );
		
		
		
     // alert("AltoPantalla"+intFrameHeight );
	 
/*
	 document.getElementById("main").style.width =intFrameWidth+"px";
		document.getElementById("main").style.height =intFrameHeight+"px";
		
		document.getElementById("main2").style.width =intFrameWidth+"px";
      	document.getElementById("main2").style.height =intFrameHeight+"px";
        */
	  //  jsapp.style.width =intFrameWidth+"px";
	  
        me.sys.fps = 60;
       if (!me.video.init("jsapp", intFrameWidth, intFrameHeight )) {
			// if (!me.video.init("jsapp", 569, 320)) {
            alert("Sorry but your browser does not support Html5. Please try another one!");
            return
        }
        me.audio.init("mp3,ogg");
        me.loader.onload = this.loaded.bind(this);
        me.loader.preload(g_ressources);
        me.state.change(me.state.LOADING)
    },

    loaded: function() {
        me.state.set(me.state.PLAY, new PlayScreen());
        me.entityPool.add("mainPlayer", PlayerEntity);
        me.entityPool.add("CollectEntity", CollectEntity);
        me.entityPool.add("HurtEntity", HurtEntity);
        me.entityPool.add("SawEntity", SawEntity);
        me.entityPool.add("MoveEntity", MoveEntity);
        me.entityPool.add("MoveEntity2", MoveEntity2);
        me.entityPool.add("Spike", Spike);
        me.entityPool.add("SpikeAuto", SpikeAuto);
        me.entityPool.add("SpikeTrigger", SpikeTrigger);
        me.entityPool.add("SpeedEntity", SpeedEntity);
        me.entityPool.add("JumpEntity", JumpEntity);
        me.entityPool.add("JumpEntity2", JumpEntity2);
        me.entityPool.add("Checkpoint", Checkpoint);
        me.entityPool.add("Teleport", Teleport);
        me.entityPool.add("Visible", Visible);
        me.entityPool.add("Invisible", Invisible);
        me.entityPool.add("StartCount", StartCount);
        me.entityPool.add("EndCount", EndCount);
        me.entityPool.add("Initial", Initial);
        me.entityPool.add("InitialBlack", InitialBlack);
        me.entityPool.add("Final", Final);
        me.input.bindKey(me.input.KEY.A, "left");
        me.input.bindKey(me.input.KEY.D, "right");
        me.input.bindKey(me.input.KEY.W, "jump", true);
        me.input.bindKey(me.input.KEY.S, "down");
        me.input.bindKey(me.input.KEY.LEFT, "left");
        me.input.bindKey(me.input.KEY.RIGHT, "right");
        me.input.bindKey(me.input.KEY.UP, "jump");
        me.input.bindKey(me.input.KEY.DOWN, "down");
	
	
	     document.getElementById("btndown").addEventListener("touchstart", function () {

 me.input.triggerKeyEvent(me.input.KEY.DOWN, true);
 btndown.style.boxShadow ="-1px 1px 50px #4B36D4";
 btndown.style.opacity ="0.5";
 
 
  if (navigator.vibrate) {
                         navigator.vibrate(50);
                     }
 
	 }, false);

	  document.getElementById("btndown").addEventListener("touchend", function () {

   me.input.triggerKeyEvent(me.input.KEY.DOWN, false);
   
             btndown.style.boxShadow ="-1px 1px 20px #4B36D4";
 btndown.style.opacity ="1";

	 }, false);
       
       
       
    
          document.getElementById("btnup").addEventListener("touchstart", function () {

  me.input.triggerKeyEvent(me.input.KEY.UP, true);
  
   btnup.style.boxShadow ="-1px 1px 50px #4B36D4";
 btnup.style.opacity ="0.5";
 
  if (navigator.vibrate) {
                         navigator.vibrate(50);
                     }
	 }, false);

	  document.getElementById("btnup").addEventListener("touchend", function () {

   me.input.triggerKeyEvent(me.input.KEY.UP, false);
   
             btnup.style.boxShadow ="-1px 1px 20px #4B36D4";
	
 btnup.style.opacity ="1";
 
 
	 }, false);

	  
       document.getElementById("btnright").addEventListener("touchstart", function () {
	
	 if (navigator.vibrate) {
                         navigator.vibrate(50);
                     }
  me.input.triggerKeyEvent(me.input.KEY.RIGHT, true);
      btnright.style.boxShadow ="-1px 1px 50px #4B36D4";
 btnright.style.opacity ="0.5";
 

	 
	 }, false);
       
            document.getElementById("btnright").addEventListener("touchend", function () {
	 
   me.input.triggerKeyEvent(me.input.KEY.RIGHT, false);
  btnright.style.boxShadow ="-1px 1px 20px #4B36D4";
 btnright.style.opacity ="1";
	 }, false);

       
        document.getElementById("btnleft").addEventListener("touchstart", function () {
   me.input.triggerKeyEvent(me.input.KEY.LEFT, true);
   
        btnleft.style.boxShadow ="-1px 1px 50px #4B36D4";
 btnleft.style.opacity ="0.5";
  if (navigator.vibrate) {
                         navigator.vibrate(50);
                     }
	 }, false);
	
	  document.getElementById("btnleft").addEventListener("touchend", function () {

   me.input.triggerKeyEvent(me.input.KEY.LEFT, false);
     btnleft.style.boxShadow ="-1px 1px 20px #4B36D4";
	 
 btnleft.style.opacity ="1";
	 }, false);
	
	
        me.state.onPause = function() {
            /*
            Function for Pause
             */
            context = me.video.getScreenFrameBuffer();
            context.fillStyle = "rgba(0, 0, 0, 0.9)";
            context.fillRect(0, (me.video.getHeight() / 2) - 100, me.video.getWidth(), 200);
            font = new me.BitmapFont("32x32_font", 24);
            font.set("left");
            measure = font.measureText("P A U S E");
            font.draw(context, "P A U S E", (me.video.getWidth() / 2) - (measure.width / 2), (me.video.getHeight() / 2) - (measure.height / 2))
        };
        me.state.change(me.state.PLAY);
        
	loading.style.display="none";
    }

};


var PlayScreen = me.ScreenObject.extend({

    onResetEvent: function() {
     // debugger
        me.levelDirector.loadLevel("level" + initialLevel);
	
	
        if (sound == "true") {
           // me.audio.enable()

        } else if (sound == "false") {
           // me.audio.disable()
        }
     
        me.game.sort();

    },
    onDestroyEvent: function() {
        me.input.unbindKey(me.input.KEY.LEFT);
        me.input.unbindKey(me.input.KEY.RIGHT);
        me.input.unbindKey(me.input.KEY.UP);
        me.input.unbindKey(me.input.KEY.DOWN);
        me.audio.stopTrack();
    }
});
window.onReady(function() {
	/*
	document.getElementById("main").style.width =intFrameWidth+"px";
		document.getElementById("main").style.height =intFrameHeight+"px";
		
		document.getElementById("main2").style.width =intFrameWidth+"px";
      	document.getElementById("main2").style.height =intFrameHeight+"px";
        */
	  //  jsapp.style.width =intFrameWidth+"px";
	  
    jsApp.onload();
	
});


var PlayerEntity = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);

        /*
        Get player at the last checkpoint
         */
        for (var i = 1; i <= checkpoint_array.length; i++) {
            if (checkpoint_cookie == i) {
                if (checkpointx_array[checkpoint_cookie] == undefined || checkpointx_array[checkpoint_cookie] == 0) {
                    checkpointx_array[checkpoint_cookie] = 100;
                    checkpointy_array[checkpoint_cookie] = 200;
                }
                this.pos.x = checkpointx_array[checkpoint_cookie]
                this.pos.y = checkpointy_array[checkpoint_cookie]
            }
        }
        this.initialyvel = 13;
        this.initialxvel = 6;
        this.setFriction(0.2, 0.1);
        this.setVelocity(this.initialxvel, this.initialyvel);
        /*
        Set all the animations
        (according with the player sprite)
         */
        this.addAnimation("stand", [0, 0]);
        this.addAnimation("jump", [1, 1]);
        this.addAnimation("walk", [2, 3, 4]);
        this.addAnimation("slide", [5, 5]);
        this.addAnimation("jump_speed", [6, 6]);
        this.addAnimation("walk_speed", [7, 8, 9]);
        this.addAnimation("invisible_animation", [10, 11, 12]);
        this.addAnimation("invisible_stand", [13, 13]);
        this.addAnimation("invisible_jump", [14, 14]);
        this.addAnimation("invisible_walk", [15, 16, 17]);
        this.addAnimation("invisible_slide", [18, 18]);
        this.addAnimation("visible_animation", [19, 20, 21]);
        this.addAnimation("dead", [22, 22]);
        this.setCurrentAnimation("walk");

        /*
         Play sound when player destroy BREAK TILES
         (only when invisible)
         */
        this.onTileBreak = function() {
            me.audio.play("wrong")
        };
        /*
        If player is invisible, he can destroy BREAK TILES
         */
        if (invisibility == true) {
            this.canBreakTile = true;
        }

        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
	

	 if ( me.levelDirector.getCurrentLevelId() === "level01" )
        {
         
		 me.game.viewport.fadeOut("#4B36D4", 1350);
	  
btnleft.style.bottom="10px";
btnright.style.bottom="10px";
btnup.style.right="0px";
btndown.style.right="30px";

	  
        }
	 
	 
	
	 if ( me.levelDirector.getCurrentLevelId() === "level02" )
        {
	  
	  me.game.viewport.fadeOut("#A12ECB", 1350);
	  

        }
        
         if ( me.levelDirector.getCurrentLevelId() === "level03" )
        {
	  me.game.viewport.fadeOut("#A12ECB", 1350);

	}
        
         if ( me.levelDirector.getCurrentLevelId() === "level04" )
        {
	  
	  me.game.viewport.fadeOut("#4B36D4", 6350);
   
        }
        
         if ( me.levelDirector.getCurrentLevelId() === "level05" )
        {
	  me.game.viewport.fadeOut("#000000", 1350);
         
	
        }
   if ( me.levelDirector.getCurrentLevelId() === "level06" )//11se repite letransportes
        {
	  me.game.viewport.fadeOut("#4B36D4", 1350);
     

        }
        
         
        
        
       if ( me.levelDirector.getCurrentLevelId() === "level16" )//11se repite letransportes
        {
	  
	    

	   back2final.style.display="block";
	       back2finaltitle.style.display="block";
	       
	       
	       btndown.style.display="none";
	       btnleft.style.display="none";
	       btnright.style.display="none";
	       btnup.style.display="none";
	       
	
        }
        
        //15 s e daño
        
        
    },
    update: function() {
      
      
      

        if (!this.alive) {
            this.vel.x = 0;
            updated = this.updateMovement()
        } else {
            if (me.input.isKeyPressed("left") || me.input.isKeyPressed("right")) {
                if (speed == false) {
                    /*
                    Change animation sprite if player is invisible or not.
                     */
                    if (invisibility == true) {
                        this.setCurrentAnimation('invisible_walk')
                    }
                    if (invisibility == false) {
                        this.setCurrentAnimation('walk')
                    }

                    /*
                    Do walk and add acceleration
                     */
                    if (me.input.isKeyPressed("left")) {
                        this.doWalk(true)
                        this.vel.x -= this.accel.x * me.timer.tick
                    } else {
                        this.doWalk(false)
                        this.vel.x += this.accel.x * me.timer.tick
                    }
                }
            }


            if (me.input.isKeyPressed("jump")) {
                if (this.doJump()) {
                    me.audio.play("jump");
                }
                /*
                If user continues to press jump while jump
                Player jumps higher
                 */
                if (me.input.isKeyPressed("jump")) {
                    if (this.jumping) {
                        this.pos.y = this.pos.y - 8;
                    }
                }

            }
            if (speed == false) {
                if (me.input.isKeyPressed("down")) {
                    /*
                    If users press down,
                    player hit box is smaller
                     */
                    this.slide();
                    this.updateColRect(10, 44, 32, 32);
                }
            }

            updated = this.updateMovement();
            if (this.pos.x >= 200) {
                me.game.viewport.move(5, 0)
            }
            if (!this.isCurrentAnimation("slide")) {
                this.updateColRect(10, 44, 1, 63);

            }
            if (this.isCurrentAnimation("slide")) {
                this.updateColRect(10, 44, 32, 32);

            }


            if ((this.vel.x == 0) && (speed == true)) {
                this.setWalk();
                speed = false;
                this.setFriction(0.2, 0.1);
            } else if ((this.vel.y < 0) || (this.vel.y > 0) || (this.falling || this.jumping)) {

                /*
                Set all the animations for jumping and stand
                 */
                if (speed == false && invisibility == false) {
                    this.setCurrentAnimation('jump')
                }
                if (speed == true) {
                    this.setCurrentAnimation('jump_speed')
                }
                if (invisibility == true) {
                    this.setCurrentAnimation('invisible_jump')
                }
            } else if ((this.vel.x == 0) && !this.jumping && !this.falling) {
                if (invisibility == true) {
                    this.setCurrentAnimation('invisible_stand')
                }
                if (invisibility == false) {
                    this.setCurrentAnimation("stand");
                }
            }


            var a = me.game.collide(this);
            if (a) {
                switch (a.type) {
                    case "speed":
                    /*
                    If user touch speed object,
                    player enter in speed mode and cant stop
                    until touch against a wall
                     */
                    this.setVelocity(20, this.initialyvel);
                    this.vel.x = 20;
                    this.setFriction(0, 0);
                    speed = true;
                    this.setCurrentAnimation('walk_speed')
                    break;
                case "Invisible":
                    /*
                     If user touch invisible power,
                     player enter in invisible mode and can
                     break tiles
                     */
                    if (invisibility == false) {
                        this.setCurrentAnimation('invisible_animation', "invisible_stand")
                        this.canBreakTile = true;
                    }
                    invisibility = true;
                    break;
                    case "Visible":
                    /*
                    For normal situations or when touch
                    visible power object
                     */
                    if (invisibility == true) {
                        this.setCurrentAnimation('visible_animation', "stand")
                        this.canBreakTile = false;
                    }
                    invisibility = false;
                    break;
                default:
                    break
                }
            }
            if (updated || !this.isCurrentAnimation("walk")) {
                this.parent();
                updated = true
            }
        }
        return updated
    },
    setWalk: function() {
        this.canBreakTile = false;
        this.setCurrentAnimation("walk");
        this.vel.x = 0;
        this.setVelocity(this.initialxvel, this.initialyvel)
	

    },
    slide: function() {
        if (invisibility == true) {
            this.setCurrentAnimation("invisible_slide");
        }
        if (invisibility == false) {
            this.setCurrentAnimation("slide");
        }
        this.setVelocity(this.initialxvel, this.initialyvel)
    },
    megaJump: function() {

        /*
       When user touch the megajump object
        */
        me.audio.play("jump");
        if (speed == false) {
            this.setVelocity(this.initialxvel, this.initialyvel * (1.7));
            this.forceJump();
            this.setVelocity(this.initialxvel, this.initialyvel)
        }
        if (speed == true) {
            this.setVelocity(20, this.initialyvel * 2)
            this.forceJump();
            this.setVelocity(20, this.initialyvel)
        }

    },
    die: function() {
        me.audio.play("die");
        swStop("beg2")
        this.alive = false;
        invisibility = false;
        speed = false;
        me.gamestat.reset();
        me.levelDirector.reloadLevel();
        me.game.viewport.fadeOut("#000000", 6350);

	
    }
});

/*
All checkoint objects
 */
var Checkpoint = me.CollectableEntity.extend({
    init: function(a, c, b) {
        b.image = "check";
        b.spritewidth = 64;
        this.parent(a, c, b);
        this.addAnimation("uncheck", [0, 0]);
        this.addAnimation("check", [1, 1]);
        this.setCurrentAnimation("uncheck");
        this.autodestroy = false;
        checkpoint_array[b.id] = b.id;
        checkpointx_array[b.id] = a;
        checkpointy_array[b.id] = c - 50;

    },
    onCollision: function(res, obj) {
        /*
        On collision, save the position of checkpoint
        in array.
         */
        me.audio.play("checkpoint");
        for (var i = 1; i <= checkpoint_array.length; i++) {
            if ((obj.pos.x - 100 <= checkpointx_array[i]) && (obj.pos.x + 100 >= checkpointx_array[i])) {
                checkpoint_cookie = i;
                this.setCurrentAnimation("check");
            }
        }
        this.collidable = false;
    },
    update: function() {
        updated = this.updateMovement()
        if (updated) {
            this.parent();
            updated = true
        }
        return updated
    }
});

/*
All teleport objects
 */
var Teleport = me.CollectableEntity.extend({
    init: function(a, c, b) {
        b.image = "teleport_img";
        b.spritewidth = 64;
        this.parent(a, c, b);
        this.addAnimation("normal", [0, 0]);
        this.addAnimation("active", [1, 2, 3]);
        this.setCurrentAnimation("normal");
        this.autodestroy = false;
        teleport_array[b.id] = b.id;
        teleportx_array[b.id] = a;
        teleporty_array[b.id] = c;
        this.nameid = b.id;
    },
    onCollision: function(res, obj) {
        /*
        On colision, save teleport position in array.
         */
        for (var i = 0; i < (teleportx_array.length);
        (i = i + 2)) {
            if (this.nameid == i) {
                obj.pos.x = teleportx_array[i + 1]
                obj.pos.y = teleporty_array[i + 1]

            }
        }
        this.setCurrentAnimation("active");
    },
    update: function() {
        updated = this.updateMovement()
        if (updated) {
            this.parent();
            updated = true
        }
        return updated
    }
});
var SpeedEntity = me.CollectableEntity.extend({
    init: function(a, c, b) {
        b.image = "speed_right";
        b.spritewidth = 32;
        this.parent(a, c, b);
        this.autodestroy = false;
        this.type = "speed"
    }
});

var Visible = me.CollectableEntity.extend({
    init: function(a, c, b) {
        b.image = "visible";
        b.spritewidth = 32;
        this.parent(a, c, b);
        this.autodestroy = false;
        this.type = "Visible"
    }
});

var Invisible = me.CollectableEntity.extend({
    init: function(a, c, b) {
        b.image = "invisible";
        b.spritewidth = 32;
        this.parent(a, c, b);
        this.autodestroy = false;
        this.type = "Invisible"
    }
});


var SawEntity = me.CollectableEntity.extend({
    init: function(a, c, b) {
        this.parent(a, c, b);
        this.addAnimation("spin", [0, 1, 2, 3, 4, 5, 6, 7]);
        this.setCurrentAnimation("spin");
        this.autodestroy = false;
    }
});


var CollectEntity = me.CollectableEntity.extend({
    init: function(x, y, settings) {
        this.visibility = false;
        this.collidable = true;
        settings.spritewidth = "64";
        settings.width = "1";
        this.parent(x, y, settings);
        this.addAnimation("move", [0, 1, 2, 3, 4, 5]);
        this.addAnimation("collected", [6, 6]);
        this.setCurrentAnimation("move");
    },
    onCollision: function(res, obj) {
        /*
        On collision with coin, time decreases 5 secounds
         */
        if (res.y > 0 || (res.x < 64 && res.x > 0)) {
            me.audio.play("collect");
            swObj.time = swObj.time - 5000
            this.collidable = false;
            this.setCurrentAnimation("collected");
        }
    }

});

/*
For all horizontal movable blocks
 */
var MoveEntity = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.startX = x;
        this.endX = x + settings.width - settings.spritewidth;
        this.pos.x = x;
        this.walkLeft = true;
        this.setVelocity(1, 0);
        this.gravity = 0;
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("move", [0, 0]);
        this.setCurrentAnimation("move");
    },
    onCollision: function(res, obj) {
        /*
        On collision, dont let the player fall with gravity
        and move player within the block
         */
        if (obj.alive) {

            if (obj.jumping && res.y < 0) {
                obj.forceJump()
                obj.jumping = false;
                obj.falling = true;

            }
            if (obj.falling) {
                obj.pos.y = this.pos.y - 63;
                obj.vel.y = 0;
                obj.falling = false;
                if (invisibility == true) {
                    obj.setCurrentAnimation('invisible_stand')
                }
                if (invisibility == false) {
                    obj.setCurrentAnimation("stand");
                }

                if (res.y > 0) {
                    if (this.walkLeft) {
                        obj.pos.x = obj.pos.x - 1;
                    }
                    if (!this.walkLeft) {
                        obj.pos.x = obj.pos.x + 1;
                    }
                }

            } else {
                obj.vel.y = initialyvel;
            }

        }
    },
    update: function() {
        if (!this.visible) return false;
        if (this.alive) {
            if (this.walkLeft && this.pos.x <= this.startX) {
                this.walkLeft = false;
            } else if (!this.walkLeft && this.pos.x >= this.endX) {
                this.walkLeft = true;
            }
            this.doWalk(this.walkLeft);
        } else {
            this.vel.x = 0;
        }
        this.updateMovement();
        if (this.vel.x != 0) {
            this.parent(this);
            return true;
        }
        return false;
    }
});


/*
 For all vertical movable blocks
 */

var MoveEntity2 = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.starty = y;
        this.endy = y + settings.height
        this.pos.y = y;
        this.upp = false;
        this.setVelocity(0, 1);
        this.gravity = 0;
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("move", [0, 0]);
        this.setCurrentAnimation("move");
    },
    onCollision: function(res, obj) {
        /*
         On collision, dont let the player fall with gravity.
         */
        if (obj.alive) {
            if (obj.jumping && res.y < 0) {
                obj.forceJump()
                obj.jumping = false;
                obj.falling = true;
            }
            if (obj.falling) {
                obj.pos.y = this.pos.y - 63;
                obj.vel.y = 0;
                obj.falling = false;
                if (invisibility == true) {
                    obj.setCurrentAnimation('invisible_stand')
                }
                if (invisibility == false) {
                    obj.setCurrentAnimation("stand");
                }
            } else {
                obj.vel.y = initialyvel;
            }

        }
    },
    update: function() {

        if (this.upp) {
            this.vel.y = -1
        }
        if (!this.upp) {
            this.vel.y = 1
        }
        if (this.pos.y >= this.endy) {
            this.upp = true
        }
        if (this.pos.y <= this.starty) {
            this.upp = false
        }
        this.updateMovement();
        if (this.vel.y != 0) {
            this.parent(this);
            return true;
        }
        return false;
    }
});

var trigger = false;
var timer_trigger;
var triggg = 0
/*
Spike will only be trigger when the user
colides with SpikeTrigger object.
 */
var Spike = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.starty = y;
        this.endy = y + settings.height;
        this.pos.y = y;
        this.upp = false;
        this.timeout = 3000;
        this.setVelocity(0, 2);
        if (settings.hard == true) {
            this.setVelocity(0, 4);
            this.timeout = 1000
        }
        this.gravity = 0;
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("move", [0, 0]);
        this.setCurrentAnimation("move");
    },
    onCollision: function(res, obj) {
        obj.die()
    },
    update: function() {
        if (trigger == true) {
            this.vel.y = 10
            timer_trigger = setTimeout("trigg()", this.timeout);
        }
        if (trigger == false) {
            this.vel.y = -5
            if (this.pos.y < this.starty) {
                this.vel.y = 0
            }
        }

        this.updateMovement();
        if (this.vel.y != 0) {
            this.parent(this);
            return true;
        }
        return false;
    }
});

/*
Timer for the Spike after get trigger
 */
function trigg() {
    triggg++;
    if (triggg == 1) {
        trigger = false;
        triggg = 0
        clearTimeout(timer_trigger)
    }
}

var SpikeTrigger = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
    },
    onCollision: function(res, obj) {
        trigger = true;

    }


});

/*
Automatic Spikes ( goes up and down )
 */
var SpikeAuto = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.starty = y;
        this.endy = y + settings.height;
        this.pos.y = y;
        this.upp = false;
        this.variable = settings.speed;
        this.gravity = 0;
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("move", [0, 0]);
        this.setCurrentAnimation("move");
    },
    onCollision: function(res, obj) {
        obj.die()
    },
    update: function() {

        if (this.upp) {
            this.vel.y = -1
        }
        if (!this.upp) {
            this.vel.y = this.variable
        }
        if (this.pos.y >= this.endy) {
            this.upp = true
        }
        if (this.pos.y <= this.starty) {
            this.upp = false
        }
        this.updateMovement();
        if (this.vel.y != 0) {
            this.parent(this);
            return true;
        }
        return false;
    }
});

var JumpEntity = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("down", [0, 0]);
        this.addAnimation("up", [1, 2]);
        this.setCurrentAnimation("down");
    },
    onCollision: function(res, obj) {

        if (this.alive) {
            obj.megaJump();
            this.setCurrentAnimation("up", "down")

        }
    }


});


var JumpEntity2 = me.ObjectEntity.extend({
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;
        this.addAnimation("up", [0, 1, 2]);
        this.setCurrentAnimation("up");
    },
    onCollision: function(res, obj) {

        if (this.alive) {
            obj.megaJump();
        }
    }


});

var HurtEntity = me.InvisibleEntity.extend({
    init: function(a, c, b) {
        this.parent(a, c, b);
        this.collidable = true;
        this.visible = true;
        this.type = me.game.ENEMY_OBJECT;
    },
    onCollision: function(a, b) {
        b.die();
    }
});


var StartCount = me.InvisibleEntity.extend({
    init: function(a, c, b) {
        b.image = "none";
        b.spritewidth = "64";
        this.parent(a, c, b);
        this.collidable = true;
    },
    onCollision: function(a, b) {
        swStart('beg2', '+');
        me.game.remove(this)

    }
});


var Initial = me.ObjectEntity.extend({
    init: function(a, c, b) {
        b.image = "portal2";
        b.spritewidth = "96";
        this.parent(a, c, b);
    }
});

var InitialBlack = me.ObjectEntity.extend({
    init: function(a, c, b) {
        b.image = "portal";
        b.spritewidth = "96";
        this.parent(a, c, b);
    }
});

var Final = me.ObjectEntity.extend({
    init: function(a, c, b) {
        b.image = "portal2";
        b.spritewidth = "96";
        this.parent(a, c, b);
    }
});


var EndCount = me.ObjectEntity.extend({
    init: function(a, c, b) {
        b.image = "portal";
        b.spritewidth = "96";
        this.parent(a, c, b);
        this.collidable = true;
    },
    onCollision: function(a, b) {
        swStop('beg2')
        time_end = swObj.cycle;
        highscore = "true";
        level_time = "0";
        level_id = "01";
        document.cookie = 'level_time=' + "0" + '; expires=Thu, 2 Aug 2022 20:47:11 UTC; path=/';
        document.cookie = 'level_id=' + "01" + '; expires=Thu, 2 Aug 2022 20:47:11 UTC; path=/';
        goHome();
	
	
	
    }
});
