var goStartMenu = function (game, options, api) {
	var self = {};
    self.name = "menu";
    var button = null;
	var pausetext = null;
	var menuBG = null;
	var menuTitle = null;
	var menuHelp = null;
	var menuCredits = null;
	var menuBtnMenu = null;
	var onCredits = null;
	var onHelp = null;
	var fishdanceMusic = null;

	var startGame = false;
	
	
	self.getObject = function () {
        return enemy;
    }
	self.setInitialPosition = function (point) {
		startPos = point;
	}
	self.preload = function () {
		game.load.image('menuBG', 'resources/images/MainMenu/menun_tausta.png');
		game.load.image('menuTitle', 'resources/images/MainMenu/title.png');
		game.load.image('menuHelp', 'resources/images/MainMenu/help screen lopullinen oikeesti.png');
		game.load.image('menuCredits', 'resources/images/MainMenu/credits.png');

		//spritesheets
		game.load.spritesheet('menuBtnPlay', 'resources/images/MainMenu/button_play.png',316,316,2);
		game.load.spritesheet('menuBtnHelp', 'resources/images/MainMenu/button_help.png',316,316,2);
		game.load.spritesheet('menuBtnCredits', 'resources/images/MainMenu/button_credits.png',316,316,2);
		game.load.spritesheet('menuBtnMenu', 'resources/images/MainMenu/nuoli takaisin.png',316,316,2);
		
		game.load.audio('fishdanceMusic', 'resources/music/fish-dance.ogg');
    }
	
	self.create = function() {
		onCredits=false;
		onHelp=false;
		menuBG = game.add.sprite(0, 0, 'menuBG' );
		menuTitle = game.add.sprite(0, 0, 'menuTitle' );
		
		fishdanceMusic = game.add.audio('fishdanceMusic');


		///////////////////////////
		//Buttons functionalities//
		///////////////////////////

		//play-button
		menuBtnPlay = game.add.button(63, 380, 'menuBtnPlay', self.actionOnClick);
		menuBtnPlay.frame = 0;
		menuBtnPlay.onInputOver.add(self.over, this);
		menuBtnPlay.onInputOut.add(self.out, this);
		//Help-button
		menuBtnHelp = game.add.button(475, 385, 'menuBtnHelp', self.actionOnClick);
		menuBtnHelp.frame = 0;
		menuBtnHelp.onInputOver.add(self.over, this);
		menuBtnHelp.onInputOut.add(self.out, this);
		//Credits-button
		menuBtnCredits = game.add.button(898, 380, 'menuBtnCredits', self.actionOnClick);
		menuBtnCredits.frame = 0;
		menuBtnCredits.onInputOver.add(self.over, this);
		menuBtnCredits.onInputOut.add(self.out, this);
		
		
	}
	self.update = function () {
	    return startGame;
    }
	// On click action and menu management
	self.actionOnClick = function(button) {
		menuBtnPlay.kill();
		menuBtnHelp.kill();
		menuBtnCredits.kill();
		menuTitle.kill();
		menuBG.kill();
		
		
		if(button==menuBtnHelp){
			menuHelp = game.add.sprite(0, 0, 'menuHelp' );
			//Menu-button
			menuBtnMenu = game.add.button(game.width-83, 20, 'menuBtnMenu', self.actionOnClick);
			menuBtnMenu.frame = 0;
			menuBtnMenu.onInputOver.add(self.over, this);
			menuBtnMenu.onInputOut.add(self.out, this);
			onHelp=true;

		}else if(button==menuBtnCredits){
			menuCredits = game.add.sprite(0, 0, 'menuCredits' );
			//Menu-button
			menuBtnMenu = game.add.button(game.width-83, 20, 'menuBtnMenu', self.actionOnClick);
			menuBtnMenu.frame = 0;
			menuBtnMenu.onInputOver.add(self.over, this);
			menuBtnMenu.onInputOut.add(self.out, this);
			onCredits=true;

			api.stopCurrentMusic();
			fishdanceMusic.play();

		}else if(button==menuBtnMenu){
		    if (onCredits == true) {
		        api.playCurrentMusic();
		        fishdanceMusic.stop();
				menuCredits.kill();
			}else if(onHelp==true){
				menuHelp.kill();
			}
			onCredits=false;
			onHelp=false;
			menuBtnMenu.kill();
			self.create();
		} else {
		    startGame = true;
		}
	}
	self.over = function(button) {
		button.frame = 1;
	}
	self.out = function(button) {
		button.frame = 0;
	}
	return self;
}
