jQuery(function() {

	// HIGHLIGHT
	$('.highlight').hover(
		function () { $(this).css('backgroundPosition','0 -' + $(this).height() + 'px'); },
		function () { $(this).css('backgroundPosition','0 0'); }
	);
		
	
	// HEADER NAVIGATION
	$('#menu-header-navigation .menu-item').hover(
		function () { if (!$(this).hasClass('current_page_item')) $(this).css('backgroundPosition','0 -' + $(this).height() + 'px'); },
		function () { if (!$(this).hasClass('current_page_item')) $(this).css('backgroundPosition','0 0'); }
	);
		
	$('#menu-header-navigation').find('.current_page_item ').prev().css('background-color', '#262626');
	$('#menu-header-navigation').css('width', (($('#menu-header-navigation > li').size() -1) * 83) +1 + 'px');

	$('.column_dashed').css('height', $('.column_dashed').parent().height() + 'px');
	
	
	// CONTACT FORM
	$('#contact_form_type').change(function () { set_contact_form(); });
	
	
	// FORM AUTO CLEAR
	$('#search').focus(function () { $(this).val(''); });
	$('#searchengine').focus(function () { $(this).val(''); });
	$('#publication').focus(function () { $(this).val(''); });
	$('#other').focus(function () { $(this).val(''); });
	
	// PRODUCT GALLERY
	$('#product_gallery .gallery_image').click(function () {
	
		$('#product_image').load(path + '/core/load_image.php?image_id=' + $(this).attr('id'));
	});
	
});

function sidebar_navigation (category) {

	$('.' + category + ' a').css('color','#1170ff');
}

function hr_fix () {

	$('.dashed').wrap('<div class="hr_dashed" />');
}

function set_contact_form () {

	$('#inquiry').load($('#path').val() + 'load_form.php?form_id=' + $('#contact_form_type').val());
}


function submit_contact () {
	
	$.ajax({
			
		type: 'POST',
		url: $('#contact_form').attr('action'),
		data: $('#contact_form').serialize(),	
		dataType: 'json',
		cache: false,
		success: function (msg) { 
		
			if (msg.response == 'success') {
			
				$('#contact_form').html('<h1>' + $('#response').val() + '</h1>');
			} else {
			
				if (msg.error.type == 'input') {
				
					for (var i = 0; i < msg.error.fields.length; i++) input_error('#' + msg.error.fields[i]);					
					$('#form_message').css('display','block');
				} else {
				
					alert('Form Submission Error');
				}
			}
		}
	});
}


function submit_msds () {
	
	$.ajax({
			
		type: 'POST',
		url: $('#msds_form').attr('action'),
		data: $('#msds_form').serialize(),	
		dataType: 'json',
		cache: false,
		success: function (msg) { 
		
			if (msg.response == 'success') {
			
				$('#msds_form').html('<h1>' + $('#response').val() + '</h1>');
			} else {
			
				if (msg.error.type == 'input') {
				
					for (var i = 0; i < msg.error.fields.length; i++) input_error('#' + msg.error.fields[i]);					
					$('#form_message').html('There were errors found with the form fields highlighted in red.').addClass('form_error');
				} else {
				
					alert('Form Submission Error');
				}
			}
		}
	});
}


function input_error (id) {

	$(id).val('').addClass('form_error');
	$(id).focus(function() { $(id).val('').removeClass('form_error'); });
	
	return true;
}

