/* a configurer pour les diaporamas */
var imgp = '/vincipark.nsf/';
var langue = trouveLangue ();

var D1_IMAGES = [
	{'src':imgp+'PaybyPhone.jpg','link':imgp+langue+'/les_services/pages/paiement_par_telephone_portable.htm','width':70,'height':70},
	{'src':imgp+'Service-2-roues.jpg','link':imgp+langue+'/les_services/pages/stationnement_motos.htm','width':70,'height':70},
	{'src':imgp+'Service-Telepeage.jpg','link':imgp+langue+'/les_services/pages/le_telepeage.htm','width':70,'height':70},
	{'src':imgp+'Service-Total-GR.jpg','link':imgp+langue+'/les_services/pages/total_gr.htm','width':70,'height':70},
	{'src':imgp+'Service-PMR.jpg','link':imgp+langue+'/les_services/pages/acces_aux_personnes_a_mobilite_reduite.htm','width':70,'height':70}
];	
var D2_IMAGES = [
			{'src':imgp+'station-de-gonflage.jpg','link':imgp+langue+'/les_services/pages/kit_de_depannage.htm','width':70,'height':70},
			{'src':imgp+'velo2.jpg','link':imgp+langue+'/les_services/pages/pret_de_velos.htm','width':70,'height':70},
			{'src':imgp+'handicap2.jpg','link':imgp+langue+'/les_services/pages/acces_aux_personnes_a_mobilite_reduite.htm','width':70,'height':70}
		];	
/* - */

function trouveLangue ()
{
 var theurl = window.document.URL;
 var regfr=new RegExp("(/fr/)", "gi");
 var regen=new RegExp("(/en/)", "gi");
 var regde=new RegExp("(/de/)", "gi");
 var reges=new RegExp("(/es/)", "gi");
 var langueurl = "fr";
 
 if (theurl.match(regfr))
 	langueurl = "fr";
 else if(theurl.match(regen))
  	langueurl = "en";
 else if(theurl.match(regde)) 
	langueurl = "de";
 else if(theurl.match(reges))
	langueurl = "es";
	
 return langueurl;
}

function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	
	$(".accordion-1").each(function() {
		accordion($(this).find('.title1 a'),$(this).find('ul'));
	});
	
	$(".accordion-2").each(function() {
		accordion($(this).find('.title2 a'),$(this).find('ul'));
	});
	
	$(".accordion-3").each(function() {
		accordion($(this).find('.title3 a'),$(this).find('ul'));
	});
	
	$(".discover").each(function() {
		accordion($(this).find('.title a'),$(this).find('ul'));
	});
	
	diaporamas();
}

function diaporamas() {
	$('#twocolumns .aside .carousel .mask').each(function() {
		var $bt_prev = $(this).prev('.prev');
		var $bt_next = $(this).next('.next');
		var $handler = $(this).find('ul');
		var $pages=$('#diaporama-1-pages');
		$pages.find('li').remove();
		for (var i=0; i<D1_IMAGES.length; i++) {
			$pages.append('<li><a href="#">'+(i+1)+'</a></li>');
		}
		
		$handler.find('li:eq(1),li:eq(2),li:eq(0) a').remove();
		$handler = $handler.find('li:eq(0)');
		$handler.css({'width':'118px','height':'60px','display':'block','position':'relative'});
		
		diaporama($handler,D1_IMAGES);
		diaporama_1_pages($handler,$pages);
		
		$bt_prev.click(function() {
			D1_IMAGES = diaporama_go(-1,D1_IMAGES);
			diaporama($handler,D1_IMAGES);
			diaporama_1_pages($handler,$pages);
			return false;
		});
		
		$bt_next.click(function() {
			D1_IMAGES = diaporama_go(1,D1_IMAGES);
			diaporama($handler,D1_IMAGES);
			diaporama_1_pages($handler,$pages);
			return false;
		});		
	});
	
	$('#twocolumns .colonnesBas .gallery .mask').each(function() {
		var $bt_prev = $(this).prev('.prev');
		var $bt_next = $(this).next('.next');
		var $handler = $(this).find('ul');
		$handler.find('li:eq(1),li:eq(2),li:eq(0) a').remove();
		$handler = $handler.find('li:eq(0)');
		$handler.css({'width':'118px','height':'60px','display':'block','position':'relative'});
		
		diaporama($handler,D2_IMAGES);
		
		$bt_prev.click(function() {
			D2_IMAGES = diaporama_go(-1,D2_IMAGES);
			diaporama($handler,D2_IMAGES);
			return false;
		});
		
		$bt_next.click(function() {
			D2_IMAGES = diaporama_go(1,D2_IMAGES);
			diaporama($handler,D2_IMAGES);
			return false;
		});
		
	});
}

function diaporama_1_pages($handler,$pages) {
	var rel=$handler.find('.fg').attr('rel');
	$pages.find('li').removeClass('active');
	$pages.find('li:eq('+(rel)+')').addClass('active');
	
	$pages.find('li a').unbind('click').click(function(){
		var t = $(this).text(); var d=t-2; if (d<0) d=4;
		
		if ( DIFF < d ) {		
			var diff = d - DIFF;			
			for (var i=0;i<diff;i++) {
				D1_IMAGES = diaporama_go(1,D1_IMAGES);
				diaporama($handler,D1_IMAGES);
				diaporama_1_pages($handler,$pages);
			}
		} else {
			var diff = DIFF-d;
			for (var i=0;i<diff;i++) {
				D1_IMAGES = diaporama_go(-1,D1_IMAGES);
				diaporama($handler,D1_IMAGES);
				diaporama_1_pages($handler,$pages);
			}
		}
		
	return false});
}

function diaporama($handler,images) {
	$handler.find('a').remove();
	
	for (var i=0;i<images.length;i++) {
		var id='diaporama-1-link-'+i;
		var img = images[i];
		var rel = (i+DIFF);
		if (rel>images.length-1) rel=Math.abs(images.length-rel);
		var $a = $('<a href="'+img.link+'" id="'+id+'" rel="'+(rel)+'"></a>');
		$a.css({'position':'absolute','display':'block','top':0,'left':0});
		
		if (i==0||i==2) {
			if (img.width==img.height) img.width 	= 40;
			img.height 	= 40;
			$a.css({'z-index':1,'top':'10px'});
			if (i==2) $a.css({'left':'auto','right':0});
			$a.show();
			$a.removeClass('fg');
		} else if (i==1) {
			if (img.width==img.height) img.width 	= 60;
			img.height 	= 60;
			$a.css({'z-index':2,'left':'50%','margin-left':-img.width/2+'px'});
			$a.show();
			$a.addClass('fg');
		} else if (i>2) {
			$a.hide();
		}
		
		$a.append('<img src="'+img.src+'" width="'+img.width+'" height="'+img.height+'"/>');				
		$handler.append($a);
	}
}

var DIFF=0;

function diaporama_go(dir,images) {
	var ret=new Array();
	DIFF += dir;
	
	if (DIFF>images.length-1) DIFF=0;
	if (DIFF<0) DIFF = images.length-1;
	
	if (dir<0) {		
		ret[0]=images[images.length-1];
	
		for (var i=1;i<images.length;i++) {
			ret[i] = images[i-1];
		}
	} else {
		ret[images.length-1]=images[0];
	
		for (var i=0;i<images.length-1;i++) {
			ret[i] = images[i+1];
		}
	}	
	
	return ret;
}

function accordion($title,$content) {	
	$title.click(function() {
		if ($content.is(':hidden')) {
			$content.slideDown('slow');
		} else {
			$content.slideUp('slow');
		}
	
		return false;
	});
	
	$content.slideUp('fast');
}

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
