(function($) {


// Fix the "Click here to activate..." for Flash/OBJECTS "feature" in IE
// karl.rudd@gmail.com - 10/05/07
if ( $.browser.msie ) {
	$(function() {
		$('object').each( function() {
			this.outerHTML = this.outerHTML;
		});
	});
}

// Switchable Fragments
// karl.rudd@gmail.com - 05/08/07
$(function() {
	// Grab all "fragment" DIVs with ids
	var allFragments = $('div.switchableFragment[@id]');
	if ( !allFragments.length )
		return;

	var allLinks = $('a');
	
	// if there is a current fragment then show it, otherwise show the first
	var currentHash = /#(.+)$/.exec( window.location.href );
	currentHash = currentHash ? currentHash[1] : allFragments[0].id;
	var toShow = $(allFragments).filter('[@id=' + currentHash + ']');
	toShow = toShow.length ? toShow[0] : allFragments[0];
	
	allFragments
		.hide()
		.each( function() {
			
			var fragmentId = this.id;
			var thisFragment = $(this);
			
			// Find any links that link to this DIV 
			var matchingLinks = $(allLinks).filter('[@href=#' + fragmentId + ']');
			
			// Tag links' click() with "showFragment" function		
			matchingLinks.click( showFragment );
	
			function showFragment()
			{
				var thisLink = this;
				
				setTimeout( function() {
					// hide all fragments and show this fragment
					allFragments.hide();
					thisFragment.show();
					
					$(matchingLinks)
						.parent()
							// remove "current" class from link parent's siblings 
							.siblings()
								.removeClass('current')
							.end()
						// tag link parent with "current" class
						.addClass('current');
		
					// remove focus from link
					thisLink.blur();
				}, 0);
				
				return false;
			}
			
			if ( this == toShow )
				showFragment.apply( toShow );
		});
});

// "Active" Link Images 
// karl.rudd@gmail.com - 15/05/07
// Assign a class of "activeImages" to a container, all the image links inside it
// will append "_active" to their "src" when their link is hovered over or focused on.
// e.g.
//	Initial:
//		<p class="activeImages">... <a href="#"><img src="blah.gif"></a>... </p>
//	Hover/Focus:
//		<p class="activeImages">... <a href="#"><img src="blah_active.gif"></a>... </p>
$(function() {
	
	var toPreload = [];
	
	$('.activeImages a > img')
		.each( function() {

			if ( /_active\.\w+$/.test( this.src ) )
				return;

			this['$origsrc'] = this.src;
			this['$activesrc'] = this.src.replace( /(\.\w+$)/, '_active$1' );

			toPreload.push( this['$activesrc'] );
			
			var $img = $(this);
			
			function active() { $img[0].src = $img[0]['$activesrc']; }
			function inactive() { $img[0].src = $img[0]['$origsrc']; }

			$(this.parentNode)	// the <a>
				.hover( active, inactive )
				.focus( active )
				.blur( inactive );
		});

	// preloading "_active" images
	var preloader = new Image();
	function preloadNext()
	{
		//console.log( this.src );
		if ( toPreload.length > 0 )
			preloader.src = toPreload.pop();
	}
	preloader.onload = preloadNext;
	preloader.onerror = preloadNext;
	preloadNext();
});

// "Rotate" randomly through a set of images
// karl.rudd@gmail.com - 05/01/07
// Initial image should have:
//		- "class" of the form "rotating maxNN delayNN"
//				- "max": where NN is the number of images in the set
//				- "delay": where NN is the delay in seconds between changes (optional)
//		- "src" must be of the form "...somethingNN.ext", where NN is the initial image

$(function() {
	$('img.rotating').each( function() {
		
		var img = this;
		
		var maxNumParts = /max(\d+)/.exec( img.className );
		if ( !maxNumParts || maxNumParts.length < 2 )
				return;
		
		var maxNum = parseInt( maxNumParts[1] ) || 1;

		var delayParts = /delay(\d+)/.exec( img.className );
		if ( !delayParts || delayParts.length < 2 )
			var delay = 1000;
		else
			var delay = parseInt( delayParts[1] ) * 1000 || 1000;
		
		var srcParts = /^(.*?)(\d+)(\.\w+)$/.exec( img.src );
		if ( !srcParts || srcParts.length < 4 )
				return;
		
		var baseName = srcParts[1];
		var baseExt = srcParts[3];

		var newNum = Math.floor( Math.random() * maxNum ) + 1;
		img.src = baseName + newNum + baseExt;
		
		function nextImage()
		{
			do {
				var newNum = Math.floor( Math.random() * maxNum ) + 1;
				var newSrc = baseName + newNum + baseExt;
			} while ( newSrc == img.src );

			img.src = newSrc;
		}
		
		setInterval( nextImage, delay );
	});
});

})(jQuery);
