this.crehabjs = this.crehabjs || {};
(function(cjs, cr){	
	var mBgms = {};
	var mSound = {};
	var that;
	function UIManager(){
		that=this;
		throw "UIManager cannot be instantiated";
	};
	//--------------------------------------------------------------------
	var p = UIManager;	
	//--------------------------------------------------------------------
	p.addSpriteSheet=function(layer,imgName,w,h,x,y,objectId,gotoAndStop,scaleX,scaleY,count){
		var steps=new Array();
		for(var i=0;i<count;i++){
			steps["step"+(i+1)]=[i];
		}
		var _spriteSheet = new cjs.SpriteSheet({
			images: [layer.parent._library.getResult(imgName)],
			frames: {width:w, height:h, count:count, regX:w/2, regY:h/2, spacing:0, margin:0},
			animations:steps
		});
		
		var obj=new cjs.Sprite(_spriteSheet);
		obj.objectId=objectId;
		obj.imageName=imgName;
		obj.x =x;
		obj.y =y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		obj.gotoAndStop(gotoAndStop);
		layer.addChild(obj);
		return obj;
	};
	//--------------------------------------------------------------------
	p.createSpriteSheet=function(library,imgName,w,h,x,y,objectId,gotoAndStop,scaleX,scaleY,count){
		var steps=new Array();
		for(var i=0;i<count;i++){
			steps["step"+(i+1)]=[i];
		}
		var _spriteSheet = new cjs.SpriteSheet({
			images: [library.getResult(imgName)],
			frames: {width:w, height:h, count:count, regX:w/2, regY:h/2, spacing:0, margin:0},
			animations:steps
		});
		
		var obj=new cjs.Sprite(_spriteSheet);
		obj.objectId=objectId;
		obj.imageName=imgName;
		obj.x = x;
		obj.y = y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		obj.gotoAndStop(gotoAndStop);
		return obj;
	};
	//--------------------------------------------------------------------
	p.addText=function(layer,text,font,color,x,y,objectId,regX,regY){
		var obj=new cjs.Text( text, font, color);
		obj.x=x;
		obj.y=y;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.getMeasuredWidth()/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.getMeasuredHeight()/2;
		}
		layer.addChild(obj);
		return obj;
	}
	//--------------------------------------------------------------------
	p.createText=function(text,font,color,x,y,objectId,regX,regY){
		var obj=new cjs.Text( text, font, color);
		obj.x=x;
		obj.y=y;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.getMeasuredWidth()/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.getMeasuredHeight()/2;
		}
		return obj;
	}
	//--------------------------------------------------------------------
	p.createShape=function(mode,values,color,x,y,objectId,regX,regY,strokeStyle,beginStroke){
		var obj=new cjs.Shape();
		if (color!==undefined){
			obj.graphics.beginFill(color);
		}
		if (strokeStyle!==undefined){
			obj.graphics.setStrokeStyle(strokeStyle);
		}
		if (beginStroke!==undefined){
			obj.graphics.beginStroke(beginStroke);
		}
		if (mode==="drawRoundRect"){
			obj.graphics.drawRoundRect(values[0],values[1],values[2], values[3], values[4]);
		}else if (mode==="rect"){
			obj.graphics.rect(values[0],values[1],values[2],values[3]);
		}else if (mode==="drawCircle"){
			obj.graphics.drawCircle(values[0],values[1],values[2]);
		}
		obj.x=x;
		obj.y=y;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.graphics.command.w/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.graphics.command.h/2;
		}
		return obj;
	}
	//--------------------------------------------------------------------
	p.addShape=function(layer,mode,values,color,x,y,objectId,regX,regY,strokeStyle,beginStroke){
		var obj=new cjs.Shape();
		if (color!==undefined){
			obj.graphics.beginFill(color);
		}
		if (strokeStyle!==undefined){
			obj.graphics.setStrokeStyle(strokeStyle);
		}
		if (beginStroke!==undefined){
			obj.graphics.beginStroke(beginStroke);
		}
		if (mode==="drawRoundRect"){
			obj.graphics.drawRoundRect(values[0],values[1],values[2], values[3], values[4]);
		}else if (mode==="rect"){
			obj.graphics.rect(values[0],values[1],values[2],values[3]);
		}else if (mode==="drawCircle"){
			obj.graphics.drawCircle(values[0],values[1],values[2]);
		}
		obj.x=x;
		obj.y=y;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.graphics.command.w/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.graphics.command.h/2;
		}
		layer.addChild(obj);
		return obj;
	}
	//--------------------------------------------------------------------
	p.addImage=function(layer,imgName,x,y,scaleX,scaleY,rotation,objectId,regX,regY){
		var obj = new cjs.Bitmap(layer.parent._library.getResult(imgName));
		obj.x=x;
		obj.y=y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rotation=rotation;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.image.width/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.image.height/2;
		}
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		layer.addChild(obj);
		return obj;
	};
	//--------------------------------------------------------------------
	p.createImage=function(library,imgName,x,y,scaleX,scaleY,rotation,objectId,regX,regY){
		var obj = new cjs.Bitmap(library.getResult(imgName));
		obj.x=x;
		obj.y=y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rotation=rotation;
		obj.objectId=objectId;
		if (regX===undefined){ 
			obj.regX=obj.image.width/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.image.height/2;
		}
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		return obj;
	};
	//--------------------------------------------------------------------
	p.addContainer=function(layer,x,y,scaleX,scaleY,rotation,objectId,regX,regY){
		var obj=new cjs.Container();
		obj.x=x;
		obj.y=y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rotation=rotation;
		obj.objectId=objectId;
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		if (regX===undefined){ 
			obj.regX=obj.x/2;
		}
		if (regY===undefined){ 
			obj.regY=obj.y/2;
		}
		layer.addChild(obj);
		return obj;
	};
	//--------------------------------------------------------------------		
	p.addCrehabLib=function(layer,name,x,y,gotoAndStop,rotation,objectId){
		var card = new crehabLib[name];	 
		card.x=x;
		card.y=y;
		card.objectId=objectId;
		if (gotoAndStop!==undefined){
			card.gotoAndStop(gotoAndStop);
		}
		if (rotation!==undefined){
			card.rotation=rotation;
		}
		layer.addChild(card);
		return card;
	};
	//--------------------------------------------------------------------		
	p.createCrehabLib=function(name,x,y,gotoAndStop,rotation,objectId){
		var card = new crehabLib[name];	 
		card.x=x;
		card.y=y;
		card.objectId=objectId;
		if (gotoAndStop!==undefined){
			card.gotoAndStop(gotoAndStop);
		}
		if (rotation!==undefined){
			card.rotation=rotation;
		}
		return card;
	};
	//--------------------------------------------------------------------		
	p.addSprite=function(layer,baseSpriteSheet,x,y,objectId,gotoAndPlay,scaleX,scaleY){
		var obj=new cjs.Sprite(baseSpriteSheet);
		obj.objectId=objectId;
		obj.x =x;
		obj.y =y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		obj.gotoAndPlay(gotoAndPlay);
		layer.addChild(obj);
		return obj;
	};
	//--------------------------------------------------------------------		
	p.createSprite=function(baseSpriteSheet,x,y,objectId,gotoAndPlay,scaleX,scaleY){
		var obj=new cjs.Sprite(baseSpriteSheet);
		obj.objectId=objectId;
		obj.x =x;
		obj.y =y;
		obj.scaleX=scaleX;
		obj.scaleY=scaleY;
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=scaleX;
		obj.gotoAndPlay(gotoAndPlay);
		return obj;
	};
	//--------------------------------------------------------------------		
	p.updateRollOverOut=function(obj){
		obj.rolloverX=obj.x*0.9995;
		obj.rolloverY=obj.rolloverX;
		obj.rolloverScale=obj.scaleX*1.08;
		obj.rolloutX=obj.x;
		obj.rolloutY=obj.y;
		obj.rolloutScale=obj.scaleX;
	}
	// Finally
	cr.UIManager = UIManager;
}(createjs, crehabjs));