AnimalsGame.Section = new Class({
	
	initialize: function(opts) {
		this.sectionName = opts['sectionName'];
		this.game = opts['game'];
		
		this.englishTitle = opts['sectionConfig'].english;
		this.hudsonTitle  = opts['sectionConfig'].hudson;
		this.levelsConfig = opts['sectionConfig'].levels;
		this.levels = [];
		
		this.animals = opts['animalsConfig'];
		
		this.setupStage();
		this.setupLevel();
	},
	
	setupStage: function() {
		var this_ = this;
		
		this.stageElements = [
			
			// background
			new AnimalsGame.Actor({
				filename: this.sectionName,
				extension: 'jpg',
				path: 'assets/images/backgrounds',
				position: { left: '0%', top: '0%' },
				dimensions: AnimalsGame.dimensions,
				interactive: false
			}),
			
			// section title
			new AnimalsGame.StageElement({
				element:  $E('h2').addClass('section-title').html('<span>' + this.hudsonTitle + '</span>')._,
				interactive: false
			}),
			
			// meta buttons
			new AnimalsGame.Actor({
				filename: 'home',
				path: 'assets/images/buttons',
				position: { left: '2%', top: '91%' },
				dimensions: AnimalsGame.buttonsDimensions,
				click: this.returnToHome.bind(this)
			})
		];
	},
	
	triggerLevelFinish: function() {
		var this_ = this;
		
		var endLevelDialogNumber = Math.random(1, 2);
		
		game.soundEffect.play('end-level-' + endLevelDialogNumber);
		
		var setupLevelOrDisplaySectionFinish;
		if (Progress.sections[ this.sectionName ].currentLevel < Config.SECTIONS[ this.sectionName ].levels.length) {
			Progress.sections[ this.sectionName ].currentLevel++;
			
			setupLevelOrDisplaySectionFinish = this.setupLevel;
		} else {
			Progress.sections[ this.sectionName ].completed = true;
			Progress.sections[ this.sectionName ].currentLevel = 1;
			
			setupLevelOrDisplaySectionFinish = this.displaySectionFinish;
		}
		
		this.veil = new AnimalsGame.StageElement({
			element:  $E('div')._,
			className: 'veil',
			interactive: false
		});
		this.dialog = new AnimalsGame.Actor({
			filename: 'end-level',
			path: 'assets/images/dialogs',
			className: 'dialog end-level-dialog',
			click: this.handleNextLevel.bind(this, setupLevelOrDisplaySectionFinish),
			autoAppend: false,
			children: [
				new AnimalsGame.Actor({
					filename: 'continue',
					path: 'assets/images/buttons',
					filenameHover: 'continue-hover',
					className: 'dialog-button end-level-dialog-button',
					interactive: false,
					autoAppend: false
				}),
				new AnimalsGame.Actor({
					filename: 'stars',
					path: 'assets/images/dialogs',
					className: 'dialog-stars end-level-dialog-stars',
					interactive: false,
					autoAppend: false
				}),
				new AnimalsGame.StageElement({
					element:  $E('h2').html( endLevelDialogNumber === 1 ? 'ᓈᒻᒪᓈᕐᐳᑎᑦ' : 'ᐱᓯᑎᐅᕗᑎᑦ' )._,
					className: 'dialog-text end-level-dialog-text',
					interactive: false,
					autoAppend: false
				})
			]
		});
		
		AnimalsGame.OnloadObserver.queueHandler((function() {
			$(AnimalsGame.STAGE).append(this.dialog.element);
		}).bind(this));
		
		this.stageElements.push(this.veil);
		this.stageElements.push(this.dialog);
	},
	
	displaySectionFinish: function() {
		var this_ = this;
		
		game.soundEffect.play('good-section');
		
		this.veil = new AnimalsGame.StageElement({
			element:  $E('div')._,
			className: 'veil',
			interactive: false
		});
		this.dialog = new AnimalsGame.Actor({
			filename: 'end-level',
			path: 'assets/images/dialogs',
			className: 'dialog end-section-dialog',
			click: this.returnToHome.bind(this),
			autoAppend: false,
			children: [
				new AnimalsGame.Actor({
					filename: 'continue',
					path: 'assets/images/buttons',
					filenameHover: 'continue-hover',
					className: 'dialog-button end-section-dialog-button',
					interactive: false,
					autoAppend: false
				}),
				new AnimalsGame.Actor({
					filename: 'stars',
					path: 'assets/images/dialogs',
					className: 'dialog-stars end-section-dialog-stars',
					interactive: false,
					autoAppend: false
				}),
				new AnimalsGame.StageElement({
					element:  $E('h2').html('ᑌᒫᑦᓯᐊᖅ')._,
					className: 'dialog-text end-section-dialog-text',
					interactive: false,
					autoAppend: false
				})
			]
		});
		
		AnimalsGame.OnloadObserver.queueHandler((function() {
			$(AnimalsGame.STAGE).append(this.dialog.element);
		}).bind(this));
		
		this.stageElements.push(this.veil);
		this.stageElements.push(this.dialog);
	},
	
	returnToHome: function() {
		this.destroy();
		this.game.setupHomeScreen();
	},
	
	handleNextLevel: function(callback) {
		this.currentLevel.destroy();
		
		this.veil.destroy();
		this.dialog.destroy();
		
		callback.call(this);
	},
	
	setupLevel: function() {
		var this_ = this;
		
		var currentLevelNumber = Progress.sections[ this.sectionName ].currentLevel;
		
		this.currentLevel = new AnimalsGame['LevelType' + this.levelsConfig[ currentLevelNumber ].type]({
			questionType: this.levelsConfig[ currentLevelNumber ].type,
			section: this,
			sectionName: this.sectionName,
			difficulty: this.levelsConfig[ currentLevelNumber ].difficulty,
			timerUnitDuration: this.levelsConfig[ currentLevelNumber ].timer_duration,
			levelNumber: currentLevelNumber
		});
	},
	
	destroy: function() {
		this.stageElements.each('destroy');
		this.currentLevel.destroy();
	}
	
});
