var Free2 = {
	images: [
	'images/logo1.gif',
	'images/logo2.gif',
	'images/logo3.gif',
	'images/logo4.gif',
	'images/logo5.gif' 
	],
	
	initialize: function () {
		Free2.randomLogo();
		Free2.librarySelect.initialize();
	},
	
 	randomLogo: function() {
		var logo = $('logo_image');
		logo.src = Free2.images[rand(Free2.images.length) - 1];
	},
	librarySelect: {
		region_select_id: 'library_finder_region_select',
		branch_select_id: 'library_finder_branch_select',
		go_id: 'library_finder_submit',
		current_region: false,
		initialize: function() {
			this.populate();
			$(this.branch_select_id).disabled = 'disabled';
			this.populateRegions();
			$(this.region_select_id).observe('change', function(e) {
				this.current_region = $F(this.region_select_id);
				this.populateBranches(this.current_region);
				$(this.branch_select_id).disabled = '';
			}.bind(this));
			$(this.go_id).observe('click', function (e) {
				Event.stop(e);
				return this.go();
				}.bind(this)
			);
		},
		populate: function() {
			$('libraryfinder').update(''+
			'<img src="images/findyourlibrary.gif" width="172" height="49" alt="Find Your Library" />'+
			'<select name="region" id="library_finder_region_select" size="1">'+
				'<option value="">Select region...</option>'+
			'</select>'+
			'<select name="branch" id="library_finder_branch_select" size="1">'+
				'<option value="">Select library...</option>'+
			'</select>'+
			'<a href="#" class="libfindergo" id="library_finder_submit"><img src="images/button_go.gif" width="36" height="21" alt="Button Go" /></a>');
		},
		go: function () {
			var www = $F(this.branch_select_id);
			if (!www || www == undefined || www == "") {
				if (this.current_region) {
					var regional = Libraries[this.current_region].clone();
					var main_branch = regional.shift();
					var www = main_branch;
				} else {
					return false;
				}
			}
			window.open(www);
		},
		populateRegions: function() {
			this.populateSelect(this.region_select_id, $H(Libraries).keys(), 'Select region . . .');
		},
		populateBranches: function(region) {
			var regional = Libraries[region].clone();
			var main_branch = regional.shift();
			this.populateSelect(this.branch_select_id, regional, 'Select library . . .');
		},
		clearSelect: function(which_select, instructions) {
			which_select = $(which_select)
			which_select.update('<option value="">'+instructions+'</option>');
		},
		populateSelect: function(which_select, with_options, instructions) {
			which_select = $(which_select);
			this.clearSelect(which_select, instructions);
			with_options.each(function(option) {
				if (Object.isArray(option)) {
					var option_value = option[1];
					var option = option[0];
				} else {
					var option_value = option;
				}
				which_select.insert("<option value=\""+option_value+"\">"+option+"</option>");
			});
			$A(which_select.options).first().selected = 'selected';
		}
		
	}
	
	
}



function rand(n)
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

Event.observe(window, 'load', Free2.initialize);