// Initialize header image fader
jQuery(document).ready(function()
{
	var _images		= jQuery("#header img");
	var _navs		= [];
	var _index		= 0;//Math.round(Math.random() * (_images.size()-1));
	var _interval	= 7000;
	var _duration	= 1200;
	var _paused		= false;
	var _text		= jQuery("#header-text");

	// All header text should be placed here
	var _texts		= [
	          		    "<a href='http://www.is-music.net/'>The mixtape exchange.</a>",
	          			"<a href='http://oscar.is-music.net/blog/is-music-continues'>is Music continues!<br/>Different from what you're used to, but we're here to stay!</a>",
	          			"<a href='http://mark.is-music.net/blog/web-developers-wanted'>Web developers wanted!<br/>Join us in sharing people's music over the world.</a>",
	          			"<a href='http://oscar.is-music.net/blog/an-interview-with-kris-redding'>Kris Redding, author of Sounds of the Deep.<br/>Read the interview we did with him!</a>",
	          			"<a href='http://mark.is-music.net/blog/meet-the-team-mark'>Meet Mark.<br/>Our technical guy and code master.</a>",
	          		];
	
	var _links		= [
	          			BASE_URL + "/",
	          			"http://oscar.is-music.net/blog/is-music-continues",
	          			"http://mark.is-music.net/blog/web-developers-wanted",
	          			"http://oscar.is-music.net/blog/an-interview-with-kris-redding",
	          			"http://mark.is-music.net/blog/meet-the-team-mark",
	          		];
	
	// Pause when entering the header
	jQuery("#header").mouseenter(function() {
		_paused = true;
	});
	
	// Unpause when leaving the header
	jQuery("#header").mouseleave(function() {
		_paused = false;
	});

	// Initially hide all but the first
	_images.each(function(i, v) {
		v = jQuery(v);

		if (i != _index)
			v.hide();

		// Little blocks for every image
		var a = jQuery("<a>");
		a.attr("class", "header-nav");
		
		// If we have a links
		if (typeof(_links[i]) != "undefined" && jQuery.trim(_links[i]) != "")
			a.attr("href", jQuery.trim(_links[i]));
		
		jQuery("#header-navs").prepend(a);
		_navs[i] = a;

		// Current image
		if (i == _index)
			a.addClass("header-nav-active");

		a.mouseover(function() {
			jQuery(_images[_index]).hide();
			jQuery(_navs[_index]).removeClass("header-nav-active");
			
			_index = i;
	
			jQuery(_images[_index]).show();
			jQuery(_navs[_index]).addClass("header-nav-active");

			_text.html(_texts[_index]);
		});
	});

	// Set initial text
	_text.html(_texts[_index]);

	// After the interval continue with the next image
	window.setInterval(function()
	{
		if (_paused)
			return;

		// Fade out the current image
		jQuery(_images[_index]).fadeOut(_duration);
		jQuery(_navs[_index]).removeClass("header-nav-active");

		// Continue to the next image or restart if needed
		var _previndex = _index;
		if (++_index >= _images.size())
			_index = 0;

		// Fade in the next image
		jQuery(_images[_index]).fadeIn(_duration);
		jQuery(_navs[_index]).addClass("header-nav-active");

		// Fade out text
		_text.fadeOut(
			_duration/2,
			function()
			{
				_text.html(_texts[_index]);
				_text.fadeIn(_duration/2);
			}
		);
	},
	_interval
	);
});
