// JavaScript Document

/* 
| Homepage Highlights v2.0
| Eric M. Roberts
| 12/9/2011
*/

/* Navigation Options */

var OpenArticleOnClickInactive			=	true;		//default is true; clicking an inactive HPHL item will make it the active one.
var OpenArticleOnClickActive				=	true;		//default is true; clicking the active HPHL item will open link.

/* Transition Options */

var DisableTransitions							=	false;	//default is false;
var ImageTransitionOnPagingControls	=	false;	//default is false; crossfade won't occur when 'previous'/'next' buttons used.
var ImageTransitionOnNavSelect			=	false;	//default is false; crossfade won't occur on selection from left navigation.
var ImageTransitionSpeed						=	750;		//default is 'slow'; 'slow','medium','fast', or integer value in microseconds.
var TitleTransitionSpeed						=	500;		//default is 'medium'; 'slow','medium','fast', or integer value in microseconds.
var TitleTransitionOnPaging					=	true;		//default is true; Title/Subtitle Slide-up effect on  prev/next button-initiated transitions. 

/* DO NOT CHANGE ANYTHING BELOW THIS POINT */

var Highlights; var TransitionTimer; var BubbleTimer;
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"");};

function HPHLObject(){this.title = null;this.subtitle = null;this.image = null;this.url = null;this.duration = 6;};

	$('document').ready(function(){
		Highlights = new HPHL();
		$('#HPHLNav ul li p').each(function(ind,val){$(this).css('top',(parseInt(64-$(this).innerHeight())*.5)-2+'px')});//E. Roberts 12/19/2011 - Vertical Centering of Nav Items
		Highlights.Init(); $('#HPHLCurrent').on('click',function(){location.href = Highlights.GetContent()[Highlights.GetCurrentIndex()].url});
		$('#HPHLPrev').on('click',function(){Highlights.Prev();});$('#HPHLNext').on('click',function(){Highlights.Next();});
		if(!OpenArticleOnClickInactive){$('#HPHLNav ul li a').click(function(e){e.preventDefault();Highlights.Clicked(e);});}
	});

function HPHL(){
	var that = this; var contents = new Array();
	var total = function(){return $('#HPHLNav ul li').size();};
	var nextI = function(){return (currentI() == (total()-1))	?	0	:	currentI() + 1;}
	var prevI = function(){return (currentI() == 0)	?	total() - 1	:	currentI() - 1;}
	var currentI = function(){return $('#HPHLNav ul li.Active').index();}
	this.Init=function(){
		$('#HPHLNav ul li').each(function(i,e){
			var obj = new HPHLObject();	var Img = $(e).find('img');
			obj.duration = parseInt(RegExp(/Duration_([0-9]+)/).exec($(this).attr('class')).pop());
			obj.title = $(e).text().trim();	obj.url = $(e).find('a').attr('href');
			obj.subtitle = $(e).find('a').attr('title'); obj.alt = $(Img).attr('alt');
			obj.src = $(Img).attr('src');	contents.push(obj);})
		Ni = nextI();	that.SetTimer(contents[Ni].duration,Ni);}
	this.GetContent = function(){return contents;}
	this.GetTotal = function(){return total();}
	this.GetCurrentIndex = function(){return currentI();}
	this.GetNextIndex = function(){return nextI();}
	this.GetPrevIndex = function(){return prevI();}
	this.GetCurrentDuration = function(){return contents[currentI()].duration;}
	this.Next = function(){return (ImageTransitionOnPagingControls)	?	that.Transition(nextI()) :	that.Switch(nextI());}
	this.Prev = function(){return (ImageTransitionOnPagingControls)	?	that.Transition(prevI()) :	that.Switch(prevI());}
	this.Clicked = function(e){var curI = Highlights.GetCurrentIndex();	var clkI = $($(e.currentTarget).parentsUntil('li').parent()).index();
		return (clkI == curI && OpenArticleOnClickActive)
		?	location.href = that.GetContent()[curI].url
		: (ImageTransitionOnNavSelect	?	that.Transition(clkI)	:	that.Switch(clkI));}	
	
	
	this.Transition=function(i){
		if(DisableTransitions) return that.Switch(i);
		var iSpeed = ImageTransitionSpeed	?	ImageTransitionSpeed	:	'slow';
		var tSpeed = TitleTransitionSpeed	?	TitleTransitionSpeed	:	'medium';
		
		clearTimeout(BubbleTimer);		
		BubbleTimer = setTimeout(function(){
			$('#MarqueeText').animate({opacity: 0},25,function(){
				$(this).animate({top:'30px'},25,function(){
					$('#MarqueeText .HPHLSubtext').hide();
					$('#MarqueeText a').attr('title',contents[i].subtitle).attr('href',contents[i].url).html(contents[i].title);
					$('#MarqueeText .HPHLSubtext').html(contents[i].subtitle);
					$('#MarqueeText .HPHLSubtext').fadeIn(tSpeed,'swing');});
			$(this).animate({top: '0px', opacity: 1},250,'swing');});},200);
			
	$('#HPHLCurrent a').attr('title',contents[i].alt);//E. Roberts 12/16/2011 - Correctly Changes title Attribute of Image On Transition
	$('#HPHLCurrent a').attr('href',contents[i].url);
	$('<img class="HPHLImage" alt="' + contents[i].alt + '" src="' + contents[i].src + '" />').insertBefore('#HPHLCurrent .HPHLImage:first-child');
	$('#HPHLCurrent .HPHLImage:not(:first-child)').fadeTo(iSpeed,0,function(){
		$('#HPHLCurrent .HPHLImage:not(:first-child)').remove();});
		$('#HPHLNav ul li.Active').removeClass('Active');	$($('#HPHLNav ul li').get(i)).addClass('Active');
		Ni = nextI();	that.SetTimer(contents[Ni].duration,Ni);}	
	
	
	this.Switch = function(i){
		$('#HPHLCurrent a').attr('title',contents[i].alt).attr('href',contents[i].url);//E. Roberts 12/16/2011 - Correctly Changes Title Attribute of Image On Switch
		$('#HPHLCurrent .HPHLImage').attr('alt',contents[i].alt).attr('src',contents[i].src);	
			
		$('#MarqueeText a').attr('alt',contents[i].alt).attr('href',contents[i].url).html(contents[i].title);		
		$('#MarqueeText .HPHLSubtext').html(contents[i].subtitle);
		
		$('#HPHLNav ul li.Active').removeClass('Active');	$($('#HPHLNav ul li').get(i)).addClass('Active');
		Ni = nextI();	that.SetTimer(contents[Ni].duration,Ni);}	
	this.SetTimer = function(seconds,itemIndex){clearTimeout(TransitionTimer); TransitionTimer=setTimeout('Highlights.' + (DisableTransitions ? 'Switch'	: 'Transition') + '('+itemIndex+')',(seconds*1000));}}
