$(document).ready(function(){
	try {
	$(".colorbox").colorbox({
		iframe:true,
		transition:'elastic',
		speed:500,
		width:465,
		height:410,
		opacity:0.33,
		overlayClose:false
	});
	brouilleURL.init();
	} catch(exception) {}
	transformTabs();
	showHideOptionals();
}); 

function selectOngletVendeur(index) {
	$('.aProposVendeur .onglets>div').each(function(i,e) {
		if ((i+1)==index) {
			if ($(e).hasClass('onglet_on')) return false;
			$(e).removeClass('onglet_off');
			$(e).addClass('onglet_on');
		} else {
			$(e).removeClass('onglet_on');
			$(e).addClass('onglet_off');
		}
	});
	
	$('.aProposVendeur .currentOnglet .loadingGif').width($('.aProposVendeur .currentOnglet').width());
	$('.aProposVendeur .currentOnglet .loadingGif').height($('.aProposVendeur .currentOnglet').height());
	$('.aProposVendeur .currentOnglet>div').hide();
	$('.aProposVendeur .currentOnglet .loadingGif').show();
	if ($('#ongletVendeur_'+index).length > 0) {
		$('#ongletVendeur_'+index).show();
		$('.aProposVendeur .currentOnglet .loadingGif').hide();
	} else {
		$('.aProposVendeur .currentOnglet').append('<div id="ongletVendeur_'+index+'"></div><div class="clear"></div>');
		$('#ongletVendeur_'+index).load($('.aProposVendeur>form input[name="template"]')[0].value+'&ongletVendeur='+index+'&rand='+Math.random(),null,function() {
			$('.aProposVendeur .currentOnglet .loadingGif').hide();
		});
	}
	return false;
}

function selectOngletFT(index) {
	$('.ongletsFT .onglets>div').each(function(i,e) {
		if ((i+1)==index) {
			if ($(e).hasClass('onglet_on')) return false;
			$(e).removeClass('onglet_off');
			$(e).addClass('onglet_on');
		} else {
			$(e).removeClass('onglet_on');
			$(e).addClass('onglet_off');
		}
	});
	
	$('.ongletsFT .currentOnglet .loadingGif').width($('.ongletsFT .currentOnglet').width());
	$('.ongletsFT .currentOnglet .loadingGif').height($('.ongletsFT .currentOnglet').height());
	$('.ongletsFT .currentOnglet>div').hide();
	$('.ongletsFT .currentOnglet .loadingGif').show();
	
	if ($('#ongletFT_'+index).length > 0) {
		$('#ongletFT_'+index).show();
		$('.ongletsFT .currentOnglet .loadingGif').hide();
		rechargePub(1,index);
	} else {
		$('.ongletsFT .currentOnglet').append('<div id="ongletFT_'+index+'"></div>');
		$('#ongletFT_'+index).load($('.ongletsFT>form input[name="template"]')[0].value+'&ongletFT='+index+'&rand='+Math.random(),null,function() {
			$('.ongletsFT .currentOnglet .loadingGif').hide();
			rechargePub(1,index);
		});
	}
	return false;
}

function rechargePub(type,index) {
	if (type==0) { // onglet FDA (1-9) ou onglet FT (11-16)
		switch (index) {
		case 1:
			PubLoader.recharge(null,true,"onglet_description");
			break;
		case 2:
			PubLoader.recharge(null,true,"onglet_ft");
			break;
		case 3:
			PubLoader.recharge(null,true,"onglet_actu");
			break;
		case 4:
			PubLoader.recharge(null,true,"onglet_assurance");
			break;
		case 5:
			PubLoader.recharge(null,true,"onglet_credit");
			break;
		case 6:
			PubLoader.recharge(null,true,"onglet_garantie");
			break;
		case 9:
			PubLoader.recharge(null,true,"onglet_transaction");
			break;
		default:
			PubLoader.recharge();
			break;
		}
	} else { // onglet FT dans la FDA
		PubLoader.recharge();
	}
}

function selectOngletFDA(index) {
	var p=index-1;
	if ( p > 13 ) {p-=11;} // onglet 15 supprime
	else if ( p > 9 ) {p-=10;} // FT
	else if ( p > 3 ) {p--;} // onglet 5 supprime
	// onglets
	$('.ongletsFDA .onglets>div').each(function(i,e) {
		if ( i == p ) {
			if ($(e).hasClass('onglet_off')) {
				$(e).removeClass('onglet_off');
				$(e).addClass('onglet_on');
			} else if ($(e).hasClass('ongletLong_off')) {
				$(e).removeClass('ongletLong_off');
				$(e).addClass('ongletLong_on');
			} else if ($(e).hasClass('ongletShort_off')) {
				$(e).removeClass('ongletShort_off');
				$(e).addClass('ongletShort_on');
			} else return false;
		} else {
			if ($(e).hasClass('onglet_on')) {
				$(e).removeClass('onglet_on');
				$(e).addClass('onglet_off');
			} else if ($(e).hasClass('ongletLong_on')) {
				$(e).removeClass('ongletLong_on');
				$(e).addClass('ongletLong_off');
			} else if ($(e).hasClass('ongletShort_on')) {
				$(e).removeClass('ongletShort_on');
				$(e).addClass('ongletShort_off');
			}
		}
	});
	
	// contenu onglet
	$('#currentOnglet .loadingGif').width($('#currentOnglet').width());
	$('#currentOnglet .loadingGif').height($('#currentOnglet').height());
	$('#currentOnglet>div').hide();
	$('#currentOnglet .loadingGif').show();
	if ($('#ongletFDA_'+index).length > 0) {
		$('#ongletFDA_'+index).show();
		$('#currentOnglet .loadingGif').hide();
		
		// re-afficher le formulaire de credit
		if (index==5) hideIframe();
		rechargePub(0,index);
	} else {
		$('#currentOnglet').append('<div id="ongletFDA_'+index+'"></div>');
		$('#ongletFDA_'+index).load($('.ongletsFDA>form input[name="template"]')[0].value+(index==1?'&credit=0':'')+'&ongletFDA='+index+'&rand='+Math.random()+(index>10?('&actionFT='+(index-10)):''),null,function() {
			$('#currentOnglet .loadingGif').hide();
			rechargePub(0,index);
			brouilleURL.init();
			transformTabs();
		});
	}
	
	// galerie
	$('#galerieFDA .loadingGif').width($('#galerieFDA').width());
	$('#galerieFDA .loadingGif').height($('#galerieFDA').height());
	$('#galerieFDA>div').hide();
	$('#galerieFDA .loadingGif').show();
	galerieType = 'galerie'+(((index==2) || (index>10))?'FT':'Normale');
	if ($('#galerieFDA .'+galerieType).length > 0) {
		$('#galerieFDA .'+galerieType).show();
		$('#galerieFDA .loadingGif').hide();
	} else {
		$('#galerieFDA').append('<div class="'+galerieType+'"></div>');
		$('#galerieFDA .'+galerieType).load($('.ongletsFDA>form input[name="template"]')[0].value+'&galerieType='+(((index==2) || (index>10))?'FT&actionFT=0':'Normale')+'&rand='+Math.random(),null,function() {
			$('#galerieFDA .loadingGif').hide();
		});
	}
	
	$('#ongletIndex').val(index);
	showHideOptionals();
	
	return false;
}

function showHideOptionals() {
	index = $('#ongletIndex').val();
	// elements optionels
	if ((index==4) || (index==5)) $('.optional').hide();
	else $('.optional').show();
	
	if ((index==1) || (index>10)) $('.liensUtiles').show();
	else $('.liensUtiles').hide();
}

function pagine(i) {
	if ($('#ongletFDA_3').length > 0) index = 3; else index = 14;
		
	$('#currentOnglet .loadingGif').width($('#currentOnglet').width());
	$('#currentOnglet .loadingGif').height($('#currentOnglet').height());
	$('#currentOnglet>div').hide();
	$('#currentOnglet .loadingGif').show();
	
	urlOnglet = $('.ongletsFDA>form input[name="template"]')[0].value
		+'&page='+i
		+'&ongletFDA='+index
		+'&rand='+Math.random();
	if (index == 14)
		urlOnglet += '&actionFT=4';
	
	$('#ongletFDA_'+index).load(
		urlOnglet,
		null,
		function() {
			$('#currentOnglet .loadingGif').hide();
			$('#ongletFDA_'+index).show();
		}
	);
}

function showGalerie(idAnn){
	$.fn.colorbox({
		href:"/occasion3/galeriePopup.cfm?idAnn="+idAnn,
		iframe:true,
		transition:'elastic',
		speed:500,
		width:770,
		height:785,
		opacity:0.33,
		overlayClose:true,
		open:true
	});
	return false;
}

function selectGaleriePhoto(elem,url) {
	$('#galerieFDA>div:visible').find('.photoSlider .photoContent img').removeClass('selected');
	$(elem).children('img').addClass('selected');
	$('#galerieFDA>div:visible').find('.photoMain>a>img').attr("src",url);
	$('#galerieFDA>div:visible').find('.photoMain>img').attr("src",url);
	elem.blur();
	PubLoader.recharge("",true,"onglet_zoom");
	return false;
}

var slideGalerieTimer;

function slideGalerie(direction) {
	$('#galerieFDA>div:visible').find('.photoSlider .photoContent').each(function(i,e) {
		$(e).scrollLeft($(e).scrollLeft()+(direction*3));
	});
	slideGalerieTimer = setTimeout('slideGalerie('+direction+');',10);
}

function startSlideGalerie(direction) {
	slideGalerieTimer = setTimeout('slideGalerie('+direction+');',10);
	return false;
}

function stopSlideGalerie() {
	clearTimeout(slideGalerieTimer);
	return false;
}

// formatte et renvoie un nombre decimal (ex: 1234567890.123456 ==> 1 234 567 890,12)
function format(i,j) {
	var n = (i+"").split('.');
	for(z=""+n[0],s="",x=z.length,y=0;x>0;s=(((++y%3==0)&&(x>1))?" ":"")+z.charAt(x---1)+s);
	return s+(n.length>1?(','+n[1].substr(0,Math.min(n[1].length,j))):'');
}
function convertFF(i) {
	return i*6.55957;
}

function checkPrixOptions() {
	total = $('#prixNeuf').val();	
	$('.ligneInfo :checkbox').each(function(i,e) {
		if ($(e).attr('checked')) total = 1*total + 1*$(e).val();
	});
	$('.ligneInfo.totalOptions .valeur').html(format(total) + '&nbsp;&euro;&nbsp;TTC ('+format(convertFF(total),2) + '&nbsp;F) ');
}
function checkLignesOptions() {
	$('.ligneInfo.ligneOption .valeur').each(function(i,e) {
		$(e).find(' :checkbox').each(function(ii,ee) {
			$(e).html($(e).html()+' ('+format(convertFF($(ee).val()),2)+'&nbsp;F)');
		});
	});
}

function cliqueEnvoiAmi() {
	PubLoader.track('Confirmation_envoi_ami');
}

function envoiAmi() {
	$('#envoiAmiForm .error').removeClass('error');
	var obj = document.getElementById('envoiAmiForm');
	if ((!obj.uEmail.value) || isBlank(obj.uEmail.value) || (!isMail(obj.uEmail.value))) {
		obj.uEmail.focus();
		$(obj.uEmail).addClass('error');
		$(obj.uEmail).find('~ span').addClass('error');
		return false;
	}
	if ((!obj.oEmail.value) || isBlank(obj.oEmail.value) || (!isMail(obj.oEmail.value))) {
		obj.oEmail.focus();
		$(obj.oEmail).addClass('error');
		$(obj.oEmail).find('~ span').addClass('error');
		return false;
	}
	if ((!obj.uCaptcha.value) || isBlank(obj.uCaptcha.value)) {
		obj.uCaptcha.focus();
		$(obj.uCaptcha).addClass('error');
		$(obj.uCaptcha).find('~ span').addClass('error');
		return false;
	}
	return false;
}

function envoiGarantie(contexte) {
	$('#garantieForm input[name="iContexte"]').val(contexte);
	$('#garantieForm').submit();
	return false;
}

function readyForPrint() {
	var total = 0;
	var offset = 0;
	$('.ligneInfo, .ligneInfoFull, .ligneInfoLast, .ligneInfoConso').each(function(i,e) {
		total = $(e).offset().top;
		if (total-offset>=820) {
			$(e).before('<br/><span class="pageBreak"/><br/>');
			offset = total;
			total = 0;
		}
	});
}

function transformTabs() {
	$('.onglet_off, .onglet_on, .ongletLong_off, .ongletLong_on, .ongletShort_off, .ongletShort_on').each(function(i,e) {
		$(this).css('cursor','pointer');
		$(this).click( function(event) {
			if (event.currentTarget.className.indexOf('_off')>0)
				$(this).find('a:first').click();
		});
		$(this).hover( function() { $(this).find('a').addClass('hover'); }, function() { $(this).find('a').removeClass('hover'); } );
	});
}
