--
-- Project: main.lua
-- Description: 
--
-- Version: 1.0
-- Managed with http://CoronaProjectManager.com
--
-- Copyright 2014 Paul Graham. All Rights Reserved.
-- 

display.setStatusBar( display.HiddenStatusBar )
local composer = require "composer" 
local scene = composer.newScene()
function scene:create ( event )
	w = display.contentWidth
	h = display.contentHeight
	local menu_group = self.view
	--local bg = display.newImage(menu_group,"dark_bg.jpg")
--	bg.x = w/2
--	bg.y = h-bg.height/2
	logo = display.newImage(menu_group,"cave.png")
	logo.x = w/2
	logo.y = 300
	logo:scale(.6,.6)
	local rail = display.newImage(menu_group,"rail2.jpg")
	rail.x = w/2
	rail.y = h-rail.height/2

	local cart = display.newImage(menu_group,"Cart.png")
	cart.y = h-108
	cart2 = display.newImage(menu_group,"Cart2.png")
	cart2.y = h-108
	cart.x = -(cart.width)
	cart2.x =w+cart.width 
	local play_button = display.newImage(menu_group,"play.png")
	play_button.x = w/2
	play_button.y = h/2+130
	play_button:scale(.8,.8)
	local about = display.newImage(menu_group,"about.png")
	about.x = w/5
	about.y = h/2+180
	about:scale(.75,.75)
	local settings = display.newImage(menu_group,"settings.png")
	settings.x = w*4/5
	settings.y = h/2+180
	settings:scale(.75,.75)
	local auth =  display.newText(menu_group,"By Austin Graham and Katey Bluel", 0, 0, nil, 36)
	auth.x = w/2
	auth.y = h - 50
	
	--local bad_apple = display.newImage(menu_group,"bad_apple.png")
	--bad_apple.x = w/2
	--bad_apple.y = h/2
	--bad_apple:scale(.5,.5)
	
	--freeze = audio.loadSound("sounds/background_frozen.mp3") : placeholder background music
	--not included as it is form a sound track
	local roll = audio.loadSound("sounds/roll.mp3")
	--audio.setVolume( 1)
	audio.fade( { channel=1, time=1000, volume=1 } )
	audio.fade( { channel=2, time=1000, volume=1 } )
	--print(audio.getDuration(roll))
	--print(audio.getVolume({channel=3}))
	if (audio.getVolume({channel=3})==.3)then
		audio.fade( { channel=3, time=3000, volume=1 } )
	elseif (audio.getVolume({channel=3})==1)then
		background = audio.play(freeze,{ channel=3, loops=-1, fadein=3000 })
	else
		audio.fade( { channel=3, time=3000, volume=1 } )
	end
	print(audio.getVolume({channel=3}))

	local menu = true
	local next_scene=""
	local cart_num=1

	--audio.dispose(roll1)
	--audio.dispose(roll2)
	--roll1:removeSelf()
	--roll2:removeSelf()
	
	local function cart_decide()
		if menu == true then
			if cart_num==1 then
				cart_num=2
				cart.x = -cart.width
				roll1 = audio.play(roll,{channel=1})
				--transition.to(cart,{time = 1800, x=w+cart.width})
				--timer.performWithDelay ( 3000, cart_decide)
				--transition.to(cart,{time = 3690, x=w+cart.width})
				transition.to(cart,{time = 2800, x=w+cart.width})
				timer.performWithDelay ( 4000, cart_decide)
			else
				cart_num=1
				cart2.x =w+cart2.width
				roll2 = audio.play(roll,{channel=2})
				--transition.to(cart2,{time = 5000, x=-cart.width})
				--timer.performWithDelay ( 6000, cart_decide)
				transition.to(cart2,{time = 3690, x=-cart.width})
				timer.performWithDelay ( 6000, cart_decide)
			end

		end
	end

	--function cart_empty()
--		cart.x = -cart.width
--		transition.to(cart,{time = 1800, x=w+cart.width})
--		timer.performWithDelay ( 3000, cart_decide)
--	end

	--local function cart_full()
--		cart2.x =w+cart2.width
--		transition.to(cart2,{time = 5000, x=-cart.width})
--		timer.performWithDelay ( 6000, cart_decide)
--	end

local function change_scene()
		play_button:removeEventListener("tap", end_menu)
		about:removeEventListener("tap", go_about)
		settings:removeEventListener("tap", go_settings)
		transition.to(play_end,{time = 500, alpha = 0})
		if(next_scene=="about")then
			composer.gotoScene("about","crossFade",1000)
		elseif (next_scene =="settings")then
			composer.gotoScene("settings","crossFade",1000)
		elseif (next_scene =="miners")then
			composer.gotoScene("miners","crossFade",1000)
		end
	end

	function end_menu()
		menu = false
		play_button:removeEventListener("tap", end_menu)
		next_scene = "miners"
		play_end = display.newImage(menu_group,"play_active.png")
		play_end.x = w/2
		play_end.y = h/2+130
		play_end:scale(.8,.8)
		play_end.alpha = 0

		transition.to(play_end,{time = 500, alpha = 1})
		transition.to(about,{time = 500, alpha = 0})
		transition.to(settings,{time = 500, alpha = 0})
		transition.to(play_button,{time = 500, alpha = 0})
		timer.performWithDelay ( 600, change_scene)
	end
	
	function go_about()
		menu = false
		about:removeEventListener("tap", go_about)
		next_scene = "about"
		play_end = display.newImage(menu_group,"play_active.png")
		play_end.x = w/5
		play_end.y = h/2+180
		play_end:scale(.75,.75)
		play_end.alpha = 0
		transition.to(play_end,{time = 500, alpha = 1})
		transition.to(about,{time = 500, alpha = 0})
		transition.to(settings,{time = 500, alpha = 0})
		transition.to(play_button,{time = 500, alpha = 0})
		--transition.to(menu_group,{time = 500, alpha = 0})
		timer.performWithDelay ( 600, change_scene)
	end
	
	function go_settings()
		menu = false
		settings:removeEventListener("tap", go_settings)
		next_scene = "settings"
		play_end = display.newImage(menu_group,"play_active.png")
		play_end.x = settings.x
		play_end.y = settings.y
		play_end:scale(.75,.75)
		play_end.alpha = 0
		transition.to(play_end,{time = 500, alpha = 1})
		transition.to(about,{time = 500, alpha = 0})
		transition.to(settings,{time = 500, alpha = 0})
		transition.to(play_button,{time = 500, alpha = 0})
		--transition.to(menu_group,{time = 500, alpha = 0})
		timer.performWithDelay ( 600, change_scene)
	end


	local function activate()
		play_button:addEventListener("tap", end_menu)
		about:addEventListener("tap", go_about)
		settings:addEventListener("tap", go_settings)
		composer.removeHidden()
	end
	timer.performWithDelay ( 1000, activate)
	timer.performWithDelay (1000,cart_decide)

end


function scene:show(event) 
	local menu_group = self.view 
	transition.to(menu_group,{time = 500, alpha = 1})

end
function scene:hide(event) 
	local menu_group = self.view 

end
function scene:destroy(event) 
	local menu_group = self.view 
	menu_group:removeSelf()
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
