
/**
 *  Add $.preLoadImages() functionality to jQuery
 */
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

(function($) {
	$.fn.extend( {
		heightLimiter: function( maxHeight ) {
			$(this).each( function() {
				var $trunk = $(this);
				if ( $trunk.height() > maxHeight ) {
					$trunk.data( 'h', $trunk.height() );
					$trunk.height( maxHeight );
					var $toggle = $( document.createElement('div') );
					$toggle.html( '<a href="javascript:void(0)">more...</a>' );
					$trunk.after( $toggle );
					var $toggleLink = $toggle.find('a');
					$toggleLink.bind( 'click', function() {
						if ( $trunk.height() > maxHeight ) {
							$trunk.animate( {height:maxHeight} );
							$toggleLink.text( 'more...' );
						}
						else {
							$trunk.animate( {height:$trunk.data('h')} );
							$toggleLink.text( 'less...' );
						}
					} );
				}
			} );
		}
	});
})(jQuery);

function parseClassTime( ct ) {
	var time = ct.substr( 11 ).split( ':' );
	var t = {
		d: parseInt( ct.substr(9,1) ),
		h: time[0],
		m: time[1],
		pm: time[0] >= 12
	};
	if ( t.h > 12 )
		t.h -= 12;
	else
		t.h = t.h - 0;
	t.s = '' + t.h + ':' + t.m + (t.pm?'pm':'am');
	return t;
}


/**
 *  document ready handler
 */
$(document).ready(function() {

	// Wire up the navigation roll-over images:
	$('#nav > li > a').each(function() {
		var src = $(this.firstChild).attr( 'src' );
		if ( src.indexOf('_up.gif') != -1 ) {
			var base = src.replace( '_up.gif', '' );
			$.preLoadImages( base + '_over.gif' );
			$(this).data( 'base', base );
			$(this).bind( 'mouseover', function() {
					$(this).find('img').attr( 'src', $(this).data('base') + '_over.gif' );
				} )
				.bind( 'mouseout', function() {
					$(this).find('img').attr( 'src', $(this).data('base') + '_up.gif' );
				} );
		}
	});
	
	//$( '.faculty-member-details' ).heightLimiter( 250 );
	
	$.ajax( {
		type: 'GET',
		url: 'http://www.wmaac.com/siteadmin/scheduleHelper',
		processData: false,
		dataType: 'json',
		success: function(data,txt) {
			var dow = ['','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
			var html = '<div class="schedule-list">';
			var d = 0;
			var classTime;
			var day, time, h, m;
			var dayIndex = 0;
			
			var classes = {};
			
			for ( var n = 0; n < data.length; n++ ) {
				classTime = data[ n ];
				if ( classTime && classTime.classId ) {
					if ( classTime.classId == 16 || classTime.classId == 19 )
						continue;
					
					t = parseClassTime( classTime.start );
					
					html += '<div class="sched-row';
					if ( t.d != d ) {
						html += ' sched-day-row';
						dayIndex++;
					}
					if ( dayIndex % 2 == 0 )
						html += ' odd';
					html += '">';
					
					html += '<div class="sched-day">&nbsp;';
					if ( t.d != d ) {
						d = t.d;
						html += dow[d];
					}
					html += '</div>';
					
					classes[ 'class' + classTime.classId ] = true;
					
					html += '<div class="sched-time class' + classTime.classId + '">' + t.s + '</div>';
					html += '<div class="sched-class class' + classTime.classId + '">' + classTime.title;
					if ( classTime.body && classTime.body.length > 0 )
						html += ' <span class="sched-desc">' + classTime.body + '</span>';
					html += '</div>';
					
					html += '</div>';
				}
			}
			html += '</div><div class="schedule-open">';
			
			html += '<div class="sched-row sched-day-row"><h3>Open Play</h3>';
			html += '<p>Our indoor play space sessions are:</p></div>';
			
			d = 0;
			for ( var n = 0; n < data.length; n++ ) {
				classTime = data[ n ];
				if ( classTime && classTime.classId ) {
					if ( classTime.classId != 16 )
						continue;
					
					t = parseClassTime( classTime.start );
					t2 = parseClassTime( classTime.end );
					
					html += '<div class="sched-row';
					if ( t.d != d ) {
						html += ' sched-day-row';
						dayIndex++;
					}
					html += '">';
					
					html += '<div class="sched-day">&nbsp;';
					if ( t.d != d ) {
						d = t.d;
						html += dow[d];
					}
					html += '</div>';
					
					html += '<div class="sched-time">' + t.s + ' - ' + t2.s + '</div>';
					
					html += '</div>';
				}
			}
			html += '</div>';
			
			$('#calendar1').html( html );
			
			for ( var k in classes ) {
				$('#calendar1 .' + k)
					.data( 'cl', k )
					.bind( 'mouseenter', function() {
						$('#calendar .' + $(this).data('cl')).addClass( 'emph' );
					} )
					.bind( 'mouseleave', function() {
						$('#calendar .' + $(this).data('cl')).removeClass( 'emph' );
					} );
			}
		},
		error: function(r, txt, err) {
			$('#calendar1').html( '<p>Error loading data - please chack back later!</p>' );
		}
	} );
	
	// Hook up the weekly calendar:
	$('#calendar').weekCalendar({
		date: new Date(2010,1,1),
		timeslotsPerHour : 4,
		allowCalEventOverlap : true,
		overlapEventsSeparate: true,
		firstDayOfWeek : 1,
		businessHours :{start: 9, end: 22, limitDisplay: true },
		daysToShow : 7,
		dateFormat: "l",
		timeFormat: "g:i",
		timeSeparator: "-",
		readonly: true,
		buttons: false,
		height : function($calendar) {
			return $(window).height() - $("h1").outerHeight() - 1;
		},
		
		eventRender : function(calEvent, $event) {
			if ( calEvent.type ) {
				var types = this.eventTypes;
				var t = types[calEvent.type];
				if ( t ) {
					$event.css("backgroundColor", t.color);
				}
			}
		},
		
		draggable : false,
		resizable : false,
		
		data : "http://www.wmaac.com/siteadmin/scheduleHelper",
		
		eventTypes : { grey: { label:"grey", color:"#D1D1D1" },
			green: { label:"green", color:"#CDE4BA" },
			purple: { label:"purple", color:"#C2BAE4" },
			red: { label:"red", color:"#CFBEC4" },
			yellow: { label:"yellow", color:"#E6E4B9" },
			gold: { label:"gold", color:"#E7D5B8" }
		},
		
		loading: function(b) {
			if ( !b ) {
				$('#calendar-loading').remove();
			}
		}
	});
	
	
});

