var g_volumeState = SS.VOLUME_STATE.VOLUME_0;

var VolumeToggle = cc.Layer.extend({
	_toggler:null,
	_menu:null,
	
	ctor:function () {
		this._super();
		this.init();
	},
	init:function () {
		var winSize = cc.director.getWinSize();
		
		var item1 =  new cc.MenuItemImage( res.volume1_png  );
		var item2 =  new cc.MenuItemImage( res.volume4_png  );

		//var volume = cc.audioEngine.getMusicVolume();
		var g_volumeState1 = cc.sys.localStorage.getItem("volume");
		if(cc.sys.localStorage.getItem("volume"))
		{
			g_volumeState1 = cc.sys.localStorage.getItem("volume");
			console.log("g_volumeState1 == " + g_volumeState1);
		}
		else
		{
			g_volumeState1 = 0;
			cc.sys.localStorage.setItem("volume", "0");
			console.log("g_volumeState122 == " + g_volumeState1);
		}

		this._toggler = new cc.MenuItemToggle.create(item1,item2,this.ChangeVolume, this);

		switch (g_volumeState1) {
			case "1":
				this._toggler.setSelectedIndex(1);
				g_volumeState =SS.VOLUME_STATE.VOLUME_1;
				cc.audioEngine.pauseMusic();
				console.log("g_volume  1");
				break;
			case "0":
				this._toggler.setSelectedIndex(0);
				g_volumeState =SS.VOLUME_STATE.VOLUME_0;
				cc.audioEngine.resumeMusic();
				console.log("g_volume  2");
				break;
			default:
				this._toggler.setSelectedIndex(0);
			break;
			}
		
		
		this._menu = new cc.Menu(this._toggler);
		this._menu.attr({
			anchorX:0,
			anchorY:0,
			x:64,
			y:700
		});
		this.addChild(this._menu);
	},

	ChangeVolume:function(){
		cc.audioEngine.playEffect(res.play_on_mp3);
		if(g_volumeState == SS.VOLUME_STATE.VOLUME_1){
			g_volumeState =SS.VOLUME_STATE.VOLUME_0;
			cc.audioEngine.resumeMusic();
			cc.sys.localStorage.setItem("volume", "0");

			if (!cc.audioEngine.isMusicPlaying()) {
				cc.audioEngine.playMusic(res.Chapter_11_music_wav, true);
			}

			console.log("volume 0");
		}else if(g_volumeState == SS.VOLUME_STATE.VOLUME_0){
			g_volumeState =SS.VOLUME_STATE.VOLUME_1;
        		cc.audioEngine.pauseMusic();
			cc.sys.localStorage.setItem("volume", "1");

			console.log("volume 1");
		}
	}
});