//
// 
//  PageController.js
//
//  Created by  on 2009-11-07.
//  Copyright (c) 2009 murat n konar. All rights reserved.
//


var PageController = new Class({

	// -------------------------------------------------------------
	//	initialize
	// -------------------------------------------------------------
	initialize: function(theDocument) 
	{ 
		var self = this // Use 'self' instead of 'this' in functions to avoid getting the wrong thing.
		
		this.document = theDocument

		// Initialize the table of contents
		this.fotosite = fotosite_content()
		this.fotosite.currentGalleryIndex = 0					// Initialize the current gallery index to zero

		// Set up banner.
		var bannerH1 = new Element('h1', {'html': this.fotosite.sitedetails.bannerHTML})
		$('banner').empty()
		$('banner').adopt(bannerH1)
		$('banner').setOpacity(0) // initially invisible

		// Setup gallery navigation.
		$('gallery-navigation').setOpacity(0) // initially invisible
		this.galleryNavigator = new GalleryNavigator($('gallery-navigation'), 'gallery-list')
		this.galleryNavigator.addSection('gallery-list')
		this.fotosite.galleries.each(function(gallery, index)
		{
			self.galleryNavigator.addItem(gallery.name, function(){self.go(index); return false /* returning false prevents href from being followed */}, index, 'gallery-list')			
		})		
		

		// Add video
		this.galleryNavigator.addSection('video-section')
		this.galleryNavigator.addItem("film", 
											function(){self.go('video'); return false /* prevents href from being followed */}, 'video', 'video-section')			



		// Add bio and Contact
		this.galleryNavigator.addSection('bio-and-contact-list')
		this.galleryNavigator.addItem(this.fotosite.sitedetails.bioTitle, function(){self.go('bio'); return false /* prevents href from being followed */}, 'bio', 'bio-and-contact-list')			
		this.galleryNavigator.addItem('Contact', function(){self.go('contact'); return false /* prevents href from being followed */}, 'contact', 'bio-and-contact-list')			

		// Attach a controller to each link in our gallery navigation to handle visual effects
		$('gallery-navigation').getElements('a').each(function(galleryLinkElem, index){
				new GalleryLinkController(galleryLinkElem)
			})

		// Set up slideshows
		this.slideshow = new Slideshow($('slideshow'))
		this.slideshow.setClickHandler(function(e){self.slideshow_click_handler(e)})
		this.slideshow.setImageDidDisplayHandler(function(){self.imageDidDisplay()})

		// Setup Bio, then hide it.
		$('bio').set('html', this.fotosite.sitedetails.bioHTML)
		$('bio-div').setStyle('opacity', 0)

		// Setup Contact, then hide it.
		$('contact').set('html', this.fotosite.sitedetails.contactHTML)
		$('contact-div').setStyle('opacity', 0)

		// figure out from the url which entry of which gallery to show
		var galleryAndEntryIndices = window.location.href.split('#')[1]
		var g = 0
		var e = 0
		if (galleryAndEntryIndices != undefined)
		{
			g = galleryAndEntryIndices.split("/")[0]
			e = galleryAndEntryIndices.split("/")[1]

			if (g == undefined) {
				g = 0
			}

			if (e == undefined) {
				e = 0
			}
		}
		
		
		// Setup copyright
		{
			var copyrightContent = this.fotosite.sitedetails.copyrightHTML;
			$('copyright').empty();
			$('copyright').adopt(new Element('p', {'html': copyrightContent}))
		}
		
		
		// Fade stuff in
		(function(){$('sheet').morph({'duration': 'long', 'opacity':1})}).delay(10);
		(function(){$('banner').morph({'duration': 'long', 'opacity':1})}).delay(100);
		(function(){$('gallery-navigation').morph({'duration': 'long', 'opacity':1})}).delay(1000);

		(function(){self.go(g, e)}).delay(1500);
	},

	// -------------------------------------------------------------
	//	slideshow_click_handler
	// -------------------------------------------------------------
	slideshow_click_handler: function (event)
	{	
		if (!this.slideshow.hidden)
		{
			var coords 			= this.slideshow.div.getCoordinates()
			var verticalDivide 	= (coords['left'] + coords['right'])/2
			var direction
		
			if (event.event.pageX > verticalDivide) {
				direction = 'next'
			}
			else {
				direction = 'previous'
			}
			this.go(this.fotosite.currentGalleryIndex, direction)
		}
	},
	
	// --------------------------------------------------
	// go
	// --------------------------------------------------
	go: function (g, e)
	{
		var bio = $('bio-div')
		var contact = $('contact-div')
		var fotonav = $('foto-navigation')


		if (g=='video')
		{
			window.location = 'video/index.html'
		}
		else if (g == 'bio' || g == 'contact') {
			var bioOpacity = (g == 'bio')?1:0
			var contactOpacity = (g == 'contact')?1:0

			bio.morph({opacity:bioOpacity})
			contact.morph({opacity:contactOpacity})
			fotonav.morph({opacity:0})
		    this.slideshow.setHidden(true)

			// Make sure navigation looks right.
			this.galleryNavigator.setCurrentByGref(g)
		
		    // Make sure the window's URL field is updated so that bookmarking works properly.
		    var newURL = window.location.href.split("#")[0] + "#" + g
		    window.location.href = newURL

			var sectionName = (g == 'bio') ? this.fotosite.sitedetails.bioTitle : 'Contact'
			document.title = this.fotosite.sitedetails.pageTitle + ':' + sectionName
		}
		else if (g >= 0 && g < this.fotosite.galleries.length)
		{
			if (g == "")
			{
				g = 0
			}
			
			// Ensure bio and contact are faded, and slideshow is not
			bio.morph({opacity:0})
			contact.morph({opacity:0})

			fotonav.morph({opacity:1})
			this.slideshow.setHidden(false)
		    
			this.fotosite.currentGalleryIndex = g
			this.galleryNavigator.setCurrentByGref(g) // Make sure navigation looks right.

			// update the foto
			var currentGallery = this.fotosite.galleries[this.fotosite.currentGalleryIndex]

			if (e != undefined)
			{
				if (e == 'next')
				{
					currentGallery.currentEntryIndex = (currentGallery.currentEntryIndex + 1) % currentGallery.entries.length
				}
				else if (e == 'previous')
				{
					currentGallery.currentEntryIndex = (currentGallery.currentEntryIndex - 1 + currentGallery.entries.length) % currentGallery.entries.length
				}
				else if (e >= 0 && e < this.fotosite.galleries[this.fotosite.currentGalleryIndex].entries.length)
				{
				    currentGallery.currentEntryIndex = e
				}
			}

		    var currentEntry = currentGallery.entries[currentGallery.currentEntryIndex]
		    this.slideshow.setImageSource(currentEntry.fotopath, currentEntry.fotocaptionHTML)

		    // Make sure the window's URL field is updated so that bookmarking works properly.
		    var newURL = window.location.href.split("#")[0] + "#" + g + '/' + currentGallery.currentEntryIndex
		    window.location.href = newURL

			document.title =  this.fotosite.sitedetails.pageTitle + ':' + currentGallery.name + ':' + currentGallery.currentEntryIndex
		}
	},
	
	// --------------------------------------------------
	// imageDidDisplay
	// --------------------------------------------------
	imageDidDisplay: function() 
	{
		var gallery = this.fotosite.galleries[this.fotosite.currentGalleryIndex]

		// Adjust vertical position based on foto dimensions
		var imageCoordinates = this.slideshow.getImageCoordinates()
		var delta = this.slideshow.div.getCoordinates()['height'] - imageCoordinates['height']
	}
});

