﻿jQuery(function() {

	// external link handling
	jQuery("a[rel=external],a[target=_blank],a[href*='http://']:not([href*='" + location.hostname + "']),a[href*='https://']:not([href*='" + location.hostname + "'])")
		.not('.list-social-media a')
		.not('a[href$=.pdf]')
		.append('<img src="/Sites/Fletcher/Resources/img/css/new_win.gif" width="22" height="15" class="external-link-image" />').attr("title", "Opens in new window").click(function() {
			window.open(this.href);
			return false;
		});

	// ticker
	if (jQuery('body.page-home').length) { FLETCHER.Ticker(); }

	// psuedo classes
	jQuery('body.page-home #primary div.degrees li:last').addClass('last');

	// open default item
	FLETCHER.OpenDefault();
	
	// accordion
	FLETCHER.enableAccordion('dl.accordion');

	// IE6 double class selector help
	jQuery('body.formal.microsite a#home-fletcher').addClass('formal');
	
	jQuery('a[href$=.pdf]').addClass('pdf-link');
	
	FLETCHER.hideOverflow();
		
});

var FLETCHER = {
	
	Ticker : function(){
	
		var theCharacterTimeout = 50;
		var theStoryTimeout     = 10000;
		var theWidgetOne        = "-";
		var theWidgetTwo        = "|";
		var theWidgetNone       = "";
		var theLeadString       = "UPDATE &nbsp;";
	
		var theSummaries = new Array();
		var theSiteLinks = new Array();
	
		var items = jQuery('ul.ticker li');
		var minheight = TUFTS.maxHeight(items);
		var theItemCount = items.length;
		
		items.each(function(i){
			var $this = jQuery('a',this);
			theSummaries[i] = $this.html();
			theSiteLinks[i] = $this.attr('href');							
		});
		
		jQuery('ul.ticker').before('<div class="ticker" style="height:'+minheight+'px"><a title="Read more..." class="tick" target="_top" href="#" id="tickerAnchor"></a></div>').remove();
		startTicker();		
		
		// Ticker startup
		function startTicker()
		{
			// Define run time values
			theCurrentStory     = -1;
			theCurrentLength    = 0;
			// Locate base objects
			if (document.getElementById) {	
				theAnchorObject     = document.getElementById("tickerAnchor");
				runTheTicker();   	
			 }
		}
		// Ticker main run loop
		function runTheTicker()
		{
			var myTimeout;  
			// Go for the next story data block
			if(theCurrentLength == 0)
			{
				theCurrentStory++;
				theCurrentStory      = theCurrentStory % theItemCount;
				theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"').replace(/<img[^>]+>/g,'');		
				theTargetLink        = theSiteLinks[theCurrentStory];
				theAnchorObject.href = theTargetLink;
				thePrefix 	     = "<span class=\"tickls\">" + theLeadString + "</span>";
			}
			// Stuff the current ticker text into the anchor
			theAnchorObject.innerHTML = thePrefix + 
			theStorySummary.substring(0,theCurrentLength) + whatWidget();
			// Modify the length for the substring and define the timer
			if(theCurrentLength != theStorySummary.length)
			{
				theCurrentLength++;
				myTimeout = theCharacterTimeout;
			}
			else
			{
				theCurrentLength = 0;
				myTimeout = theStoryTimeout;
			}
			// Call up the next cycle of the ticker
			setTimeout(runTheTicker, myTimeout);
		}
		// Widget generator
		function whatWidget()
		{
			if(theCurrentLength == theStorySummary.length)
			{
				return theWidgetNone;
			}
		
			if((theCurrentLength % 2) == 1)
			{
				return theWidgetOne;
			}
			else
			{
				return theWidgetTwo; 
			}
		}


	},
	
	OpenDefault : function(){
			
		jQuery('dl.collapsible dt.open a').click();
	
	},
	
	enableAccordion : function(el) {
		
		if(!jQuery(el).length) { return; }
		// flags for animation helpers
		var moving = 0,
			allmoving = 0;			
		
		jQuery(el).find("dd").hide();
		jQuery(el).find("dt").click(function(e){
			if(moving == 0) {
				moving = 1;
				// if(jQuery(e.target).is("a")) { moving = 1; }
				if(jQuery(this).is('.open-toggle')) {
					jQuery(this).parents('dl').find('dd:visible').slideUp("fast",function(){ moving = 0; }).end().find('dt').removeClass('open-toggle');
				} else {
					jQuery(this).parents('dl').find('dd:visible').slideUp("fast").end().find('dt').removeClass('open-toggle');
					jQuery(this).toggleClass("open-toggle").next("dd").slideToggle("fast",function(){ moving = 0; allmoving = 0; });	
				}
			}
			return false;
		});
		jQuery(el + ' dt.open:first').click();
		
	},
	
	hideOverflow : function() {
		
		var obj = jQuery('.block-home-news');
		if(!obj.length) { return; }
		
		var box = obj.height(),
			off = Math.floor(obj.position().top),
			lis = obj.find('li'),
			adj = 3;
		
		lis.each(function(){			
			var li 	= jQuery(this),
				h	= Math.floor(li.outerHeight(true)),
				p	= Math.floor(li.position().top);			
			if(p - off + h - box > adj) { li.hide(); }			
		});
		
	}	
	
};

jQuery.fn.or = function(s) { return jQuery(this).length ? jQuery(this) : jQuery(s); };

