function homepage_flash_embed () {
	var so = new SWFObject("/flash/banner_controller.swf", "homepage-flash", "942", "354", "8", "#000");
		so.addParam("wmode", "opaque");
		so.addVariable("xPath", "/banner_xml")
		so.write("flash-img");
}


// Hover Behaviour for Nav etc.
var HoverBehavior = Class.create({
	initialize: function() {
		$A(document.styleSheets).each( function(stylesheet) {
		$A(stylesheet.rules).each( function(rule) {
		if( rule.selectorText.match(/:hover/i) ) {
			stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
		}
		});
	});

	$A(arguments).each( function(arg) {
		$$(arg).each( function(tag) {
			Event.observe(tag, 'mouseenter', function() { Element.addClassName(tag, 'hover'); }, true);
			Event.observe(tag, 'mouseleave', function() { Element.removeClassName(tag, 'hover'); }, true);
			});
		});
	}
});


// Pop Image from utils
function popImage(img, alt) {
	url = '/imageviewer.php?pic='+img+'&alt='+alt;
	window.open(url, 'ImageZoom', 'width=435,height=495');
}

// Scan for rel=blank
function popExternalLinks () {
	$$('a[rel="blank"]').each(function(link){link.target = "_blank"});
}


var InputDefaultValue = Class.create({	
	
	initialize: function(field){
		// Ensure container exists
		if (!$(field)) { return false; }

		this.field = $(field);
		this.defaultValue = $F(this.field);
	
		// Clear field on click
		Event.observe(this.field, "click", function(){
			if ($F(this.field)==this.defaultValue) { 
				$(this.field).clear(); 
			}
			$(this.field).activate();
		}.bind(this));
	
		// Restore field's default text when empty
		Event.observe(this.field, "blur", function(){
			if (!$(this.field).present()) { 
				$(this.field).setValue(this.defaultValue); 
			}
		}.bind(this));
	}
});



var Accordion = Class.create({	
	selector : null,
	current_section : null,
	animating : false,
	
	initialize: function(selector, options){
	  	
		// Ensure selector is good
		if (!$$(selector).pop()) { return false; }
	
		// Override default options
		this.options = Object.extend({
			duration: 0.5 	// transition speed
		}, options || {});
		
		var events_container = $$(selector).pop().up();
		//events_container.setStyle({ height: events_container.getHeight()+'px' });
		
		$$(selector).each(function(section) {
			
			// Find current section
			if ((!this.current_section) && (section.hasClassName('active'))) {
				this.open(section);
				this.current_section = section;			
			}

			// Set a section id based on the link href
			section.id = section.down('h2 a').href.split('#').pop();

			// Secttion Toggle Link - clicking
			section.down('h2 a').observe('click', this.activate.bind(this,section), false);
			
			section.down('.event_body').hide();
			//this.close(section,0);

		}.bind(this));			
	},


	activate : function(section, event) {	
			
		var old_section = this.current_section;		

		// Do nothing if already animating or the current panel was clicked
		if (this.animating) { return false; }
		//if (section==old_section) { return false; }

		// set flag
		this.animating = true;

		if (section.hasClassName('open')){
			this.close(section);
		} else {
			this.open(section);
		}

		// Update the current panel
		this.current_section = section;
		
		// Update the URL for any bookmarking
		location.hash = section.id.split('event-').pop();	
		
		// Don't the browser scroll up or down to the id
		if (event) { event.stop() }	
	},
	
	activate_original : function(new_section, event) {		
		var old_section = this.current_section;		

		// Do nothing if already animating or the current panel was clicked
		if (this.animating) { return false; }
		if (new_section==old_section) { return false; }

		// set flag
		this.animating = true;

		// Animate both panels
		this.close(old_section);
		this.open(new_section);

		// Update the current panel
		this.current_section = new_section;
		
		// Update the URL for any bookmarking
		location.hash = new_section.id;		
		
		// Don't the browser scroll up or down to the id
		if (event) { event.stop() }		
	},	
	
	close : function(section) {
		if (!section) { return; }
		
		var args = Array.prototype.slice.call(arguments); 
		var duration = (args.length>1) ? args.pop() : this.options.duration;
		
		// Remove the old section's active class
		section.removeClassName('active');
		section.addClassName('animating');
		section.select('.view').pop().update('+ View Details');
		
		// Animate up, and remove the open class when done
		new Effect.BlindUp( section.down('.event_body'), { 
			duration: duration,
			afterFinish: function() {
				section.removeClassName('open');
				section.removeClassName('animating');
				this.animating=false;				
			}.bind(this)
		});		
	},
	
	open : function(section) {		
		if (!section) { return; }
		
		var args = Array.prototype.slice.call(arguments); 
		var duration = (args.length>1) ? args.pop() : this.options.duration;		
		
		// Add the active class to the new panel (make it red)
		section.addClassName('active');
		section.addClassName('animating');							
				
		// Animate down, and add the open class when done
		new Effect.BlindDown( section.down('.event_body'), { 	
			duration: this.options.duration,
			afterFinish: function(){
				section.addClassName('open');
				section.removeClassName('animating');				
				section.select('.view').pop().update('- Hide Details');
				this.animating = false;				
			}.bind(this)
		});		
	}
	
});






// Global Window Onload
Event.observe(window, 'load', function() {	
		
		// This should be an IE only thing but Safari 3.0.4 apparently doesn't support it.
		//$$('#sub_banner div:last-child').each(function(tag){tag.setStyle({margin: 0}); });				

		popExternalLinks();
});



// Global DOM onload
document.observe("dom:loaded", function() {

	new Accordion('#event .event_section'); // event details	

	// Applies to the media and internation forms (see contact_controller.php)
	if ($('ag_contact')) {
	
		// Add required *'s to the form's labels
		$$('#ag_contact dl dd .required').each(function(el){
			el.up().previous().insert({top:'<span style="color: rgb(255, 0, 0);">*</span>&nbsp;'});
		});
	
		// Validate for required forms
	    var valid = new Validation('ag_contact');
	}

/*
	// This will apply to all tables in the content
	$$('#content table').each(function(table){
		
		// The first row will be classed odd
		var current_row = 'odd';
		
		// Loop through every row in this table
		table.down(0).childElements().each(function(row){

			// If there's no row before this one, it's the top row
			if (!row.previous(0)) {
				row.addClassName('top');				
			// If there's no row after this one, it's the bottom row
			} else if (!row.next(0)) {
				row.addClassName('bottom');
			}

			// Label this row odd or even and toggle for the next one
			row.addClassName(current_row);
			current_row = (current_row=='odd') ? 'even' : 'odd';

			// Loop through every cell in this row
			row.childElements().each(function(cell){
				
				// If there's no cell before this one, it's the first cell
				if (!cell.previous(0)) {
					cell.addClassName('first');
				// If there's no cell after this one, it's the last cell
				} else if (!cell.next(0)) {
					cell.addClassName('last');
				}								
			});
		}); 
	});
*/

	
	 // Make room for CLF
	if ($('topnav')) { $('topnav').setStyle({right:'355px'}); }	
		
	// IE FIXES
	// if (Prototype.Browser.IE) {
	// 	$$('#nav li:last-child').each( function(tag) { tag.addClassName('last-child'); });
	// 	new HoverBehavior('#nav li'); // keep at the end!
	// }

});