/*
 * Carousel effect.
 */

$.fn.carousel = function() {

	function repeat(str, num) {
		return new Array(num + 1).join(str);
	}
	
	return this.each(function() {
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> ul'),
			$items = $slider.find('> li'),
			$single = $items.filter(':first'),
			singleWidth = $single.outerWidth(), 
			visible = Math.ceil($wrapper.innerWidth() / singleWidth), // Note: doesn't include padding or border
			currentPage = 1,
			pages = Math.ceil($items.length / visible);
		
		// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
		if (($items.length % visible) != 0) {
			$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
			$items = $slider.find('> li');
		}
		
		// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
		$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
		$items = $slider.find('> li'); // Reselect
		
		// 3. Set the left position to the first 'real' item
		$wrapper.scrollLeft(singleWidth * visible);
        
		// 4. Paging function
		function gotoPage(page) {
			var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir * visible * n;
				
			$wrapper.filter(':not(:animated)').animate({
				scrollLeft : '+=' + left
			}, 500, function() {
				if (page == 0) {
					$wrapper.scrollLeft(singleWidth * visible * pages);
					page = pages;
				} else if (page > pages) {
					$wrapper.scrollLeft(singleWidth * visible);
					// Reset back to start position
					page = 1;
				}
				currentPage = page;
			});
			return false;
		}
		
		$wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
		
		// 5. Bind to the forward and back buttons
		$('a.back', this).click(function() {
			return gotoPage(currentPage - 1);
		});
		
		$('a.forward', this).click(function() {
			return gotoPage(currentPage + 1);
		});
		
		// Create a public interface to move to a specific page
		$(this).bind('goto', function(event, page) {
			gotoPage(page);
		});
	});
};

$(document).ready(function() {
	$('.carousel').carousel();
});

/*
 * jPlayer as a stylish audio and playlist player.
 */
 
$(document).ready(function() {

	var playItem = 0;
	
	var myPlayList = [
		{name:"Cyclone (Mindheart Remix)", mp3:"/wp-content/themes/mindheartproductions/audio/cyclone_mindheart_remix.mp3"},
		{name:"European Righteous Kill", mp3:"/wp-content/themes/mindheartproductions/audio/european_righteous_kill.mp3"},
		{name:"IV (Remix)", mp3:"/wp-content/themes/mindheartproductions/audio/iv_remix.mp3"},
		{name:"Whut", mp3:"/wp-content/themes/mindheartproductions/audio/whut.mp3"}
	];
	
	// Local copy of jQuery selectors, for performance
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	
	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay
		},
		nativeSupport: false, oggSupport: true, customCssIds: false, swfPath: "/wp-content/themes/mindheartproductions/js/", errorAlerts: true, warningAlerts: true
	})
	
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));
	})
	
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});
	
	$("#jplayer_previous").click(function() {
		playListPrev();
		return false;
	});
	
	$("#jplayer_next").click(function() {
		playListNext();
		return false;
	});
	
	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			// $("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#jplayer_playlist_item_"+i).data("index", i).click(function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange(index);
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}
	
	function playListInit(autoplay) {
		if(autoplay) {
			playListChange(playItem);
		} else {
			playListConfig(playItem);
		}
	}
	
	function playListConfig(index) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
	}
	
	function playListChange(index) {
		playListConfig(index);
		$("#jquery_jplayer").jPlayer("play");
	}
	
	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange(index);
	}
	
	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange(index);
	}
});

/*
 * Fancybox plugin, a tool for displaying images.
 */

$(document).ready(function() {
	$("a.image, a[rel=gallery]").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	: 'over'
	});
});

/*
 * Animating back-to-top anchor.
 */

$(document).ready(function() {
	$('a#top').click(function() {
		$('body,html').animate({
			scrollTop: 0
		},
			500);
	});  
});

/*
 * SFWObject plugin activation for promo banners.
 */

swfobject.embedSWF("/promos/promo_1.swf", "promo_1", "300", "100", "9.0.0");
swfobject.embedSWF("/promos/promo_2.swf", "promo_2", "300", "100", "9.0.0");

/*
 * Appending on-the-fly code for creating buying buttons in the CD posts.
 */

$(document).ready(function() {
	var $boton_itunes = $('div.disco p a:contains(iTunes)');
	$boton_itunes.parent().parent().addClass('boton_itunes');
	$boton_itunes.wrapInner('<span>').append('<img src="http://devel.mindheartproductions.com/wp-content/themes/mindheartproductions/images/blank.gif" alt="Blank image">');
	var $boton_fnac = $('div.disco p a:contains(FNAC)');
	$boton_fnac.parent().parent().addClass('boton_fnac');
	$boton_fnac.wrapInner('<span>').append('<img src="http://devel.mindheartproductions.com/wp-content/themes/mindheartproductions/images/blank.gif" alt="Blank image">');
});

