// Namespace object for current site
var cTHEFOOT = Class.create({
		
	initialize: function() {
		
		// Constants
		this.PATH_IMAGES = 'assets/images/';
		
		// Component constants
		this.GALLERY_PLAYMODE_NONE = 0;
		this.GALLERY_PLAYMODE_SEQUENTIAL = 1;
		this.GALLERY_PLAYMODE_RANDOM = 2;
		this.GALLERY_SCROLL_DIR_NONE = 0;
		this.GALLERY_SCROLL_DIR_VERTICAL = 1;
		this.GALLERY_SCROLL_DIR_HORIZONTAL = 2;
		
		// Properties
		this.o_current_menulink = null;
		this.o_cache_images = null;
		this.a_menulinks = [];
		this.v_rollover_image_id = '';
		this.server_error = '';
		this.galleries = new Array();
		
	},
		
	/*
		Handle an app error
	*/
	showError: function(v_error_msg){
		alert(v_error_msg);
	}, // this.showError()
	
	/*
		Load a flash movie using swfobject
	*/
	showFlashMovie: function (
	  v_path_to_swf,
	  v_movie_id,
	  v_width,
	  v_height,
	  v_version,
	  o_flash_vars,
	  o_params,
	  o_attributes
	){
		swfobject.embedSWF( 
			v_path_to_swf, 
			v_movie_id, 
			v_width, 
			v_height, 
			v_version, 
			'', 
			o_flash_vars, 
			o_params, 
			o_attributes
		);
	}, // this.showFlashMovie()
	
	/*
		Get the current page name
	*/
	getCurrentPage: function(){
		var v_path = window.location.pathname;
		return (this.getPageFromURL(v_path) || 'index.php');
	}, // this.getCurrentPage()
	
	/*
		Get the current page name
	*/
	getPageFromURL: function(v_url){
		return (v_url.substring(v_url.lastIndexOf('/') + 1));
	}, // this.getPageFromURL()
	
	/*
		Return a 2-dim array of width, height or access as object properties,
		of given window element
	*/
	getWindowSize: function(o_win) {
		var a_dims = [];
		o_win = o_win ? o_win : window;
		a_dims.width = a_dims[0] = o_win.document.viewport.getWidth();
		a_dims.height = a_dims[1] = o_win.document.viewport.getHeight();
		return a_dims;
	}, // getWindowSize()
	
	/*
		Show a selected menu image
	*/
	showMenuOption: function(v_class){
		if (!this.v_rollover_image_id.blank() && !v_class.blank()){
			$(this.v_rollover_image_id).className = v_class;
		}
	}, // this.showMenuOption()
	
	/*
		Restore the currently selected menu image 
	*/
	restoreCurrentMenuOption: function(){
		$(this.v_rollover_image_id).className = THEFOOT.o_current_menulink.up().className;
	}, // this.restoreCurrentMenuOption()
	
	/*
		Cache/Preload images
		Images cache object is created by the server
	*/
	cacheImages: function(){
		if(this.o_cache_images){
			var a_images = this.o_cache_images.images;
			for (var i = 0; i < a_images.length; i++){
				var o_image = new Image();
				o_image.src = a_images[i];
			}
		}
	}, // this.cacheImages()
	
	/*
		Create an array of image elements, pre-cached
	*/
	
	/*
		Create a new image gallery and add it to the galleries collection
	*/
	createImageGallery: function(v_container_id, a_images, v_display_count, v_gallery_name){

		// Create new gallery
		var o_gallery = {
			container:	$(v_container_id),
			images:		a_images,
			thumbs:		new Array()
		}
		this.galleries.push(o_gallery);
		if (!o_gallery.container){this.showError('Invalid DOM container element');return false;}
		if (!o_gallery.images){this.showError('Invalid images array');return false;}

		// Create thumb containers
		for (var i = 1; i <= v_display_count; i++){
			o_gallery.container.insert({'top': new Element('div').addClassName('thumb')});
		}

		// Grab and format thumb elements
		//$$("div#" + v_container_id + " div.thumb").each(function(o_thumb, v_index){
		o_gallery.container.immediateDescendants().each(function(o_thumb, v_index){		
			// Create loading spinner
			var o_img_loading = new Element('img', {
				'src'	: 'assets/images/spinner.gif', 
				'alt'	: 'Loading image...',
				'title'	: 'Loading image...'
			});
			o_img_loading.id = 'thumb-img-' + v_index;
			
			// Create wrapper window
			var o_wrapper = new Element('div').addClassName('wrapperwin').insert({
				"top": o_img_loading
			});
			o_wrapper.id = 'thumb-win-' + v_index;
				
			// Insert DOM element into UI and center
			o_thumb.id = 'thumb-container-' + v_index;
			var o_thumbobj = {
				"container"		: o_thumb,
				"window"		: o_wrapper,
				"image"			: o_wrapper.down()
			}
			o_thumb.insert({'top': o_thumbobj.window});
			
			// Center thumb
			/*o_thumbobj.window.setStyle({
				'marginLeft': (o_thumbobj.window.getWidth() - o_thumbobj.image.getWidth()) / 2,
			});*/

			// Add thumb to gallery thumbs array
			o_gallery.thumbs.push(o_thumbobj);			
			
		});		

		// Cache and display each thumbnail image
		o_gallery.images.each(function(v_url, v_index){
		
			// Build dynamic thumb url
			var v_thumburl = THEFOOT.SITE_ROOT + 'assets/media/galleries/thumbnail.php?' +
				'max_h=137&galleryname=' + v_gallery_name +
				'&filename=' + THEFOOT.getPageFromURL(v_url);		
			// Cache the image
			var o_image = new Image();
			o_image.src = (v_thumburl || '');
	
			// Update the gallery with the cached image
			var o_img_dom = o_gallery.thumbs[v_index].image;
			o_img_dom.src = o_image.src;
			//var v_offset = ($j.browser.msie) ? 30 : (o_img_dom.width - 136) / 2;			
			o_img_dom.writeAttribute({
				"title": "", "alt": " "
			}).setStyle({
				//'marginLeft': '-' + v_offset + 'px',
				'height': '137px',
				'width': 'auto'
			}); // o_gallery.thumbs[v_index].window.height()
		
			// Add lightbox
			o_img_dom.wrap(new Element('a', {
				'href': v_url,
				'id': 'thumb-link-' + v_index,
				'rel': 'lightbox'
			}));
					
		});
		
		// Create the lightbox
		$j('#' + v_container_id + ' a[@rel*=lightbox]').lightBox();
		
	}, // this.createImageGallery()
	
	/*
		Highlight current menu item
	*/
	highlightCurrentMenuItem: function(){
		this.a_menulinks = $$('div#page-tabs ul li a');
		var v_pagethis = this.getCurrentPage();
		for (var i = 0; i < this.a_menulinks.length; i++){
			var o_node = this.a_menulinks[i];
			if (this.getPageFromURL(o_node.href) == v_pagethis){
				this.o_current_menulink = o_node;
				o_node.up().addClassName('current');
			}
		}
	}, // this.highlightCurrentMenuItem()
	
	/*
		Attach menu rollover handlers
	*/
	attachMenuHandlers: function(){
		if (this.a_menulinks){
			this.a_menulinks.each(function(o_menulink){
				o_menulink.observe('mouseover', function(evt){
					this.showMenuOption(evt.element().up().className);										 
				}.bind(this));
				o_menulink.observe('mouseout', this.restoreCurrentMenuOption.bind(this));
			}.bind(this));	
		}
	}, // this.attachMenuHandlers()
	
	/*
		Page initialisation code
	*/
	initPage: function(){		
		
		// Preload images
		//this.cacheImages();
			
		// Highlight current menu item
		this.highlightCurrentMenuItem();
		
		// Attach menu roll-over handlers
		//this.attachMenuHandlers();
		
		// Process any server errors
		if (this.server_error.length > 0){
			alert(this.server_error);
		}

	} // this.initPage()
		
}); // thefoot app namespace

/////////////////////////////
// Create our namespace
/////////////////////////////
window.THEFOOT = new cTHEFOOT();

