var _msw = null;
function _mswNext()
{
	_msw.move(1);
}
function _mswShow()
{
	_msw.show(true);
}
function _mswPause()
{
	_msw.paused = true;
}
function _mswResume()
{
	_msw.paused = false;
}

MixtapeSwitcher = function(containerID)
{
	this.perpage = 3;
	this.curpage = 0;
	this.paused = false;
	this.mustShow = false;
	this.busy = false;
	
	this.effect = "slide";
	this.propsHide = { direction: "left" };
	this.propsShow = { direction: "right" };
	this.speed = 800;
	this.interval = 12000;
	
	this.navpane = jQuery("<div>");
	this.navpaneHeight = 25;
	
	// Get all mixtape divs
	var mixtapeDivs = jQuery("#" + containerID + " div.mixtape");
	mixtapeDivs.remove();
	
	// Contains the pages
	this.divs = [];
	
	// This is where the pages will be placed in
	this.container = jQuery("#" + containerID);
	this.container.height(this.perpage * 120 + this.navpaneHeight + 25);
	
	// Make sure the thing is paused on mouse over and resumes when removed
	this.container.mouseenter(function() { _mswPause(); });
	this.container.mouseleave(function() { _mswResume(); });
	
	// Create all pages
	this.pageCount = Math.ceil(mixtapeDivs.length / this.perpage);
	for (var i = 0; i < this.pageCount; i++)
	{
		// Create a new page
		var div = jQuery("<div>");
		div.attr("id", "mixtape-page-" + i);
		div.addClass("mixtape-page");
		div.css("position", "absolute");
		div.hide();
		
		// Add mixtapes to this page
		var from = i * this.perpage;
		var to = from + this.perpage;
		for (var j = from; j < to; j++)
		{
			jQuery(mixtapeDivs[j]).addClass("mixtape-frontpage");
			div.append(mixtapeDivs[j]);
		}
		
		this.divs[i] = div;
		this.container.append(div);
	}
	
	// Create a navpane that can be used to navigate to the previous or next page
	this.navpane.addClass("mixtapes-frontpage-navpane");
	this.navpane.css("margin-top", this.container.height() - this.navpaneHeight);
	
	// Add prev button
	var prev = jQuery("<div>");
	prev.addClass("prev");
	prev.height(this.navpaneHeight);
	prev.click(
		function()
		{
			_msw.move(-1, true);
		}
	);
	
	// Add next button
	var next = jQuery("<div>");
	next.addClass("next");
	next.height(this.navpaneHeight);
	next.click(
			function()
			{
				_msw.move(1, true);
			}
		);
	
	// Finally add the navpane
	this.navpane.append(prev);
	this.navpane.append(next);
	this.container.append(this.navpane);
	
	_msw = this;
	setMixtapeDescriptionClicks();
	
	// Show the first one and start the interval
	jQuery(this.divs[0]).show();
	setInterval(_mswNext, this.interval);
};

MixtapeSwitcher.prototype.updateHeights = function()
{
	var pageID = this.divs[this.curpage].attr("id");
	var mixtapes = jQuery("#" + pageID + " > .mixtape");
	var height = 0;
	for (var i = 0; i < mixtapes.length; i++)
	{
		height += jQuery(mixtapes[i]).height();
	}
	this.container.height(height + this.navpaneHeight + 25);
	this.navpane.css("margin-top", this.container.height() - this.navpaneHeight);
}

MixtapeSwitcher.prototype.closeAllDescriptions = function()
{
	jQuery("div.mixtape > .mixtape-description").hide();
	this.updateHeights();
}

MixtapeSwitcher.prototype.move = function(delta, force)
{
	force = force !== true ? false : true;
	delta = typeof(delta) == "number" && (delta == 1 || delta == -1) ? delta : 1;
	
	// Don't do this when paused or busy, except when forced
	if ((this.paused && !force) || this.busy)
		return;
	
	this.closeAllDescriptions();
	this.busy = true;
	
	// Advance to the next page
	var prevpage = this.curpage;
	this.curpage += delta;
	if (this.curpage >= this.pageCount) this.curpage = 0;
	else if (this.curpage < 0) this.curpage = this.pageCount - 1;
	
	// Override when paused
	this.mustShow = true;
	
	// Depending on the direction, hide/show with the correct effect
	if (delta == -1)
	{
		// Move backwards
		jQuery(this.divs[prevpage]).hide(this.effect, this.propsShow, this.speed);
		jQuery(this.divs[this.curpage]).show(this.effect, this.propsHide, this.speed);
		setTimeout("_msw.busy = false;", this.speed);
	}
	else
	{		
		// Move forward
		jQuery(this.divs[prevpage]).hide(this.effect, this.propsHide, this.speed);
		jQuery(this.divs[this.curpage]).show(this.effect, this.propsShow, this.speed);
		setTimeout("_msw.busy = false;", this.speed);
	}
};

function onMixtapeDescriptionShown()
{
	if (typeof(_msw) != "undefined" && _msw != null)
	{
		_msw.updateHeights();
	}
}

var _supressMixtapeDescription = false;
function setMixtapeDescriptionClicks()
{
	jQuery(".mixtape a, .mixtape img").click(
		function()
		{
			_supressMixtapeDescription = true;
		}
	);
	
	jQuery(".mixtape").click(
		function()
		{
			if (!_supressMixtapeDescription)
			{
				var duration = jQuery(this).hasClass("mixtape-frontpage") ? 0 : 400;
				
				var parts = this.id.split('-');
				var d = jQuery("#mixtape-description-" + parts[1]);
				
				if (d.is(":visible"))
				{
					d.hide("blind", {}, duration, onMixtapeDescriptionShown);
				}
				else
				{
					d.show("blind", {}, duration, onMixtapeDescriptionShown);
				}
			}
			_supressMixtapeDescription = false;
		}
	);
}

jQuery(document).ready(setMixtapeDescriptionClicks);