


var use_ajax_navigation = !Browser.Engine.trident; // Not yet working in IE

													// N.B. From JZ, 9/11: This is how it was when
													// I found it...  It's a pity not to make the
													// fancy animations work in IE, which is still
													// the most common internet browser.

var old_page;

var site = {'index':	{ 'page': 'index',	'menutache_x': 90, 'title_addendum':	'A Creature Collective', 'menutache_src': 'images/design/u_index.gif'  },
'badger-badger-band':	{ 'page': 'badger-badger-band',	'menutache_x': 165, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_badger-badger-band.gif'  },
'workshops':	{ 'page': 'workshops',	'menutache_x': 322, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_workshops.gif'  },
'events':	{ 'page': 'events',	'menutache_x': 416, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_events.gif'  },
'performance':	{ 'page': 'performance',	'menutache_x': 482, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_performance.gif'  },
'gallery':	{ 'page': 'gallery',	'menutache_x': 580, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_gallery.gif'  },
'bookings-and-contact':	{ 'page': 'bookings-and-contact',	'menutache_x': 647, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_bookings-and-contact.gif'  },
'secret-garden-party-2010':	{ 'page': 'secret-garden-party-2010',	'menutache_x': 416, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_events.gif'  },
'secret-garden-party-2010-gallery':	{ 'page': 'secret-garden-party-2010-gallery',	'menutache_x': 580, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_gallery.gif'  },
'login':	{ 'page': 'login',	'menutache_x': 0, 'title_addendum':	'Badger Badger', 'menutache_src': 'images/design/u_index.gif'  },
};


var slideshow;

// -------------------------------------------------------------------------------------------------
// Interface Initialisation
// -------------------------------------------------------------------------------------------------

function go_badger()
{
	if (typeof window.console == 'undefined')
	{
		console = new Object;
		console.log = function() { ; };
		console.dir = function() { ; };
	}

//	console.log('GO BADGER');	
	
	old_page = page_from_href(document.location.href);
	
	$('menu_links_strip').getElements('a').each( function(a)
		{
			a.addEvent('click', function() { return content_fetch(this); });
		}
	);
	
	if (typeof page != 'undefined')
	{
		console.dir(page);
		var params = new Object;
		params.page = page;
		params.xaction = "fetch_page";
		bajax(params, 'content_area');
	}
	
	// Tooltips
	new FancyTips($$('.tooltip'));

	/*
	FancyTips.addEvents(
	{
		'show': function(tip) {
			tip.fade('in');
		},
		'hide': function(tip) {
			tip.fade('out');
		}
	});
	*/
	
	content_init();
	
	// Autoplay, after a short delay
	// (function() {music.toggle(); }).delay(2000);

}





function page_from_href(thehref)
{
	var prefix = '?page=';
	var start_pos = thehref.lastIndexOf(prefix);
	if (start_pos > -1)
	{
		var rv = thehref.slice(start_pos+6);
		if (rv) return rv;
		else return 'index';
	}
	else
	return 'index';
}





function content_init()
{
	if ($('slideshow') && $defined(slideshow_data))
	{
		// Start slideshow
		slideshow = new Slideshow('slideshow',
									slideshow_data,
									{
										captions: true,
										controller: false,
										delay: 4000,
										duration: 1500,
										width: 666,
										height: 355,
										hu: '/zpage/data/binary/photos/'
									}
								);
	}
	
	if ($$('a[rel=quickbox]') && !Browser.Engine.trident)
	{
		// Initialise gallery, if not IE
		new QuickBox({ resizeDuration: 100, animateCaption: true, initialWidth: 500, initialHeight: 300, padding: 10 });
	}

	// MooScroll - for customised scrollbars on DIVs
	var myScroll = new MooScroll({});
	
	// Quotes
	if ($('quote_data')) ticker.init();
	
}

window.addEvent('domready', go_badger);

// -------------------------------------------------------------------------------------------------
// Load contents with XMLHTTPRequest
// -------------------------------------------------------------------------------------------------

var ajax_request_broker_url = '/ajax/ajax_request_broker.php';

function content_fetch(el)
{

	if (!use_ajax_navigation) return true;
	
	var page = page_from_href(el.href);

	if (old_page == 'index' && page != 'index')
	{
		// Position the playercontroller
		var pc = new Fx.Morph('playercontroller', { duration: 1500, transition: 'bounce:out' });
		pc.start({'right': 0});
	}
	else if (page == 'index')
	{
		// Hide the standard header
		$('the_artful_badger_banner').fade('out');
		// Position the playercontroller
		var pc = new Fx.Morph('playercontroller', { duration: 1500, transition: 'bounce:out' });
		pc.start({'right': 220});
	}
	
	// First stop slideshows, galleries, etc.
	slideshow = null;
	
	// Change the menu highlight
	$('menu_links_strip').getElements('a').each( function(a)
		{
			if (a.getStyle('opacity') == 1) a.fade(0.7);
		}
	);
	$(el).fade(1);

	// Fade out the menu moustache
	$('menutache').fade('out');

	var fx = new Fx.Tween('content_area', { duration: 300, property: 'opacity'} );
		fx.start(0).chain(
		function() {
			var params = new Object;
			params.page    = page;
			params.xaction = "fetch_page";
			bajax( params, 'content_area', function() {content_loaded(page)} );
		}
	);
	return false;
}

function content_loaded(p)
{
	if (old_page == 'index' && p != 'index')
	{
		// Show the standard header
		$('the_artful_badger_banner').setStyles({ 'opacity': 0, 'display': 'block'});
		$('the_artful_badger_banner').fade('in');
	}

	
	document.title = 'The Artful Badger - ' + site[p].title_addendum;
	document.body.id = site[p].page;

	content_init();
	$('content_area').fade('in');

	var mt = $('menutache');
	mt.setStyle('opacity', 0);

	mt.set('src', site[p].menutache_src);
	(function() { $('menutache').setStyle('left', site[p].menutache_x + 'px'); mt.fade('in'); }).delay(500);
	
	old_page = p;
	
	
	
}

function bajax(params, target_id, on_complete)
{
	// POST Ajax

	var t = $(target_id);
	if (!t)
	{
		console.log('Target not found: ' + target_id);
		return;
	}
	
	var bajax = new Request.HTML(
		{
			url: ajax_request_broker_url,
			data: params,
			update: t,
			onSuccess: function()
				{
					t.removeClass('ajaxload');
					if (on_complete)
					{
						on_complete();
					}
				}
		}
	).post();
}

// -------------------------------------------------------------------------------------------------
// MP3 Player
// -------------------------------------------------------------------------------------------------

var mp3_to_play = 'badger_badger_greatest_hits.mp3';

var music = new Object;
music.p = new Object;

// An array containing names of file to play
music.p.playlist = [
	'insinuate_mix_v3_dl2_128m.mp3',
	'badger_badger_greatest_hits_128m.mp3',
	'live_at_nhac_mix1_128m.mp3',
	'screaming_mix_v1_128m.mp3',
	'the_squirrel_grove_v3_128m.mp3',
	'wwib_mix1_v2_dl2_128m.mp3'
];
music.p.playlist_index = 0;
music.p.playlist_randomise = true;

music.p.has_played = false;

music.toggle = function()
{
	if (typeof myListener == 'undefined')
	{
		console.log('Waiting for myListener');
	}
	
	var div = $('player_playpause');
	var inf = $('player_playpause').getElement('span');
	var fx = new Fx.Tween('player_playpause', { duration: 300, property: 'opacity'} );
	console.dir(myListener);
	if (myListener.isPlaying == 'true')
	{
		// Pause
		console.log('Pause');
		fx.start(1).chain(
			function() { this.start(0) },
			function() { div.removeClass('pause').addClass('play'); this.start(1) }
		);
		inf.set('html', 'Listen to<br \/>Badger Badger');
		music.pause();
	}
	else
	{
		// Play
		console.log('Play');
		fx.start(1).chain(
			function() { this.start(0) },
			function() { div.removeClass('play').addClass('pause'); this.start(1) }
		);
		inf.set('html', 'Pause<br \/>the groove');
		music.play();
	}
}

music.play = function()
{
	if (music.p.has_played == false && music.p.playlist_randomise)
	{
		// Randomise the playlist order
		console.log('Randomising the playlist order');
		music.p.playlist = music.p.playlist.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
		console.log('New order is...');
		// for (var i=0; i<music.p.playlist.length; i++) console.log(i + ': ' + music.p.playlist[i]);
	}

	if (myListener.position == 0)
	{
		console.log('Playing: ' + music.p.playlist[music.p.playlist_index]);
		getFlashObject().SetVariable("method:setUrl", '/mp3/' + music.p.playlist[music.p.playlist_index]);
	}
	getFlashObject().SetVariable("method:play", "");
	getFlashObject().SetVariable("enabled", "true");
}
music.pause = function()
{
	getFlashObject().SetVariable("method:pause", "");
}
music.stop = function()
{
	getFlashObject().SetVariable("method:stop", "");
}
music.set_volume = function()
{
	getFlashObject().SetVariable("method:setVolume", volume);
}
music.set_mp3 = function(mp3)
{
	music.stop();
	getFlashObject().SetVariable("method:setUrl", '/mp3/' + mp3);
}

function getFlashObject()
{
	return document.getElementById("myFlash");
}

// -------------------------------------------------------------------------------------------------
// Tool Tips
// -------------------------------------------------------------------------------------------------

var FancyTips = new Class({

	Implements: [Options],

	options : {
		tipcontents: 	'.tipcontents',
		className:		'tooltip_bubble'
	},

	initialize: function (elements, options)
	{

		this.setOptions(options);

		// Set up the fancy tips by inserting the tip contents element
		// into the title attribute of the element that is to have the tooltip
		elements.each(function (el)
		{
			contents_element = el.getElement(this.options.tipcontents) ? el.getElement(this.options.tipcontents) : el.getNext(this.options.tipcontents);
			if (contents_element) el.set('title', contents_element.get('html'));
		},
		this);

		this.tips = new Tips(elements, this.options);
	},
	
	attach: function(elements, options)
	{
		// Attach to one or more elements
		$$(elements).each(function(element)
		{
			var t = element.getElements('.tooltip');
			t.each(function(el)
			{
				contents_element = el.getElement(this.options.tipcontents) ? el.getElement(this.options.tipcontents) : el.getNext(this.options.tipcontents);
				if (contents_element)
				{
					el.set('title', contents_element.get('html'));
					this.tips.attach(el);
				}
			}, this);
		}, this);
	}
	
});

// -------------------------------------------------------------------------------------------------
// Simple script for news ticker - convert an unordered list full of links into a news ticker
// By Rich K
// PORT TO MOOTOOLS
// -------------------------------------------------------------------------------------------------

var ticker = new Object;

ticker.p = new Object;

ticker.init = function()
{
	ticker.p.display = ticker.get_DOM_ref('quote');
	ticker.p.data	 = ticker.get_DOM_ref('quote_data');
	
	// Find <li> in quote_data and start ticker
	ticker.p.items = ticker.p.data.getElementsByTagName('li');
	ticker.p.current = 0;
	ticker.diplay_item();
}

ticker.diplay_item = function()
{
	var item = ticker.p.items[ticker.p.current].getElementsByTagName('b')[0].cloneNode(1);
	var old  = ticker.p.display.getElementsByTagName('b');
	if (old.length) ticker.p.display.removeChild(old[0]);
	ticker.p.display.appendChild(item);
	ticker.p.opacity = 0;	
	ticker.display_item_fade_in();
	
	ticker.p.current++;
	if (ticker.p.current == ticker.p.items.length) ticker.p.current = 0;
	var t = setTimeout('ticker.diplay_item()', 7000);
}

ticker.display_item_fade_in = function()
{
	if (ticker.p.opacity > 1) ticker.p.opacity = 1;
	ticker.set_opacity(ticker.p.opacity)
	if (ticker.p.opacity < 1)
	{
		var t = setTimeout('ticker.display_item_fade_in()', 100);
	}
	ticker.p.opacity += 0.15;
}

ticker.set_opacity = function(fraction)
{
	ticker.p.display.style.filter = 'alpha(opacity=' + (fraction * 100) + ')';
	ticker.p.display.style.opacity = fraction;
}

ticker.get_DOM_ref = function(obj)
{
	// cross-browser function to get an object's DOM reference, given its id
	if (document.getElementById && document.getElementById(obj)) return document.getElementById(obj); // W3C DOM
	if (document.all && document.all(obj)) return document.all(obj); // MSIE 4/5/6
	return false;
}

