$(document).ready(function () {
	
	// header (sidebar) navigation (also see sIFR config as that controls the subnavigation on/off)
	pageheader = {
		
		init: function() {
			$headerNavigation = $('#header .navigation');
			pageheader.bindNavEvents();
			
			$headerNavigation.find('.subnavigation').each(function(i){
				$(this).css('background-position', '0px ' + (i*28+4)+'px');
			});
		},
		
		bindNavEvents: function() {
			$('#header').bind('mouseleave', function() {
				pageheader.subNavHide();
			});
		},
		
		subNavShow: function(element) {
			// hide all navigation
			pageheader.subNavHide();
			
			// reposition tab to match element currently being displayed
			// tabOffset = $(element).parents().filter('li.navitem').offset();
			// tabOffset = tabOffset.top - 50;
			// tabOffset = "0 " + tabOffset + "px";
			// $headerNavigation.find('li .subnavigation').css({backgroundPosition:tabOffset});
						
			//update title
			if( $(element).parent().hasClass("navitem") )
			{
				pageheader.updateLinkDescription($(element).attr('title'));
				$(element).parent().addClass('hover');
			}
			else
			{
				pageheader.updateLinkDescription($(element).parent('li.navitem h2').attr('title'));
				$(element).parents().filter('li.navitem').addClass('hover');
			}
			
			// fade in element and add class
			// $(element).parents().filter('li.navitem').addClass('hover');
		},
		
		subNavHide: function(element) {
			$headerNavigation.find('li.navitem').removeClass('hover');
		},
		
		updateLinkDescription: function(content) {
			$headerNavigation.find('.description').html(content);
		}
		
	};
	
	// the experimenta ribbon
	experimenta = {
		init: function() {
			$experimentanavigation = $('#experimentanavigation');
			
			// calculate the height of the experimenta slider and hide it (note: 18px is assumed to be the height of the control icon).
			offsetHidden = -$experimentanavigation.find('.items').height();
			$experimentanavigation.find('.items').css({top: offsetHidden});
			experimenta.bindEvents();
		},
		
		open: function() {
			$experimentanavigation.find('.items').animate({top:'18px'}, 1000);
		},
		
		close: function() {
			$experimentanavigation.find('.items').animate({top:offsetHidden}, 1000);
		},
		
		bindEvents: function() {
			$experimentanavigation.find('.close').click( function() {
				experimenta.close();
			});
			$experimentanavigation.find('.control').click( function() {
				// if list is open, close it, otherwise open it.
				if( $experimentanavigation.find('.items').css('top') == '18px' ) {
					experimenta.close();
				} else {
					experimenta.open();
				}
				return false;
			});
		}
		
	};
	
	pageheader.init();
	
	$("#header .navigation li.navitem h2").hover(
		function(){
			pageheader.subNavShow($(this));
		},
		function(){
			
		}
	);
	
	
	experimenta.init();
	
	// Show/Hide icon labels in project listing template
	$('li.projectlist_icon a').hover(
		function() { 
			var iconContainer = $(this).parents('.projectlist_links');
			$('.icon_label', iconContainer).fadeIn();
			$('.icon_label span', iconContainer).replaceWith( "<span>" + $(this).attr("title") + "</span>" );
		},
		function() {
			var iconContainer = $(this).parents('.projectlist_links');
			$('.icon_label', iconContainer).hide();
		}
	);
	
	/* 
	----------------------
	Hide unused elements
	----------------------
	*/
	
	// Hide project list icons on DOM ready
	$('.tableofcontents li a').each(function (){
		if($(this).attr('href') == "") {
			$(this).parent().remove();
		}
	});
	
	//Hide table of contents slots if empty
	$('li.projectlist_icon a').each(function (){
		if($(this).attr('href') == "") {
			$(this).parent().remove();
		}
	});
	
	// Remove left column quote marks if empty
	var quote = $(".aside q");
	for(var i = 0; i < quote.length; i++) {
		q = $(quote[i]);
		// is q empty?
		if(!q.text().match(/[a-z]./)) {
			q.hide();
		}
	}
	
	// Hide content images if default text
	$('.article img').each(function() {
		var defaultImg = "/templates/sctwww/images/temp/temp.png"; 
		if( $(this).attr('src') == defaultImg ) {
			$(this).remove();
		}
	});
	
	// Remove case links
	$('li.case_link a').each(function (){
		if($(this).attr('href') == "") {
			$(this).parent().remove();
		}
	});

	// Hide project list icons on DOM ready
	$('p.more a').each(function (){
		if($(this).attr('href') == "") {
			$(this).parent().remove();
		}
	});
	
	$('#signup').focus(function(){
		_value = $(this).val();
		_prompt = "Enter your email";
		if( _value == _prompt )
			$(this).val("")
	});
	
	$('#signup').blur(function(){
		if( $(this).value == "")
			$(this).val("Enter your email");
	});

});