var bioDialogs =  new Array();
var headshotsOffset;
jQuery(document).ready(function() {
	document.getElementById('headshots').style.visibility = "hidden";
	headshotsOffset = $('#headshots li:eq(3)').offset();
	headshotsOffset = headshotsOffset.left;
	$('#headshots li').css('cursor','pointer');
    jQuery('#headshots').jcarousel({
        start: 3,
        itemLoadCallback: closeDialogs,
        initCallback: delayCarouselShow
    });
    
    var dialogName;
    
    $('#headshots li').hover( // add hover class to #headshots
    	function () {
    		$(this).addClass('hover');
    	},
    	function () {
    		$(this).removeClass('hover');
    	} 
    );
	$('#headshots li').click(function(e) {
		closeDialogs();
		dialogName = $(this).children('img').attr('alt'); // use img alt attribute for associative array
		if ($(this).data('alreadyDialog') != 1) { // check if dialog already initialized; if not, create and set status to initialized
			$(this).data('alreadyDialog', 1);
			bioDialogs[dialogName] = $(this).children('div').dialog( {
				autoOpen: false,
				width: 400,
				height: 'auto',
				closeText: 'close [x]'
			});
			bioDialogs[dialogName].dialog('open');
		}
		else { // dialog already initialized, so just open it
			bioDialogs[dialogName].dialog('open');
		}
		return false;
	});
});
function delayCarouselShow () { 
	if ($('#headshots li:eq(3)').offset().left < headshotsOffset) {
		$('#headshots').css('visibility','visible');
		getTweets();
		return true;
	}
	else {
		window.setTimeout("delayCarouselShow()", 100);
		return false;
	}
}
function closeDialogs () {  // close any already open dialogs
	for (var bioDialog in bioDialogs) {
    	if (bioDialogs[bioDialog] != null && bioDialogs[bioDialog].dialog('isOpen') == true) {
    		bioDialogs[bioDialog].dialog('close');
    	}
    }
}
function getTweets () {
	getTwitters('tweet', { 
		id: 'churnless', 
		count: 1, 
		enableLinks: true, 
		ignoreReplies: true, 
		clearContents: true,
		template: '%text% <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
	});
}
