
jQuery(document).ready(function ($) {

//	$('#header').hide();


	// hide navigation lists / leave only the headers

	// hide the lists next to the title
	var elements = $('#sidebar li.widget:has(ul) .widgettitle');
	elements.next('ul').hide();

	// re-show current page section
	// sorry, i know that's a nasty hack
	var elementsToUnhide = $('#sidebar li.widget:has(ul:has(.current_page_ancestor, .current_page_parent, .current_page_item)) .widgettitle');
	elementsToUnhide.next('ul').show();

	// add click handler to toggle the hidden lists
	elements.click(function (event) {
		$(this)
			.next('ul').slideToggle('fast', positionTwitterSidebar)
		;
	});

	// add some styling
	elements.addClass('pointer');


	// add a light switch to the top navigation
	var eBody = $('body');

	// create light switch
	$('#header .navigation').append('<li id="lightSwitch" class="page-item"><a title="Light switch" href="#">Light switch</a></li>');

	// add click handler
	$('#lightSwitch').click(function (event) {
		event.preventDefault();

		// if light is off
		if (eBody.hasClass('invert')) {
			// turn light on
			eBody.removeClass('invert');
			$.cookie('lightSwitch', 'on', lightSwitchCookieOptions);

		// if light is on
		} else {
			// turn light off
			eBody.addClass('invert');
			$.cookie('lightSwitch', 'off', lightSwitchCookieOptions);
		}
	});

	// if light is off by cookie
	if ($.cookie('lightSwitch') == 'off') {
		// turn light off
		eBody.addClass('invert');
	}

	// if light is on by cookie
	if ($.cookie('lightSwitch') == 'on') {
		// turn light off
		eBody.removeClass('invert');
	}


	// make the navigation foldable
	var
			navItems = $('#cases-list > li')
		,	selector = ''
		,	elements = null
	;

	// define visible lists
	selector = '.current_page_ancestor, .current_page_parent, .current_page_item';
	elements = navItems.find(selector).parent('ul'); 
	elements.addClass('current_page_path');

	// hide non-visible lists
	selector = 'ul:not(.current_page_path)';
	elements = navItems.find(selector); 
	elements.hide();

	// add link click event
	selector = 'a:not(:only-child)'; // every a that is not a page link
	elements = navItems.find(selector); 
	elements.click(function (event) {
		event.preventDefault();
		$(this)
			.blur()
			.next('ul').slideToggle('fast', positionTwitterSidebar)
		;
	});

	
	// onClick remove default text of searchform
	$('#searchform #s')
		.click(function (event) {
			if ($(this).val() == 'search...') {
				$(this).val('');
			}
		})
		.focus(function (event) {
			if ($(this).val() == 'search...') {
				$(this).val('');
			}
		})
		.blur(function (event) {
			if ($(this).val() == '') {
				$(this).val('search...');
			}
		})
	;

	// position the twitter feed box

	positionTwitterSidebar();

	function positionTwitterSidebar()
	{
		var twittfeedBoxContainer = $("#main-content > div.page, #main-content > div.blog-archives");
		var sidebarBox = twittfeedBoxContainer.find("#sidebar");
		var twittfeedBox = twittfeedBoxContainer.find("#twitter-sidebar");
		
		twittfeedBox.css({"margin-top":"0px"});
		var twittfeedBoxContainerHeight = twittfeedBoxContainer.height();
		var sidebarBoxHeight = sidebarBox.height();
		var twittfeedBoxHeight = twittfeedBox.height();

		var twittfeedBoxSpaceTop = twittfeedBoxContainerHeight-twittfeedBoxHeight-sidebarBoxHeight;
		var twittfeedBoxContainerNewHeight = sidebarBoxHeight+twittfeedBoxSpaceTop+twittfeedBoxHeight;

		if (twittfeedBoxContainerNewHeight <= twittfeedBoxContainerHeight) {
			twittfeedBox.css({"margin-top":twittfeedBoxSpaceTop+"px"});
		}
	}

	$(window).resize(positionTwitterSidebar);


	// external links open in new window
	$("a.external").click(function(){ window.open(this.href); return false; });


	// lightbox stuff
	var lightboxes = $("a.lightbox");

	// make them a colorbox
	lightboxes.colorbox({
			transition:"fade"
		,	scalePhotos:true
		,	maxWidth:"95%"
		,	maxHeight:"95%"
		,	speed:250
	});

	// create captions from image title
	lightboxes.each(function(){
		img = $(this).find('img');
		caption = false;

		switch (true) {
		case (img.attr('alt') != ''):
			caption = img.attr('alt');
			break;

		case (img.attr('title') != ''):
			caption = img.attr('title');
			break;

		case ($(this).attr('title') != ''):
			caption = $(this).attr('title');
			break;

		default:
			break;
		}

		// prevent default tooltips
		img.removeAttr('title');
		$(this).removeAttr('title');

		if (caption !== false) {
			$(this).append('<span class="caption"><span class="inner">'+ caption +'</span></span>');
		}
	});

	// comments
	$('#comments-wrap').hide();

	$('#reveal-comments > a').click(function(event){
		//event.preventDefault();
		$(this).blur();
		$('#comments-wrap').toggle();
	});

}); // end $(document).ready...

