function fadeNav(){
	$("div#courses tr:odd, ul#team-menu li:odd").addClass("alt");	
}


function scarf(){
	//find the courses
	var calendar = $('#calendar');
	var calendarHTML = calendar.find('table#course-tableHeadID0EA').html();
		
	if(!calendarHTML){
		$(calendar).show().html('<div id="training_error">We\'re sorry, but our online training calendar is temporarily unavailable. Please call (214) 613-4444 for more information.</div>');
		$('#loading_calendar').remove();
		return false;
	}
	
	//throw away the rest
	calendar.empty();

	
	$('<table width="100%" cellpadding="0" cellspacing="0">' + calendarHTML + '</table>')
	.appendTo(calendar)
		.find('tr')
			.removeClass('odd-row')
			.removeClass('even-row')
			.removeClass('ms-alternating')
			.not('.course-table-header')
				.hover(
					function(){
						$(this).addClass('tr-hover');
					},
					function(){
						$(this).removeClass('tr-hover');
				})
			.end()
			.find('td')
				.removeAttr('class')
				.find('table tr')
					.addClass('nested')
				.end()
				.find(':last-child')
					.attr('nowrap','nowrap')
				.end()
				.find("a[href$='.pdf']")
					.each(function(){
						$(this).addClass("pdf");		
						var href = $(this).attr("href");

						var anchorname = "";
						if (href.lastIndexOf("/")===-1) {
							anchorname = href;
						} else {
							anchorname = href.substr((href.lastIndexOf("/")+1));
						}
						$(this).attr("href", "http://courses.improvingenterprises.com/Courses/" + anchorname);		

					})
				.end()
				.find('a.thickbox')
					.each(function(){
						var href = $(this).attr('href');
						var newhref = href.split('http://website.')[1];
						$(this).attr('href','http://courses.' + newhref);
					})
				.end()
				.find('span:hidden')
					.remove()
				.end()
			.end()
		.end()
		.find('tr')
			.not('tr.nested')
			.addClass('not-nested');
			
	

	//do-si-do
	$('#loading_calendar').remove();
	$('#filter').fadeIn('slow');
	calendar.fadeIn('slow');
	
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;

	//poll to see if the url has changed
	currentURLHash = window.location.hash.substr(1);
	changeFilterTab(currentURLHash);
	setInterval("checkHash();", 200);
}

function checkHash(){
	var liveHash =  window.location.hash.substr(1);
	if(currentURLHash != liveHash){
		changeFilterTab(liveHash);
		currentURLHash =  liveHash;	
	}
}

function changeFilterTab(whichTab){
	$('#filter td').removeClass('active_filter');	
	
	if(!whichTab || whichTab == "All"){
		$("tr").show();	
		$('a.All').parents('td').addClass('active_filter');
		
	} else {
		var cal = $("#calendar");
		var calendarTr = cal.find("tr");

		$('a.' + whichTab).parents('td').addClass('active_filter');

		calendarTr.not('tr.nested').not('tr:first-child').removeClass('alt').hide();		

		$("." + whichTab).show();		
	}
}

function stripeTable(whichTable) {
	$(whichTable).find('tr.not-nested').each(function(index){
		if($(this).is(':visible')){
			var i = index % 2;
			if(i == 0){
				$(this).addClass('alt');
			}
		}
	});
}

function fixBioLink(){
/*
	$("ul.team-menu li a").each(function(){
		var link = $(this).attr('href');
		console.log(link);
		var consultants ="http://improvingenterprises.com/about/team/consultants/";
		var leadership ="http://improvingenterprises.com/about/team/leadership/";
		
		$(this).attr('href', link + 'bio/');
		
	});
*/

}

function calendarSetup(){
	return false;
}

function formatFeaturedCourses(){
	$('td.name').each(function(index) {
		var $price = $(this).find('div:nth-child(2)').text();
		var $date = $(this).find('div:last-child').text();
		var $name = $(this).find('div:first-child').text().split(' -- ')[1];
		
		$(this)
			.parents('table')
				.find('td b')
					.remove()
				.end()
				.find('.date')
					.html($date)
				.end()
				.find('.price')
					.html($price)
				.end()
				.find('.name')
					.find('div')
						.hide();				
		$(this).append($name);
	});
}

var courses = {
	init : function(){
		//Reset the UI
		$('.description, #course-description').hide();
			
		//poll to see if the url has changed
		setInterval("courses.checkHash();", 200);
	
		tb_init('a.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;

		var $courseDisplay = $('#course-description');
		var $courseTable = $('#training-courses');
	
		//If url contains a course hash, show the course or apply a filter		
		currentURLHash = window.location.hash.substr(1);
		if(currentURLHash.length){
			courses.translateHash(currentURLHash);
		}
				
		this.countCourses();
		this.stripeTable();
		this.formatThickBox();
		this.menuPosition();
		this.truncateDescriptions('.summary span');

		
		//bind to close
		$('.close_course').live('click', function(){
			$courseTable.fadeIn('fast', function(){
				$courseDisplay.hide();
				courses.stripeTable();
			});
			courses.menuPosition();
		});
		
		//show description
		$('a.show_description').live('click', function(){
			code = $(this).attr('href');
			courses.translateHash(code.substr(1));
			scrollTo(0,0);
		});
		
/*
		//filter courses
		$('#courses_filter a').live('click', function(){
			var cat = $(this).attr('id')
			cat = cat.substr(2);		
			courses.filterCourses(cat);
		});
*/
				
		$('.print_page').live('click', function(){
			window.print();
			return false;
		});		
	},
	
	truncateDescriptions : function(container) {
		var fullText = {};
		var summaryText = {};
		
		$(container).each(function(index){			
			var rawText = $(this).text();
			var full = $.trim(rawText);	
			
			if(full.length > 250){
				//start the full text for later
				fullText[index] = full;
				
				//slice off first 250 chars
				var summary = full.slice(0,250);		
				
				//find the last whole word
				var last = summary.lastIndexOf(' ');
				summary = summary.split(0,last);
				
				//stash the summarized text for later
				summaryText[index] = summary[0];
								
				// //add an expando link at the end
				summary[0] += '...';

				//update the DOM
				$(this).text(summary[0]);	
			}
		});
		
		$(container).delegate('.expando', 'click', function(){
			var pointer = $(this).attr('id');
			var key  = pointer.split('-');
		
			$(this).parents(container).html(fullText[key[1]] + ' <a href="#" class="collapso" id="' + pointer + '">Show less</a>');
			return false;
		});
		
		$(container).delegate('.collapso', 'click', function(){
			var pointer = $(this).attr('id');
			var key  = pointer.split('-');
		
			$(this).parents(container).html(summaryText[key[1]] + ' <a href="#" class="expando" id="' + pointer  +  '">Read more</a>');
			return false;
		});
	},
	
	menuPosition : function() {
		var filter = $('#courses_filter');

		
		if(filter.length){
			//hacky. Webkit doesn't grab the offset until after $(document).ready() has fired.
			setTimeout(function(){
				menuP = filter.offset();		
			}, 750);
			
			//just in case menuP doesn't get set in time

			var content = $("#content");

			$(document).unbind('scroll').scroll(function(){
				var windowP = $(window).scrollTop();

				//menu is in the viewport
				if(windowP < menuP.top){
					$('#training_sidebar').removeClass('bottom').removeClass('fixed');
				}

				//viewport has scrolled past menu
				if(windowP > menuP.top){
					$('#training_sidebar').removeClass('bottom').addClass('fixed');
				}

				//menu hit the bottom of the content area
				if((content.offset().top + content.height()) < ($('#training_sidebar').height() + windowP)){
					$('#training_sidebar').removeClass('fixed').addClass('bottom');
				}

			});
		}
		
	},
	
	checkHash : function (){
		var liveHash =  window.location.hash.substr(1);
		
		if(liveHash.length){
			if(currentURLHash != liveHash){
				courses.translateHash(liveHash);
				currentURLHash =  liveHash;	
			}
		} else {
			//hash is blank, reset the layout
			courses.translateHash('All');
			currentURLHash =  'All';
		}
	},
	
	translateHash : function (code){
		//Examen the URL hash
		//If the hash is a filter, apply the filter
		//If the hash is a course, hide the course table and show the course detail
	
		// reset the UI if hash is blank or All
		if(code == 'All'){	
			$('#training-list, #training_description, #training_banner, #training_title').show();
			$('#course-description').hide();
			courses.filterCourses('All');
			
			return false;
		}
		
		//Examine the hash to determine it's a filter or a course detail
		
		var hash = code.split("course_");
		if(hash[1]){
			//it's a course
			code = '#' + code;
		
			if($(code).length){
				var $desc 	= $(code).find('.description').html();
				
				//if the hash is valid, show the desc
				$('#course-description').html($desc).fadeIn();
				$('#training-list, #training_banner, #training_description, #training_title').hide();
				
			} else {
				//or reset the hash
				window.location.hash = "";				
			}
			
		} else {
			//it's a category
			courses.filterCourses(hash[0]);
		}
	},
	
	filterCourses : function (category) {
		//filter the courses table by category
		var $courseTable = $('#training-courses');
		var listTop = $('#training-list').offset();
		var windowTop = $(window).scrollTop();
	
		if(category == 'All'){
			$courseTable.find('div.course').show();
			
			$('#courses_filter')
				.find('li.active_filter')
					.removeClass()
				.end()
				.find('#f_All')
					.parents('li')
						.addClass('active_filter');
			$('#training_title').html('All Training Courses');
			return false;

		}
				
/*
		$courseTable.find('div.course').slideDown(100).not('.' + category).slideUp(100, function(){
			// if(windowTop > listTop.top){
			// 	$('html, body').animate({scrollTop: listTop.top}, 'slow');
			// }
		});
*/
	
		$('#courses_filter li.active_filter').removeClass();
		$('#f_' + category).parents('li').addClass('active_filter');
		
		var c = category.replace(/-/, " ");
		
		$('#training_title').html(c + ' Training Courses');
	},
	
	stripeTable : function (){
		$('#training-courses tr').removeClass('alt');
		$('#training-courses tr:visible:odd').addClass('alt');
	},
	
	countCourses : function(){
		//count the courses and populate the counts in the category filter
		
		catCount = {};
		
		//create the categories
		$('#courses_filter a').not('#f_All').each(function (index) {
			var id = $(this).attr('id');
			id = id.substr(2);			
			catCount[id] = 0;
		});
		
		$('#training-courses div.course').each(function(){
			var classes = $(this).attr('class');
			var className = classes.split(" ");
			
			//extract the classes
			for(var c = 0; c < className.length; c++){
				var cat = className[c];
				catCount[cat]++;
				
			}			
		});
		
			
		for(var cat in catCount){
			if(!catCount.hasOwnProperty(cat)){
				continue;
			} 
			
			$('#f_' + cat).find('strong').text('(' + catCount[cat] + ')');

		}
		
		$('#f_All').find('strong').text('(' + $('#training-courses div.course').length + ')');
	
	},
	
	formatThickBox : function () {
		$('iframe').ready(function() {
		  $('body', $('iframe').contents()).html('Hello World!');
		});

	}
};

function replaceFonts() {
	Cufon.replace('#content h1, .cufon');
}

var contact = {
	requestDate : function(){
		var course = $('h1 strong').text();
		$('#wpcf_msg').text("I'd like to request a date for " + course);
	}
};





