$(document).ready(function($){

	// Slideshow
	if ($('#featured-slides').length > 0) {
		$('#featured-slides').cycle({
			fx:      				'fade',
			timeout:  				5000,
			pause:					true,
	    	pauseOnPagerHover:		true,
	    	before: 				featuredCaption,
			pager:					'#featured-slide-nav ul',
			pagerAnchorBuilder:		pagerFactory
		});
	}
	
	function pagerFactory(idx, slide) {
		var count = $('.featured-slide').size() - 1;
		var s = idx > count ? ' style="display:none"' : '';
		return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
	};
	
	function featuredCaption() {
		var caption = $(this).find('h3 a').html();
		$('#featured-slide-nav-caption').fadeOut('fast', function() {
			$(this).html(caption).fadeIn();
		});
	}
	
	// Overlabel
	$('.overlabel').overlabel();
	
	// Twitter
	$("#tweets").tweet({
		username: "mobmag",
		avatar_size: 40,
		count: 5,
		loading_text: "loading&hellip;"
	});
	
	
	// jPlayer
	var playItem = 0;
	var myPlayList = [
		{name:"This Is For My People",mp3:"http://mobmag.in/music/01_This_Is_For_My_People.mp3"},
		{name:"But It Rained",mp3:"http://mobmag.in/music/02_But_It_Rained.mp3"},
		{name:"Nada Nada",mp3:"http://mobmag.in/music/03_Nada_Nada.mp3"},
		{name:"Khidki",mp3:"http://mobmag.in/music/04_Khidki.mp3"},
		{name:"Ganga",mp3:"http://mobmag.in/music/05_Ganga.mp3"},
		{name:"Ghir Ghir",mp3:"http://mobmag.in/music/06_Ghir_Ghir.mp3"}
	];
	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		},
		swfPath: "/wp-content/themes/mob/js/jquery/jplayer"
	})
	.jPlayerId("play", "player_play")
	.jPlayerId("pause", "player_pause")
	.jPlayerId("stop", "player_stop")
	.jPlayerId("loadBar", "player_progress_load_bar")
	.jPlayerId("playBar", "player_progress_play_bar")
	.jPlayerId("volumeMin", "player_volume_min")
	.jPlayerId("volumeMax", "player_volume_max")
	.jPlayerId("volumeBar", "player_volume_bar")
	.jPlayerId("volumeBarValue", "player_volume_bar_value")
	.onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		var myPlayedTime = new Date(playedTime);
		var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
		var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
		$("#play_time").text(ptMin+":"+ptSec);

		var myTotalTime = new Date(totalTime);
		var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
		var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
		$("#total_time").text(ttMin+":"+ttSec);
	})
	.onSoundComplete( function() {
		playListNext();
	});
	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});
	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});
	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#playlist_item_"+i).data( "index", i ).hover(
				function() {
					if (playItem != $(this).data("index")) {
						$(this).addClass("playlist_hover");
					}
				},
				function() {
					$(this).removeClass("playlist_hover");
				}
			).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").play();
				}
			});
		}
	}
	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}
	function playListConfig( index ) {
		$("#playlist_item_"+playItem).removeClass("playlist_current");
		$("#playlist_item_"+index).addClass("playlist_current");
		playItem = index;
		$("#jquery_jplayer").setFile(myPlayList[playItem].mp3);
	}
	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_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 );
	}

});


