/* 
----------------------------------
jQuery Minimal Tabify:
----------------------------------
Set up tabs from the children of the selected element, with no bells or whistles. 
Each Child (Tab) needs its own ID
Author:Ben Hull
This Version: 0.1, Mar 1, 2010

*/

(function($) {

    $.fn.tabify = function(options) {
        debug(this);

        // build main options before element iteration
        var opts = $.extend({}, $.fn.tabify.defaults, options);
		  var container;
        // iterate and reformat each matched element
        return this.each(function() {
            container = $(this);	

            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
				$.fn.tabify.prepareTabs($(this), o);
				$.fn.tabify.createNavigation($(this), o);
				
        });
    };

    function debug($obj) {
        if (window.console && window.console.log)
        window.console.log('Tabifying: ' + $obj.size());
    };

	 $.fn.tabify.prepareTabs = function($tabGroup, o) {
		
		var firstTabSelected = false;
		
		$tabGroup
			.addClass(o.groupClasses)
			.bind('tabify.show', function(event, id){
				//Fix IE - which prepends the whole URL to the href
				if (id.split('#').length > 1) {
					id = '#' + id.split('#')[1]
				};
				$(id).addClass('tabPanelSelected').siblings().removeClass('tabPanelSelected');
			})
			.children()
				.each(function(i){
					var $this = $(this);
				
					$this.addClass(o.itemClasses);
					
					$this.find('h2:first, h3:first, h4:first, h5:first').addClass('tabPanelTitle');
					
					if (!$this.children(':not(.tabPanelTitle, :empty)').length) $this.addClass('empty');

				});
			$tabGroup.children().not('.empty, .static').eq(0).addClass('tabPanelSelected');
    };

    $.fn.tabify.createNavigation = function($tabGroup, o) {
		var navCanvas = $('<ul class="' + o.navClasses + '"></ul>'),
			 firstTabSelected = false;
		
		$tabGroup
			.children().each(function(i){
				
				var $this = $(this);
				var	addClass = $this.is('.empty') ? ' empty' : '';
					addClass += $this.is('.static') ? ' static' : '';
				var	headingText = $this.find('.tabPanelTitle').html(),
					navItem;
					 
					
					 if (!$this.is('.static')) {
						navItem = $('<li class="' + o.navItemClasses + addClass + '" id="tabFor_' + $(this).attr('id') + '"><a href="" rel="#' + $(this).attr('id') + '"><span>' + headingText + '</span></a></li>');
					 	navItem.find('a')
							.bind('focus, click', function(){
								var $this = $(this);
								//Select this item
								$this.closest('li').addClass('selected').siblings(':not(.static)').removeClass('selected');
							
								//Show the corresponding tab:
								$tabGroup.trigger('tabify.show', [$this.attr('rel')]);
								return false;
							})
							.click(function(){
								//return false;
							});
					} else {
						//Dont add click events for static tabs
						navItem = $('<li class="' + o.navItemClasses + addClass + '" id="tabFor_' + $this.attr('id') + '"><span>' + headingText + '</span></li>');
					}
						
				navCanvas.append(navItem);
			});
		
		if (o.alwaysShowTabs || $tabGroup.children().length > 1)  {//Only show the nav if there is more than one tab (or if 'alwaysShow' is enabled).
				$tabGroup.prepend(navCanvas).find('.' + o.navClasses + ' > li').not('.empty, .static').eq(0).addClass('selected');
		}
    };

    $.fn.tabify.defaults = {
        groupClasses: 'tabGroup',
        itemClasses: 'tabPanel',
		  navClasses: 'tabNavigation',
		  navItemClasses: 'tab'
    };


})(jQuery);

/* 
----------------------------------
jQuery Minimal Expand/collapse:
----------------------------------
Apply expand/collapse behaviour to elements:
Each Child (Tab) needs its own ID
Author:Ben Hull
This Version: 0.1, Mar 1, 2010

*/

(function($) {

    $.fn.expandable = function(options) {
        debug(this);

        // build main options before element iteration
        var opts = $.extend({}, $.fn.expandable.defaults, options);
		  var container;
        // iterate and reformat each matched element
        return this.each(function() {
            container = $(this);	

            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
				$.fn.expandable.prepareExpandables($(this), o);
				
        });
    };

    function debug($obj) {
        if (window.console && window.console.log)
        window.console.log('Expandables: ' + $obj.size());
    };

	 $.fn.expandable.prepareExpandables = function($expGroup, o) {
	
		var $toggle = $expGroup
			.find('.toggle').each(function(){
				var $toggle = $(this),
					$togglePanel = $toggle.closest('.expandable'),
					$toggleContent = $togglePanel.find('.contents');
					
					$toggle.click(function(){
						$togglePanel.toggleClass('expanded');
						$toggleContent.slideToggle(250);
					});
			});
			
    };

    $.fn.expandable.defaults = {};


})(jQuery);
