
	//
	// Calendar Setup
	//
	$(document).ready(function() {
		//
		// On click of a calendar checkbox: if a title, expand all, if not
		// load up the selected calendar
		//
		$("#resorg").change( function() {
			var show = this.value;
			var last = "true";
			if(show == "all")
				last = "false";
			toggleCalendar( show, show, last );
		});


		//
		// On mouseover/mouseout of an item highlight it with gray.
		//
		$('div.calendar_item').live('mouseover', function() {
			$(this).addClass('stripe');
		}).live('mouseout', function() {
			$(this).removeClass('stripe');
		});


		//
		// launch date picker
	    //

		$("input.dcms-date").datepicker({
			dateFormat: 'yy-mm-dd',
			showOn: 'both',
			buttonImage: '/img/cal-icon.png',
			buttonImageOnly: true
		});

	});


	//
	// toggleCalendar
	//
	// if we are showing this calendar, first decide if the data has been loaded
	// if so, simply show the events, if not queue up the ajax call to load the calendar data
	//
	// if not showing this calendar, hide all of its events.
	//
	// @param code - the code name of the calendar
	// @param show - true if we are turning this calendar on, false if we're turning it off.
	//
	function toggleCalendar( code, show, last ) {

		$("#events-container").html("");

		var $lbl = $('#result-label');
		if( $('div.' + code).length) {
			$('div.' + code).show();
		} else {
	
			$("#filterbox").css('background', 'url(/img/loader.gif) no-repeat left top');

			if(code == 'all') {
				toggleAllCalendars();
			} else {

				var url = '/calendar/load/';
				url += '?calendar=' + code;
				url += '&start=' + $('#startdate').val();
				url += '&stop=' + $('#enddate').val();
	
				if($('#search').val())
					url += '&search=' + $('#search').val();
	
				//$("#resorg").val(code);
	
				dynamItLoadDataQueue( url, 'calendar', function(data) {
					
					eval( 'var gdata = ' + data + ';' );
					$lbl.html($('#resorg :selected').text());
					if(last == "true") { 
						$('#filterbox').css('background', 'none');
					}
					loadCalendar(data);
					$lbl.append('<b> (' + $('.calendar_item').length + ')</b>');
				});
			}
		}	
	}


	//
	// loadCalendar
	//
	// look at each event already in the calendar and through all of the events
	// we've loaded via ajax. while the ajaxed events have an earlier start time,
	// insert it before our current calendar event.
	//
	// if a loaded event has a later start time, go to the next existing event to
	// find where it fits.
	//
	// note that if a certain event has already been loaded by a previous calendar
	// we do not duplicate it, rather note it belongs to the new calendar as well.
	//
	// @param gdata - event data loaded via ajax in json format
	//
	function loadCalendar( gdata ) {
eval( 'var gdata = ' + gdata + ';' );
		
		if( gdata.length ) {

			var j = 0;

			var $events = $("#events-container").children('div.calendar_item');
			var $e;

			for( var i = 0; i < $events.length; i++ ) {
				$e = $events.eq(i);

				while( j < gdata.length && parseInt(gdata[j]['starttime']) < parseInt($e.attr('stamp')) ) {

					var $cur = $("div[titlehash='" + gdata[j]['titlehash'] + "']");
					if( $cur.length && $cur.attr('stamp') == gdata[j]['starttime'] ) {
						$cur.addClass(gdata[j]['code']).show();
					} else {
						$e.before( calendarRow(gdata[j]) );
					}

					j++;

				}
			}

			while( j < gdata.length ) {

				var $cur = $("div[titlehash='" + gdata[j]['titlehash'] + "']");
				if( $cur.length && $cur.attr('stamp') == gdata[j]['starttime'] ) {
					$cur.addClass(gdata[j]['code']).show();
				} else {
					$("#events-container").append( calendarRow(gdata[j]) );
				}

				j++;
			}

		}

	}


	//
	// calendarRow
	//
	// build the html for a single calendar row.
	//
	// @param e - javascript object for a particular event
	// @return (string) html to display a single calendar row.
	//
	function calendarRow( e )  {

		var code = e['code'];
		var type = e['type'];
		var tags = '&nbsp;';

		// in description field of google calendar, format is description|page_url|member_ticket_url|regular_ticket_url

		var explode = e['description'].split("|");
		var desc = explode[0];
		var url = explode[1];
		var member = explode[2];
		var ticket = explode[3];


		if( !e['where'].length ) {
			e['where'] = "";
		}
		

// date
var row = '' +
'<div class="calendar_item ' + code + ' ' + type + '" stamp="' + (e['starttime']) + '" titlehash="' + e['titlehash'] + '">';

// date
row+='<div class="date">' + date( "m\/d\/y", (e['starttime']) ) + '</div>';

// time
row+='<div class="time">';
if(e['allday'] == 1)
	row+= 'All Day';
else row+= date( "h:ia", e['starttime']);

if(e['where'] != '')
	row+='<br />' + e['where'];

var callbl = "";

if(code == "cafeteria-menu")
	callbl = "Cafeteria Menu";
if(code == "school-calendar")
	callbl = "School Calendar";
if(code == "prefects-servers")
	callbl = "Prefects / Servers";

row+= '<br/>' + callbl;

row+='</div>';

// title
row+='<div class="title">';
row+='<span class="emph">' + e['title'] + '</span>';
row+='</div>';


// desc
if(desc) {
	//row+=desc; 
}

row+='</div>' +
'<div class="divider clarity">&nbsp;</div>';

		return row;

	}

	function newsLinks(on) {
		var other = (on == 'News') ? 'Events' : 'News';
		$('#btnAll' + on).show();
		$('#btnAll' + other).hide();

	}

	function refreshCalendar(f) {
		var url = '/calendar/display';
		url += '?start=' + f.start.value;
		url += '&stop=' + f.stop.value;

		$('ul.calendar_subnav li label input:checked').each(function() {

			url += '&q[]=' + $(this).attr('id');

		});


		location.href = url;
		return false;
	}
	
	// populate the search
	function populateSearch(term) {
		$("#search").val(term);
	}

	