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();
HoverBehavior.prototype = {
	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);
			});
		});
	}
};


// Homepage quickfact ticker
var QuickFactScroller = Class.create({
	initialize: function(){
		var t = this;
		this.fact_list = $('fact_list');
		if(this.fact_list){
			this.facts = this.fact_list.childElements();
			this.total = this.facts.length;
			this.slideNext();
			new PeriodicalExecuter(function(pex){t.fadeCurrent(); t.slideNext();}, 10);
		}
	},
	
	getRandom: function(){
		next_item = Math.round(Math.random()*this.total);
		if(next_item == this.last_item){next_item = this.getRandom}
		return next_item-1;
	},
	
	fadeCurrent: function(){
		new Effect.Fade(this.current_item, {queue: {position: 'end', scope: 'quickfact'}} );
		new Effect.Move(this.current_item, { x: 100, y: 0, mode: 'absolute', queue: {position: 'end', scope: 'quickfact'} });
	},
	
	slideNext: function(){
		var random = this.getRandom();
		var item = this.facts[random];
		if(item){
			new Effect.Appear(item, { queue: {position: 'end', scope: 'quickfact'} });
			new Effect.Move(item, { x: -750, y: 0, mode: 'absolute', queue: {position: 'end', scope: 'quickfact'}  });
			this.current_item = item;
			this.last_item = random;
		}
	}
	
});

// Toggles Bios and "active_bio" class
var BioToggle = Class.create({
	initialize: function(){
			$$('.bio').each(function(bio){
				
				// Hide all bio details (for IE, this must be done via JS so corners.js works properly)
				bio.select('.bio_details')[0].setStyle({display:'none'});
				
				// Toggle description on click
				Event.observe(bio, 'click', function(){this.select('.bio_details')[0].toggle(); this.toggleClassName('active_bio'); return false;});
			});
	}
});


// Band Lightwindow Functionality
var BandLightbox = Class.create({
	initialize: function(){
		// Run through each featured band to 'lightwindow' the detail page.
		$$('.featured_band').each(function(band) {
			Event.observe(band, 'click', function(){
					//band_id = this.id.gsub('band_', '');
					band_id = this.id;
					band_id = band_id.replace(/[^0-9]/g, '');
					myLightWindow.activateWindow({ 	href: '/music/band-detail.html?band='+band_id, 
																					width: 820, 
																					height: 417,
																					title: ' ',
																					iframeEmbed: false,
																					type: 'page' });
				});
		});
	}
});


function signupPopup() {
	
	// Regular body image link
	$$('.bodyimage a img').each(function(img){
		
		// Identify it by alt tag, 'news-subscribe'
		if (img.alt=='news-subscribe') {
			
			// Launch pop up window at specific size on click
			var popuplink = $(img).up(0).href
			Event.observe($(img).up(0), 'click', function(){
				window.open(popuplink, 'subscribe', 'width=400, height=500');
			});
			
			// Make sure the regular link doesn't do anything anymore
			$(img).up(0).href = '#subscribe';
			$(img).up(0).target = '';
		}
	});	
}


// Adds target="_top" to all links on the masthead for pages used in iframes
function addTargetTopToNav(id){
	if ($(id)) {
		$$('#'+id+' #masthead a').each(function(a){ 
			if ((!a.href.hash) && (!a.target)) { a.target = '_top'; }		
		});
	}
}

// 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));
	}
});



// Global Window Onload
Event.observe(window, 'load',
	function() {		
		//if($('flash-img')){homepage_flash_embed();}		
		new QuickFactScroller();
		new BioToggle();
		new BandLightbox();
		
		// 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}); });
		
		

		
		// Make topnav links work in iframe in blog (id from nterchange page)
		addTargetTopToNav('page180');	
		
		popExternalLinks();

		


});



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

	// Make subscribe window popup
	signupPopup();
	
	 // 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'); });

		$$('.nav_band').each(function(tag){tag.observe('click', function(){window.location = this.down('a').href;})});
		new HoverBehavior('#nav li'); // keep at the end!
	}

});