function	initComparatorField(make, showcase)
{
	$('.to_comparator').click(function() {
		return_value = getAjax('testComparator', 'id=' + $(this).attr('id') + '&make=' + make + '&showcase=' + showcase);
		if (return_value.indexOf('add') != -1) {
			$(this).addClass('checkbox_b_check');
			$('#comparator_zone').removeClass('small');
			$('.up').hide();
			$('.down').show();
			$('#for_blocs').append('<div id="' + $(this).attr('id') + '_bloc"></div>');
			refreshAjax('affComparator', 'id=' + $(this).attr('id') + '&make=' + make + '&showcase=' + showcase, $(this).attr('id') + '_bloc');
			refreshAjax('countComparator', 'make=' + make + '&id=' + $(this).attr('id') + '&showcase=' + showcase, 'nb_comparatif');
		}
		else if (return_value.indexOf('delete') != -1) {
			$(this).removeClass('checkbox_b_check');		
			$('#' + $(this).attr('id') + '_bloc').remove();
			refreshAjax('countComparator', 'make=' + make + '&id=' + $(this).attr('id') + '&showcase=' + showcase, 'nb_comparatif');
			if ($('#for_blocs div').length == 0) {
				$('#comparator_zone').addClass('small');
				$('.up').show();
				$('.down').hide();		
			}
		}
		else {
			alert('Vous ne pouvez comparer que 4 vehicules maximum');
		}
	});
	$('.magnify').click(function() {
		$(this).removeClass('init');
		$('#comparator_zone').toggleClass('small');
		$('.magnify').show();
		$(this).hide();
	});
}


function	removeBloc(el, make, showcase)
{
	var id = $(el).attr('id').split('_');
	$('#' + id[1] + '_' + id[2] + '_bloc').remove();
	$('#' + id[1] + '_' + id[2]).removeClass('checkbox_b_check');
	getAjax('testComparator', 'id=' + id[1] + '_' + id[2] + '&make=' + make + '&showcase=' + showcase);
	refreshAjax('countComparator', 'showcase=' + showcase + '&make=' + make + '&id=' + id[1] + '_' + id[2], 'nb_comparatif');
	if ($('#for_blocs div').length == 0) {
		$('#comparator_zone').addClass('small');
		$('.up').show();
		$('.down').hide();		
	}
}

function	initFilterS()
{
	$('#manual, #auto').click(function() {
		$(this).toggleClass('checkbox_check');
		var val = '';
		if ($('#manual').hasClass('checkbox_check'))
			val = 'manual';
		if ($('#auto').hasClass('checkbox_check'))
			val = (val == 'manual' ? 'all' : 'auto');
		$('#transmission').val(val);
	});
	
	$('#petrol, #diesel').click(function() {
		$(this).toggleClass('checkbox_check');
		var val = '';
		if ($('#petrol').hasClass('checkbox_check'))
			val = 'petrol';
		if ($('#diesel').hasClass('checkbox_check'))
			val = (val == 'petrol' ? 'all' : 'diesel');
		$('#carburant').val(val);
	});	
	
	$('#d3, #d5').click(function() {
		$(this).toggleClass('checkbox_check');
		var val = '';
		if ($('#d3').hasClass('checkbox_check'))
			val = '3';
		if ($('#d5').hasClass('checkbox_check'))
			val = (val == '3' ? 'all' : '5');
		$('#doors').val(val);
	});		
	$('.preview_color').click(function() {
		$(this).toggleClass('preview_color_unselect');
		if ($(this).hasClass('preview_color_unselect'))
			$('#color_' + $(this).attr('id')).val('');
		else
			$('#color_' + $(this).attr('id')).val('1');
	});
	
	$('#version').keyup(function() {
		completion();
	});
	$('#version').focus(function() {
		if ($('#version').val() != '')
			completionAjax();
	});
}

function completion()
{
	if ($('#version').data('waiting') == true)
		return ;
	$('#version').data('waiting', true);
	setTimeout("completionAjax()", 700);
}

function completionAjax()
{
	$('#version').data('waiting', false);
	if ($('#version').val() == '')
		$('#version_zone').hide();
	else {
		$('#version_zone').show();
		refreshAjax('completionVersion', 'value=' + escape($('#version').val()) + '&carmodel=' + $('#carmodel_id').val() + '&make_id=' + $('#make_id').val() + '&type=' + $('#type').val(), 'version_zone');
	}
}

function offerSlider(sens)
{
	var position = $('#flip').data('position');
	position += ((sens == 'left') ? -1 : 1);
	if (position < 0)
		position = 0;
	if (position > 4)
		position = 4;
	$('#flip').data('position', position);
	jQuery( '#flip' ).jcoverflip( 'current', position);

}

function initSlider()
{
	var size = 170;
	var sizeh = 242;
	var sizedata = 36;
	var reduce_by = 35;
	jQuery( '#flip' ).jcoverflip({
		current:2,
		time:400,
		beforeCss: function( el, container, offset ){
			width = (size - ((offset + 1) * reduce_by));
			height = parseInt(100 - (width * 100 / el.find( 'div' ).width()));
			height = el.find( 'div' ).height() - parseInt(height * el.find( 'div' ).height() / 100);
			el.find( 'div' ).css('z-index', 9 - (offset + 1));
			el.find('div.data_content').css('height', parseInt((height - 16) * 32 / 100) + 'px');		
			el.find( 'div.pix_offer p, div.pix_offer span').css('font-size', (9 - offset) + 'px');	
			el.find( 'div.pix_offer p').css({'line-height' : (1.6 - ((offset + 1) * 0.2)) + 'em', 'height' : (1.6 - ((offset + 1) * 0.2)) + 'em'});		
			el.find('.hide_b_all').hide();
			if (offset == 1)
				el.find('.hide_b_last').hide();			
			else
				el.find('.hide_b_last').show();						
			return [
				$.jcoverflip.animationElement( el, { left: (container.width( ) / 2 - (size / 2) - ((offset + 1) * size) + (offset * reduce_by) + ((offset + 1) * reduce_by) - 10)+'px', bottom: 0 }, { } ),
				$.jcoverflip.animationElement( el.find( 'div.pix_offer' ), { opacity: 1, height: (height) + 'px', width: (width) + 'px', bottom:((offset + 1) * 22) + 'px' }, {} )
			];
		},
		afterCss: function( el, container, offset ){
			width = (size - ((offset + 1) * reduce_by));
			height = parseInt(100 - (width * 100 / el.find( 'div' ).width()));
			height = el.find( 'div' ).height() - parseInt(height * el.find( 'div' ).height() / 100);
			el.find('div').css('z-index', 9 - (offset + 1));
			el.find('div.data_content').css('height', parseInt((height - 16) * 34 / 100) + 'px');		
			el.find( 'div.pix_offer p, div.pix_offer span').css('font-size', (9 - offset) + 'px');	
			el.find( 'div.pix_offer p').css({'line-height' : (1.6 - ((offset + 1) * 0.2)) + 'em', 'height' : (1.6 - ((offset + 1) * 0.2)) + 'em'});				
			el.find('.hide_b_all').hide();
			if (offset == 1)
				el.find('.hide_b_last').hide();			
			else
				el.find('.hide_b_last').show();					
			return [
				$.jcoverflip.animationElement( el, { left: ((container.width() / 2) - (size / 2) + ((offset + 1) * size) - (offset * reduce_by) + 10 ) + 'px', bottom: 0 }, { } ),
				$.jcoverflip.animationElement( el.find( 'div.pix_offer' ), { opacity: 1, width: (width) + 'px', height: (height) + 'px', bottom:((offset + 1) * 22) + 'px'}, {} )
			];
		},
		currentCss: function( el, container ){
			el.find('div').css('z-index', 9);
			el.find('div.data_content').css('height', sizedata + '%');
			el.find( 'div.pix_offer span').css('font-size', '13px');
			el.find( 'div.pix_offer p').css({'font-size': '11px', 'line-height': '1.6em', 'height': '1.6em'});				
			el.find('.hide_b_all').show();			
			return [
				$.jcoverflip.animationElement( el, { left: (container.width() / 2 - (size / 2)) + 'px', bottom: 0 }, { } ),
				$.jcoverflip.animationElement( el.find( 'div.pix_offer' ), { opacity: 1, width: (size) + 'px', height:(sizeh) + 'px', bottom:0}, { } )
			];
		},

		change: function(event, ui){
			$('#flip').data('position', ui.to);
			jQuery('#scrollbar').slider('value', ui.to*25);
		}	
	});
	$('#flip').data('position', 2);
	jQuery('#scrollbar').slider({
		value: 50,
		stop: function(event, ui) {
			if (event.originalEvent) {
				var newVal = Math.round(ui.value/25);
				jQuery( '#flip' ).jcoverflip( 'current', newVal );
				jQuery('#scrollbar').slider('value', newVal*25);
			}
		}
	});
}

function initBudget(el, min, max)
{
	var i = 0;
	$(el).slider({
		range: true,
		min:min,
		max:max,
		step:5000,
		values: [$('#budget_min').val(), $('#budget_max').val()],
		slide: function( event, ui ) {
			$('#' + $(el).attr('id') + ' .low').html((ui.values[0] == max ? '+' : '') + ui.values[0]); $('#budget_min').val(ui.values[0]);
			$('#' + $(el).attr('id') + ' .high').html((ui.values[1] == max ? '+' : '') + ui.values[1]); $('#budget_max').val(ui.values[1]);			
		}
	});
	$(el).find('.ui-slider-handle').each(function() {
		if (i == 0) {
			$(this).css({'margin-top': '15px'});
			$(this).html('<span class="low sup-handle">' + $('#budget_min').val() + '</span>');
			i = 1;
		}
		else {
			$(this).css('margin-top', '-15px');		
			$(this).html('<span class="high sup-handle">' + ($('#budget_max').val() == 50000 ? '+' : '') + $('#budget_max').val() + '</span>');			
		}
	});
}

function initBudgetLoa(el, min, max)
{
	var i = 0;
	$(el).slider({
		range: true,
		min:min,
		max:max,
		step:100,
		values: [$('#budget_loa_min').val(), $('#budget_loa_max').val()],
		slide: function( event, ui ) {
			$('#' + $(el).attr('id') + ' .low').html((ui.values[0] == max ? '+' : '') + ui.values[0]); $('#budget_loa_min').val(ui.values[0]);
			$('#' + $(el).attr('id') + ' .high').html((ui.values[1] == max ? '+' : '') + ui.values[1]); $('#budget_loa_max').val(ui.values[1]);			
		}
	});
	$(el).find('.ui-slider-handle').each(function() {
		if (i == 0) {
			$(this).css({'margin-top': '15px'});
			$(this).html('<span class="low sup-handle">' + $('#budget_loa_min').val() + '</span>');
			i = 1;
		}
		else {
			$(this).css('margin-top', '-15px');		
			$(this).html('<span class="high sup-handle">' + ($('#budget_loa_max').val() == 1500 ? '+' : '') + $('#budget_loa_max').val() + '</span>');			
		}
	});
}

function changeMiniature(el)
{
	var id = $(el).attr('id');
	var splits = id.split('_');
	$('.photo_' + splits[1]).hide(); 
	$('#' + id + '_big').show();
	$('.mini_' + splits[1]).removeClass('with_border');		
	$(el).addClass('with_border');
}

function initQtip(type, width)
{
	width = (width == undefined ? 350 : width);
	$('.' + type).each(function() {
		$(this).qtip({
			position : {
				corner : {
					target : 'topRight',
					tooltip : 'bottomLeft'
				}
			},
			adjust: {screen: true },			
			style : {
				color : '#C0C0C0',
				width:width
			},	
			content: {
				url : '/ajax.php?page=affTip',			
				data : {type: type, id: $(this).attr('id')},
				method : 'post'
			},
			tip : 'bottomLeft'
		});
	});
}

function initFormS()
{
	$('.type .radiobox').click(function() {
		$('.type .radiobox').removeClass('radiobox_check');	
		$(this).addClass('radiobox_check');
		$('#type').val($(this).attr('id'));
		$('.type .state img').attr('src', '/pix/search-ok.png');
	});
	$('#make_s').click(function() {
		$('#d_make_s').toggle();
		$('#d_model_s').hide();		
	});
	$('#d_make_s div').click(function() {
		$('#carmodel_id').val('');
		$('#d_make_s').hide();
		var value = $(this).attr('id').split('_');
		$('#make_id').val(value[1]);
		$('#make_s').html($(this).html());
		$('.make .state img').attr('src', (value[1] != '' ? '/pix/search-ok.png' : '/pix/search-grey.png'));
		$('.model').hide();
		$('.make_' + value[1]).show();
		$('#model_s').html('Choisir un mod&egrave;le');
		$('#version').val('');
		affBudgetType();
	});
	$('#model_s').click(function() {
		$('#d_make_s').hide();
		$('#d_model_s').toggle();
	});	
	$('#d_model_s div').click(function() {
		if ($(this).html() == 'Choisir d\'abord une marque')
			$('#model_s').html('Choisir un mod&egrave;le');
		else
			$('#model_s').html($(this).html());		
		$('#d_model_s').hide();
		var value = $(this).attr('id').split('_');		
		$('#carmodel_id').val(value[1]);
		$('#version').val('');		
		affBudgetType();
	});
}

function	affBudgetType()
{
	if ($('#make_id').val() == 1 || $('#make_id').val() == 2) {
		$('.choice_budget').show();
	}
	else {
		$('.budget_loa').hide();
		$('.budget_global').show();
		$('#b_global').addClass('radiobox_check');
		$('#b_loa').removeClass('radiobox_check');		
		$('.choice_budget').hide();	
	}
}

function	checkSearch()
{
	var error = 0;
	if ($('#type').val() == '') {
		$('.type .state img').attr('src', '/pix/search-ko.png');
		error += 1;
	}
	else
		$('.type .state img').attr('src', '/pix/search-ok.png');	
		
	if ($('#make_id').val() == '') {
		$('.make .state img').attr('src', '/pix/search-ko.png');
		error += 1;
	}
	else
		$('.make .state img').attr('src', '/pix/search-ok.png');		
	return (error == 0 ? true : false);
}	


