function get_companies_by_country(cid,lang) {
	$.get(SITE_BASE + 'trimo.proxy.php', {CID:cid,site_lang:SITE_LANG}, function(data) {
		$('#companies').html(data);
		
		$('#country-selector option[value='+cid+']').attr('selected', 'selected');
		$('#country-box option[value='+cid+']').attr('selected', 'selected');
		
	}, 'html');
}

$(function () {
	
	/* image gallery */
	var current_gallery_index = 0;
	var all_images = $('#gallery .img').length;
	var main_image = $('#gallery>a img');
	var main_gallery = $('a.gallery-trigger');
	
	
	main_image.css({
		cursor: 'pointer'
	});
	
	$('#gallery .img[rel="0"]').addClass('selected');
	
	
	main_gallery.fancybox({
		hideOnContentClick: true,
		titlePosition: 'inside',
		overlayOpacity: '0.7',
		overlayColor: '#000'
	});
	
	function change_image(index) {
		index = Math.min(index, all_images-1);
		index = Math.max(0, index);
		// cycle:
		//if (index < 0) index = all_images-1;
		//if (index > all_images-1) index = 0;
		current_gallery_index = index;
		
		$('.gallery-trigger').addClass('hidden');
		$('.gallery-trigger:nth-child('+(current_gallery_index+1)+')').removeClass('hidden');
		$('#gallery .img').removeClass('selected');
		$('#gallery .img[rel="'+index+'"]').addClass('selected');
	}
	
	$('#gallery .img').click(function() {
		change_image(parseInt($(this).attr('rel')));
		return false;
	});
	
	$('#gallery #prev').click(function() {
		change_image(current_gallery_index-1);
		return false;
	});
	
	$('#gallery #next').click(function() {
		change_image(current_gallery_index+1);
		return false;
	});
	
	/* video documents */
	$(".panel.video a").fancybox({
		overlayOpacity: '0.7',
		overlayColor: '#000'
	});
	
	$('#box-dealer select').change(function() {
		if($('option:selected', this).val() != '')
			$(this).parent('form').submit();
	});
	
	/* entry page video */
	$("#video-trigger, .downloads-video").fancybox({
		overlayOpacity: '0.7',
		overlayColor: '#000'
	});
	
	/* colors overlay */
	$(".image a[rel]").fancybox({
		hideOnContentClick: true,
		titlePosition: 'inside',
		overlayOpacity: '0.7',
		overlayColor: '#000'
	});
	
	
	/* country selector on dealers finder */
	$('#country-selector').change(function() {
		var val = $(this).val();
		if (val)
			get_companies_by_country(val);
	});
	
	/* references detail gallery */
	$('#ref_gallery a').click(function() {
		$('#ref_gallery a').removeClass('current');
		$(this).addClass('current');
		index = parseInt($(this).attr('ref'));
		$('#ref_image img').hide();
		$('#ref_image img:nth-child(' + index + ')').fadeIn();
		return false;
	});
	
	$('.references .list li').hover(
		function(){$('span', this).fadeIn();}, 
		function() {$('span', this).fadeOut();}
	);
	
	/* downloads language switch */
	$('#catalogue-selector select').change(function() {
		$('#catalogue-selector').submit();
	});
	
});
