this.crehabjs = this.crehabjs || {};
(function(cjs, cr){	
	var mBgms = {};
	var mSound = {};
	var that;
	function Common(){
		that=this;
		throw "Common cannot be instantiated";
	};
	//--------------------------------------------------------------------
	var p = Common;	
	//--------------------------------------------------------------------
	//--------------------------------------------------------------------
	p.getSeriesNum=function(startNum,endNum){
		var ran=new Array();
		for(var i=0;i<endNum-startNum;i++){
			ran[i]=startNum+i;
		}
		var result=this.getRandomArray(ran);
		return result;
	};
	//--------------------------------------------------------------------
	p.getRandomArray=function(array){
		var arr=array.slice(0);
		var len=arr.length;
		var result=new Array();
		while(len>0){
			var j=Math.floor(Math.random()*1000000%len);
			result.push(arr[j]);
			if (j>-1) {
				arr.splice(j, 1);
			}
			len--;
		}
		return result;
	};
	//--------------------------------------------------------------------
	p.getRandomArraySeriesNum=function(startNum,endNum){
		var ran=new Array();
		for(var i=0;i<endNum-startNum;i++){
			ran[i]=startNum+i;
		}
		var result=this.getRandomArray(ran);
		var ranResult=p.getRandomArray(result);
		return ranResult;
	};
	//--------------------------------------------------------------------
	p.addBasicEvent=function(objs,inThat){
		if (inThat===undefined){
			if (Array.isArray(objs)==true){
				inThat=objs[0].parent.parent;
			}else{
				inThat=objs.parent.parent;
			}
		}
		if (Array.isArray(objs)==true){
			for(var i=0;i<objs.length;i++){
				objs[i].on('click', inThat.onClick, inThat);
				objs[i].on('rollover', inThat.onRollover,inThat);
				objs[i].on('rollout', inThat.onRollout,inThat);
			}
		}else{
			objs.on('click', inThat.onClick, inThat);
			objs.on('rollover', inThat.onRollover,inThat);
			objs.on('rollout', inThat.onRollout,inThat);
		}
	};
	
	// Finally
	cr.Common = Common;
}(createjs, crehabjs));

// Restore session state onto crehabjs, then wrap it with a Proxy
// so cr.Common / cr.UIManager still work, and cr.round = 1 auto-persists.
(function () {
    var _state = JSON.parse(sessionStorage.getItem('cr_state') || '{}');
    for (var k in _state) { crehabjs[k] = _state[k]; }
})();
var cr = new Proxy(crehabjs, {
    set: function (target, prop, value) {
        target[prop] = value;
        try {
            var _state = JSON.parse(sessionStorage.getItem('cr_state') || '{}');
            _state[prop] = value;
            sessionStorage.setItem('cr_state', JSON.stringify(_state));
        } catch (e) {}
        return true;
    },
    get: function (target, prop) {
        return target[prop];
    }
});