
	//
	// 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";
			$('#q').val(show);

			trackEvent(show);
			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: 'mm-dd-yy',
			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');

				var url = '/presentations/load/';
				url += '?calendar=all';
				url += '&start=' + $('#startdate').val();
				url += '&stop=' + $('#enddate').val();
	
				if($('#resorg').val() && $('#resorg').val() != "all")
					url += '&search=' + $('#resorg').val();
	
				//$("#resorg").val(code);
	
				dynamItLoadDataQueue( url, 'calendar', function(data) {
					
					eval( 'var gdata = ' + data + ';' );
					$lbl.html($('#resorg :selected').text());
					$('#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];

		// catco has a different format
		if(code == "catco") {
			var catcoxplode = desc.split('"');
			url = catcoxplode[1];
			ticket = catcoxplode[3];

			desc = '';
		}



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

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

if(url) {
	var targ = '';
	if(e['cal_title'] != "CAPA")
		targ = 'target="_blank"';

	row+='<a '+ targ +' href="' + url + '" title="' + e['title'] + '">';
}

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

if(url) {
	row+='</a>';
}

// row+='<br />' + e['cal_title'];

var datestring = '';

if( date("i", e['starttime']) == 0 )
	datestring = 'l, g a';
else
	datestring = 'l, g:i a';

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

row+='</div>';


row+='	<div class="membertickets">';
/*
if(member)
	row+='<a class="btn member-tickets" href="' + member + '" title="CAPA Member Tickets">CAPA Member Tickets</a>';
*/
if(ticket)
	row+='<a target="_blank" class="btn buy-tickets" href="' + ticket + '" title="Buy Tickets">Buy Tickets</a>';

row+='</div>';

row+='<div class="presented">';

if(desc) {
	row+=desc; 
}

row+='</div>' +
'</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 = '/presentations/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);
	}

	
