﻿$(document).ready(function() {

    //Smooth PageScroll
    function filterPath(string) { return string.replace(/^\//, '').replace(/(index|default).[a-zA-Z]{3,4}$/, '').replace(/\/$/, '') } var locationPath = filterPath(location.pathname); $('a[href*=#]').each(function() { var thisPath = filterPath(this.pathname) || locationPath; if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, '')) { var $target = $(this.hash), target = this.hash; if (target) { var targetOffset = $target.offset().top; $(this).click(function(event) { event.preventDefault(); $('html, body').animate({ scrollTop: targetOffset }, 1000, function() { location.hash = target }) }) } } });

    //sets up the focus events for form elements
    $(".page_form input:text, .page_form input:password, .page_form textarea").focus(function() {
        $(this).addClass("activeElement");
    }).blur(function() {
        $(this).removeClass("activeElement");
    });
    $(".page_form input:checkbox").css("border", "0");
    $(".page_form input:submit, .page_form input:reset").focus(function() {
        $(this).addClass("activeElement");
    }).blur(function() {
        $(this).removeClass("activeElement");
    });


    //prepare external links
    $('#content a:not(:has(img),".button")').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).append(' <img src="/graphics/external.png" alt="external link">');


    /* --------------------------------------------
    * multitoggles
    * -------------------------------------------- */

    function showToggle(el) {
        $(el).parents('div.toggle').addClass('open').find('.slidetarget').slideDown();
        setToggleAll(el);
    }
    function hideToggle(el) {
        $(el).parents('div.toggle').removeClass('open').find('.slidetarget').slideUp();
        setToggleAll(el);
    }

    function setToggleAll(el, mtoggle) {
        var mtoggle = mtoggle || $(el).parents('div.mtoggle').get(0);
        var allOpen = $(mtoggle).find('.toggle.open').length == $(mtoggle).find('.toggle').length;
        if (allOpen) {
            $(mtoggle).find('h2 a')
				.unbind('click')
				.find('span').html('hide all').end()
				.bind('click', function(e) {
				    $(mtoggle).find('h3 a').each(function() { hideToggle(this); })
				    return false;
				});
        } else {
            $(mtoggle).find('h2 a')
				.unbind('click')
				.find('span').html('show all').end()
				.bind('click', function() {
				    $(mtoggle).find('h3 a').each(function() { showToggle(this); })
				    return false;
				});
        }

    }

    // set up the "show/hide all" link			
//    $(".mtoggle h2")
//		.append(' <span>show all</span>')
//		.wrapInner('<a href="#" class="multi-toggler"></a>');

    $('div.mtoggle').each(function() { setToggleAll(null, this) });


    // initialize the individual toggles
    $(".toggle").each(function() {
        $(this).find('.slidetarget').hide();

        $(this).find('h3:first')
			.click(function(e) {
			    e.preventDefault();
			    var isOpen = $(this).parents('div.toggle').hasClass('open');
			    if (isOpen) {
			        hideToggle(this);
			    } else {
			        showToggle(this);
			    }
			})
			.find('a').append('<span>Expand Details</span>');
    });

});
