var Forms = {
	assignWithDefaultState: function(selector, _defaultValue)
	{
		$(document).ready(function()
		{
			var currentElem = $(selector);

			// set the default value data attribute
			var defaultValue = (_defaultValue != undefined ? _defaultValue : currentElem.attr('value'));
			currentElem.data('default', defaultValue);

			// if the value is default, add please select class
			if (currentElem.data('default') == currentElem.attr('value'))
			{
				currentElem.addClass('please-select').data('autoSet', true);
			}

			// bind focus and blur events
			$(selector).focus(function()
			{
				// select the elem and remove the select class
				var elem = $(this);
				elem.removeClass('please-select');

				// set the value to empty if it is the default
				if (elem.attr('value') == elem.data('default') && elem.data('autoSet') == true)
				{
					elem.attr('value', '');
				}
			}).blur(function()
			{
				// if the value isnt empty, do nothing
				if ($(this).attr('value') != '')
				{
					$(this).data('autoSet', false);
					return;
				}
				
				// as the value is empty, re-add class and set to default
				$(this).addClass('please-select').attr('value', $(this).data('default'))
					.data('autoSet', true);
			});
		});
	}
}

function initExpanders()
{
	$('.expander-content').hide();
	$('.expandable .content').append('<div class="expander collapsed"><span>show answer</span></div>');
	$('.expander').click(expander_OnClick);
}

function expander_OnClick()
{
	var expander = $(this);
	expander.toggleClass('expanded').toggleClass('collapsed');
	expander.parents('.expandable').find('.expander-content').slideToggle(300);

	expander.find('span').text(expander.hasClass('expanded') ? 'hide answer' : 'show answer');

	var category = "Frequently Asked Questions",
		eventType = "Expander Click",
		param = expander.hasClass('expanded') ? 'Expanded' : 'Collapsed';
		
	trackGAEvent(category, eventType, param, null);
}

// Binds teh catalogue quick navigation sidecar events
function initCatalogueQuickNavigationTab()
{
	// set the pages and options
	var pages = $('.catalogue-quick-nav .page'),
		options = $('.catalogue-quick-nav .nav .option'),
		selectedIndex = options.index($('.catalogue-quick-nav .nav .selected'));

	selectedIndex = (selectedIndex == -1 ? 0 : selectedIndex);
	
	// hide all pages and set correct seleted state
	pages.hide().eq(selectedIndex).show();
	options.removeClass('selected').eq(selectedIndex).addClass('selected');

	// add selectable and bind clicking
	$('.catalogue-quick-nav .nav').addClass('selectable');
	options.click(function()
	{
		// update the selected class
		options.removeClass('selected');
		$(this).addClass('selected');
		// show the correct page
		pages.hide().eq(options.index(this)).show();
	});
}

// Sets the hover event for the share this anchored featured
function initShareThisPage()
{
	$('#sharePage').hoverIntent(function()
	{
		$(this).animate({ 'left': '0' }, 500);
	},
	function()
	{
		$(this).animate({ 'left': '-120px' }, 500);
	});
}

// Dynamically adds the top menu arrows to the top navigation
function addMenuArrows()
{
	$('#header .menu li.level1 ul.level2').parent().append('<span class="menu-arrow">&nbsp</span>');
}

// Determines if the browser is ie6
function isBrowserIE6()
{
	return ($.browser.msie && $.browser.version < 7);
}

// Determines if the browser is ie7
function isBrowserIE7()
{
	return ($.browser.msie && $.browser.version < 8);
}

// Tracks the user clicking previous and next in a carousel
function bindCarouselPrevNextEventTracking(carouselType)
{
	var category = "Carousel Interaction",
		eventType = "Click";
		
	$('.jcarousel-prev').click(function()
	{
		trackGAEvent(category, 'Left ' + eventType, carouselType, null);
	});

	$('.jcarousel-next').click(function()
	{
		trackGAEvent(category, 'Right ' + eventType, carouselType, null);
	});
}

$(document).ready(function()
{
	// remove the no-js class from the body
	$('body').removeClass('no-js');

	// if the browser isnt ie 6, load expanders and menu arrows
	if (!isBrowserIE6() && !isBrowserIE7())
	{
		initExpanders();
	}
	if (!isBrowserIE6())
	{
		addMenuArrows();
	}

	// initiality the quick navigation
	initCatalogueQuickNavigationTab();

	// set calendar input and default text
	$('input.calendar').datepicker({ dateFormat: 'dd/mm/yy' });
	Forms.assignWithDefaultState('.bag-quote textarea', 'Additional notes');

	// bind the share this hover
	initShareThisPage();

	// hide sidecar border if no sidecars
	if ($('#sideCars div.sidecar').length <= 1)
	{
		$('#sideCars #upperBorder').css('background', 'none');
	}
});
