$(function() {
	
	// turn on JS elements
	$(".js-only").show();
	
	// but don't show the hidden ones
	$(".js-hidden").hide();
	
	
	
	/* ============================================
	ENABLE CHATTING, IF SOMEONE'S THERE TO LISTEN
	============================================ */
	
	// hide 'em first, just in case
	$("#live-chat-tag").hide();
	$("#sidebar-contact-chat").addClass('disabled');
	$("#sidebar-contact .disabled").live('click', function(e) {
		e.preventDefault();
		return FALSE;
	});
	// (cont. at bottom of script)
	

	
	
	
	/* ============================================
	SITE-WIDE MODAL BOX
	============================================ */
	
	// on load, find 'em, wrap 'em and hide 'em
	$(document).ready(function() {
	$('.modal').each(function() {
	
		// gather elements
		var modal_class = $(this).attr('id');
		var modal_title = $(this).attr('title');
		var modal_content = $('.'+modal_class).html();
		
		// get rid of the placeholder
		$('.'+modal_class).remove();
		
		// wrap the content with the good stuff
/*
		var modal_window = "
<div class='"+modal_class+" js-only modal-container'>
	<div class='modal-background'></div>
	<div class='modal-box'>
		<div class='modal-header'>
			<h3>"+modal_title+"</h3>
			<a href='' class='modal-close'>X</a>
			<br class='clear' />
		</div>
		<div class='modal-content'>
			"+modal_content+"
			<br class='clear' />
		</div>
	</div>
</div>
";
*/
		
		// add it all to top of dom
		$('body').prepend("<div class='"+modal_class+" js-only modal-container'><div class='modal-background'></div><div class='modal-box'><div class='modal-header'><h3>"+modal_title+"</h3><a href='' class='modal-close'>X</a><br class='clear' /></div><div class='modal-content'>"+modal_content+"<br class='clear' /></div></div></div>");
		
		// make it fullscreen and hidden
		$('.'+modal_class).css('height', $(document).height()).hide();
		
	});
	});
	
	// on target click, get the id and find the modal with a matching class
	$('.modal').click(function(e) {
		e.preventDefault();
		var modal_class = $(this).attr('id');
		$('.'+modal_class).show();
	});
	
	// hide modals when background clicked
	$('.modal-background').click(function() {
		$(this).parent().hide();
	});
	
	// hide modals when you click the "X" too
	$('.modal-header .modal-close').click(function(e) {
	e.preventDefault();
		$(this).parent().parent().parent().hide();
	});
	
	
	
	
	/* ============================================
	SIDEBAR LATEST UPDATES INTERFACE
	============================================ */
	
	// give them fancy scrollbar interfaces
	$('.social-widget-panel').jScrollPane(
	{
		showArrows: false,
		verticalGutter: 12
	}
	);
	
	// hide all panels
	$('.social-widget-panel').hide();
	
	// show only Twitter by default
	$('#social-widget-header-twitter').addClass('current');
	$('#social-widget-panel-twitter').show();
	
	// handle switching
	$('#social-widget-header a').click(function(e) {
		
		e.preventDefault();
		
		// lose the other 'current' state
		$('#social-widget-header .current').removeClass('current');
		
		// bring up the appropriate panel
		$('.social-widget-panel').hide();
		var panel_id = $(this).attr('class');
		$('#'+panel_id).show();
		
		// give the button the 'current' state
		$(this).parent().addClass('current');
		
	});
	
	
	
	/* ============================================
	MAKE DROPDOWN MENU EXTRA FANCY
	============================================ */
	
	// initiate dropdown for main menu
	dropdown('menu-main-navigation', 'hover', 140);
	
	// make the top levels unclickable
	$('#menu-main-navigation li ul, #footer-main-navigation li ul, #site-map-links li ul').prev().css( {cursor: 'default', outline: 'none'} ).click(function(){return false;});
	
	// give it a sweet fade in/out effect
	$("#menu-main-navigation").hover(
		function(){
			$('#menu-main-navigation li ul').css('opacity', '0.0').animate({opacity: 1.0}, 300);
		},
		function(){
			$('#menu-main-navigation li ul').css('opacity', '0.0');
		}
	);
	
	
	
	/* ============================================
	ALLOW FOR TABBED FOOTER PANELS
	============================================ */
	
	$('#footer-picker div div').click(function() {
		
		// get rid of the other classes
		$('.footer-here').removeClass('footer-here');
		
		// give it to what was just clicked
		$(this).parent().addClass('footer-here');
		
		$('#footer-containers .footer-container').hide();
		
		// switch the state for the matching panel as well
		var active_panel = $(this).parent().attr('id');
		$('#footer-containers .'+active_panel).show();
		
	});
	
	
	
	/* ============================================
	KNOWLEDGE CENTER FILTERING
	============================================ */
	
	$('.sortable-table tbody tr').removeClass('eligible').addClass('eligible');
	
	$("input[name='filter_criteria[]']").click(function() {
		
		// one family's checkbox at a time
		var selected = $(this).attr('checked');
		$(this).closest('ul').find('input').attr('checked', '');
		if (selected) { $(this).attr('checked', 'checked'); }

		
		// reset everything to "shown", temporarily
		$('#resources-table tbody tr').removeClass('eligible').addClass('eligible').show();
		
		// get the values of all the other checked boxes
		var filtered_criteria = new Array();
		$.each($("input[name='filter_criteria[]']:checked"), function() {
			filtered_criteria.push($(this).val());
		});
		
		// for each filtered_criteria
		$.each(filtered_criteria, function(index, filtered_criterion) {
			
			// get eligible rows and hide ones that don't match
			$.each($("#resources-table tbody tr.eligible"), function() {
				
				if ($(this).hasClass(filtered_criterion)) {} else {
					$(this).removeClass('eligible').hide();
				};
				
			});
			
		});
		
	});
	
	
	
	/* ============================================
	HANLDE TABLE COLUMN SORTING
	============================================ */
	
	$(".sortable-table th").click(function() {
		
		// clear out the previous "live" states
		$('.sortable-table th').removeClass('current');
		$(this).addClass('current');
		
		// toggle asc/desc state
		if ($(this).hasClass('asc')) {
			$(this).removeClass('asc').addClass('live');
			descending = false;
		} else {
			$(this).addClass('asc');
			descending = true;
		}
		
		// define the variables
		var $criterion = $(this).attr('id');
		var $trs = $(".sortable-table tbody tr.eligible");
		var $row_vals = [];
		var $col_vals = [];
		
		// index the html of all the rows in the tbody
		$.each($trs, function(i) {
			$row_vals.push($(this).html());
			$col_vals[i] = {};
			$col_vals[i]['orig_id'] = i;
			$col_vals[i]['html'] = $(this).find('.'+$criterion).html();
		});

		// do stuff I don't totally understand, but keeps the original keys in place
		$col_vals.sort(function(a,b) {
			var x = a.html;
			var y = b.html;
			if (descending) {
				return ((x < y) ? -1 : ((x > y) ? 1 : 0));
			} else {
				return ((x > y) ? -1 : ((x < y) ? 1 : 0));
			}
		});
		
		// for each sorted column html, roll out the html of the row that matches the "i" of the sorted html
		$.each($col_vals, function(index, value) {
			$($trs[index]).html($row_vals[value['orig_id']]);
		});
		
	});
	
	
	
	/* ============================================
	HANDLE GREYSCALE IMAGES
	============================================ */
	
	$(".pixastic-greyscale").pixastic("desaturate");
	$(".pixastic-greyscale").load(function() {
		$(".pixastic-greyscale").pixastic("desaturate");
	});
	
	$(".home-feature").hover(
		function(){
			$(this).find('.pixastic-greyscale').fadeOut(200);
		},
		function(){
			$(this).find('.pixastic-greyscale').fadeIn(200);
		}
	);
	
	
	
	/* ============================================
	LINKS POINTING OUTSIDE OPEN IN NEW WINDOWS
	============================================ */
	
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").not('.modal').click( function() {
		window.open(this.href);
		return false;
	});
	
	
	
});



/* ============================================
  LUCKY ORANGE INTEGRATION
============================================ */

// show 'em if they're available
function lucky_orange_agent_available() {
	$(function() {
		
		// make the tag slide in
		$("#live-chat-tag").css('display', 'block').css('right', '-32px').animate({
			right: '0'
		}, 2000);
		
		// make the sidebar fade in
		$("#sidebar-contact-chat").removeClass('disabled');
		
	});
}	


