/****
 * Slide Toggle Accordion
 * Copyright (c) 2009 Rueegg Tuck Partner GmbH (http://www.rtpartner.ch)
 * @author Simon Tuck <stu@rtpartner.ch>
 ****/

/****
 * TODO:
 * - Compatibility with jQuery 1.3.1
 ****/

var mySpeed = 'fast';

var clickShowTitle = 'Bitte klicken, um Inhalt anzuzeigen';
var clickHideTitle = 'Bitte klicken, um Inhalt zu verstecken';
var clickShowText = 'Inhalt Anzeigen';
var clickHideText = 'Inhalt Ausblenden';
var titleSelector = '.AccordionTabTab';
var contentSelector = '.foldingContentWrap';
var openClass = 'open';
var closedClass = 'closed';
var toggleFlagClass = 'current_toggle_element';
var internalAnchorClass = 'anchorlink';
var anchorClass = 'anchor';
var addHashToUrl = true;
// Behaviour of other elements when an element is clicked.
// Set to false to keep other elements unchanged when an element is clicked
// Default (true) will close all other elements.
var closeOthersOnClick = true;

function insertTitleLinks() {
	// Wrap the content of the title in a descriptive link
	$(titleSelector).filter('.' + closedClass).wrapInner('<span class="acc-title" />').append('<div class="hideMe">'+clickShowText+'</div>').wrapInner('<a href="javascript:void(0);" title="' + clickShowTitle + '"></a>');
	$(titleSelector).filter('.' + openClass).wrapInner('<span class="acc-title" />').append('<div class="hideMe">'+clickHideText+'</div>').wrapInner('<a href="javascript:void(0);" title="' + clickHideTitle + '"></a>');
}

function addTitle(el) {
	var title = el.find('.acc-title').text().replace(/^\s*[0-9]*\:?[0-9]*\s*(?:Uhr)?\s*/, '').replace(/\s*$/, '')
	if (el.parents('H3').length == 0)
	{
		jQuery('<h3 class="hideMe"/>').text(title)
			.prependTo(el.parent(titleSelector).next());
	}
}

function showHideContent() {
	var anchor = '#' + document.location.hash.replace(/^#/, '');
	if(anchor && anchor != '#' && $('a').is(anchor + ' ~ ' + contentSelector)) { // Given a valid target anchor...
		$('a:not(' + anchor + ') ~ ' + contentSelector).hide(); // Hide all "contentWrap" that are not siblings of the target anchor
		$('a:not(' + anchor + ') ~ ' + titleSelector).removeClass(openClass).addClass(closedClass); // Change class of all "contentWrap" that are not siblings of the target anchor
		$('a' + anchor + ' ~ ' + titleSelector + ':first').removeClass(closedClass).addClass(openClass); // Change class of the "contentWrap" sibling of target anchor
		$('a' + anchor + ' ~ ' + contentSelector + ':first').show(); // Change class of the "contentWrap" sibling of target anchor
	} else { // Else open all elements matching ".open"
		$(titleSelector + ':not(.' + openClass + ')').next(contentSelector).hide();
		$(titleSelector + ':not(.' + openClass + ')').removeClass(openClass).addClass(closedClass);
	}
}

function foldContent(el) {
	var toggleMe = el.parent(titleSelector).next(contentSelector), cb; // Element to toggle
	toggleMe.addClass(toggleFlagClass); // Flag the element to toggle
	el.parent(titleSelector).addClass(toggleFlagClass); // Flag the element's title
	if(toggleMe.css('display') == "none") { // Set the correct class for the newly toggled element
		el.parent(titleSelector).removeClass(closedClass).addClass(openClass);
		el.attr('title', clickHideTitle); // Change the link description
		el.find('DIV.hideMe').html(clickHideText);
		addTitle(el); setHash(el);
		cb = function() { setHash(el); };
		dontCloseOthersOnClick = false; // Enable folding of other content when opening an item
	} else {
		el.parent(titleSelector).removeClass(openClass).addClass(closedClass);
		el.attr('title', clickShowTitle); // Change the link description
		el.parent(titleSelector).next().find('H3.hideMe').remove(); // remove header for opened item
		el.find('DIV.hideMe').html(clickShowText);
		unsetHash(); // nothing is selected, unset the anchor
		dontCloseOthersOnClick = true; // Don't fold other content when closing an item
	}
	toggleMe.stop(true, true).slideToggle(mySpeed, cb); // Toggle the element
	if(closeOthersOnClick && !dontCloseOthersOnClick) closeOthers(cb);
	toggleMe.removeClass(toggleFlagClass); // Remove current element flag
	el.parent(titleSelector).removeClass(toggleFlagClass); // Remove the flag from the element's title
}

function setHash(el)
{
	if(addHashToUrl) {
		if(anchor = getHash(el)) {
			// scroll to the given anchor
			window.location.hash = anchor;
			// Add the anchor to the tip a friend link
			href = $('a#sendLink').attr('href');
			href = href.replace(/%23\w*$/img, '') + '%23' + anchor;
			$('a#sendLink').attr('href', href);
		}
	}
}

function unsetHash(el)
{
	if(addHashToUrl) {
		// unset but dont go to top
		window.location.hash = 'na';
		// Remove any anchors from the tip a friend link
		href = $('a#sendLink').attr('href');
		href = href.replace(/%23\w*$/img, '');
		$('a#sendLink').attr('href', href);
	}
}

function getHash(el)
{
	anchor = el.parent(titleSelector).prev('a.' + anchorClass).attr('id');
	if(anchor && $('a').is(anchor + ' ~ ' + contentSelector)) {
		return anchor;
	} else {
		return false;
	}
}

function closeOthers(cb) {
	var closeMe = $(contentSelector + ':visible').not('.' + toggleFlagClass); // Elements to close are all those not flagged "current", but visisble
	closeMe.stop(true, true).slideUp(mySpeed, cb); // Close all other elements
	$(titleSelector + ':not(.' + toggleFlagClass + ')').removeClass(openClass).addClass(closedClass); // Change class of all other elements
	$(titleSelector).filter('.' + closedClass).find('a').attr('title', clickShowTitle); // Change the link description for all closed elements
}

function accordion() {
	$(titleSelector + ' a').click( function() {
		foldContent($(this));
		return false;
	});
}

function linkToAccordion(){
	$('a.' + internalAnchorClass).click( function() {
		if($(this).attr('hash') + ' ~ ' + contentSelector) {
			el = $('a' + $(this).attr('hash') + ' ~ ' + titleSelector + '> a');
			var toggleMe = el.parent(titleSelector).next(contentSelector); // Element to toggle
			if(toggleMe.css('display') == 'none') {
				toggleMe.addClass(toggleFlagClass); // Flag the element to toggle
				el.parent(titleSelector).addClass(toggleFlagClass); // Flag the element's title
				el.parent(titleSelector).removeClass(closedClass).addClass(openClass);
				el.attr('title', clickHideTitle); // Change the link description
				dontCloseOthersOnClick = false; // Enable folding of other content when opening an item
				toggleMe.stop(true, true).slideToggle(mySpeed); // Toggle the element
				if(closeOthersOnClick && !dontCloseOthersOnClick) closeOthers();
				toggleMe.removeClass(toggleFlagClass); // Remove current element flag
				el.parent(titleSelector).removeClass(toggleFlagClass); // Remove the flag from the element's title
			}
		}
	});
}

function jumpToAnchor() {
	// If the URL contains an anchor, jump to the appropraite locaction
	var anchor = document.location.hash;
	if(anchor && $('a' + anchor)) window.location.hash=anchor;
}

$(function() {
	if ($(titleSelector).length > 1)
	{
	showHideContent();
	insertTitleLinks();
	accordion();
	linkToAccordion();
	jumpToAnchor();

		// add a title for open sections (if el is not already within a title)
		var anchor = '#' + document.location.hash.replace(/^#/, '');
		if(anchor && $('a').is(anchor + ' ~ ' + contentSelector))
		{ addTitle($('a' + anchor + ' ~ ' + titleSelector + ':first').find('A')); }

	}
	else
	{
		$(titleSelector).removeClass(openClass + ' ' + closedClass)
	}
});

