/**

**/

recipes = {

	init:function() {
		
		// Get reference to <img> of recipe.
		el_image   = $('recipe_image');
		//image_path = "http://www.fish-media.net/client2/church&manor/images/recipes/";
		image_path = "images/recipes/";
		
		images = new Array();
		
		links = recipes.get_recipe_links();
	
		recipes.prepare_links(recipes.get_recipe_links());
		
	},
	
	preload_image:function(image_name) {
	
		image_to_load = new Image(248, 248);	
		image_to_load.src = image_name;
		
		var index = images.length;

		images[index] = image_to_load;
	
	},
	
	prepare_links:function(links) {
		
		for (var i=0; i < links.length; i++) {
			str_link_id = links[i].getAttribute('id').toString();
			recipes.preload_image(image_path + links[i].getAttribute('id') + '.jpg');
			Event.observe(links[i], 'mouseover', this.listener(links[i]), false);
		}
	
	},
	listener:function(el_link) {
		return function(e) {
			recipes.switch_image(el_link);
		}
	},
	get_recipe_links:function() {
	
		// Does the browser support?
		if (!document.getElementsByTagName)
			return false;
		
		// Reference to <ul> of links
		link_container = $('recipe_list');

		links = link_container.getElementsByTagName("a");
		return links;
	},
	switch_image:function(el_link) {
		
		// Get references to current recipe link (highlighted white)
		current_recipe_links = document.getElementsByClassName('current_recipe');
		current_recipe_link = current_recipe_links[0];

		
		// Remove the current link class of the previously selected recipe
		Element.removeClassName(current_recipe_link, 'current_recipe');
		
		// Make the newly selected recipe the current link
		Element.addClassName(el_link, 'current_recipe');
		str_image_path = image_path + el_link.getAttribute('id') + '.jpg';
		
		arr_index = recipes.get_image_index(el_link.getAttribute('id'));
		
		// Change the image		
		el_image.src = images[arr_index].src;

	},
	get_image_index:function(str_image) {
		// Get the order of the image
		recipe_links = link_container.getElementsByTagName('a');
		
		for (var j=0; j < recipe_links.length; j++) {
			if (recipe_links[j].getAttribute('id') == str_image) {
				return j;
			}
		}
		
		return false;
		
		
	}
	
}

Event.observe(window, 'load', recipes.init, false);
