$(function() {
	$("#accordion h2").each(function(index, item){
		item.tabIndex = 0;
		$(item).prepend("<img class='accodionalt' width='0' height='0'"+"src='/img/accordion_alt.jpg' alt='click to open'>");
	});

	//$("#case").height(Math.max(getWindowHeight()-190, 350));
	
	if(window.location.hash)
	{
		var hash = window.location.hash.split('/');	
		var activePanel = hash[0];	
	}
	else 
	{
		var activePanel = null;		
	}
	
	$("#accordion").accordion({fillSpace: false, autoHeight: false, 
	                           collapsible: true, animated: false,
	                           change: accordionChange, active: activePanel});

	$('h2#'+window.location.hash+' img.accodionalt').attr('alt', 'click to close');
	
	if(hash)
	{
		showHash(hash);
	}

	$(window).hashchange(hashChange);	
	
	if($('#activity').length > 0)
	{	
		var act = $('#activity');
		var ind = act.attr('data-indicator');
		var pos = act.attr('data-start');
		
		update_activity(+ind, +pos);
	}
	
	$("input:checkbox.toggle").click(function(event)
	{
		var box = $(event.target);
		var div = $("div#"+box.attr('id')+"_content");
		
		div.css('display', box.attr('checked') ? 'block' : 'none');
	});
	
	$("a.toggle").click(function(event)
	{
		var box = $(this);
		
		var div = $("#"+box.attr('id')+"_content");
		
		div.animate({height: 'toggle'}, 250, function()
		{
			box.find('img').each(function(index, img)
			{
				if(img.alt == 'Open')
				{
					img.alt = 'Close';
					img.src = '/img/minus.png';			
				}
				else
				{
					img.alt = 'Open';
					img.src = '/img/plus.png';
				}
			});
  		});

		return false;
	});
});

function update_activity(ind, pos)
{
	var act = $('#activity');
	var page = (pos / 10) + 1;
	
	$.post('/activity.php', {"indicator": ind, "start": pos}, function(data){
		act.html(data);
		act.prepend($("<h4>").text("Page "+page));
	
		$('#activity_prev').click(function(){update_activity(ind, +pos-10);return false;});
		$('#activity_next').click(function(){update_activity(ind, +pos+10);return false;});	
	});
}


function hashChange()
{
	if(String(window.location).indexOf('#') == -1)
	{
		$("#accordion").accordion("activate", 0);
	}

	var hashstring = window.location.hash;

	if(hashstring)
	{
		var hash = hashstring.split('/');	
		
		if(!$(hash[0]).next().is(":visible"))
		{
			$("#accordion").accordion("activate", hash[0]);
		}
		
		showHash(hash);
	}
}

function showHash(hash)
{
	if(hash[1])
	{
		$(hash[0]+hash[1])[0].scrollIntoView();
	}
	else if(hash[0])
	{
		$(hash[0])[0].scrollIntoView();
	}
}



function accordionChange(event, ui)
{
	if(ui.newHeader.attr('id'))
	{
		if(window.location.hash.indexOf(ui.newHeader.attr('id')) == -1)
		{
			window.location.hash = ui.newHeader.attr('id');
		}
	}
	else if(String(window.location).indexOf('#') != -1)
	{
		window.location.hash = null;
	}
	
	$(ui.oldHeader).children('img.accodionalt').attr('alt', 'click to open');
	$(ui.newHeader).children('img.accodionalt').attr('alt', 'click to close');
	
	setTimeout(function(){
		var first = ui.newContent[0];
		first.tabIndex = -1;
		first.style.outline = 0;
		first.focus();
		
		if(String(window.location).indexOf('#') == -1)
		{
			$(window).scrollTop(0);
		}
	}, 0);
}



function getWindowHeight()
{
	if(self.innerHeight)
	{
		return self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientWidth)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return frameHeight = document.body.clientHeight;
	}
}

function smartReload()
{
	if(document.all)
	{
		history.go(0);
	}
	else
	{
		window.location.reload();
	}
}
