// JavaScript Document
function mgwidget(howmany, mm, md, my){

	// this year mardi gras is:
	var mgmon = 2;
	var mgday = 21;

	$(document).ready(function(){
		$.get('/xml/mardigraskrewes.xml', function(xml) {
			// get all the currents for later use
			var allcurrents = [];
			var allsorted;
			$(xml).find('entry').each(function(){
				if ( $(this).find('gsxcurrent').text() != '' ){
					allcurrents.push( $(this).find('gsxcurrent').text() );
				}
			});
			allsorted = allcurrents.sort();

//// function to collect entries for current day and sort them
			var widcurrents = [];
			var widsorted;
			function getcurrent(t) {
				// get a list of eligible krewes and sort them
				// if the krewe has a current parade index add the index to the array list
				$(xml).find('entry').each(function(){
					if ( $(this).find('gsxcurrent').text() != '' && $(this).find('gsxpostponeddate').text() != '' ) { // if there is text in postponeddate
						if ( $(this).find('gsxpostponeddate').text() == 'x' ){ // if parade is cancelled do nothing
						} else if ( $(this).find('gsxpostponeddate').text() == t ){ // if the postponement is the today
							widcurrents.push( $(this).find('gsxcurrent').text() );
						}
					} else if ( $(this).find('gsxcurrent').text() != '' && $(this).find('gsxparadedate').text() == t ){ // the entry has no postponement so use the paradedate
						widcurrents.push( $(this).find('gsxcurrent').text() );
					}
					widsorted = widcurrents.sort();
				});

				var inuyasha = widsorted.length;
				if ( inuyasha >= 1 ){
					// hooray! there's at least one entry for today, spit out some entries!
					// find the entry to start listing from
					var istart;
					var kagome = allsorted.length;
					for ( var i = 0; i < kagome; i++ ){
						if ( widsorted[0] == allsorted[i] ){
							istart = i;
						}
					}

					for ( var i=0; i<howmany; i++ ){
							$(xml).find('gsxcurrent:contains('+allsorted[istart]+')' ).each(function(){
								// check to make sure similarly spelled names are not duped
								var kt = $(this).text();
								if ( kt == allsorted[istart] ){
									var ispost = ''; // is this entry postponed
									var $kw = $(this).parent();
									var gsxparish = $kw.find('gsxparish').text();
									var gsxfamiliarname = $kw.find('gsxfamiliarname').text();
									var gsxtagname = $kw.find('gsxtagname').text();
									var gsxfamiliarname = $kw.find('gsxfamiliarname').text();
									var gsxfullname = $kw.find('gsxfullname').text();				

									// get the paradedate - check for postponed - replace slashes with underscores
									var gsxparadedate;
									if ( $kw.find('gsxpostponeddate').text() == '' ) {
										gsxparadedate = $kw.find('gsxparadedate').text();
									} else if ( $kw.find('gsxpostponeddate').text() == 'x' ) {
										ispost = ' class="cancelled"';
										gsxparadedate = $kw.find('gsxparadedate').text();
									} else {
										ispost = ' class="ispost"';
										gsxparadedate = $kw.find('gsxpostponeddate').text();
									}

									// gsxparadedate = gsxparadedate.replace(/\//g,'_');

									var mmonth = ['January','February','March'];
									var mm = gsxparadedate.split('/')[0];
									var dd = gsxparadedate.split('/')[1];
									var yy = gsxparadedate.split('/')[2];

									if ( mm == 1 ){
										mm = 'Jan';
									} else if ( mm == 2 ){
										mm = 'Feb';
									} else if ( mm == 3 ){
										mm = 'Mar';
									}
									gsxparadedate = mm+'&nbsp;'+dd

									// time functions start
									// check for postponed time
									var gsxparadetime;
									if ( $kw.find('gsxpostponedtime').text() == '' ) {
										gsxparadetime = $kw.find('gsxparadetime').text();
									} else {
										ispost = ' class="ispost"';
										gsxparadetime = $kw.find('gsxpostponedtime').text();
									}

									// czech for all the times because parades can be after something or ish
									if ( gsxparadetime.indexOf('after') == 0 ){
										// if _after_ is found in date do nothing, use that string!
									} else if ( gsxparadetime.indexOf('TBA') == 0 ){
										// if _after_ is found in date do nothing, use that string!
									} else {
										var hh = gsxparadetime.split(':')[0];
										var mm = gsxparadetime.split(':')[1];
										var pp;

										if ( hh > 12 ){
											hh = hh-12;
											pp = 'p.m.';
											gsxparadetime = hh + ':' + mm + ' ' + pp;
										} else if ( hh < 12 ){
											pp = 'a.m.';
											gsxparadetime = hh + ':' + mm + ' ' + pp;
										} else {
											pp = 'p.m.';
											if ( mm === '00' ){
												gsxparadetime = 'Noon';
											}
											else {
												gsxparadetime = hh + ':' + mm + ' ' + pp;
											}
										}
									}

									$('<li'+ispost+'><a href="http://www.mardigras.com/index.ssf?'+gsxtagname+'" title="'+gsxfullname+' Parade">'+gsxfamiliarname+'</a><br /><span class="time">'+gsxparish+' - '+gsxparadedate+' - '+gsxparadetime+'</span></li>').appendTo('#mgwidget ul');
								}
							});
							istart++;
						}
				} else {
					// didnt find an entry for today, increase the day and go back to the indexes
					if ( md > 30 ){ // 31 days max in a month, so go to the next month
						mm++;
						md = 1;
						var today = mm+'/'+md+'/'+my;
						getcurrent(today);
					} else {
						md++;
						var today = mm+'/'+md+'/'+my;
						getcurrent(today);
					}
				}
			}

			if ( mm > mgmon ){ // is today REALLY after mardigras?
				$('<span>There are no more parades, Mardi Gras is passed<br />See you in 2013!!</span>').appendTo('#mgwidget');
			} else if ( mm == mgmon ){ // if mardi gras is this month
				if ( md > mgday ){ // if mardi gras is passed
					$('<span>There are no more parades, Mardi Gras is passed<br />See you in 2013!!</span>').appendTo('#mgwidget');
				} else { // mardi gras is today or in the near future
					var thedate = mm+'/'+md+'/'+my;
					getcurrent(thedate);
				}
			} else { // mardi gras month is neither this month nor in the past, its yet to happen so do something
				var thedate = mm+'/'+md+'/'+my;
				getcurrent(thedate);
			}

		});
	});
}

