$('document').ready(
	function() {
		
		$('.product-selector').change(
			function() {
				$('#instanceID').val($(this).val());
				this.form.submit();
			}
		);
		
		$('#content .section h2').click(
			function() {
				$('#content .section h2').removeClass('selected');
				$(this).addClass('selected');
				
				$('#content .section .tab').addClass('hidden');
				$('#'+$(this).attr('rel')).removeClass('hidden');
			}
		);
		
		$('#content .gallery a.thumb').click(
			function() {
				$(this).parents('.gallery').find('.gallery-pic:not(#'+$(this).attr('rel')+')').addClass('hidden');
				$('#'+$(this).attr('rel')).removeClass('hidden');
			}
		);
		
		$('.gallery-thumbs .left').click(
			function() {
				scroll(
					$(this).parents('.gallery-thumbs').find('div.slide'),
					$(this).parents('.gallery-thumbs').find('div.slide div'),
					1
				);
			}
		);
		
		$('.gallery-thumbs .right').click(
			function() {
				scroll(
					$(this).parents('.gallery-thumbs').find('div.slide'),
					$(this).parents('.gallery-thumbs').find('div.slide div'),
					-1
				);
			}
		);
		
		$('.map .city').click(
			function() {
				$('#content .contact').addClass('hidden');
				$('#content *[rel="'+$(this).attr('id')+'"]').removeClass('hidden');
			}
		);
	}
);

function scroll(mask, container, dir) {
	var maskWidth = mask.innerWidth();
	var contWidth = container.innerWidth();
	
	if (contWidth <= maskWidth) {
		return;
	}
	var step = dir * (container.find('a:eq(1)').offset().left - container.find('a:eq(0)').offset().left);
	
	var left = container.offset().left - mask.offset().left;
	var minLeft = (maskWidth - contWidth);

	if (left+step >= minLeft && left+step <= 0 ) {
		container.animate({'left': '+='+step+'px'}, 'fast');
	}
}

