
function recordOutboundLink(href, category, action) {
  _gat._getTrackerByName()._trackEvent(category, action);
  setTimeout('document.location = "' + href + '"', 100);
}

// creates HTML5 elements - IE needs this to know how to style elements
function createHTML5() {
		if (navigator.appName == 'Microsoft Internet Explorer') {
		var HTML5Elements = ['abbr','article','aside','audio','canvas','details','figcaption','figure','footer','header','hgroup','mark','meter','nav','output','progress','section','summary','time','video'];
		var x;
		for (x in HTML5Elements) {
			document.createElement(HTML5Elements[x]);
		}
	}
}

$(document).ready(function(){

	backgroundImages();		// add background images
	listenResize();			// listen for window resizing
	trackLinks();			// record outbound links on aside clicks
	newsletterButton();		// set up fancy box for newsletter button
	asideAccordion();		// set up accordion in sponsors
	
});

function newsletterButton() {
	$('.newsletterButton').fancybox({
		'overlayOpacity' : 0,
		'overlayColor' : '#000',
		'titleShow' : false
	});
}

function trackLinks() {
	$('a.leftTile').live('click',function(){
		var href = $(this).attr('href');
		recordOutboundLink(href,'Sponsor Links',href);
		return false;
	});
	$('article a').live('click',function(){
		var href = $(this).attr('href');
		recordOutboundLink(href,'Outbound Links',href);
		return false;
	});
	$('#ncpride_header a').live('click',function(){
		var href = $(this).attr('href');
		recordOutboundLink(href,'Header Links',href);
		return false;
	});
}

function asideAccordion() {
	$('a.leftTile').hover(
		function(){
			var nativeH = $(this).find('.businessName').outerHeight() + $(this).find('.businessDescription').outerHeight();
			$(this).find('.businessInfo').animate({
				'height': nativeH
			});
		},
		function(){
			$(this).find('.businessInfo').animate({
				'height': 48
			});
		}	
	);
}

function listenResize() {
	$(window).resize(
		function(){
			resize();
		}
	);	
}

// add nav to left
function addNav() {
	var nav = $('<div id="nav" class="leftSection" style="padding-bottom: 18px; role="navigation"></div>');
	var navHeader = $('<h3></h2>');
	var navToggle = $('<a href="#" class="jumpLink">Jump to Section...</a>');
	var navLinks = $('<div class="inner jumpSection" style="display: none;"></div>');
	var navUL = $('<ul></ul>');
	var linkNumber = 0;
	$('#content h1').each(function() {
		$(this).attr('id','section'+linkNumber);
		navUL.append('<li><a href="#section'+linkNumber+'">'+$(this).text()+'</a></li>');
		linkNumber++;
	});
	navLinks.append(navUL);
	navHeader.append(navToggle);
	nav.append(navHeader);
	nav.append(navLinks);
	$('aside').prepend(nav);
	resize();	
}

//resizes nav and other items
function resize() {
	var winH = $(window).height();
	var winW = $(window).width();
	var imgR = '';
	var winR = winW / winH;
	$('nav').css('height',winH - 30);
	$('#ncpride_background').css({'width':winW,'height':winH});
	$('#ncpride_background img').each(function(){
		$(this).css({'width':'','height':''});
		imgR = $(this).width() / $(this).height();
		if ( imgR < winR ) {			
			$(this).css({'height':'auto !important','width':'100% !important'});
			$(this).attr('width',winW);
			$(this).attr('height',winW*(1/imgR));
		} else {			
			$(this).css({'width':'auto !important','height':'100% !important'});
			$(this).attr('height',winH);
			$(this).attr('width',winH*imgR);			
		}
	});
}

//simulates a click in the nav
function navClick(link) {
	$('nav a').removeClass('selected');	
	link.addClass('selected');
}

// adds backgroundimage div
function backgroundImages() {
	var deviceAgent = navigator.userAgent.toLowerCase();
	var touch = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (!touch && !($.browser.msie && $.browser.version <7 ) ) {
		var background = $('<div id="ncpride_background" style="display: none;"></div>');
		background.css({
			'position' : 'fixed',
			'top' : 0,
			'width' : '100%',
			'left' : 0		
		});
	
		var bgImages = [];
		bgImages[0] = 'http://ncpride.org/trianglelocalevents/backgrounds/2.jpg';
		bgImages[1] = 'http://ncpride.org/trianglelocalevents/backgrounds/1.jpg';
		bgImages[2] = 'http://ncpride.org/trianglelocalevents/backgrounds/3.jpg';
		bgImages[3] = 'http://ncpride.org/trianglelocalevents/backgrounds/4.jpg';
		for ( i = 0; i < bgImages.length; i++ ) {
			background.append('<img style="width: 100% !important; height: 100% !important;" src="'+bgImages[i]+'" alt="background" />');
		}
		$(window).load(function(){
			$('body').prepend(background);
			background.fadeIn();
			background.cycle({
				fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				timeout: 5000,
				speed: 600,
				fit: 1
			});
			resize();
		});
	}
}


