/*	TITLE		: gc.js
	
	DECRIPTION	: Javascript library
	
	REQUIREMENTS: jQuery.js

	HISTORY		:	2008-06-08	SJW	Added email
					2010-04-05  SJW Added tech
----------------------------------------------------------------------
*/
/*
----------------------------------------------------------------------
	TITLE:		: table-of-contents.js
	
	DESCRIPTION	: Build table of contents
	
	IMPORTANT   : There is a bug I can't find. This doesn't work unless its is the first script!
	
	HISTORY		: 2011-09-17 SJW Initial version
----------------------------------------------------------------------
*/
/*
* jQuery tocBuilder plugin, version 1.1.2.
* Go to http://proofbydesign.com/Resources/ for more information and documentation.
*
* Copyright (c) 2011 Rob Kent <rob.kent@proofbydesign.com>
* Licensed under the MIT License:
*   http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
    var props = {
        type: 'classes',
        insertBackLinks: true,
        backLinkText: 'Back',
        startLevel: 1,
        endLevel: 6,
        textCallback: null
    }

    var methods = {
        init: function (options) {
            // '$that' is the div that will contain the TOC. We are going to empty this destructively.
            var $that = this;

            jQuery.extend(props, options);

            if (props.type !== 'headings') {
                props.type = 'classes';
            }

            if (typeof (props.startLevel) !== 'number' || props.startLevel < 1) {
                props.startLevel = 1;
            }

            if (typeof (props.endLevel) !== 'number' || props.endLevel < 1) {
                props.endLevel = 6;
            }

            if (props.startLevel > props.endLevel) {
                var temp = props.endLevel;
                props.endLevel = props.startLevel;
                props.startLevel = temp;
            }

            $that.empty();
            var TOCTargetId = $that.attr('id') + "targetTOC";

            // Create a target for the 'Back' link (from a heading to the TOC).
            $("<a/>")
                .attr({
                    'id': TOCTargetId,
                    'name': TOCTargetId,
                    'class': 'tocBackTarget'
                }).appendTo($that);

            // If props.type=headings, select all Hx elements, otherwise select all elements with a tocEntry class; 
            // each tocEntry element should have a tocLevel attribute whose value should be a number.
            var selector;
            if (props.type === 'headings') {
                selector = ':header';
            }
            else {
                selector = '.tocEntry';
            }
            $(selector).each(function (index) {
                var $this = $(this);
                var level = 1;

                if (props.type === 'headings') {
                    // Use the number from the tag: h1, h2, etc.
                    level = parseInt(this.tagName.substring(1));
                }
                else {
                    // Use the value of the tocLevel attribute.
                    var test = $this.attr('tocLevel');
                    if (test !== undefined && parseInt(test) !== NaN) {
                        level = parseInt(test);
                    }
                }
				
                // Only process entries whose level is in range.
                if (level < props.startLevel || level > props.endLevel) return;

                var targetId = "toc_" + level + "_" + index;

                // Check if a previous TOC has already processed and stored the title. If so,
                // use that one so that we don't include our own back link in the text when creating
                // multiple TOCs with the same heading.
                var headingText = $this.data('headingText');
                if (headingText === undefined) {
                    // have we got a callback?
                    if (props.textCallback) {
                        headingText = props.textCallback($this);
                    }
                    else {
                        headingText = jQuery.trim($this.text());
                    }
                    $this.data('headingText', headingText);
                }

                // create an anchor and append it to the heading.
                var backLink = $("<a>" + props.backLinkText + "</a>")
                    .attr({
                        'class': 'tocBackLink',
                        'name': targetId,
                        'id': targetId,
                        'href': '#' + TOCTargetId,
                        'title': 'Go back to the table of contents'
                    });

                if (props.insertBackLinks !== true) {
                    // remove the text content of the backlink.
                    backLink.text('');
                }

                backLink.prependTo($this);

                // create a toc line at the correct level.
                var $TOCLine = $("<div class='TOCLine' />").attr('class', 'tocLevel' + level.toString());

                // create a toc entry and append it to the toc line div and append that to the toc div.
                var $entry = $("<a>" + headingText + "</a>")
                .attr({
                    'title': headingText,
                    'href': '#' + targetId,
                    'class': 'tocLink'
                })
                .appendTo($TOCLine)
                .parent()
                .appendTo($that);
            });

            return this.show();
        },

        disable: function (keepElement) {
            // empty the TOC element of all links and hide the element, unless keepElement=true.
            this.empty();
            if (props.insertBackLinks === true) {
                $("a.tocBackLink").remove();
            }
            if (keepElement !== true) {
                this.hide();
            }
            return this;
        },

        rebuild: function () {
            // clear and rebuild the TOC using the original options.
            this.tocBuilder('disable');
            this.tocBuilder('init');
            return this;
        }
    };

    $.fn.tocBuilder = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tocBuilder');
        }
    };
})(jQuery);
$(document).ready(function() 
    { 
		$("#toc").tocBuilder({ backLinkText : '' })
	}) 

/*
----------------------------------------------------------------------
	TITLE:		: email.js
	
	DESCRIPTION	: Convert <span class="email"> into <a href="mailto:"> links
	
	HISTORY		: 2008-06-08 SJW Initial version
----------------------------------------------------------------------
*/
$(document).ready(function(){
// Default domain name
	var domain = "glenfieldchurches.org.uk";

// Regular expression to match all spaces
	var from = / /g;

// Loop through all <span class="email"> tags
	
	$('span.email').each(function(){
// Read the 'rel' attribute
		var title = $(this).attr('title');

// Read the name of the person between the <span> and </span> tags
		var person = $(this).html();

		if(title == null || title == ""){
// No 'title' attribute specified.
// Check to see if we recognise the person. 
// If known, then use their known email address.
			switch(person){
			case "Robert Northage":
				email = "rhnorthage" + "@super.net";
				break;
			default:
// If person is unknown then replace all spaces with a dot and append the default domain name
				email = person.replace(from, ".") + "@" + domain;
			}
		} else {
// 'title' attribute specific. Check for '#' in the email address.
			if(title.indexOf("#") == -1){
// No '#' in the email address, so append the default domain
				email = title + "@" + domain;
			} else {
// '#' in the email address, so replace it with a '@'
				email = title.replace("#","@");
			}
		};

// Create an <a> tag
		var anchor = document.createElement("a");
// Set the 'href' attrribute to the email address (prefix with mailto:)
		anchor.setAttribute("href", "mailto:" + email);
// Create a text node with the name of the person (taken from the <span> tag). Replace " [at] " with "@"
		var person = person.replace(/ \[\at\] /, "@");
		var text = document.createTextNode(person);
// Append the text node to the <a> tag
		anchor.appendChild(text);
// Append the <a> tag to the document, immediately after the <span>...</span>. Then remove the <span> tag. 
		$(this).after(anchor).remove();
	});
});

/*
----------------------------------------------------------------------
	TITLE:		: tech.js
	
	DESCRIPTION	: Allow double-click of #tech <p> to show technical details
	
	HISTORY		: 2010-04-05 SJW Initial version
----------------------------------------------------------------------
*/
$(document).ready(function(){
	$('#tech').each(function(){
		$(this).hide();
		$('#status').dblclick(function(){
			$('#tech').show();
		});
	});
});
/*
----------------------------------------------------------------------
	TITLE:		: expand-thumb.js
	
	DESCRIPTION	: Double-click photo to expand thumbnail
	
	HISTORY		: 2011-03-11 SJW Initial version
----------------------------------------------------------------------
*/

$(document).ready(function(){
	expanded = false;
	$("#swap").click(function() {
		if(expanded){
			$("#swap .full").hide('slow', function(){
				$("#swap .thumb").show('slow');
			});
			expanded = false;
		} else {
			$("#swap .thumb").hide('slow', function(){
				$("#swap .full").show('slow');
			});
			expanded = true;
		}
	});
	$("#swap .thumb").append("<div><i>Click to expand</i></div>");
	$("#swap .full").append("<div><i>Click to reduce</i></div>");
})


