// JavaScript Document

var primaryFeatureTabsApi;

$(document).ready(function(){

		// initialize image rotator slideshow (jQueryTools Tabs) on homepage:
		// http://flowplayer.org/tools/tabs/slideshow.html
		if($("#image-rotator-controller-tabs").length){
			$("#image-rotator-controller-tabs").tabs("div#image-rotator-panes > div.image-rotator-pane",{
		
				// enable "cross-fading" effect
				effect: 'fade',
				fadeInSpeed: 1000,
				fadeOutSpeed: 1000,
			
				// start from the beginning after the last tab
				rotate: true
		
			}).slideshow({
				
				autoplay: true,
				interval: 10000,
				clickable: false
				
			});
		}

		pickRandomImage();

	
	
}); // end doc ready


//Random Image Picker
/*
Look for an array named featureImages and a div with #feature-box.
If they exist, pick at random an item from the array.
array example:

	featureImages[
							["filename1.jpg","alt text 1"],
							["filename2.jpg","alt text 2"]
							]
*/
function pickRandomImage(){
	if($("#feature-box").length && typeof(featureImages)!="undefined"){
		//get the dimensions of the feature-box for use in the randomly picked image
		//These should be the same, but this ensures it
		var h = $('div#feature-box').height();
		var w = $('div#feature-box').width();
		//remove any exiting images from the feature-box
		$('#feature-box img').remove();
		//get random index of array
		var randomIndex = Math.round(Math.random()*(featureImages.length-1));
		//append random image to #feature-box
		$('<img src="' + featureImages[randomIndex][0] + '" alt="' + featureImages[randomIndex][1] + '" height="' + h + '" width="' + w + '">').appendTo('#feature-box');
	}
}




/*----------------------------------------------------------------*/
/* General purpose functions */


function convertSecondsToDuration(seconds){
	var t = seconds;
	var h = Math.floor(t / 3600);
	t %= 3600;
	var m = Math.floor(t / 60);
	var s = t % 60;
	
	var output = "";
	
	//hours
	output += (h > 0 ? h + ':' : '');
	
	//minutes (only zero pad if there are hours
	if(m>0){
		output += (h > 0 ? zeroPad(m,2) : m)
	}

	//seconds
	output += ':' + zeroPad(s,2);
	
	return output;
	//return (h > 0 ? zeroPad(h,2) + ':' : '') + (m > 0 ? zeroPad(m,2) : '') + (':' + zeroPad(s,2));
}

//add leading zeros
function zeroPad(num,count) {
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + numZeropad;
	}
	return numZeropad;
}








/**
 * Truncate a string to the given length, breaking at word boundaries and adding an elipsis
 * @param string str String to be truncated
 * @param integer limit Max length of the string
 * @return string
 
 http://snipplr.com/view.php?codeview&id=16108
 
 */
var truncate = function (str, limit) {
	var bits, i;
	if ("string" !== typeof str) {
		return '';
	}
	bits = str.split('');
	if (bits.length > limit) {
		for (i = bits.length - 1; i > -1; --i) {
			if (i > limit) {
				bits.length = i;
			}
			else if (' ' === bits[i]) {
				bits.length = i;
				break;
			}
		}
		bits.push('...');
	}
	return bits.join('');
};
// END: truncate

