$(function(){

	anim = 'on';				//	Animate open div's upon arrival, possible values on and off
	time = 600;					//	Animation speed
	aClass = 'accord';			//	Accordion class
	sClass = 'switch';			//	Accordion switch class Note: only one element inside the accordion can have this class
	autoclose = 'off';			//	Setting to autoclose accordions [Other than the one being activated] that may be open
	toggle = 'on';				//	Setting to toggle open and closing of accordions

	$accord = $('.' + aClass);
	$accord.each(function(){
		$(this).data('status', 'off');
		$(this).data('ht', $(this).height());
	});
	cht = $accord.children('h2:first').outerHeight();
	$accord.height(cht);
	loc = ((window.location.hash).replace("#", '+'));
	//for(i=0;i<6;i++) loc = (loc.replace("%23", '+'));
	loc = loc.split('+');
	//alert(loc);
	for (i=1; i<loc.length; i++){
		if (loc[i] != ''){
			$vis = $('#' + loc[i]);
			if (anim == 'on'){
				$vis.data('status', 'on').animate({
					height: $vis.data('ht')
				}, time).addClass('isOpen');
			}
			else {
				$vis.data('status', 'on').height('auto').addClass('isOpen');
			}
		}
	}
	$('a.' + sClass).click(function(){
		$div = $($(this).attr('href'));
		if ($div.data('status') == 'on' && $(this).parent().attr('tagName') != 'H2'){
			$tar = $div.children('h2:first').children('a').eq(0);
			if ($div.data('cloning') != 'done'){
				$div.data('cloning', 'done');
				$clone = $tar.clone();
				cWidth = $tar.width();
				$clone.addClass('highlight').css({
					width: cWidth,
				});
				$tar.before($clone);
				flicker($clone, 400);
			}
			else {
				flicker($tar, 200);
			}
		}
		if ($div.data('status') == 'off'){
			$div.animate({
				height: $div.data('ht')
			}, time)
			id = $div.data('status', 'on').addClass('isOpen').attr('id');
			winloc('off', autoclose, id);
		}
		else if (toggle == 'on' && $(this).parent().attr('tagName') == 'H2') {
			$div.animate({
				height: cht
			}, time)
			$div.data('status', 'off').removeClass('isOpen');
			winloc('on', autoclose, '');
		}
		if (autoclose == 'on'){
			$accord.not($div).animate({
				height: cht
			}, time).data('status', 'off');
		}
		return false;
	});
});

function winloc(status, autoclose, id){
	newloc = '';
	
	//oldloc = ((String(window.location)).replace("%23", '+'));
	//for(i=0;i<6;i++) oldloc = (oldloc.replace("%23", '+'));
	temper = String(window.location).split('#');
	oldloc = temper[1].split('+');
	//alert(oldloc);
	if (autoclose != 'on') {
		for (i=0; i<oldloc.length; i++){
			if (oldloc[i] != $div.attr('id') && oldloc[i] != ''){
				newloc += oldloc[i] + '+';
			}
		}
	}
	hash = '';
	/*if (newloc == '') {
		hash = '+';
	}*/
	if (status == 'on') {
		window.location = temper[0] + '#' + newloc;
	}
	else {
		window.location = temper[0] + '#' + newloc + id;
	}
}
function flicker(obj, ftime){
	obj.fadeIn(ftime);
	setTimeout(function(){
		obj.fadeOut(ftime);
	}, ftime);
}
