//custom javascript code
//this file will include jquery.ready functions
	$(document).ready( function() {

		$('a[href*="nyssa.memberpath.com"]').each(function() {
			var originalDestination = $(this).attr('href');
			var newDestination = originalDestination.replace("nyssa.memberpath.com","www.nyssa.org");
			$(this).attr('href', newDestination);
		});

//event tracking for simple <a> links to Wall St Training
//http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html

		//$('a[href*="www.wstselfstudy.com"]').each(function() {
		//var originalDestination = $(this).attr('href');
		//var newEvent = "_gaq.push(['_trackEvent', 'Wall St. Training', 'Exit Link', '"+originalDestination+"']);";
		//$(this).attr('onclick', newEvent);
		//});

		// for each anchor that references an external host
		$('a[href*="www.wstselfstudy.com"]').click(function() {
			// Google Track Event docs are here:
			// http://www.google.com/support/analyticshelp/bin/answer.py?answer=1033068
			// format for the onclick event is here:
			// http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html         
			var destination = $(this).attr('href');
			var destinationHostname = this.hostname;
			var eventCategory = "Wall St. Training";
			var eventAction = "Exit Link";
			var eventLabel = destination;
			var eventValue = 1;
			var eventNoninteraction =new Boolean(1); // true = we don't want an exit link to interfere with bounce tracking
			_gaq.push(['_trackEvent', eventCategory, eventAction, eventLabel, eventValue, eventNoninteraction]);
			setTimeout('document.location = "' + destination + '"', 100);
			return false;
		});


		$('a[class="micro-text"][href$="MasterCalendar.aspx"]').text('Upcoming Programs');
		
		$('a[class="micro-text"][href$="MasterCalendar.aspx"]').attr('href','http://nyssa.memberpath.com/Programs/UpcomingPrograms.aspx');
		
		if ( $('#dnn_ctr1169_dnnTITLE_lblTitle').text() == 'Upcoming Programs' ) {
			$('#dnn_ctr1169_dnnTITLE_lblTitle').attr('style','color: #2E353A; font-family: Arial;	font-size: 19px; outline: none;	display: block;	font-weight: bold; margin: 0.67em 0px;');
		}
		
		
		function evaluateStatus(startDate,endDate,soldOut) {
			// hides registration buttons if appropriate, returns message text
			var regClosed = false;
			var pastEvent = false;
			var inSession = false;
			var hideRow = false;
			var detailMessage = '';
			var listMessage = '';
			var one_day=1000*60*60*24; //milliseconds in a day
			var displayDaysAfter = 0; //default late registration period
			var nowEpoch = new Date().getTime();
			var startdateEpoch = Math.floor(Date.parse(startDate)/one_day) * one_day;  // midnight of the start date
			var enddateEpoch = Math.floor(Date.parse(endDate)/one_day) * one_day + one_day; //midnight following the end date
			
			var duration = (enddateEpoch - startdateEpoch)/one_day; //program duration
			var daystoStart = (startdateEpoch - nowEpoch)/one_day;

			if (soldOut) {
				regClosed = true;
			}

			if (enddateEpoch < nowEpoch) { // event has ended
				pastEvent = true;
				regClosed = true;
			}
			else if (daystoStart > 0) {} // event has not started yet, do nothing
			else if (daystoStart <= 0) { // event has started
					inSession = true;
					if (duration < 11) { // late registration period of 0 days
						lateRegPeriod = 0;
					}
					else if (duration < 85) { // late registration period of 14 days
						lateRegPeriod = 14*one_day;
					}
					else if (duration < 180) { // late registration period of 42 days
						lateRegPeriod = 42*one_day;
					}
					
					if ( nowEpoch > (startdateEpoch + lateRegPeriod) ) { // We are past the late registration period
						regClosed = true;
					}
					else { //we are in the late registration period
						//nothing to do here
					}
				}
			
	
			if (pastEvent) {
				detailMessage = '<p><strong style=\"color:red\">Past Event Information</strong> – This is a past program that was held as a service to NYSSA members and the investment community. To register for similar future events please view our <a href=\"http://www.nyssa.org/Programs/UpcomingPrograms.aspx\">Upcoming Programs calendar</a>.</p>';
				hideRow = true;
			}
			if (inSession && regClosed) {
				detailMessage = '<p><strong style=\"color:red\">In-Session Event Information</strong> – This program is in-session but the late registration period has ended. To register for similar future events please view our <a href=\"http://www.nyssa.org/Programs/UpcomingPrograms.aspx\">Upcoming Programs calendar</a>.</p>';
				listMessage = '<p class="listDetail"><strong style=\"color:red\">Registration is closed.</strong></p>';
				//hideRow = true;
			}
			if (soldOut && !pastEvent) {
				detailMessage = '<p><strong style=\"color:red\">This event is sold out.</strong> – To register for similar future events please view our <a href=\"http://www.nyssa.org/Programs/UpcomingPrograms.aspx\">Upcoming Programs calendar</a>.</p>';
				listMessage = '<p class="listDetail"><strong style=\"color:red\">This event is sold out.</strong></p>';
				hideRow = false;
			}
			if (inSession && !soldOut && !regClosed) {
				detailMessage = '<p><strong style=\"color:green\">Seats are still available!</strong> – This program is in-session but seats are still available.</p>';
				listMessage = '<p class="listDetail"><strong style=\"color:green\">Seats are still available!</strong> – This program is in-session but seats are still available.</p>';
			}
			
			
			if ( regClosed ) {
				$('#registerbutton1').remove();
				$('#registerbutton2').remove();
				$('#bundlebutton1').remove();
				$('#bundlebutton2').remove();
			}
			return [detailMessage,listMessage, hideRow];
		}
		
		
		
		$('.ListTitleBlock').each( function() {
			var startDate = $(this).children('span.hiddenstartdate').text();
			var endDate = $(this).children('span.hiddenenddate').text();
			var soldOut = ($(this).children('span.hiddensoldout').text() == 'soldout;');
			var statusMessage = '';
			var hideRow = false;
			result = evaluateStatus(startDate,endDate,soldOut);
			statusMessage = result[1];
			hideRow = result[2];
			$(this).children('span.hiddenstartdate').after(statusMessage);
			if ( hideRow ) {
				$(this).parent().remove();
			}
		});

		
		if ( $('div.event-detail').children('span.hiddenstartdate').text() != "" ) {
			var startDate = $('div.event-detail').children('span.hiddenstartdate').text();
			var endDate = $('div.event-detail').children('span.hiddenenddate').text();
			var soldOut = ($('div.event-detail').children('span.hiddensoldout').text() == 'soldout;');
			var statusMessage = '';
			var hideRow = false;
			result = evaluateStatus(startDate,endDate,soldOut);
			statusMessage = result[0];
			hideRow = result[2];
			$('div.event-detail').children('span.hiddenstartdate').after(statusMessage);
		};
	});


