/*
---------------------------------------------------------------------------------------

	Form Manager
		- manages form elements replaced with images

---------------------------------------------------------------------------------------
*/

var formManager = new Object();

formManager.eventHandlers = {
	checkbox: function(event) {
		if (this.checked == true) {
			$(this).addClass('checked');
		} else {
			$(this).removeClass('checked');
		}
	},
	radioButton: function(event) {
		$('input[@type="radio"][@name="' + this.name + '"]', this.form).each(function(item) {
			formManager.eventHandlers.checkbox.call(this);
		});
	}
}

formManager.add = function(inputType, context) {
	if (jQuery.isReady == false) {
		formManager.add.queue.push({
			inputType: inputType,
			context: context
		});
	} else {
		switch (inputType) {
			case 'radio':
			case 'checkbox':
				$('input[@type="' + inputType + '"]', context).each(function(item) {
					$(this).addClass('styled');
					addEvent(this, 'click', formManager.eventHandlers[inputType]);
					formManager.eventHandlers[inputType].call(this);
				});
				break;
// 			case 'text':
// 			case 'password':
// 			case 'textarea':
// 				break;
			default:
				return;
		}
	}
}
formManager.add.queue = [];

formManager.init = function() {
	for (var i = 0; i < formManager.add.queue.length; i++) {
		formManager.add(
			formManager.add.queue[i].inputType,
			formManager.add.queue[i].context
		);
	}
}

if (jQuery) {
	$(document).ready(formManager.init);
}


/*
---------------------------------------------------------------------------------------

	Tabs
		- initializes all tabs on page

---------------------------------------------------------------------------------------
*/
$(document).ready(function() {
	if (!W3CDOM) {
		return;
	}
	
	$('.category-container').addClass('tabbed');
	$('.category-list > li > a').each(function() {
		$(this).html(
			'<span class="tab-top"><span></span></span>' +
			'<span class="tab-body"><span><span class="tab-content">' +
			$(this).html() +
			'</span></span></span>'
		);
	});

	$('.category-container').tabs({
		tabStruct: 'div > div',
		bookmarkable: false,
		selectedClass: 'current',
		hideClass: 'inactive',
// 		fxFade: true,
// 		fxSpeed: 100,
		onShow: function(clickedAnchor, clickedElement, hiddenElement) {
// 			hiddenElement.style.display = '';
			if (hiddenElement.style.opacity) {
				hiddenElement.style.opacity = '';
			}
// 			$(hiddenElement).addClass('inactive');

			if (clickedElement.style.opacity) {
				clickedElement.style.opacity = '';
			}
			clickedElement.style.display = '';
		}
	});
	
	// Tab strip items at either end, used for the empty area without tabs
	$('.category-list').prepend('<li class="tab-strip start" />').append('<li class="tab-strip end" />');
	
	// Following code sets the left placement of the end tab strip, can't figure out a better way...
	$('.category-list > .tab-strip.end').css('left', '0');
	// jQuery's :not() doesn't like multiple classes?
	$('.category-list > li:not(.end)').each(function() {
		var parentElement = $(this).parent('.category-list')[0];
		var tabStripElement = $('.tab-strip.end', parentElement)[0];
		$(tabStripElement).css('left',
			parseInt($(tabStripElement).css('left')) +
			parseInt($(this).outerWidth()) +
			'px'
		);
	});
});


/*
---------------------------------------------------------------------------------------

	Theme
		- initializes and handles theme change functionality, including select box

---------------------------------------------------------------------------------------
*/
$(document).ready(function() {
	if (!W3CDOM) {
		return;
	}
	
	if(!document.styleSheets && !(window.opera && document.childNodes) && !(window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 && document.createElement)) {
		return;
	}

// 	var currentTheme = retrieveCookie('theme');
	var currentTheme = $.cookie('theme');
	if (currentTheme) {
		changeStyle(currentTheme);
	}

	var themeSelect = $('#theme-select-list')[0];
// 	var themeSelectOptions = themeSelect.childNodes;

	// Select the current option if it's the current theme for browsers that don't automatically
	// check if a page is out of date when clicking on a previously-visited link, such as Opera
// 	for (var i = 0; i < themeSelectOptions.length; i++) {
// 		if (currentTheme == themeSelectOptions[i].value) {
// 			themeSelectOptions[i].selected = true;
// 		};
// 	};
	$('option', themeSelect).each(function(i) {
// 		var currentTheme = retrieveCookie('theme');
		var currentTheme = $.cookie('theme');
		if (currentTheme == $(this).val()) {
// 			this.selected = true;
			$(this).attr('selected', 'selected');
		}
	});
	/* --- Style changing functionality --- */
	addEvent(themeSelect, 'change', function() {
		changeStyle(this.options[this.selectedIndex].value);
		$.cookie('theme', this.options[this.selectedIndex].value,
			{expires: 365 * 5, path: siteRoot}
		);
	});
});


/*
---------------------------------------------------------------------------------------

	Contact Page Form

---------------------------------------------------------------------------------------
*/
// $(document).ready(function() {
// 	if ($(document.body).attr('id') != 'contact-page') {
// 		return;
// 	}
// 	$('#contact-form').ajaxForm({
// 		target: '#content-wrapper',
// 		beforeSubmit: function(formData, jqFormObject, options) {
// 			var returnValue = true;
// 			if (!$('#contact-form-author').attr('value')) {
// 				alert('You must enter your name.');
// 				returnValue = false;
// 			} else if (!$('#contact-form-email').attr('value')) {
// 				alert('You must enter your e-mail address.');
// 				returnValue = false;
// 			} else if (!$('#contact-form-message').attr('value')) {
// 				alert('You must enter a message.');
// 				returnValue = false;
// 			}
// 
// 			if (returnValue == true) {
// 				$(':input', jqFormObject).attr('disabled', 'disabled');
// 				$('button[@name=send]', jqFormObject).text('Sending...');
// 			}
// 			return returnValue;
// 		}
// 	});
// });


/*
---------------------------------------------------------------------------------------

	Tooltips

---------------------------------------------------------------------------------------
*/
$(document).ready(function() {
	// Modify global settings
	$.extend($.fn.Tooltip.defaults, {
		track: true,
		showURL: false
	});

// 	$('#content a[@href]').Tooltip({
// 		showURL: true
// 	});
// 	$('#navigation a, #link-buttons a').Tooltip();
	$('#navigation a').Tooltip({
		showBody: ' - '
	});
	$('#content a[@href], #link-buttons a').Tooltip({
		extraClass: 'no-heading'
	});
	// For Moonbeam
	$('#content a[@href]').each(function() {
		this.moonbeamCaption = $(this).attr('title');
	});

	// Insert additional HTML
	$tooltipChildren = $('#tooltip').children();
	$('#tooltip').append(
		'<div class="tooltip-top"><div></div></div>' +
		'<div class="tooltip-body"><div>' +
			'<div class="tooltip-content"></div>' +
		'</div></div>'
	);
	$('#tooltip .tooltip-content').append($tooltipChildren);
});
