﻿var blank,
	rootPath,
	currentPage,
	relPath='',
	relPathArray=[],
	iframeWidth,
	iframeHeight,
	formData1,
	formData2,
	formData3,
	formData4,
	formData5,
	formData6;

function hideAndPositioning(){
	if(jQuery.browser.msie && jQuery.browser.version < 9) {
		blank = new Image();
		blank.src = '/static/images/blank.gif';
		function fixPng(png) {
		  var src = png.src;
		  if (!png.style.width) png.style.width = $(png).width();
		  if (!png.style.height) png.style.height = $(png).height();
		  png.onload = function() { };
		  png.src = blank.src;
		  png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		}
		$('#fond .visuelVehicHp, #fond .presentationVehicule h3 img').each(function(){
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
	
	if($('.financMain').length) $('#gmac').show();
	else  $('#gmac').hide();
	
	if($(".menuGamme").length) {
		
		if(!isMobile) $(".menuGamme").css({position:"absolute",top:"28px",left:"32px",padding:"0"});
		$(".menuGamme ul").hide();
		$(".menuGamme h2 img").hide();
		if($(".menuGamme ul").length) {
			if(!isMobile){
				$(".menuGamme h2 img").css({display:"inline"});
				$(".menuGamme").hover(function(){
					$(this).css({cursor:"pointer"}).find('ul').stop(false,true).slideDown();
				},function(){
					$(this).css({cursor:"default"}).find('ul').stop(false,true).slideUp();
				});
			} else {
				$(".menuGamme ul").show();
				$(".menuGamme h2 img").show().css({float:'left',marginTop:'10px'}).insertAfter($(".menuGamme h2"));
			}
		}
		
		
		if(!isMobile){
			$(".avantages").css({paddingTop:"60px"});
			
			/*$(".financMain").hover(function(){
				$(".financRoll").fadeIn();
			},function(){
				$(".financRoll").fadeOut();
			});*/
			
			$("#fond .equipements").css({position:"absolute"});
			$("#fond .equipements li").css({position:"absolute",width:"55px",height:"55px"});
			$("#fond .equipements li .equiptsDetails").hide();
		
			$("#fond .equipements li")
			.unbind()
			.hover(function(){
					if(!$(this).hasClass("active")) $(this).addClass("spotOver");
					$(this).css({cursor:"pointer"});
				},function(){
					if(!$(this).hasClass("active")) $(this).removeClass("spotOver");
					$(this).css({cursor:"default"});
			})
			.click(function(){
				cacherEquipements(this);
			});
		}/* else {
			$(".financMain").click(function(){
				if($(".financRoll").is(':hidden')) $(".financRoll").stop(false,true).fadeIn();
			else
				$(".financRoll").stop(false,true).fadeOut();
			});
		}*/
		/*$(".financRoll").hide();*/
	
		if(!isMobile) {
			iframeWidth = "980px";
			iframeHeight = "540px";
		} else {
			iframeWidth = "322px";
			iframeHeight = "1800px";
		}
		$('.demandeEssai a').colorbox({iframe:true, fastIframe:false, opacity:0.8, width:iframeWidth, height:iframeHeight, href: function(){
									var encodedValues = makeFormDatas();
									var url = "https://webservices.yourprod.net/essai-promo/promo/";
									return url+'?'+encodedValues;
								}
							});
		$('#fond .voirMentions a').colorbox({inline:true,opacity:0.8,scrolling:false, width:iframeWidth});
	
	}
	
}
	
function cacherEquipements(obj){
	if(obj!=undefined) {
		$("#fond .equipements li").each(function(i){
			if(i != $(obj).index()) $(this).removeClass("active");
		});
		if($(obj).hasClass("active")) {
			$(obj).removeClass("active");
			$("#fond .conteneurDetails").fadeOut(function(){
				$(this).empty();
			});
		} else {
			$(obj).addClass("active");
			if($("#fond .conteneurDetails").is(":visible")) {
				$("#fond .conteneurDetails").fadeOut(function(){
					$(this).empty();
					montrerEquipement(obj);
				});
			} else {
				montrerEquipement(obj);
			}
		}
	} else {
		$("#fond .equipements li").each(function(i){
			$(this).removeClass("active");
		});
		$("#fond .conteneurDetails").fadeOut(function(){
			$(this).empty();
		});
	}
};
	
function montrerEquipement(obj){
	$(obj)
	.addClass("active").removeClass("spotOver")
	.find(".equiptsDetails").clone().appendTo("#fond .conteneurDetails");
	$("#fond .conteneurDetails .equiptsDetails").show();
	$("#fond .conteneurDetails").fadeIn();
};

function grammarArticle(text) {
	var reg=new RegExp("^opel","gi");
	if(reg.test(text)) text = text.replace(reg,'');
	return text;
}

function makeFormDatas() {
	var $intro = $('<div />');
	var textModel = $('#fond .menuGamme h2').text();
	
	$intro.append('<h3 style="width:700px;">'+textModel+'</h3>');
	if($('#fond .offrePrix').html()) $intro.find('h3').append(' <span>'+$('#fond .offrePrix').html()+'</span>');
	$intro.find('sup').remove();
	$intro.find('small').remove();
	$intro.append($('#fond .avantages .autreAvantageClient:first').clone());
	$intro.append($('#fond .avantages .avantageClient:first').clone());
	
	formData1 = $intro.html();
	formData2 = grammarArticle(textModel);
	formData3 = $('#fond #linkConfig a').attr('href');
	formData4 = $('#fond #linkBrochure a').attr('href');
	formData5 = $('.content').attr('rel');
	formData6 = $('#fond #linkGamme a').attr('href');

	var formData = {
		"promo_header":escape(formData1),
		"model_text":formData2,
		"link_config":formData3,
		"link_brochure":formData4,
		"promo_moi":formData5,
		"link_gamme":formData6
	}

	return $.param(formData, true);

}
	

$(document).ready(function() {
	
	if($('body').hasClass('home'))  {
		currentPage = 'homeParticuliers';
		rootPath = window.location.href;
	}
	
	if($('body').hasClass('home') && $('body').hasClass('entreprise')) currentPage = 'homeEntreprise';
	
	if($('body').hasClass('page')) currentPage = 'page';
	
	
	if(isMobile) {
		
		var linkcss = document.createElement('link');
		linkcss.setAttribute('rel','stylesheet');
		linkcss.setAttribute('type','text/css');
		linkcss.setAttribute('href','/static/css/layout-mobile.css');
		document.getElementsByTagName('head')[0].appendChild(linkcss);
		
		var addLib = document.createElement('script');
		addLib.setAttribute('language','javascript');
		addLib.setAttribute('type','text/javascript');
		addLib.setAttribute('src','/static/js/jquery.scrollTo-1.4.2-min.js');
		document.getElementsByTagName('head')[0].appendChild(addLib);
		
		$('.menuGamme h2 img').attr('src','/static/images/fleche-menu-mobile.png');
		
		$('#mentionsLegales .close-popin').live('click', function(e) {
			$.colorbox.close();
		});
	}
	
	$('.aside a').live('click', function(e) {
	    window.open($(this).attr('href'));
	   	e.preventDefault();
	});
	
	$('footer a').live('click', function(e) {
		if($(this).parent().attr('id') != 'retourAccueil') {
	    window.open($(this).attr('href'));
	   	e.preventDefault();
		}
	});
	
	
		
		hideAndPositioning();
		
		if(window.location.hash == "#essai" && currentPage == 'page') {
			$.colorbox({iframe:true, fastIframe:false, opacity:0.8, width:iframeWidth, height:iframeHeight, href: function(){
									var encodedValues = makeFormDatas();
									var url = "https://webservices.yourprod.net/essai-promo/promo/";
									return url+'?'+encodedValues;
								}
			});
		}
	
});


