$(document).ready(function() {
						
	// Options   
	var dropBoxSpeed = 8000;		// Interval between changing the current drop box in milliseconds
	var greyOpacity = 0.6;			// Opacity for greying out the page
	var boxPadding = 2;				// Padding for box on fade out
	var matrixSource = '/includes/matrixajax.php?';	// File to get matrix data from.
	var matrixCloseButtonUrl = '/images/matrix-close.png';	// URL for matrix close button
	//---------------------------------					
						
	var current_li = $('#header li:first-child');
	var disable_dropTimer = $('#header-right').length ? true : false;
	var finished = true;
	var next_action = false; // store the next action if we were busy...
	
	function tryNextAction() {
		if (next_action) {
			var act = next_action();
			next_action = false;
			act();
		}
	}
	
	function setAct(a) {
		next_action = a;
	}
	
	function dropTimer() {	   
		if (disable_dropTimer) return;
		$('#header').everyTime(dropBoxSpeed, 'drop-box-timer', function() {
			finished = false;
            var box = $(this).find('.drop-box.visible');
			box.slideUp(400, function() {
				$(this).addClass('hidden').removeClass('visible');
				// find the next link
				current_li = current_li.next();
				if (!current_li.length) {
					current_li = $('#header li:first-child');
				}
				var the_link = current_li.find('a:first-child');
				var link_class = the_link.attr('class').substr(5);
				var box_class = 'box-' + link_class;
				$('.' + box_class).addClass('visible').removeClass('hidden').slideDown(400, function() { updateSelectedLink(); finished = true; tryNextAction(); });
			});
		});
	}
	
	function getIdClass(box) {
		var classes = box.attr('class').split(' ');
		// Find the box id class
		var idclass = false;
		for (var i = 0; i < classes.length; i++) {
			if (classes[i].substr(0, 4) == 'box-') {
				idclass = classes[i].substr(4);
			}
		}
		return idclass;
	}
	
	function attention(original_drop_box) {
		$('#header-c').css('height', 235);
		var closeButton = $('<div>CLOSE</div>');
		var hc = $('#header-c');
		closeButton.css({'z-index': 10000, 'background': '#fff', 'cursor': 'pointer', 'display': 'none', 'position': 'absolute', 'top': hc.outerHeight() + hc.position().top + 2, 'left': hc.position().left - 2, 'padding': '2px 6px', 'text-align': 'center', 'width': 80});
		$('body').append(closeButton);
		closeButton.fadeIn(800);
	
		function hideFunc() {
			closeButton.fadeOut(400, function() { $(this).remove(); });
			finished = false;
			var in_matrix = $('#header-right-matrix').length;
			function afterUp(f) {
				$('#wrapper').css('background-image', 'url(/images/bg.gif)');
				$('#header-right-matrix .matrix-container').fadeOut(400, function() { $(this).remove(); });
					$('#header-right-matrix').attr('id', 'header-right-home');
					$('#header-right-home, #header-left, #header-c').animate({height: 170}, 400, function() {
	//					if (!in_matrix) {
							$('#header-right-home, #header-right-matrix').fadeOut(400, function() {
								$('#header-right-home, #header-right-matrix').attr('id', 'header-right').removeClass('generic');
								$('#header-right').fadeIn(400);
								f && f();
							});
	//					}
					});
			}
			function reset() {
				
				
				// Replace drop box with small drop box

				if (!$('.drop-box.visible').length) {
					afterUp(function() {
						original_drop_box.removeClass('drop-box').addClass('drop-box-small').addClass('visible').removeClass('hidden').slideDown(400);
						updateSelectedLink();
						finished = true;
						tryNextAction();
					});
				}
				$('.drop-box.visible').slideUp(400, function() {
						$(this).removeClass('visible').addClass('hidden');
					afterUp(function() {

						original_drop_box.removeClass('drop-box').addClass('drop-box-small').addClass('visible').removeClass('hidden').slideDown(400);
						updateSelectedLink();
						finished = true;
						tryNextAction();
					});
				});
			}
			// Reset back to normal...
			if ($('#header-right-matrix').length) {
				// if in matrix mode, reset back to normal.
/*				$('#header-right-matrix').fadeOut('fast', function() {
					$('#header-right-matrix .matrix-container').remove();
					$('#header-right-matrix').attr('id', 'header-right-home');
				}).fadeIn('fast', afterUp);*/
				afterUp(function() {
						original_drop_box.removeClass('drop-box').addClass('drop-box-small').addClass('visible').removeClass('hidden').slideDown(400);
						updateSelectedLink();
						finished = true;
						tryNextAction();
				});
			}
			else {
				reset();
			}
		}
		$('#header-right, #header-left').animate({height: 233}, 200, function() {
			$('#header-c').seekAttention({
				hideOnHover: false,
				pulse: false,
				blur: false,
				opacity: greyOpacity,
				paddingTop: boxPadding,
				paddingLeft: boxPadding,
				paddingRight: boxPadding,
				paddingBottom: boxPadding,
				onHide: hideFunc
			});
			$('.sa-pulse-overlay').css('display', 'none');	// we don't want a pulser so we can interact with the hole made.
			closeButton.click(function() {
			    $('.sa-pulse-overlay').remove();
	               $('div[class*="sa-blur"],.sa-overlay,').fadeOut(400,function(){
	                    $('div[class*="sa-blur"],.sa-overlay,').remove();
	               });	
				hideFunc();
			});
			$('#header-right').fadeOut(400, function() {
				$('#header-right').attr('id', 'header-right-home');
				$('#header-right-home').addClass('generic').fadeIn(400);
				// focus on the header
			});
		});
	}
	
	function updateSelectedLink() {
		var visible_box = $('.drop-box.visible, .drop-box-small.visible');
		if (!visible_box.length) return;	// ?
		$('#header li a:first-child').removeClass('selected');
		var idclass = getIdClass(visible_box);
		if (idclass) {
			var link_class = 'link-' + idclass;
			$('.' + link_class).addClass('selected');
		}
	}
	
	dropTimer();
	updateSelectedLink();
	
	// Change drop box
	$('#header ul.header-links li a:first-child').click(function(e) {
	 	if (!finished) { setAct(function() { $(this).trigger('click'); }); return; }
		finished = false;
		// get link class
		current_li = $(this).parent();
		$('#header li a:first-child').removeClass('selected');
		var link_class = $(this).attr('class').substr(5);
		$(this).addClass('selected');
		var drop_class = 'box-' + link_class;
		if ($('#header-right').length) {
			// this is a subpage, so make the header taller
			$('#wrapper').css('background-image', 'url(/images/bg-hp.gif)');
			var original_drop_box = $('.drop-box-small');
			// hide the small drop box and replace with a big one
			attention(original_drop_box);
			$('.drop-box-small').slideUp(400, function() {
				$(this).removeClass('drop-box-small').addClass('drop-box');
				$(this).removeClass('visible').addClass('hidden');
				$('.drop-box.' + drop_class).slideDown(400, function() { finished = true; tryNextAction(); }).removeClass('hidden').addClass('visible');
				updateSelectedLink();
			});
			
		}
		else {
			// If we're currently in matrix view, switch back to normal view.
			if ($('#header-right-matrix').length) {
				$('#header-right-matrix').fadeOut('fast', function() {
					$('#header-right-matrix .matrix-container').remove();
					$('#header-right-matrix').attr('id', 'header-right-home');
					$('.drop-box.visible').removeClass('visible').addClass('hidden');
				}).fadeIn('fast', function() {
					$('.drop-box.' + drop_class).addClass('visible').removeClass('hidden').slideDown(400);
					updateSelectedLink();
					finished = true;
					tryNextAction();
				});
				
			}
			else {
				// Drop timer is stopped so we can restart it after
				$('#header').stopTime('drop-box-timer').find('.drop-box.visible').slideUp(400, function() {
					$(this).removeClass('visible').addClass('hidden');
					
					$('.drop-box.' + drop_class).slideDown(400).removeClass('hidden').addClass('visible');
					updateSelectedLink();
					finished = true;
					tryNextAction();
				});
			}
			dropTimer();
		}
		e.preventDefault();
	});

	function fillMatrix(cls, timer) {
		$.get(matrixSource + cls, function(data) {
			if ($('#header-right-matrix .matrix-container')) {
				$('#header-right-matrix .matrix-container').remove();
			}
			
			var cont = $('<div style="display: none;" class="matrix-container"></div>').append(data);
			cont.find('table').columnHover({eachCell:true,hoverClass:'hover',ignoreCols:[1]});
			cont.find('table td, table th').hover(function() {
				$(this).parent().find('td, th').each(function() {
					$(this).addClass('hovert');
				});
			}, function() {
				$(this).parent().find('td, th').removeClass('hovert');
			});
			cont.append($('<img src="' + matrixCloseButtonUrl + '" class="matrix-closer" alt="Hide" title="Hide matrix" />').click(function(e) {
				// hide matrix
				$('#header-right-matrix').fadeOut(400, function() {
					cont.remove();
					$('#header-right-matrix').attr('id', 'header-right-home').fadeIn(400, function() {
						$('.drop-box.visible').slideDown(400);
						if (timer) dropTimer();
					});
				});
			}));
			
			cont.find('img.demo-hover').hover(function() {
				this.src = '/images/demo-icon-hover.gif';
			}, function() {
				this.src = '/images/demo-icon.gif';
			});

			cont.find('img.matrix-closer').hover(function() {
				this.src = '/images/matrix-close-hover.png';
			}, function() {
				this.src = '/images/matrix-close.png';
			});
	
			
			updateSelectedLink();
			$('#header-right-matrix').addClass('generic');
			$('#header-right-matrix').append(cont);
			cont.fadeIn();
			finished = true;
			tryNextAction();
		});
	}
	
	// Show the matrix view from a dropbox link
	$('.drop-box p, .drop-box-small p').click(function(e) {
		if (!finished) { setAct(function() { $(this).trigger('click'); }); return; }
		finished = false;
		var theclass = getIdClass($('.drop-box.visible, .drop-box-small.visible'));
		var drop_class = 'box-' + theclass;
		// we can't have gotten here while in the matrix view so dont bother checking
		// in a solution page?
		if ($('#header-right').length) {
			var original_drop_box = $('.drop-box-small');
			original_drop_box.slideUp(400, function() { original_drop_box.removeClass('visible').addClass('hidden'); });
			original_drop_box.removeClass('drop-box-small').addClass('drop-box');
			attention(original_drop_box);
			// this is a subpage, so make the header taller
			$('#wrapper').css('background-image', 'url(/images/bg-hp.gif)');
			$('#header-right, #header-left').animate({height: 233}, 400, function() {
				$('#header-right').attr('id', 'header-right-home');
				$('#header-right-home').fadeOut(400, function() {
					$('#header-right-home').attr('id', 'header-right-matrix').fadeIn(400);
					$('.' + drop_class).removeClass('hidden').addClass('visible');
					fillMatrix(theclass, false);
				});
			});
		}
		
		else {
			// Stop drop box timer and hide it.
			$('#header').stopTime('drop-box-timer').find('.drop-box.visible').slideUp(400, function() {
				if (!$('.' + drop_class).hasClass('visible')) {
	
					$(this).removeClass('visible').addClass('hidden');
					$('.' + drop_class).addClass('visible').removeClass('hidden');
				}
				updateSelectedLink();
				// Switch to matrix
				$('#header-right-home').fadeOut('fast', function() {
					$('#header-right-home').attr('id', 'header-right-matrix');
					fillMatrix(theclass, true);
				}).fadeIn();
			});
		}
	});
	
	// Show the matrix view
	$('#header li a.m').click(function(e) {
		if (!finished) { setAct(function() { $(this).trigger('click'); }); return; }
		finished = false;
   		current_li = $(this).parent();
		$('#header li a:first-child').removeClass('selected');
		var link_class = $(this).prev().attr('class').substr(5);
		$(this).prev().addClass('selected');
		var drop_class = 'box-' + link_class;
		if ($('#header-right-matrix').length) {
			$('.drop-box.visible').removeClass('visible').addClass('hidden');
			$('.' + drop_class).removeClass('hidden').addClass('visible');
			updateSelectedLink();
			$('#header-right-matrix .matrix-container').fadeOut(400, function() { $(this).remove(); fillMatrix(link_class); });
		}
		else if ($('#header-right').length) {
			var original_drop_box = $('.drop-box-small');
			original_drop_box.slideUp(400, function() { original_drop_box.removeClass('visible').addClass('hidden'); });
			original_drop_box.removeClass('drop-box-small').addClass('drop-box');
			attention(original_drop_box);
			// this is a subpage, so make the header taller
			$('#wrapper').css('background-image', 'url(/images/bg-hp.gif)');
			$('#header-right, #header-left').animate({height: 233}, 400, function() {
				$('#header-right').attr('id', 'header-right-home');
				$('#header-right-home').fadeOut(400, function() {
					$('#header-right-home').attr('id', 'header-right-matrix').fadeIn(400);
					$('.' + drop_class).removeClass('hidden').addClass('visible');
					fillMatrix(link_class, false);
				});
			});

		}
		else {
			// Stop drop box timer and hide it.
			$('#header').stopTime('drop-box-timer').find('.drop-box.visible').slideUp(400, function() {
				if (!$('.' + drop_class).hasClass('visible')) {
	
					$(this).removeClass('visible').addClass('hidden');
					$('.' + drop_class).addClass('visible').removeClass('hidden');
				}
				updateSelectedLink();
				// Switch to matrix
				$('#header-right-home').fadeOut('fast', function() {
					$('#header-right-home').attr('id', 'header-right-matrix');
					fillMatrix(link_class, true);
				}).fadeIn();
			});
		}
		e.preventDefault();
	});
	
	// Articles
	$('.box img.add-icon').attr('expanded', 'false').click(function() {
		if ($(this).attr('expanded') == 'true') {
			var box = $(this).parent().parent().parent();
			box.find('.box-inner, .box-mid').css('overflow', 'hidden').animate({height: '100px'}, 400, function() {
				box.find('.box-inner').css('height', '100px'); 
			});
			this.src = "/images/box-icon-add-hover.png";
			$(this).attr('expanded', 'false');
		}
		else {
			var box = $(this).parent().parent().parent();
			box.css('height', 'auto');
			box.find('.box-mid').css('height', 'auto');
			box.find('.box-top').css('overflow', 'hidden');	// for some reason box-top keeps growing by 2 pixels when you expand, so hide it
			box.find('.box-inner').css('width', '271px').animate({height: '369px'}, 400, function() {
				$(this).css('overflow', 'auto');
			});

			this.src = '/images/box-icon-minus-hover.png';
			$(this).attr('expanded', 'true');
		}
	});
});