/*======================================================================
	Hijack Jackrabbit Code. hijackRabbit. Get it? It's funny. 
----------------------------------------------------------------------*/
$(document).ready(function(){
	
	var jrSrc = 'http://app.jackrabbitclass.com/Openings.asp?id=496420&ShowClosed=1&Closed=full&sort=Cat1,Class,Days,Times&showCols=Instructors,Cat1,Room&hidecols=Gender,Ages,Description,Session',
		orginalDocumentWrite = document.write,
		currentSubheading = '';
		$jackrabbit = $('<div id="jackrabbit"></div>'),
		colClassesArr = [
			'col-id', 
			'col-duplicate-name', 
			'col-register',
			'col-class-type',
			'col-class-name',
			'col-day', 
			'col-time', 
			'col-duration',
			'col-slots', 
			'col-instructors', 
			'col-starts', 
			'col-ends', 
			'col-cost'
		];
	
	/*======================================================================
		HIJACK DOCUMENT.WRITE
		--------------------------------------------------------------------
		So jackrabbit's code doesn't totally blow up our page when used 
		with jQuery. 
	----------------------------------------------------------------------*/
	document.write = function(badCode){
		//instead, write in a DOM-friendly manner
		$(badCode).appendTo($jackrabbit);
	};
	
	/*======================================================================
		LOAD JACKRABBIT DATA
		--------------------------------------------------------------------
		Due to the brute force injection of document.write, we have to call 
		each successive table as a callback to allow for insertion of 
		headings. 
	----------------------------------------------------------------------*/
	
	/*-- LOAD ADULT CLASSES --*/
	$('<h2 id="adult-classes">Adult Classes</h2>').appendTo($jackrabbit);
	$.getScript(jrSrc + '&Cat3=Adult%20Classes', function(){
		/*-- LOAD KIDS CLASSES --*/
		$('<h2 id="kids-classes">Kids Classes</h2>').appendTo($jackrabbit);
		$.getScript(jrSrc + '&Cat3=Kids%20Classes', function(){
			//restore document.write
			document.write = orginalDocumentWrite;
			//clean up the html
			formatData();
			
			/*-- LOAD FAMILY CLASSES --*/
			/*$('<h2 id="family-classes">Family Classes</h2>').appendTo($jackrabbit);
			$.getScript(jrSrc + '&Cat3=Family%20Classes', function(){
				//restore document.write
				document.write = orginalDocumentWrite;
				//clean up the html
				formatData();
			});*/
		});
	});
	
	/*======================================================================
		FORMAT JACKRABBIT DATA
		--------------------------------------------------------------------
		Remove the cruft, add in the proper hooks, slice and dice the markup
	----------------------------------------------------------------------*/
	var formatData = function() {
		$jackrabbit
			.find('style')
				.remove()
			.end()
			.find('table')
				.removeAttr('border')
				.removeClass('openings')
				.addClass('base jr jr-schedule')				
				.find('tr')
					.removeAttr('style')
					.removeAttr('class')
					.each(function() {
						var $this = $(this),
						 	$tds = $this.find('td').removeAttr('style');
							
						//assign proper classnames for table columns
						for(var i=0; i < $tds.length; i++) {
							$tds.eq(i).addClass(colClassesArr[i]);
						}	
						
						/*-- NOT CHAINED DUE TO DESTRUCTIVE MANIPULATION --*/
						//class ID and duplicate name which are dumped in when register link is shown
						$('td.col-id, td.col-duplicate-name', $this).hide();
						$('td.col-register', $this).appendTo($this);
						$('td.col-duration', $this).insertAfter($('td.col-ends', $this));
						$('td.col-day, td.col-time', $this).insertAfter($('td.col-class-name', $this));
						$('td.col-instructors', $this)
							.insertAfter($('td.col-time', $this))
							.each(function(){
								//remove trailing commas
								var instrText = $.trim($(this).text());
								if(instrText.charAt(instrText.length - 1, 1) == ',')
									$(this).text(instrText.substring(0, instrText.length - 1))
							})
						$('td.col-class-name', $this).each(function(){
							var $className = $(this),
								classNameText = $.trim($(this).text()),
								hashIndex = classNameText.indexOf('//');
								
							if(hashIndex >= 0) {
								classNameText = classNameText.substring(0, hashIndex);
						
								//strip extra text utilized only in the admin
								$className.text(classNameText);
							}
						})
						$('td.col-slots', $this).each(function(){
							var $slots = $(this);
							if($slots.html() != 'full') {
								$slots.html('open');
							}
						})
						$('td.col-cost', $this).each(function(){
							var $cost = $(this)
							$cost.html('$' + $cost.html());
						})
						
						var $classType = $('td.col-class-type', $this),
							classTypeText = $classType.text();
						if(classTypeText != currentSubheading && classTypeText != 'Cat1'){
							var subheading = '<tr class="subheaders"><td colspan=' + (colClassesArr.length - 3) + '>' + classTypeText + '</td></tr>';
							$(subheading).insertBefore($this);
							currentSubheading = classTypeText;
						}
						$classType.hide();
					})
				.end()
				//.find('td.col-class-type')
					//.hide()
				//.end()
				.find('tr:first')
					.addClass('headers')
					.find('td.col-day')
						.html('Day')
					.end()
					.find('td.col-time')
						.html('Time')
					.end()
					.find('td.col-instructors')
						.html('Instr.')
					.end()
					.find('td.col-slots')
						.html('Slots')
					.end()
					.find('td.col-starts')
						.html('Starts')
					.end()
					.find('td.col-ends')
						.html('Ends')
					.end()
					.find('td.col-duration')
						.html('Weeks')
					.end()
					.find('td.col-cost')
						.html('Cost')
					.end()
				.end()
			.end()
			.appendTo('#jr-schedule');
	};
	
	/*======================================================================
		JUMP TO LINKS
	----------------------------------------------------------------------*/
	$('#jr-schedule p.jump-to a').click(function(){
		var target = $(this).attr('href'),
 			hashIndex = target.lastIndexOf('#');
	
		//strip path injected by Joomla and grab only the hash
		target = target.substring(hashIndex);

		$.scrollTo(target, 500, {offset: -50});
		return false;
	});
	
	
});