// ---------------------------------------------------------------------
// constructor
// ---------------------------------------------------------------------
	
/**
 * @author Keith Pumpernickel
 * 
 * This object is the singleton that represents the user interface and 
 * its functions.
 */
app = {
	
	// these properties provide basic information about the application
	name:    "kerridugan.com",
	authors: ["Keith Pumpernickel", "Kerri Dugan"],
	version: "1.0",
	
	// these properties define the package structure. the toolkit should do this
	// automatically, but if we ever decide to compress the scripts, pre-defining
	// the structure simplifies matters.
	event: {},
	components: {
		view: {}
	},
	util: {}
};



//---------------------------------------------------------------------
// define namespaces
//---------------------------------------------------------------------

dojo.registerModulePath("app.event", "../event");
dojo.registerModulePath("app.components", "../components");
dojo.registerModulePath("app.util", "../util");



//---------------------------------------------------------------------
// import files
//---------------------------------------------------------------------

dojo.require("app.components.NavBar");
dojo.require("app.components.NavItem");
dojo.require("app.components.Slide");
dojo.require("app.components.Slideshow");
dojo.require("app.components.view.AbstractProvider");
dojo.require("app.components.view.ArtistStatementProvider");
dojo.require("app.components.view.ArtistBioProvider");
dojo.require("app.components.view.ContactProvider");
dojo.require("app.components.view.ExhibitionsProvider");
dojo.require("app.components.view.HtmlProvider");
dojo.require("app.components.view.HomeProvider");
dojo.require("app.components.view.SlideshowProvider");
dojo.require("app.util.ImageResource");



//---------------------------------------------------------------------
// initialization
//---------------------------------------------------------------------

/**
 * This function initializes the application.
 */
app.initialize = function() {
	
	// performance optimizations
	var ir = app.util.ImageResource;
	
	
	//------------------------------------------------------
	// set up content providers
	//------------------------------------------------------
	
		// current images slideshow
		var xmlPath			= "content/current/slideshow.xml";
		var imgPath			= "content/current/image/";
		var thumbPath		= "content/current/image/thumbs/";
		var viewCurrent 	= new app.components.view.SlideshowProvider(xmlPath, imgPath, thumbPath, true);
		
		// archive images slideshow
		var xmlPath			= "content/archive/slideshow.xml";
		var imgPath			= "content/archive/image/";
		var thumbPath		= "content/archive/image/thumbs/";
		var viewArchive 	= new app.components.view.SlideshowProvider(xmlPath, imgPath, thumbPath, true);
		
		// static pages- Content is dispensed through js docs
		//var viewStatement 	= new app.components.view.ArtistStatementProvider();
		//var viewExhibitions	= new app.components.view.ExhibitionsProvider();
		//var viewBio 		= new app.components.view.ArtistBioProvider();
		//var viewContact		= new app.components.view.ContactProvider();
		
		// Uncomment the appropriate page below, comment the corresponding one above, to tweak content manually
		var viewStatement 	= new app.components.view.HtmlProvider("content/artistStatement.html");
		var viewExhibitions	= new app.components.view.HtmlProvider("content/exhibitions.html");
		var viewBio 		= new app.components.view.HtmlProvider("content/artistBio.html");
		var viewContact		= new app.components.view.HtmlProvider("content/contact.html");
		
		
		// home page
		var viewHome 		= new app.components.view.HomeProvider();
		viewHome.addImage(ir.home1);
		viewHome.addImage(ir.home2);
		viewHome.addImage(ir.home3);
		
		
	//------------------------------------------------------
	// set up nav bar
	//------------------------------------------------------
	
		// create a new menu bar
		var navBar = new app.components.NavBar();
		
		// create nav item action fns
		var fnCurrent 		= function() { app.setContent(viewCurrent.getContent()); 		};
		var fnArchive 		= function() { app.setContent(viewArchive.getContent()); 		};
		var fnStatement 	= function() { app.setContent(viewStatement.getContent()); 		};
		var fnExhibitions	= function() { app.setContent(viewExhibitions.getContent()); 	};
		var fnBio 			= function() { app.setContent(viewBio.getContent()); 			};
		var fnContact 		= function() { app.setContent(viewContact.getContent()); 		};
		
		// create nav items
		var navItemCurrent 		= new app.components.NavItem(ir.navPriCurrentMap,		fnCurrent);
		var navItemArchive 		= new app.components.NavItem(ir.navPriArchiveMap,		fnArchive);
		var navItemStatement 	= new app.components.NavItem(ir.navPriStatementMap, 	fnStatement);
		var navItemExhibitions	= new app.components.NavItem(ir.navPriExhibitionsMap,	fnExhibitions);
		var navItemBio 			= new app.components.NavItem(ir.navPriBioMap, 			fnBio);
		var navItemContact 		= new app.components.NavItem(ir.navPriContactMap, 		fnContact);
		
		// add items to bar (in order from left to right)
		navBar.addNavItem(navItemCurrent);
		navBar.addNavItem(navItemArchive);
		navBar.addNavItem(navItemStatement);
		navBar.addNavItem(navItemExhibitions);
		navBar.addNavItem(navItemBio);
		navBar.addNavItem(navItemContact);
		
		// get container element and add menu bar
		var navContainer = document.getElementById("nav");
		navContainer.appendChild(navBar.getElement());
	
	
	//------------------------------------------------------
	// connect logo to nav bar
	//------------------------------------------------------
		
		// get reference to logo 
		var imgLogo = document.getElementById("imgLogo");
		
		// when clicked, clear nav bar state and load home view
		dojo.connect(
			imgLogo, "onclick", 
			function(evt) {
				navBar.clearState();
				app.setContent(viewHome.getContent()); 
			}
		);
	

	//------------------------------------------------------
	// force home contact
	//------------------------------------------------------
		
		// state is clear; just add content
		app.setContent(viewHome.getContent()); 
}


/**
 * This function replaces the html content in the main content area.
 * 
 * @param {Element} content the new content to be displayed
 */
app.setContent = function(content) {
		
	// clear current content
	app.clearContent();
	
	// get content element
	var element = document.getElementById("contentCell");
	
	// add child to element
	if (element) {
		element.appendChild(content);
	}
}


/**
 * This function clears the html content in the main content area.
 */
app.clearContent = function() {
	
	// get content element
	var element = document.getElementById("contentCell");
	
	// if found, remove all child nodes
	if (element) {
		var child = null;
		while (element.hasChildNodes()) {
			child = element.lastChild;
			element.removeChild(child);
		};
	}
}
