﻿/// <reference path="http://ajax.microsoft.com/ajax/jquery/jquery-1.5-vsdoc.js" />

$(function () {
	master.init();
});

window.onload = function() {
	setupSidemenuHeight();
};

var master = function () {


	init = function () {

		initNav();
		initTabs();
		
		//console.log('done');

	};

	initNav = function () {

		var $selected = $('nav#mainmenu .selected'),
			frag = '<span></span>';

		$selected.append(frag);

	};

	initTabs = function () {

		// add class for any tab links on the page
		$('.tab-links').addClass('tab-links-active');
	
		// set live click event for nav items in the newly created tabs menu
		$('.tab-nav a').live('click', function() {

			var theid = $(this).attr('href');
			if ($(this).attr('href').indexOf("/") != -1) {
				// IE 6 & 7 return absolute path, normalise so it's just the href attribute
				theid = $(this).attr('href').substring($(this).attr('href').lastIndexOf("/") + 1, $(this).attr('href').length);
			}
			var selectedPage = $('#' + theid);
			$(selectedPage).parent().children('li').hide();
			$(selectedPage).show();
			$(this).parent().parent().children('li').removeClass('selected');
			$(this).parent().addClass('selected');

			return false;
		});
	
		// create the tabs menu for each tabbed interface
		var interfaceNo = -1;
		$('.tabbed-interface').each(function(){
		
			$(this).children('ul:first').addClass('tab-pages');
		
			interfaceNo ++;
			$(this).addClass('tabs-active');
			var navHtml 	= '<ul class="tab-nav">';
			$(this).children('ul').children('li').each(function(intIndex){
				var id = interfaceNo + "" + intIndex;
				navHtml += '<li><a href="' + id + '">' + $(this).children('h2:first').html() + '</a></li>';
				$(this).children('h2:first').remove();
				$(this).attr("id", id);
				$(this).hide();
			});
			navHtml += '</ul>';
			$(this).prepend(navHtml);
		
			// call click to activae first tab
			$(this).find('.tab-nav li a:first').trigger('click');
		})
	
	};

	setupSidemenuHeight = function () {

		//console.log('start');

		//if related list is empty, hide it and return
		if (!$('#sidemenu').children().length > 0) {
			$('#sidemenu').hide();
			return true;
		}

		//console.log('sm height = ' + $('#sidemenu').height());

		// Equalise heights / columns
		if ($('#maincontent').height() > $('#sidemenu').height()) {
			$('#sidemenu').height($('#maincontent').height());
		}



		//console.log('end');
	}

	return { init: init }
} ();

