/**
 *
 */

function productSelectorHomepage()
{
	var _ps = this;

	_ps.obj = $('#prosel');
	_ps.stateList = $('.select_state .container .state');

	// Hover event for each map area.
	$('.select_state .container map area', _ps.obj).each(function()
	{
		new stateArea($(this));
	});

	// Need cover checkboxes.
	_needCoverGroup = $('.need_cover .checkbox', _ps.obj);
	var _needCoverFamily = new psCheckbox($('.need_cover .family', _ps.obj), _needCoverGroup);
	var _needCoverCouple = new psCheckbox($('.need_cover .couple', _ps.obj), _needCoverGroup);
	var _needCoverSingle = new psCheckbox($('.need_cover .single', _ps.obj), _needCoverGroup);

	// I want checkboxes.
	var _iWantGroup = $('.i_want .checkbox', _ps.obj);
	var _iWantBasics = new psCheckbox($('.i_want .basics', _ps.obj), _iWantGroup);
	var _iWantInBetween = new psCheckbox($('.i_want .inbetween', _ps.obj), _iWantGroup);
	var _iWantEverything = new psCheckbox($('.i_want .everything', _ps.obj), _iWantGroup);

	// Submit button.
	$('.i_want .getaquote').click(function()
	{
		var _url = '/Product-Selector/#';

		// Load the selected state
		var _selectedState = $('.select_state .container .selected', _ps.obj);
		if( _selectedState.length != 0 )
		{
			_url += 'state=' + _selectedState.attr('rel') + '&';
		}

		var _needCoverSelected = $('.need_cover .checkbox.checked', _ps.obj);
		if( _needCoverSelected.length != 0 )
		{
			_url += 'member_class=' + _needCoverSelected.attr('rel') + '&';
		}

		var _iWantSelected = $('.i_want .checkbox.checked', _ps.obj);
		if( _iWantSelected.length != 0 )
		{
			_url += 'want=' + _iWantSelected.attr('rel') + '&';
		}

		location.href = _url;
	});

	function stateArea( obj )
	{
		var _stateArea = this;
		_stateArea.obj = obj;
		_stateArea.img = $('.select_state .' + _stateArea.obj.attr('rel'));

		try
		{  
			document.createEvent("TouchEvent");  
			// Do not do hovers for touch events.
		} catch (e)
		{  
			// Hover state events.
			_stateArea.obj.hover(function()
			{
				_stateArea.img.addClass('active');
			}, function()
			{
				_stateArea.img.removeClass('active');
			});
		}

		_stateArea.obj.click(function()
		{
			// Select just this state.
			_ps.stateList.removeClass('selected');
			_stateArea.img.addClass('selected');
		});
	}
	
	//Fancy checkbox object.
	function psCheckbox( obj, group, callback )
	{

		var _checkbox = this;
		_checkbox.obj = obj;
		_checkbox.group = group;
		_checkbox.callback = callback;

		// Returns true if the checkbox is checked, false otherwise.
		_checkbox.checked = function()
		{
			return _checkbox.obj.hasClass('checked');
		}

		// Toggles the checkbox on or off.
		_checkbox.toggle = function( clicked )
		{
			( _checkbox.checked() )?_checkbox.off(clicked):_checkbox.on(clicked);
		}

		// Checks the checkbox.
		_checkbox.on = function( clicked )
		{
			// If there is a group of checkboxes, unselected the others.
			if( _checkbox.group != undefined )
			{
				_checkbox.group.removeClass('checked');
			}

			_checkbox.obj.addClass('checked');
			if( clicked && typeof(_checkbox.callback) == 'function' )
			{
				_checkbox.callback();
			}
		}

		// Unchecks the checkbox.
		_checkbox.off = function( clicked )
		{
			_checkbox.obj.removeClass('checked');
			if( clicked && typeof(_checkbox.callback) == 'function' )
			{
				_checkbox.callback();
			}
		}

		// Bind the click event to the checkbox.
		_checkbox.obj.click(function()
		{
			_checkbox.toggle(true);
		});

		// If there is a label, bind that event also.
		// Don't do it for ie, as ie binds the on click event for the a to the matching label.
		if( undefined == jQuery.browser.msie )
		{
			$('label[for="' + _checkbox.obj.attr('id') + '"]').click(function()
			{
				_checkbox.toggle();
			});
		}

		return _checkbox;
	}
}

$(document).ready(function()
{
	var ps = new productSelectorHomepage();
});
