
var initObj;
var Ymmp = YUI( {
	timeout :10000
});
//mmp_uniq_player

Ymmp.use("base", "node", "event", "console", "anim", function(Y) {
		
	//player model
	function PlayerModel(){
		PlayerModel.superclass.constructor.apply(this, arguments);
		//Init Event
		Y.on("key", function(e){if(!this.get("animated")){this.move(3);}}, document, "down:39", this);
		Y.on("key", function(e){this.clearMove()}, document, "up:39", this);
		
		Y.on("key", function(e){if(!this.get("animated")){this.move(-3);}}, document, "down:37", this);
		Y.on("key", function(e){this.clearMove()}, document, "up:37", this);
		//Jump
		Y.on("key", function(e){this.jump()}, document, "down:38", this);
		Y.on("key", function(e){this.stopJump();}, document, "up:38", this);
		//Shoot
		Y.on("key", function(e){this.shoot()}, document, "press:32", this);
		//
		this.prepare();
	}
	
	PlayerModel.NAME = "playermodel";
	
	PlayerModel.ATTRS = {
		playerHtmlNode : {
			value : Y.get('#mmp_uniq_player')
		},
		playerHtmlMaskNode : {
			value : Y.get('#mask_mmp_uniq_player')
		},
		animBase : {
			value : 'modelX'
		},
		animStandByRight:{
			value : 'modelX_standby_right'
		},
		animStandByLeft:{
			value : 'modelX_standby_left'
		},
		animMoveRight : {
			value : 'modelX_moveright'
		},
		animMoveLeft : {
			value : 'modelX_moveleft'
		},
		animShootRight : {
			value : 'modelX_shootright'
		},
		animShootLeft : {
			value : 'modelX_shootleft'
		},
		animMoveJumpRight : {
			value : 'modelX_jumpright'
		},
		animMoveJumpLeft : {
			value : 'modelX_jumpleft'
		},
		animCurrent : {
			value : 'modelX_moveright'
		},
		timer : {
			value : null
		},
		timerToGround : {
			value : null
		},
		animTweenMove : {
			value : null
		},
		animated : {
			value : false
		},
		initJump : {
			value : 0
		},
		lastPos:{
			value : []
		},
		moveX:{
			value:0
		},
		moveY:{
			value:0
		},
		lastMove:{
			value:'Right'
		},
		isfire:{
			value:false
		}
	}
	
	Y.extend(PlayerModel, Y.Base, {
		prepare:function(){
			this.get("playerHtmlNode").addClass(this.get("animBase"));
			this.setAnim("animStandByRight");
			this.set("lastMove", "Right");
			this.set("animated", false);
			this.set("animTweenMove",  new Y.Anim({}));
			this.set("timer",Y.later((1000/24), this, 'animRun' , [], true));
			this.launchTimerGround();
		},
		setAnim:function(animId){
			this.get("playerHtmlNode").setAttribute("class", "");
			this.get("playerHtmlNode").addClass(this.get("animBase"));
			this.set("animCurrent", animId);
			this.get("playerHtmlNode").addClass(this.get(animId))
		},
		launchTimerGround:function(){
			this.set("timerToGround",Y.later(50, this, 'animToGround' , [], true));	
		},
		stopTimerGround:function(){
			if(!Y.Lang.isNull(this.get("timerToGround"))){
				this.get("timerToGround").cancel();
			}
		},
		removeAnim:function(animId){
			this.get("playerHtmlNode").removeClass(this.get(animId));
			this.setAnim("animStandBy"+this.get("lastMove"));
			this.set("animated", false);
			this.set("isfire", false);
		},
		move:function(valuePos){
			this.set("moveX", valuePos);
			this.set("animated", true);	
			//ANIMS
			if(valuePos > 0){
				this.setAnim("animMoveRight");
				this.set("lastMove", "Right");
			}else {
				this.setAnim("animMoveLeft");
				this.set("lastMove", "Left");
			}		
		},
		clearMove:function(){
			this.removeAnim(this.get("animCurrent"));
			this.set("moveX", 0);
			this.set("moveY", 0);
		},
		shoot:function(){
			var animID = "";
			if(this.get("isfire")){
				return;
			}
			this.set("isfire", true);
			//create DOM element
			var _elementBullet = Y.get(document.createElement("div"));
			//create BULLET animation
			var _animElementBullet = new Y.Anim({
				node : _elementBullet,				
				duration : 2,
				easing : Y.Easing.easeNone
			});
			
			//swith left or right
			if(this.get("lastMove") == "Right"){
				animID = "animShootRight";
				_elementBullet.addClass("bullet_right");
				_animElementBullet.set("to", {left: function(node){return node.getX()+800;}});
			}else {
				animID = "animShootLeft";
				_elementBullet.addClass("bullet_left");
				_animElementBullet.set("to", {left: function(node){return node.getX()-800;}});
			}
			
			this.setAnim(animID);
			
			//change anim in 500 ms
			Y.later(500, this, 'removeAnim' , [animID], false);
			//INIT bullet
			_elementBullet.addClass("bullet");
			_elementBullet.setXY(this.get("playerHtmlNode").getXY());
			Y.get("#level").appendChild(_elementBullet);
			//Bullet ontween to hitest
			_animElementBullet.on('tween', function() {
				if(!initObj.get("mapObj").checkPosition(this.get('node'))){
					this.stop();
				}
			});
			//Bullet onend to remove from dom
			_animElementBullet.on("end", function(){
				var node = this.get('node');
				node.get('parentNode').removeChild(node);
			});
			//RUN Anim
			_animElementBullet.run();
		},
		jump:function(to){
			
		  if(!this.get("animTweenMove").get("running") ){
			this.stopTimerGround();						
			this.set("animTweenMove",  new Y.Anim({
				node : '#mmp_uniq_player',
				to:{
					 top: function(node) { 
						return node.getY() - 100;
					}
				},
				duration : 0.5,
				easing : Y.Easing.easeOut
			}));
			
			this.set("lastPos", this.get("animTweenMove").get("node").getXY());
			this.get("animTweenMove").on('tween', function() {
				if(this.get("lastMove") == "Right"){
					this.setAnim("animMoveJumpRight");
				}else {
					this.setAnim("animMoveJumpLeft");
				}
				if(!initObj.get("mapObj").checkPosition(this.get("playerHtmlNode"))){
					this.get("animTweenMove").detachAll("end");
					this.get("animTweenMove").stop();
					this.get("animTweenMove").get("node").setXY(this.get("lastPos"));
					this.get("animTweenMove").get("node").setStyle("left",Math.round(this.get("animTweenMove").get("node").getX())+"px")	
					this.get("animTweenMove").get("node").setStyle("top", Math.ceil(this.get("animTweenMove").get("node").getY())+"px")
					this.downToGround();
				}else{				
					this.set("lastPos", this.get("animTweenMove").get("node").getXY());
				}
			}, this);
			
			this.get("animTweenMove").on('end', function() { 
				this.downToGround();
			}, this);
			
			this.get("animTweenMove").run();
		 }
		},
		stopJump:function(){
			if(this.get("animTweenMove").get("running") ){
				this.get("animTweenMove").stop();
			}
		},
		downToGround : function(){
			
		  if(!this.get("animTweenMove").get("running") ){
			
			this.set("animTweenMove",  new Y.Anim({
				node : '#mmp_uniq_player',
				to:{ top: initObj.get("mapObj").get("height")},
				duration : 0.40,
				easing : Y.Easing.easeIn
			}));

			this.get("animTweenMove").on('tween', function() {
				if(!initObj.get("mapObj").checkPosition(this.get("animTweenMove").get("node"))){
					this.get("animTweenMove").stop();
					var element = initObj.get("mapObj").getPosition(this.get("animTweenMove").get("node"));	
					this.get("animTweenMove").get("node").setY(element.getY()-this.get("animTweenMove").get("node").get("offsetHeight")-1);
				}
			}, this);
			this.get("animTweenMove").on('start', function() {
				this.launchTimerGround();
			}, this);
			this.get("animTweenMove").on('end', function() {
				if(this.get("moveX") > 0){
					this.setAnim("animMoveRight");
				}else if(this.get("moveX") < 0){
					this.setAnim("animMoveLeft");
				}else{
					this.removeAnim(this.get("animCurrent"));
				}
			}, this);
			this.get("animTweenMove").run();
			
			}
		},
		animRun : function(){	

			if(this.get("playerHtmlNode").getX()<-35){
				this.get("playerHtmlNode").setX(Y.get("#level").get('offsetWidth'))	
			}
			if(this.get("playerHtmlNode").getX()>Y.get("#level").get('offsetWidth')+2){
				this.get("playerHtmlNode").setX(-34)	
			}
			var lastPos = this.get("playerHtmlNode").getXY();
			this.get("playerHtmlNode").setX(this.get("playerHtmlNode").getX()+this.get("moveX"));
			this.get("playerHtmlNode").setY(this.get("playerHtmlNode").getY()+this.get("moveY"));
			if(!initObj.get("mapObj").checkPosition(this.get("playerHtmlNode"))){
				this.get("playerHtmlNode").setXY(lastPos);
			}
			this.get("playerHtmlMaskNode").setXY(this.get("playerHtmlNode").getXY());
			
		},
		animToGround : function(){
			if(!this.get("animTweenMove").get("running") ){
				if(initObj.get("mapObj").checkPosition(this.get("playerHtmlMaskNode"))){
					this.downToGround();
				}
			}	
		}
		
	});

	// Map Controller
	function MapController() {

			MapController.superclass.constructor.apply(this, arguments);
			this.prepare();
		}

		MapController.NAME = "mapcontroller";

		MapController.ATTRS = {
			width : {
				value :'723'
			},
			height : {
				value :'223'
			},
			descriptionLevel:{
				value:null
			}
		}

		Y.extend(MapController, Y.Base, {
			prepare : function() {
				var body = Y.get(document.body);
				this.set("width", Y.get("#level").get('offsetWidth'));
				this.set("height", Y.get("#level").get('offsetHeight'));
				var boxes = Y.all("#level .boxeslevel");
				this.set("descriptionLevel", boxes);	
			},
			checkPosition:function(element){
				for(var i=0; i<this.get("descriptionLevel").size(); i++){
					var item = this.get("descriptionLevel").item(i);
					//in X pos with item
					if(item.inRegion(element)){
						return false;
					}
				}
				return true;
			},
			getPosition:function(element){
				for(var i=0; i<this.get("descriptionLevel").size(); i++){
					var item = this.get("descriptionLevel").item(i);
					//in X pos with item
					if(item.inRegion(element)){
						return item;
					}
				}
				return false;
			}
		});


	// Init Controller 
	function InitController() {
		InitController.superclass.constructor.apply(this, arguments);
	}

	InitController.NAME = "initcontroller";

	InitController.ATTRS = {
		url:{
			value:null
		},
		mapObj : {
			value : null
		},
		playerObj : {
			value : null
		}
	}

	Y.extend(InitController, Y.Base, {
		initGame : function(){
			this.set("mapObj", new MapController());
			this.set("playerObj",  new PlayerModel());
		},
		getUrl : function() {
			if (this.get("url") == null) {
				var linksNodeList = Y.all("link");
				for ( var i = 0; i < linksNodeList.size(); i++) {
					var link = linksNodeList.item(i).getAttribute('href');
					if (link.indexOf("css") >=0 && link.indexOf("http://") !==0) {
						this.set("url", link.substring(0, link.indexOf("css")));
						break;
					}
				}
			}
			return this.get("url");
		}
	});

	//Launch on domready
	Y.on("domready", function() {
	  	/*var basic = new Y.Console({ 
			    boundingBox: '#basic' 
			}).render(); // note the inline render()*/
		initObj = new InitController();
		initObj.initGame();
	});
});
