$(function(){

	// Original html of mount finder
	var reset_finder_html = $('#mount_finder').html();
	
	finder_actions();
	
	function finder_actions()
	{
		var code_url = '/mounts/fetch_codes';
		$('#msearch_code').autocomplete(code_url);
		
		// Set placeholder text on text input
		msearch_placeh();
		
		var msearch_value = $('#msearch_code').val();
		
		// Remove completely search button from 123 area - not need if javascript enabled
		$('#m123_submit').remove();
	
		// Product type chosen - populate brands
		$('input[name=m123_type]').change(function () {
			fetch_manus();
		});
		
		// Brand chosen - populate models
		$('select#m123_manu').change(function () {
			model_list($(this).val(), $('input[name=m123_type]:checked').val());
		});
		
		// Detects when model selected
		$('select#m123_model').change(function(){
			// If no model selected then don't submit
			if ($(this).val() != '0')
			{
				remove_filters();
				m123_click();
				$('#msearch_code').val('');
				$("#mounts_123_form").submit();
			}
		});
		
		
		$('#mfilter_submit').click(function () {
			if ($('#finder_fork').hasClass('finder_fork_123'))
			{
				$('#msearch_code').val('');
			}
			else
			{
				$('input[name=m123_type]:first').attr('checked','checked');
				$('#m123_manu').val('0');
				$('#m123_model').val('0');
			}
			
			$("#mounts_123_form").submit();
			return false;
		});
		
		// If place holder text present then make product code blank before submitting the form
		$('#msearch_submit').click(function () {
			if (msearch_value == '' || msearch_value != $('#msearch_code').val())
			{
				remove_filters();
				msearch_click();
			}
	
			$('input[name=m123_type]:first').attr('checked','checked');
			$('#m123_manu').val('0');
			$('#m123_model').val('0');
			
			if ($('#msearch_code').val() == $('#msearch_code').prev().text())
			{
				$('#msearch_code').val('');
			}
			
			$("#mounts_123_form").submit();
			return false;
		});
		
		// Click detected on 123 section
		/*$('#finder_123').click(function () {
			$('#finder_fork').removeClass('finder_fork_open');
			$('#finder_filters').remove();
			$('#finder_fork').removeClass('finder_fork_search');
			$('#finder_fork').addClass('finder_fork_123');
			$('#msearch_code').val('');
			msearch_placeh();
		});*/
		
		// Click detected on product code search section
		/*$('#finder_search').click(function () {
			msearch_click();
		});*/
		
		// Click detected on product code search field
		$('#msearch_code').click(function () {
			//msearch_click();
		});
		
		// Click detected on product code search field
		$('#msearch_code').focus(function () {
			if ($(this).val() == $(this).prev().text())
			{
				$(this).val('');
				//msearch_click();
			}
		});
		
		$('#msearch_code').blur(function () {
			msearch_placeh();
		});
		
		$('.finder_reset').click(function () {
			finder_reset();
			return false;
		});
	
	}
	
	// Function to reset finder as it was when page loaded
	function finder_reset()
	{
		$('#mount_finder').html(reset_finder_html);
		finder_actions();
	}
	
	// Function to fetch brands - json
	function fetch_manus()
	{
		model_list('0', '0');
		
		$.getJSON('/mounts/fetch_manufacturers/'+$('input[name=m123_type]:checked').val(), function (j) {
			var options = '';
			for (var i = 0; i < j.length; i++)
			{
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			$('#m123_manu').html(options);
			$('#m123_manu option:first').attr('selected', 'selected');
		});
	}
	
	// Function to fetch models - json
	function model_list(brand_id, type)
	{
		if ($('#finder_fork').hasClass('finder_fork_123'))
		{
			$('#finder_fork').removeClass('finder_fork_open');
			$('#finder_filters').remove();
		}
		
		$.getJSON('/mounts/fetch_models/'+brand_id+'/'+type, function (j) {
			var options = '';
			for (var i = 0; i < j.length; i++)
			{
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			$('#m123_model').html(options);
			$('#m123_model option:first').attr('selected', 'selected');
		});
	}
	
	// function to handle placeholder text
	function msearch_placeh()
	{
		if ($('#msearch_code').val() == '')
		{
			$('#msearch_code').val($('#msearch_code').prev().text());
		}
	}
	
	// function to handle actions need when clicking on product code search area
	function msearch_click()
	{
		model_list('0', '0');
		$('input[name=m123_type]:first').attr('checked','checked');
		fetch_manus();
		$('#m123_manu option:first').attr('selected', 'selected');
		$('#finder_fork').removeClass('finder_fork_123');
		$('#finder_fork').addClass('finder_fork_search');
	}
	
	
	function m123_click()
	{
		$('#finder_fork').removeClass('finder_fork_open');
		$('#finder_filters').remove();
		$('#finder_fork').removeClass('finder_fork_search');
		$('#finder_fork').addClass('finder_fork_123');
		//$('#msearch_code').val('');
		msearch_placeh();
	}
	
	// Remove the filters from the mountfinder feature
	function remove_filters()
	{
		$('#finder_fork').removeClass('finder_fork_open');
		$('#finder_filters').remove();
	}
	
	//fade out any notices/warnings
	//$('.notice').fadeIn('slow').fadeTo(2000, 1).fadeOut('slow');
	$('.notice').fadeIn('slow').fadeTo(2000, 1).animate({
		top: '-=' + 25 + 'px',
		opacity: 0
	},500);
	
});


