$(function(){
	var resourcesUrl = $('#resources_url').val();
	
	// Frontpage content fade-in
	$('#frontpage-content').css({top: -30, opacity: 0}).animate({top: 0, opacity: 1}, 800, 'swing');
	
	
	// Photo Lightbox
	
	$('a.lightbox, #photogallery a').lightBox({
		imageLoading:	resourcesUrl + 'frontend/plugins/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
		imageBtnClose: 	resourcesUrl + 'frontend/plugins/jquery-lightbox-0.5/images/lightbox-btn-close.gif',
		imageBtnPrev:	resourcesUrl + 'frontend/plugins/jquery-lightbox-0.5/images/lightbox-btn-prev.gif',
		imageBtnNext:	resourcesUrl + 'frontend/plugins/jquery-lightbox-0.5/images/lightbox-btn-next.gif'
	}); 

	// Quotation race/type selectors
	
	function switchRaceSelector(race) {
			$('.race-selector').attr('name', 'inactive').hide();
			$('#race-selector-' + race).attr('name', 'item[race]').show();
		}

	function switchTypeSelector(race) {
			$('.type-selector').attr('name', 'inactive').hide();
			$('#type-selector-' + race).attr('name', 'item[type]').show();
		}

	switchRaceSelector($('#plant-selector').val());
	switchTypeSelector($('#plant-selector').val());

	$('#plant-selector').bind('change', function() {
			switchTypeSelector($(this).val());
			switchRaceSelector($(this).val());
		});

	
	// Submit buttons
	
	$('.submit').click(function(){
			$(this).parents('form').submit();
			return false;
		});
	
	$('#menu-primary > li').hoverIntent({    
		     over: 			function() {
		     					$('ul').show()
		     					$('ul li', this).hide().slideDown(300);
		     				},
		     out: 			function() {
		  						$('ul, ul li', this).hide();
			     			},
		     sensitivity:	20,
		     timeout: 		50
		});
		
	
	// Quotation edit amount button
	
	$('#quotable-overview .cell-amount input').focus(function() {
			$(this).parent().find('.button').css('visibility', 'visible');
		});	
	$('#quotable-overview .cell-amount .button').click(function() {
			var amount = $(this).parent().find('input').val();
			var url = $(this).attr('data-url') + amount;
			$(this).attr('href', url);
		});
	
	
	// Contactform business/individual switch
	
	var contactformTypeSwitch = (function(elem) {
			if ($(elem).val() == 'company') {
				$('#contact-form tr#company').show();
			}
			else {
				$('#contact-form tr#company').hide();
			}
		});
	$('#contact-form tr#type-select select').change(function(){contactformTypeSwitch(this)});
	contactformTypeSwitch('#contact-form tr#type-select select');
	
});

