﻿function activateAddress(initialTab) {
	try {
		var currentTab = 0;
		var tabs,
		tabulation = false,
		initialTab = initialTab,
		navSelector = '#tabs .ui-tabs-nav',
		navFilter = function (el) {
			return $(el).attr('href').replace(/^#/, '');
		},
		panelSelector = '#tabs .ui-tabs-panel',
		panelFilter = function () {
			$(panelSelector + ' a').filter(function () {
				return $(navSelector + ' a[title=' + $(this).attr('title') + ']').size() != 0;
			}).each(function (event) {
				$(this).attr('href', '#' + $(this).attr('title').replace(/ /g, '_'));
			});
		};

		if ($.address.value() == '' || $.address.value() == '/') {
			$.address.value(initialTab);
			window.location.hash = window.location.hash.replace("/", "");
			//this.scrollTop(0);
		}

		// Address handler
		$.address.history(false).strict(false).wrap(true).init(function (event) {
		//$.address.crawlable(true).history(false).strict(false).wrap(true).init(function (event) {

			// Adds the ID in a lazy manner to prevent scrolling
			$(panelSelector).attr('id', initialTab);

			// Enables the plugin for all the content links
			$(panelSelector + ' a').address(function () {
				return navFilter(this);
			});

			panelFilter();

			// Tabs setup
			tabs = $('#tabs')
					.tabs({
						load: function (event, ui) {
							// Filters the content and applies the plugin if needed
							$(ui.panel).html($(panelSelector, ui.panel).html());
							panelFilter();
						},
						fx: {
							opacity: 'toggle',
							duration: 'fast'
						},
						select: function (event, ui) {
							currentTab = ui.index;
						}
					})
					.css('display', 'block');

			// Enables the plugin for all the tabs
			$(navSelector + ' a').click(function (event) {
				tabulation = true;
				$.address.value(navFilter(event.target));
				tabulation = false;
				return false;
			});

		}).change(function (event) {

			var current = $('a[href=#' + event.value + ']:first');

			// Sets the page title
			$.address.title($.address.title().split(' | ')[0] + ' | ' + current.text());

			// Selects the proper tab
			if (!tabulation && (tabs != null)) {
				tabs.tabs('select', current.attr('href'));
			}

		}).history(true);

		// Hides the tabs during initialization
		//document.write('<style type="text/css"> #tabs { display: none; } </style>');
	}
	catch (err) {
		//alert(err.Description);
	}
}
