// var dudeCount = 15;

function random (ceil) { var ceil = ceil || 10; return Math.floor(Math.random()*ceil)+1; }
function pad(number, length, padChar) {
	var padChar = padChar || '0';
	var str = '' + number;
	while (str.length < length) { str = padChar + str; }
	return str;
}

jQuery(function ($) {

	/** Activate scrolling news ticker **/
	if ($().liScroll) {
		$('div#ticker>ul').liScroll();
	}

	/** Activate rotating banner **/
	var banner = $('div#banner');

	$('#banner-features').scrollable({
		circular: true,
		easing: 'easeInOutExpo',
		items: '.items',
		speed: 1000,
		onBeforeSeek: function (e, i) {
			var info = banner.find('div.feature div.info');
			info.slideUp('medium');
		},
		onSeek: function (e, i) {
			setTimeout(function() {
				banner.find('div.feature div.info').slideDown('fast');
				update_dude_url();
				setTimeout(function() { show_dude(); }, 500);
			}, 250);
		}
	}).navigator({
		// indexed: true,
		navi: 'div#banner-navigator'
	}).autoscroll({
		interval: 8000
	});

	/** Display banner dude **/
	// TODO Remove these from global variable scope
	var headshot = $('#banner-headshot');
	var headshotLink = headshot.find('a');
	var headshotPhoto = headshot.find('img');

	/**
	 * Bring headshot overlay into view
	 */
	function show_dude () {
		headshot.animate({bottom: '-5px', right: '0'}, 'slow', 'easeOutExpo');
	}

	/**
	 * Slide headshot overlay out of view
	 */
	function hide_dude () {
		headshot.animate({bottom: '-400px', right: '-50px'}, 'medium', 'easeInExpo');
	}
	
	/**
	 * Replace headshot overlay with a randomly selected one
	 */
	function swap_dude () {
		headshotPhoto.attr('src', '/UserFiles/images/banners/headshots/'+pad(random(dudeCount), 3)+'.png');
		// headshotPhoto.attr('src', '/img/public/banners/headshots/'+dudes.sort(randOrder)[0]);
	}

	/**
	 * Set the headshot overlay URL to match the current feature item URL
	 */
	function update_dude_url () {
		var scrollableApi = $('#banner-features').data('scrollable');
		headshotLink.attr('href', $(scrollableApi.getItems()[scrollableApi.getIndex()]).find('a').attr('href'));
	}

	if ($('#banner-features').length) {
		setTimeout(function() { /*swap_dude();*/ update_dude_url(); show_dude(); }, 500);
	}

	/** Balance Home Page Boxes **/
	// setTimeout(function() {
	// 	$('div.line.balance').each(function () {
	// 		var boxes = $(this).find('div.box');
	// 		var heights = [];
	// 		boxes.each(function () { heights.push($(this).height()); });
	// 		var matchHeight = Math.max.apply(Math, heights);
	// 		boxes.each(function () {
	// 			if ($(this).height() < matchHeight) {
	// 				var bd = $(this).find('.bd');
	// 				bd.animate({
	// 					height: ((matchHeight - $(this).height()) + bd.height())
	// 				}, 'fast', 'easeInExpo');
	// 			}
	// 		});
	// 	});
	// }, 1000);

	/** Balance Content Columns **/
	setTimeout(function () {
		var boxes = $('div#main-content, div#left-col, div#right-content');
		if (boxes.length > 2) {
			var main = $('#main-content');
			var right = $('#right-content');
			var mainHeight = main.height();
			var rightHeight = right.height();
			var offset = 4;

			if (rightHeight > mainHeight) {
				main.animate({
					height: (rightHeight - offset)
				}, 'slow', 'easeInExpo');
			} else {
				right.animate({
					height: (mainHeight + offset)
				}, 'slow', 'easeInExpo');
			}
		}
	}, 500);

	setTimeout(function () {
		var notifier = $('div#notifier');
		if (notifier.html() && notifier.html().length !== 0) {
			notifier.hide().slideDown('slow');
		}
	});

	/** Modal Dialogs **/
	if ($().colorbox) {
		$('a.lightbox').colorbox({
			initialHeight: 0,
			initialWidth: 0,
			maxHeight: "90%",
			maxWidth: "90%",
			current: "",
			onComplete: function () { $('#colorbox').addClass('cboxShadowed'); },
			onCleanup: function () { $('#colorbox').removeClass('cboxShadowed'); }
		});
	}

});

