this.crehabjs = this.crehabjs || {};
(function(cjs, cr){	
	var mBgms = {};
	var mSound = {};
	function SoundManager(){
		throw "Sound cannot be instantiated";
	};
	//--------------------------------------------------------------------
	var p = SoundManager;	
	//--------------------------------------------------------------------
	p.playSound = function(pId, pLoopInt){
		if(pLoopInt != 0 && pLoopInt != -1) {
			pLoopInt = 0;
		}
		mSound[pId] = cjs.Sound.play(pId, cjs.Sound.INTERRUPT_EARLY, 0, 0, pLoopInt);
	};
	//--------------------------------------------------------------------
	p.stopAllSound = function(){
		for(var key in mSound){
			this.stopMS(key);
		}
	};
	//--------------------------------------------------------------------
	p.stopMS = function(pId){
		if(mSound.hasOwnProperty(pId) && mSound[pId].playState !== cjs.Sound.PLAY_FINISHED){
			TweenLite.to(mSound[pId], 0.1, {
				volume:0,
				onCompleteParams:[mSound[pId]],
				onComplete:function(pSound){
					pSound.stop();
				}
			});
		}
	};
	//--------------------------------------------------------------------
	p.playBgm = function(pId,volume){
		if(mBgms.hasOwnProperty(pId) && mBgms[pId].playState !== cjs.Sound.PLAY_FINISHED){
			return;
		} else if(mBgms.hasOwnProperty(pId) && mBgms[pId].playState == cjs.Sound.PLAY_FINISHED){
			mBgms[pId].play({loop:-1,volume:volume});
		} else if(!mBgms.hasOwnProperty(pId)){
			mBgms[pId] = cjs.Sound.play(pId, cjs.Sound.INTERRUPT_EARLY, 0, 0, -1,volume);
		}
	};
	//--------------------------------------------------------------------
	p.stopBgm = function(pId){
		if(mBgms.hasOwnProperty(pId) && mBgms[pId].playState !== cjs.Sound.PLAY_FINISHED){
			TweenLite.to(mBgms[pId], 0.6, {
				volume:0,
				onCompleteParams:[mBgms[pId]],
				onComplete:function(pSound){
					pSound.stop();
				}
			});
		}
	};
	//--------------------------------------------------------------------
	p.stopAllBgm = function(){
		for(var key in mBgms){
			this.stopBgm(key);
		}
	};
	//--------------------------------------------------------------------
	p.setBgmVolume = function(pId, pVolume){
		if(mBgms.hasOwnProperty(pId) && mBgms[pId] !== cjs.Sound.PLAY_FINISHED){
			TweenLite.to(mBgms[pId], 1.0, {volume:pVolume});
		}
	};
	//--------------------------------------------------------------------
	p.getDuration = function(pId){
		var instance = createjs.Sound.createInstance(pId);
		return instance.duration/1000;
	}
	//--------------------------------------------------------------------
	p.getAllSounds = function(){
		return mSound;
	}
	// Finally
	cr.SoundManager = SoundManager;
}(createjs, crehabjs));