﻿/*extern $, Dom, Event, Lang, MENUCODE */
/*global HOMEPAGE */
/*members INAV000022, INAV000023, INAV000024, INAV000025, INAV000026, 
    INAV000027, INAV000028, INAV000029, current, data, flipImage, 
    hasOwnProperty, height, image, images, init, initPreload, isString, 
    items, logo, menu, menus, newitem, none, onDOMReady, parent, parentNode, 
    replaceChild, resolve, src, subscribe, width
*/
HOMEPAGE = {

	data: {
		parent : null,              // Main image parent node
		image  : 'HOMEPAGE_Image',  // Main image ID (resolved to image img)
		width  : 782,               // Width of the main image
		height : 462,               // Height of the main image
		images : {                  // Hash of all sub-menu pictures, by li.T1 id
			none      : '/images/layout/pichome.jpg',
			INAV000022: '/images/layout/picabout.jpg',
			INAV000023: '/images/layout/piceducation.jpg',
			INAV000024: '/images/layout/picresearch.jpg',
			INAV000025: '/images/layout/picregional.jpg',
			INAV000026: '/images/layout/picpatient.jpg',
			INAV000027: '/images/layout/picadmissions.jpg',
			INAV000028: '/images/layout/picemployment.jpg',
			INAV000029: '/images/layout/picgiving.jpg'
		}
	},

	/** Loop over items in the given object. If the item value is a string,
	  * replace the value with the results of a document.getElementById(value)
	  * @param {Object}
	  */
	resolve: function (o) {
		for (var x in o) {
			if (Lang.hasOwnProperty(o, x) && Lang.isString(o[x])) {
				o[x] = $(o[x]);
			}
		}
	},

	/** Perform initialization.
	  * Called automatically during onDOMReady
	  * <ol>
	  *     <li>Resolve various elements
	  *     <li>Preload images
	  *     <li>Attach events
	  * </ol>
	  */
	init: function () {

		this.resolve(this.data);
		this.data.parent = this.data.image.parentNode;

		MENUCODE.data.newItem.subscribe(this.flipImage, this, true);

		// Preload our images
		this.initPreload();

	},

	flipImage: function (e, args) {
		var id  = args[0];
		var map = this.data.images;
		if (Lang.hasOwnProperty(map, id) === false) {
			return;
		}
		
		this.data.parent.replaceChild(map[id], this.data.image);
		this.data.image = map[id];

	},

	/** Preload main image files. */
	initPreload: function () {
		var map = this.data.images;
		var w = this.data.width;
		var h = this.data.height;
		var src;
		for (var x in map) {
			if (Lang.hasOwnProperty(map, x)) {
				src = map[x];
				map[x] = new Image(w, h);
				map[x].src = src;
			}
		}
	}
};
Event.onDOMReady(HOMEPAGE.init, HOMEPAGE, true);
