/*

Pages are accessed by their indexes, which are always non-negative. 

"Subpages" are separated by slashes ("/"). So "4/12" means the 13th foto (remember, indexing is zero based) in 
the 5th gallery.

It's possible in the future that galleries may have sub galleries, so we could have a path like "Flowers.Roses.7" which
would translate to the 7th foto in the sub-gallery named "Roses" in the gallery named "Flowers".

*/

// ------------------------------------------------------------------------------------------------------------
//	constants
// ------------------------------------------------------------------------------------------------------------

// Note: IE barfs if 'const' is used instead of 'var'

var kFadeDuration = 750
var kImageMargin = 80 // should be an even number

var globalz = undefined// we'll attach globals to g

// ------------------------------------------------------------------------------------------------------------
//	initPage
// ------------------------------------------------------------------------------------------------------------
function initPage()
{
	globalz = new Object()
	
	globalz.initialized = true
	globalz.currentPath = undefined
	
	var initialPath = pathFromURL()
	var galleryIndex = initialPath.split(kSeparator)[0]
	
	
	// ** hide the page while we build it.
	globalz.pagewrap = $('pagewrap')
	globalz.pagewrap.store('fader', new Fx.Tween(globalz.pagewrap, {property:'opacity', duration: kFadeDuration, fps: 30}))

	// prepare entry cluster
	globalz.entryCluster 			= $('entry-cluster')
	globalz.entryImage 				= $('entry-image')
	globalz.entryCaption 			= $('entry-caption')
	globalz.entryCaptionText 		= $('caption-text')
	globalz.entryCaptionTextShadow 	= $('caption-text-shadow')
	globalz.entryCluster.setOpacity(0)	
	globalz.entryCluster.store('fader', new Fx.Tween(globalz.entryCluster, {property:'opacity', duration: kFadeDuration, fps: 30, wait: true}))

	globalz.cssTextShadowsSupported = Browser.Engine.webkit /*Safari, Konqueror, others*/ || Browser.Engine.presto; /*Opera*/
	if (globalz.cssTextShadowsSupported)
	{
		globalz.entryCaptionTextShadow.destroy()
	}
	
	// ** prepare spinner
	var faderCompleteFunction = function (){
		if (!globalz.entrySpinner.spinner.isOnDuty()) {
			globalz.entryCluster.setStyle('visibility', 'visible')
			globalz.entryCluster.retrieve('fader').start(1).chain(	function(){
																		if (globalz.entryCaptionText.get('html').length > 0){
																			//globalz.entryCaption.morph({'opacity': 0.9});
																		}
																	})
		}
	}

	var spinnerDiv = newSpinnerNode(faderCompleteFunction)
	globalz.entrySpinner 	= $('entry-spinner')
	globalz.entrySpinner.adopt(spinnerDiv)
	globalz.entrySpinner.spinner = spinnerDiv
			
	// ** Load up our sites toc and details
	globalz.toc = new Toc()
	globalz.siteDetails = new SiteDetails()

	// Setup an array to keep tack of where in a gallery the user was
	globalz.currentFotoIndexes = []
	for (var g = 0; g < globalz.toc.galleries.length; g++){
		globalz.currentFotoIndexes[g] = 0
	}

	// ** Setup gallery navigation
	var extras = [	{linkText:'Contact', 	linkClickHandler:function(){showOverlay("contact");return false;}},
					{linkText:'Bio', 		linkClickHandler:function(){showOverlay("bio");return false;}}]

	globalz.navOverlayController = new NavOverlayController($('nav-overlay'), sections(globalz.toc), extras, {onDutyZIndex:7000, offDutyZIndex:5000})

	// ** a special mouse handler so user can click anywhere
	globalz.pagewrap.addEvent('click', pagewrapClickHandler)
	
	// ** attach handlers to items in previous-next=cluster
	globalz.previousButton = $('previous')
	globalz.nextButton = $('next')
	globalz.menuButton = $('menu-button')


	// ** Setup previous-next arrows
	var menuButtonAction = showMenu
	globalz.arrowsController = new ArrowsController(globalz.previousButton, globalz.nextButton, goPreviousOrNextEventHandler, globalz.menuButton, menuButtonAction)
	
	// ** Setup fotonav controller
	globalz.fotonavController = new FotonavController($('fotonav-indexes'))

	// ** If the user is coming here 'fresh', roll-in the navOverlay
	if (!$defined(window.location.href.split("#")[1])){
		globalz.navOverlayController.animateIn()
	}

	// ** Special stuff for Safari
	if (window.webkit)
	{
		// This creates a frame which prevents this page from getting cached in Safari's back forward cache.
		// See <http://developer.apple.com/internet/safari/faq.html#anchor5>
		document.body.appendChild(new Element('iframe', {style:'height:0px;width:0px;visibility:hidden', src:'about:blank'}))
	}

	// ** Setup bio node
	globalz.bioNode = $('bio')
	globalz.bioNode.store('fader', new Fx.Tween(globalz.bioNode, {property: 'opacity', duration: 500, fps: 30, wait: true}))
	globalz.bioNode.retrieve('fader').set(0);
	globalz.bioNode.addEvent('click', function(){hideOverlay(globalz.bioNode.id);return false})
	
	
	
	var bioHTML = globalz.siteDetails.bio.replace("&quot;", "'")
	$('bio-inner').innerHTML = globalz.siteDetails.bio.replace(/&quot;/g, "'")

	// ** Setup the contact node. 
	globalz.contactNode = $('contact')
	globalz.contactNode.store('fader',  new Fx.Tween(globalz.contactNode, {property:'opacity', duration: 500, fps: 30, wait: true}))
	globalz.contactNode.retrieve('fader').set(0)
	globalz.entryCluster.setOpacity(0)	
	globalz.contactNode.addEvent('click', function(){hideOverlay(globalz.contactNode.id);return false})
	
	var contactHTML = globalz.siteDetails.contact.replace("&quot;", "'")
	$('contact-paragraph').innerHTML = globalz.siteDetails.contact.replace(/&quot;/g, "'")

	// 20090808 mnk:
	// fix (?) for unclickable links on overlays.
	fixLinksInNode($('contact'))
	fixLinksInNode($('bio'))


	// Setup some event handlers
	window.addEvent('keydown', function(){});	
	document.addEvent('keydown', keyDownHandler);	

	window.addEvent('resize', function(e){handleResize(e)})
	handleResize() // call once to get synced up

	$('banner-image').addEvent('mousedown', function (){return false;}) // prevent user from dragging element (can f*ck up Safari by causing an autoscroll)


	// show our page
	globalz.pagewrap.retrieve('fader').start(1)

	function gotofirstfoto(){goto(initialPath)}
	(gotofirstfoto).delay(1000);
}


// ------------------------------------------------------------------------------------------------------------
//	keyDownHandler
// ------------------------------------------------------------------------------------------------------------
function keyDownHandler(anEvent)
{
	if (anEvent.key == 'up' || anEvent.key == 'down') {
		// Prevent up/down keys from scrolling in FF 
		return false;
	}
	else if (anEvent.key == 'left' || anEvent.key == 'right') {
		if (!globalz.navOverlayController.isVisible()) {
			return goPreviousOrNextEventHandler(anEvent)
		}
		else {
			// Prevent left/right keys from scrolling in FF 
			return false;
		}
	}
	else {
		return true;
	}
}

// ------------------------------------------------------------------------------------------------------------
//	pagewrapClickHandler
// ------------------------------------------------------------------------------------------------------------
function pagewrapClickHandler(e)
{
	if (e == undefined){
		// :(  IE.
		e = new Event(window.event)
	}

	// NOTE: MooTools does not extend e.target $ 'for performance reasons', so we have to do it.
	if ('a' == $(e.target).get('tag')) {   
		return true
	}
	else {
		goPreviousOrNextEventHandler(e)
		return false
	}
}


// ------------------------------------------------------------------------------------------------------------
//	showMenu
// ------------------------------------------------------------------------------------------------------------
function showMenu()
{
	globalz.navOverlayController.animateIn()
}


// ------------------------------------------------------------------------------------------------------------
//	showOverlay
// ------------------------------------------------------------------------------------------------------------
function showOverlay(overlayID)
{
	goto(globalz.currentPath + kSeparator + overlayID)
}

// ------------------------------------------------------------------------------------------------------------
//	hideOverlay
// ------------------------------------------------------------------------------------------------------------
function hideOverlay(overlayID)
{
	$(overlayID).retrieve('fader').start(0)

	// Strip the overlay id off the path and go there
	var pathComponents = globalz.currentPath.split(kSeparator)
	goto(pathComponents[0] + kSeparator + pathComponents[1])
}

// ------------------------------------------------------------------------------------------------------------
//	handleResize
// ------------------------------------------------------------------------------------------------------------
function handleResize(e)
{
	var windowSize 			= window.getSize()
	var windowWidth 		= windowSize.x
	var windowHeight 		= windowSize.y

	globalz.entryCluster.setStyle('top', (kImageMargin/2) +'px')		
	globalz.entryCluster.setStyle('height', (windowHeight - kImageMargin ) +'px')
	fixUpEntryComponentsDimensions()		
	
	globalz.pagewrap.setStyle('height', windowHeight)
}

