
Cufon.replace(".tmen a, .ptop span, .cart .left .size1, .pack .right i, .prix2l, .prix1r i", {
  fontFamily: "pi"
});
Cufon.replace(".xcnt .size1, .xcnt .size2, .ltop span, .cart .left .size2, .cart .left .size3, .pack .size1, .pack .size2", {
  fontFamily: "pn"
});
Cufon.replace(".prix2r", {
  fontFamily: "pbi"
});
Cufon.replace(".btm span, .btm a, .btm2 span, .btm2 a", {
  fontFamily: "pl",
	hover: true
});

var _g_gcnt_h = null;

$(function(){
	_g_gcnt_h = $(".gcnt").height();
	$(window).trigger("resize");
	initCgPop();
	Cufon.now();
});


function isCanvasSupported()
{
  var elem = document.createElement('canvas');
  return (elem && elem.getContext && elem.getContext('2d'));
}

$(document).ready(function() {

	if (!isCanvasSupported())
	{
		$("#bg_anim").hide();
		return;
	}
	
	var canvas = $('#bgc');
	var ctx = canvas[0].getContext('2d');
	var props = {xpos: 0, ref_pos:0};
	var win_width = $(document).width();
	var win_height = $(document).height();
	

	var _w = $(document).width();
	var lps = -40;
	
	canvas[0].width = 1280;
	canvas[0].height = 650;
	
	
	$(window).bind("resize", function() {
		win_width = $(document).width();
		rsz();
	});
	
	$(window).bind("load", function() {
		rsz();
	});
	
	
	function rsz() {
		//console.log(win_width + " / " + $(document).height());
		var w = win_width;
		var h = win_width * 650/1280;
		win_height = $(document).height();
		_w = w;
		lps = -40;
		
		if(h < win_height-83) {
			h = win_height-83;
			w = (win_height-83) * 1280/650;	
			_w = w;
			lps = (w - win_width)/2 - 40;			
		}
		
		var triangle_left = (win_width - 975)/2 + $('.ptop').position().left + 354/2 + 185;
		
		props.ref_pos = triangle_left * 1280/w;					
		canvas.css({'width':w, 'height':h});
		$('#bg_anim').css({'height':$(".gcnt").height()});
		update();
	}
	
	
	var tl = new Image()
	tl.src = "uploads/images/site/bg_test_tl.jpg";
	tl.onload = function() {
		ctx.drawImage(tl, -200, 0);
		br.src = "uploads/images/site/bg_test_br.jpg";
	}
	
	
	var br = new Image();
	br.onload = function() {
		ctx.save();						
		rsz();				
	}
	
	
	function update() {					
		ctx.drawImage(tl, (1280/_w-1)*177 - 30 - lps - 200, 0);					
		ctx.save();
		mask();
		ctx.clip();
		ctx.drawImage(br, (1280/_w-1)*177 - 30 - lps, 0);	
		ctx.restore();
	}
	
	function mask() {										
		ctx.beginPath();
		ctx.moveTo(props.ref_pos - props.xpos, 0);
		ctx.lineTo(1280, 0);
		ctx.lineTo(1280, 650);
		ctx.lineTo(props.ref_pos-649-props.xpos, 650);
		ctx.lineTo(props.ref_pos-props.xpos, 0);
		ctx.closePath();
	}
	
	
	TWEEN.start();				  
	
	$('#v_particuliers').bind("mouseover", function(){
		rsz();
	    var tween = new TWEEN.Tween(props).to({xpos: -192*1280/win_width}, 500).easing( TWEEN.Easing.Circular.EaseOut ).onUpdate(update).start();
	});
	
	$('#v_utilitaires').bind("mouseover", function(){
		rsz();
		var tween = new TWEEN.Tween(props).to({xpos: 192*1280/win_width}, 500).easing( TWEEN.Easing.Circular.EaseOut ).onUpdate(update).start();
	});
	
	$('#v_particuliers, #v_utilitaires').bind("mouseout", function(){
		var tween = new TWEEN.Tween(props).to({xpos: 0}, 500).easing( TWEEN.Easing.Back.EaseOut ).onUpdate(update).start();
	});
	
});


// bg

var _g_resize_bg = null;

function initBg (timed)
{
	
	if (isCanvasSupported())
	{
		return;
	}	
	
	clearTimeout(_g_resize_bg);
	var min = 975;
	var iw = 1280;
	var ih = 650;
	var pt = 855;
	var w = Math.max($(window).width(), min);
	var h = Math.max($(window).height(), Math.min($(document).height(), _g_gcnt_h + 20));
	$(".bg-img").css("width", w + "px").css("height", $(document).height() + "px");
	var nw = w;
	var nh = ih * (nw / iw);
	var byw = true;
	if (nh < h)
	{
		byw = false;
		nh = h;
		nw = iw * (nh / ih);
	}
	if ((nw < min) || (nh < ih))
	{
		nw = iw;
		nh = ih;
	}
	var tp = (w - min) / 2 + $(".ptop").position().left + $(".ptop").width();
	var ip = nw * pt / iw;
	var left = tp - ip;
	if ((left < 0) && (left + nw < w))
	{
		if (byw)
		{
			nw = w + Math.abs(left) * 3;
			nh = ih * (nw / iw);
		}
		else
		{
			nh = h + Math.abs(left) * (nh / nw) * 3;
			nw = iw * (nh / ih);		
		}
		ip = nw * pt / iw;
		left = tp - ip;
	}
  $(".bg-img img").css("width", nw + "px").css("height", nh + "px").css("left", (left - 1) + "px");
	if (!timed)
	{
		_g_resize_bg = setTimeout("initBg(true)", 500);
	}
}

// min height

$(window).resize(function(){
	if (_g_gcnt_h <	$(window).height())
	{
		$(".gcnt").css("height", Math.max($(window).height(), 600) + "px");
	}
	initBg();
});

// condition gen. popup

function initCgPop ()
{
	$("#cgl, #cgl *").click(function(){
		var c = $(".cnt:last");
		if (c.find(".cg").length == 0)
		{
			$.get("?page=conditions-"+LANGUAGE, null, function(data){
				c.append(data);
				$(".cg-s").jScrollPane({
				 showArrows: true,
				 verticalDragMinHeight: 18,
				 verticalDragMaxHeight: 18
				});			
				Cufon.replace(".cg h1, .cg h2", {
					fontFamily: "pi"
				});			
			}, "html");
		}
		$(".cg").show();
		$(window).scrollTop(0);
		return false;
	});
}

