function appel_ajax(url_href, dest,append){
	var  reg=new  RegExp("^(.*)/(.*)#(.*)$", "g");
	var location = window.location
	var tab = reg.exec(location);
	url_href = url_href.replace('.html','');
	url_href = url_href.replace(tab[1],'');
	url_href = url_href.replace('/','');
	var url_data = 'pages/'+ url_href +'.html';
	var url_css = 'pages/css/'+ url_href +'.css';
	var url_js = 'pages/js/'+ url_href +'.js';
	var url_fond = tab[1] + '/pages/images/fond_'+ url_href + '.jpg';
	var scriptObj = document.createElement('link');
	
	if(css.join().indexOf(url_css) == -1){
		scriptObj.type = 'text/css';
		scriptObj.rel = 'stylesheet';
		scriptObj.href = url_css;
		document.getElementsByTagName("head")[0].appendChild(scriptObj);
		css.push(url_css);
		$("<img />").attr("src", url_fond).load()
	}else{
		$('head link').each(function(index){
			if($(this).attr('href').indexOf(url_css) != -1){
				$('head').append(this)
			}
		})
	}

	$.ajaxq ("vittelqueue", {
				url: url_data,
        		dataType: "html",
  				error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                },
				success: function(html){
					if(append==1){
						$(dest).append(html)
					}else{
						$(dest).after(html);
					}
  				}
			})
	$.ajaxq ("vittelqueue", {
		url: url_js,
		dataType: "script"
	})
	
}

function display_content(conteneur_obj,background_obj,insert_after,content_obj,fleche_menu_obj,ind_link){
    
		background_obj.insertAfter(insert_after);
		background_obj.parent().children('.fond_content').not(background_obj).insertAfter(insert_after);
		if(typeof(ind_page) == 'undefined'){
			move_page(0,content_obj)
		}
		
		background_obj.parent().children('.active').animate({
			left:750,
			opacity:0
		},450,function(){
			$(this).removeClass('active')
			$(this).css({
      			'left' : '-600px',
      			'opacity' : '1',
      			'z-index' : '6'
    		});
		})
		background_obj.fadeIn(500, function(){
			background_obj.parent().children('.'+background_obj.attr('class')).not(background_obj).css('display','none');
			$(insert_after).css('display','none')
			fleche_menu_obj.animate({
				left:125
			},500)
			content_obj.animate({
				left:150
			},500,function(){
				$(this).css('z-index',15)
				$(this).addClass('active');
                                for(var i= 0; i < entree_obj.length; i++)
                                {
                                    if(ind_link-1 != i){
                                        entree_obj[i]['obj'].bind('click', evt_click_smenu)
                                    }
                                }
                                menu_activ.bind('click',back_to_menu)
/*
				if(ind_link){
					if(ind_link == 1){
						entree_obj[1]['obj'].bind('click',evt_click_smenu);
						entree_obj[2]['obj'].bind('click',evt_click_smenu);
						menu_activ.bind('click',back_to_menu)

					}
					if(ind_link == 2){
						entree_obj[0]['obj'].bind('click',evt_click_smenu);
						entree_obj[2]['obj'].bind('click',evt_click_smenu);
						menu_activ.bind('click',back_to_menu)
					}
					if(ind_link == 3){
						entree_obj[0]['obj'].bind('click',evt_click_smenu);
						entree_obj[1]['obj'].bind('click',evt_click_smenu);
						menu_activ.bind('click',back_to_menu)
					}
				}else{
						entree_obj[0]['obj'].bind('click',evt_click_smenu);
						entree_obj[1]['obj'].bind('click',evt_click_smenu);
						entree_obj[2]['obj'].bind('click',evt_click_smenu);
				}
                        */
			})
		})
	if(state_submenu == 1){
		savoir_entree_obj.css('display','none');
		conteneur_obj.children('.fleches_entree').animate({
			left:-11
		},300)
	}else{
		savoir_entree_obj.css('display','none');
	}

}

function loadContent(hash) {
		if(hash != "") {
			var chemin = hash.replace('#','').split('/')
			var url = '';
			if(chemin.length == 1){
				delete	state_index;
				if(state[chemin[0]] != 1 ){
					url=hash+'.html';
					appel_ajax(url, '#body_2',1);
					state_index = 1
				}else{
					action_back_to_menu();
				}
			}
			if(chemin.length == 2){
				if(state[chemin[0]] != 1 ){
					url=chemin[0] +'.html';
					return_ajax = appel_ajax(url, '#body_2',1)
				}
				if(state[chemin[1]] != 1 ){
					state_submenu = 1;
					url=chemin[1]+'.html';
					appel_ajax(url, '#fond_'+chemin[0]);
				}else{
					history_content(tab_ss_menu[chemin[1]])
				}
			}	
			if(chemin.length == 3){
				ind_page = chemin[2]
				if(state[chemin[0]] != 1 ){
					url=chemin[0] +'.html';
					return_ajax = appel_ajax(url, '#body_2',1)
				}
				if(state[chemin[1]] != 1 ){
					url=chemin[1]+'.html';
					appel_ajax(url, '#fond_'+chemin[0]);
				}else{
					history_content(tab_ss_menu[chemin[1]])
					move_page(ind_page,content_obj[chemin[1]])

				}
				
			}
       }else{
       		set_flash()
       }
}

function supDefault(event){
	event.preventDefault();
}

function history_content(cible){
    entree_obj[cible-1]['obj'].trigger('click')
}

function move_menu(cible){
    for(var i= 0; i < pos.length; i++){
        pos[i].css('z-index',4+i).animate({
		width:125,
		height:125,
		top:750 - (pos.length -i) * 125,
		left:0
	}, vitesse_deplacement)
    }   
}
function evt_click_smenu(){
        for(var i= 0; i < entree_obj.length; i++)
        {
            entree_obj[i]['obj'].unbind('click', evt_click_smenu)
        }
	menu_activ.unbind('click',back_to_menu)
	
	str_replace = 'http://'+window.location.host+window.location.pathname
	str_replace = str_replace.replace('index.htm','')
	
	var menu = $(this).attr('href').replace(str_replace,'').replace('.html','')
	var tab_menu = menu.split('/').reverse();
	var ind_smenu = tab_menu[0];
	if(state[ind_smenu] == 0){
		$.history.load(menu)
	}else{
		display_content(conteneur_obj,fond_obj[ind_smenu],fond_conteneur_obj,content_obj[ind_smenu],$('#fleches_entree_'+tab_ss_menu[ind_smenu]),tab_ss_menu[ind_smenu])
		$.history.load(menu)
	}
        
        for(var i= 0; i < pos.length; i++){
            if(pos[i] == entree_obj[tab_ss_menu[ind_smenu]-1]['obj']){
                pos[i] = pos[0];
            }
        }
        pos[0] = entree_obj[tab_ss_menu[ind_smenu]-1]['obj'];
        move_menu(tab_ss_menu[ind_smenu])

	
	return false;
}

function back_to_menu(){
	url = $(this).attr('href').replace('.html', '');
	$.history.load(url)
}

function action_back_to_menu(){
    for(var i= 0; i < entree_obj.length; i++)
    {
        entree_obj[i]['obj'].unbind('click', evt_click_smenu);
    }
    
	fond_conteneur_obj.parent().children('.fond_content').insertBefore(fond_conteneur_obj);
	fond_conteneur_obj.fadeIn(850,function(){
		fond_conteneur_obj.parent().children('.fond_content').css('display','none')
		for(var i= 0; i < entree_obj.length; i++)
                {
                    entree_obj[i]['obj'].bind('click', evt_click_smenu)
                }
	})
	conteneur_obj.children('.fleches_entree').animate({
		left:-11
	},300)
	fond_conteneur_obj.parent().find('.content_content.active').animate({
			left:750,
			opacity:0
	},450,function(){
			$(this).css({
      			'left' : '-600px',
      			'opacity' : '1',
      			'z-index' : '6'
    		});
			$(this).removeClass('active')
	})
        /* TODO */
	for(var i= 0; i < entree_obj.length; i++)
        {
            entree_obj[i]['obj'].animate({
		width:250,
		height:250,
		top:entree_obj[i]['top'],
		left:entree_obj[i]['left']
            }, vitesse_deplacement, function(){
                savoir_entree_obj.fadeIn(300)
                state_submenu = 0;
                
            });
        }
}

function change_tittle(new_title){
	document.title = 'Vittel - ' + new_title;
}
function set_size_bodys(){
		// Calcul de la taille des deux body
	var width_body1 = ($(window).width() - 1000) * 0.2 + 250;
	var width_body2 = ($(window).width() - 1000) * 0.8 + 750;
	var height_body = $(window).height();
	if(height_body < 750){
		height_body = 750;
	}
        if(width_body1 + width_body2 > 1000){
                $('body').css('width','100%')
		.children('#body_1')
		.width(width_body1)
		.height(height_body)
		.next()
		.width(width_body2)
		.height(height_body)
		.css({
			'position':'absolute',
			'left':width_body1
		});
	}else{
		$('#body_2').css({
                    'height':height_body,
                    'position':'absolute',
                    'left':'250px',
                    'width':'750px'
                    
                    });
                $('body').width(1008);
	}
}

function pagination(ind){
	content_obj[ind].children('#content_txt').width(content_obj[ind].children('#content_txt').children().length * 560);
	
	content_obj[ind].children('#pagination').children('#precedent').click(function(){
		content_obj[ind].children('#pagination').children('#suivant').css('display','block');
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			content_obj[ind].children('#pagination').children('#suivant').css('display','block');
			if(content_obj[ind].children('#content_txt').css('left') == '-520px'){
				content_obj[ind].children('#pagination').children('#precedent').css('display','none');
			}
			content_obj[ind].children('#content_txt').animate({
				left:'+=560px'
			},500)
		}else{
		var number_page = $('#pagination img').index($(this).parent().children('.hover'))-1;
		if(number_page >= 0){
			if(number_page == 0){
				$(this).css('display','none');
			}
			var img_number_page = $(this).parent().children('img:eq('+number_page+')');
			content_obj[ind].children('#content_txt').animate({
				left:-(number_page * 560) + 40
			},500,function(){
				if ($.browser.msie && $.browser.version.substr(0,1)<7) {
					content_obj[ind].children('#pagination').children('img').each(function(index){
						if(index != number_page){
							$(this).removeClass('hover')
						}
					})
				}else{
					content_obj[ind].children('#pagination').children('img').each(function(index){
						if(index != number_page){
							$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt')+'A.png')
							$(this).removeClass('hover')
						}
					})
					img_number_page.attr('src','pages/images/chiffres_pages/'+img_number_page.attr('alt')+'C.png')
					img_number_page.addClass('hover')
				}
			})
		}
		}
	})
	
	content_obj[ind].children('#pagination').children('#suivant').click(function(){
		content_obj[ind].children('#pagination').children('#precedent').css('display','block');
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			content_obj[ind].children('#pagination').children('#precedent').css('display','block');
			if(content_obj[ind].children('#content_txt').css('left') == ((560*2+80)-content_obj[ind].children('#content_txt').width())+'px'){
				content_obj[ind].children('#pagination').children('#suivant').css('display','none');
			}
			content_obj[ind].children('#content_txt').animate({
				left:'-=560px'
			},500)
		}else{
			var number_page = $('#pagination img').index($(this).parent().children('.hover'))+1;
			var number_page_max = $('#pagination img').length;
			if(number_page <= number_page_max){
				if(number_page == number_page_max){
					$(this).css('display','none');
				}
				var img_number_page = $(this).parent().children('img:eq('+number_page+')');
				content_obj[ind].children('#content_txt').animate({
					left:-(number_page * 560) + 40
				},500,function(){
					content_obj[ind].children('#pagination').children('img').each(function(index){
						if(index != number_page){
							$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt') +'A.png')
							$(this).removeClass('hover')
						}
					})
					img_number_page.attr('src','pages/images/chiffres_pages/'+img_number_page.attr('alt') +'C.png')
					img_number_page.addClass('hover')
					
				})
			}
		}
	
	})
	
	content_obj[ind].children('#pagination').children('img').click(function(){
		var number_page = content_obj[ind].children('#pagination').children('img').index($(this));
		var img_number_page = $(this);
		var number_page_max = parseInt($(this).parent().children('img').length-1);
		
		if(number_page == number_page_max){
				content_obj[ind].children('#pagination').children('#suivant').css('display','none');
		}else{
				content_obj[ind].children('#pagination').children('#suivant').css('display','block');
		}
		if(number_page == 0){
				content_obj[ind].children('#pagination').children('#precedent').css('display','none');
		}else{
				content_obj[ind].children('#pagination').children('#precedent').css('display','block');
		}
		
		content_obj[ind].children('#content_txt').animate({
			left:-(number_page * 560) + 40
		},500,function(){
			content_obj[ind].children('#pagination').children('img').each(function(index){
				if(index != number_page){
					$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt') +'A.png')
					$(this).removeClass('hover')
				}
			})
			img_number_page.attr('src','pages/images/chiffres_pages/'+img_number_page.attr('alt') +'C.png')
			img_number_page.addClass('hover')
		})
	})
	
	content_obj[ind].children('#pagination').children('img').hover(function(){
		if(! $(this).hasClass('hover')){
			var number_page = content_obj[ind].children('#pagination').children('img').index($(this));
			$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt')+'B.png')
		}
	},function(){
		if(! $(this).hasClass('hover')){
			var number_page = content_obj[ind].children('#pagination').children('img').index($(this));
			$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt')+'A.png')
		}
	})
}

function move_page(ind_page_fn,content_obj){
	if(ind_page_fn > 0){
		content_obj.children('#pagination').children('#precedent').css('display','block');
	}else{
		content_obj.children('#pagination').children('#precedent').css('display','none');
		content_obj.children('#pagination').children('#suivant').css('display','block');
	}

	if(ind_page_fn == content_obj.children('#pagination').children('img').length-1){
		content_obj.children('#pagination').children('#precedent').css('display','block')
		content_obj.children('#pagination').children('#suivant').css('display','none')
	}
	
	content_obj.children('#content_txt').animate({
		'left':-(ind_page_fn * 560)+40+'px'
		},500);
	content_obj.children('#pagination').children('img').each(function(index){
		if(index != ind_page_fn){
			$(this).attr('src','pages/images/chiffres_pages/'+$(this).attr('alt')+'A.png')
			$(this).removeClass('hover')
		}
	})
	content_obj.children('#pagination').children('img').eq(ind_page_fn).attr('src','pages/images/chiffres_pages/'+content_obj.children('#pagination').children('img').eq(ind_page_fn).attr('alt')+'C.png').addClass('hover');
	delete ind_page;
}


function set_flash(){
/*
		flashCascade = $('#flashContent');
		flashCascade.flash(
			{
				swf: 'swf/fond_dvp_durable_v2.swf',
				width: 315,
				height: 730,
				wmode: 'transparent',
				flashvars: {
					vson: vson
				}
			}
		);*/
                

        /*        
		var flashContent = $('#flashContent');
		flashContent.flash(
			{
				swf: 'swf/anim_home_TDF.swf',
				width: 1500,
				height: 545,
				wmode: 'transparent'
			}
		)*/
		jQuery('#parallax').jparallax({
									yparallax:false,
									xtravel:1
								})
		var flashPromo = $('#carre_promotions');
		flashPromo.flash(
			{
				swf: 'swf/anim_promos_VITTEL_TDF.swf',
				width: 250,
				height: 250,
				wmode: 'transparent'
			}
		);

	}

