/*
 * Author: Joe Tan (joetan54@gmail.com)
 */

/*
 * Homepage Feature rotation
 */

var featureDelay = 8000;

// setup feature tabs
jQuery(function($) {
	$('#feature .tabs a').each(function(i, elem) {
		$(this).data('pos', i);
	});

	// animate feature
	$('#feature .tabs a').click(function() {
		$(this).addClass('selected').siblings().removeClass('selected');
		var offset = $(this).data('pos') * -695;
		$('#feature .window').animate({left:offset}, 300)
		if ($('#feature').data('timer')) clearTimeout($('#feature').data('timer'));
		
		return false;
	});
	$('#feature .tabs a').filter(':first').addClass('selected').end().filter(':last').addClass('last');

});

function rotateFeatures() {
	var $ = jQuery;

	if ($('#feature').data('timer')) clearTimeout($('#feature').data('timer'));
	var current = $('#feature .tabs a.selected');
	if (current.next().length > 0) {
		current.next().click()
	} else {
		$('#feature .tabs a:first').click();
	}
	$('#feature').data('timer',  setTimeout(rotateFeatures, featureDelay));
}
jQuery(function($) {
	$('#feature').data('timer',  setTimeout(rotateFeatures, featureDelay));
});



/*
 * Homepage podcast player
 */
var playerObj = new Object();
playerObj.onInit = function() {
	this.position = 0;
};
playerObj.onUpdate = function() {
	var $ = jQuery;
	var seconds = Math.round(this.position/1000);
	var minutes = Math.floor(seconds / 60);
	
	seconds = seconds - (minutes*60);
	if (seconds < 10) seconds = "0"+seconds;
	if (minutes < 10) minutes = "0"+minutes;
	var statusMessage = ''
	if (this.isPlaying == "true") {
		statusMessage = ' ';
	} else {
		statusMessage = 'paused ';
	}
	statusMessage = statusMessage + minutes + ':'+seconds;

	if (this.bytesPercent >= 100) {
		var totalSeconds = Math.round(this.duration/1000);
		var totalMinutes = Math.floor(totalSeconds / 60);
		totalSeconds = totalSeconds - (totalMinutes*60);
		if (totalSeconds < 10) totalSeconds = "0"+totalSeconds;
		if (totalMinutes < 10) totalMinutes = "0"+totalMinutes;

		statusMessage = statusMessage + ' of ' + totalMinutes +':'+totalSeconds;
	} else if (this.bytesPercent > 0) {
		var totalSeconds = Math.round(this.duration/1000);
		var estimatedSeconds = Math.floor((totalSeconds / this.bytesPercent) * 100);
/*
		var totalMinutes = Math.floor(estimatedSeconds / 60);
		totalSeconds = estimatedSeconds - (totalMinutes*60);
		if (totalSeconds < 10) totalSeconds = "0"+totalSeconds;
		if (totalMinutes < 10) totalMinutes = "0"+totalMinutes;

		statusMessage = statusMessage + ' of ' + totalMinutes +':'+totalSeconds;
*/
		statusMessage = statusMessage + ' ('+this.bytesPercent+'%)';
	}
	$('#podcast .status').html(statusMessage);
}


jQuery(function($) {
	$('#podcast .play').click(function() {
		if (playerObj.isPlaying == "true") {
			stopMessage();
		} else {
			playMessage($(this).attr('href')); // play the current message
		}
		return false;
	});
	$('#podcast .message:not(:first)').hide();
	
	$('#podcast #controls').hover(function() {
		$('#podcast .campuses').removeClass('hide');
		setTimeout(function() {
			var $ = jQuery;
			if (!$('#podcast .campuses').hasClass('hide')) $('#podcast .campuses').slideDown(300).addClass('visible');
		}, 400);
	}, function() {
		$('#podcast .campuses').addClass('hide');
		setTimeout(function() {
			var $ = jQuery;
			if ($('#podcast .campuses').hasClass('hide')) $('#podcast .campuses').slideUp(100).removeClass('visible').removeClass('hide');
		}, 1000);
	});
	
	
	$('#podcast .campuses a:first').addClass('selected');

	$('#podcast .campuses a').each(function(i, elem) {
		$(this).data('pos', i);
	}).click(function() {
		stopMessage();
		
		$(this).addClass('selected').siblings().removeClass('selected');
		$('#podcast .message').eq($(this).data('pos')).show().siblings('.message').hide();
		$('#podcast .campus').html($(this).filter('a').text());
		$('#podcast .status').html('');
		
		$('#podcast .campuses').slideUp(100).removeClass('visible')
		return false;
	});

	var flashvars = {
		listener:'playerObj'
	}
	var params = {
		AllowScriptAccess:'always'
	}
	swfobject.embedSWF(baseURL + '/swf/player_mp3_js.swf', 'player', '1', '1', '7.0.0', false, flashvars, params);

}); 
function getFlashPlayer() {
	return document.getElementById('player');
}
function stopMessage() {
	var $ = jQuery;
	if (playerObj.isPlaying == "true") getFlashPlayer().SetVariable("method:pause", "");
	$('#podcast').removeClass('playing');
}
function playMessage(mp3) {
	var $ = jQuery;
	if (playerObj.isPlaying == "true") return false;

	if ($('#podcast').data('mp3') != mp3) {
		$('#podcast').data('mp3', mp3);
		getFlashPlayer().SetVariable("method:setUrl", mp3);
		getFlashPlayer().SetVariable("enabled", "true");
	}
	$('#podcast').addClass('playing');
	getFlashPlayer().SetVariable("method:play", "");
}


/*
 * Events scrolling widget
 */

jQuery(function($) {
	$('#events .window').jScrollPane({
		scrollbarWidth:15,
		scrollbarMargin:3,
		showArrows:true
	});
});