/* Stories home page component - Operations executed when the page loads
 * 
 * University of Ottawa
 * Computing and Communications Services
 */


var Stories = {
	
	objStoryDisplayed : null,
	objRotationId : null,
	intRotationLength : 6000, /* milliseconds */
	
	init : function () {
		$('#stories-tabs li a').each( function() {
			$(this).attr( 'href', '#' + $(this).parent().attr('id').replace( /-tab/, '') );
		});
		$('#stories-spotlight li').each( function() {
			$(this).attr( 'id', $(this).attr('id') + '-panel' );
		});
		$('#stories-spotlight li').removeClass('active');
		
		$('#stories-tabs li a').click( function() {
			Stories.displayStory( $(this).attr('href') );
			Stories.stopRotation();
		});
		
		if ($(window.location.hash+'-panel').length > 0) { /* the referenced story exists */
			Stories.displayStory( window.location.hash );
		} else {
			Stories.displayStory( Stories.getDefaultStory() );
		}
		
		$(window).load(function () {
			/* Wait until images are done loading before starting the rotation */
			Stories.startRotation();
		});
		
	},
	
	displayStory : function ( strStoryId ) {
		var strStoryId = ( strStoryId!=null && strStoryId!='' ) ? strStoryId : Stories.getDefaultStory();
		$('#stories-spotlight li').fadeOut('normal');
		$('#stories-spotlight li'+strStoryId+'-panel').fadeIn('normal');
		$('#stories-tabs li').removeClass('active');
		$('#stories-tabs li'+strStoryId+'-tab').addClass('active');
		Stories.objStoryDisplayed = $('#stories-spotlight li'+strStoryId+'-panel');
	},
	
	getDefaultStory : function () {
		return '#' + $('#stories-spotlight li').eq(0).attr('id').replace( /-panel/, '');
	},
	
	displayNextStory : function () {
		var objNextStory = Stories.objStoryDisplayed.next();
		var objFirstStory = Stories.objStoryDisplayed.siblings().eq(0);
		
		if (objNextStory.attr('id') == undefined) {
			objNextStory = objFirstStory;
		}
		
		Stories.displayStory( "#" + objNextStory.attr('id').replace( /-panel/, '')  );
	},
	
	startRotation : function () {
		Stories.objRotationId = setInterval (Stories.displayNextStory, Stories.intRotationLength);
	},
	
	stopRotation : function () {
		clearInterval (Stories.objRotationId);
	}
}
