var ltie7 = ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent));
$(function(){
	if (ltie7) {
		$("#footerList li:first-child, #accesSpecial + li").css({borderLeft: 'none'});
		$("#sousMenu .menuSsItem:first-child").css({borderTop: 'none'});
		$("#intro + #content").css({background: 'none', border: 'none'});
		$("th:first-child, td:first-child").css({border: 'none'});
		$(".menuSsListDisplayed .menuSsItem:first-child").css({border: 'none'});
		$(".rubLItText > :first-child").css({marginTop: 0});
	}
	
	homeInputsText.init();
	
	ticker.init();
	if ($('body').is('#home')) {
		homeFormsText.init();
	}
	gestionFAQ.init();
	
	$("#formAER").submit(function(){
		$("#load").show();
		
		return true;
	});
	
});


var homeInputsText = function(){
	function init(){
		$('#annexesFormRechercheText,#competence').each(function(){
			var elmt=$(this);
			inserLabelIntoInput(elmt.attr('value'),elmt);
		});
	}
	return {init:init}
}();

var homeFormsText = function(){
	function init(){
		$('#annexesFormCompText').each(function(){
			var elmt=$(this);
			inserLabelIntoInput(elmt.siblings('label'),elmt);
		});
	}
	return {init:init}
}();



function inserLabelIntoInput (value,input) {
	if (input.val() && value) {
		input.focus(function(){if(input.val() == value) input.val('');});
		input.blur(function(){if(input.val() == '')input.val(value);});
		
	}
}

var ticker = function() {
	var newsTicker, recallMove, movingObject;
	var widthGetter, widthGetterElmts;
	var aLineElmts = [];
	var aLineElmtsLength = [];
	var aLineElmtsTotalLength=0;
	var moveDelay=50;
	if(ltie7) var moveDelay=90;
	var moveLength=2;
	var listContainer='li';
	var listContainerSeparationWidth=0;
	var newsTicker= $('#headerPartenaires');
	var listContainerExclude=$('#conseilRegional');
	var counter=0;
	function init() {
		newsTickerElmts = $('>'+listContainer,newsTicker).not(listContainerExclude);
		getTotalWidth();
		newsTicker.width(aLineElmtsTotalLength+'px');
		aLineElmts = $(listContainer,newsTicker);
		
		lineMove(newsTicker);
	}

	function lineMove() {
		newsTicker.bind('mouseover.ticker',function(){lineStop();}).bind('mouseout.ticker',function(){lineAnimate(newsTicker)}).find('>'+listContainer+':visible >a').bind('focus.ticker',function(){newsTicker.css({left: '2px'});listContainerExclude.css({left: '140px'});lineStop();}).bind('blur.ticker',function(){lineAnimate(newsTicker);});
		lineAnimate(newsTicker);
	}
	
	function lineAnimate(){
		recallMove = window.setTimeout(lineAnimate,moveDelay);
		newsTicker.css({left: (parseInt(newsTicker.css('left'))-moveLength)+'px'});
		listContainerExclude.css({left: (parseInt(listContainerExclude.css('left'))+moveLength)+'px'});
		if (parseInt(newsTicker.css('left')) < -aLineElmtsLength[0]) {
			lineSwap();
		}
	}

	function lineSwap() {
		movingObject = newsTicker.find(listContainer+':first');
		movingObject.insertBefore(listContainerExclude);
		newsTicker.css({left: (parseInt(newsTicker.css('left'))+aLineElmtsLength[0])+'px'});
		listContainerExclude.css({left: (parseInt(listContainerExclude.css('left'))-aLineElmtsLength[0])+'px'});
		aLineElmtsLength.push(aLineElmtsLength.shift());
	}
	
	function getTotalWidth() {
		widthGetter=$('<div id="widthGetter"></div>');
		widthGetter.css({position: 'absolute', top: '-999px', left: '-999px', width: '1000em'});
		$('body').append(widthGetter);
		widthGetter.append(newsTickerElmts.clone());
		widthGetterElmts=widthGetter.find(listContainer).css({display: 'block', position: 'absolute', whiteSpace: 'nowrap'});
		widthGetterElmts.each(function(){
			aLineElmtsLength[widthGetterElmts.index(this)]=(parseInt($(this).width())+listContainerSeparationWidth);
		});
		widthGetter.remove();
		var aLineElmtsLengthSize=aLineElmtsLength.length;
		if (!aLineElmtsLengthSize) return;
		for (var i=0; i < aLineElmtsLengthSize; i++) {
			aLineElmtsTotalLength=(aLineElmtsTotalLength+aLineElmtsLength[i])
		}
	}

	function lineStop() {window.clearTimeout(recallMove);}

	return{init:init,stop:lineStop,start:lineAnimate};
}();

var gestionFAQ = function(){
	var intro, headerFAQ;
	function init(){
		intro = $("#content .intro");
		
		$("h2",intro).click(function() {
			if ($(this).next(".response").is(".close")) FAQopen($(this));
			else FAQClose($(this));
		});
	}
	
	function FAQopen(elmt){
		intro.find("h2").each(function(){
			if (!$(this).next(".response").is(".close")) FAQClose($(this));
		});
		elmt.css('background','url(/design/limousin/images/deplierbas.gif) no-repeat 0 2px').next(".response").removeClass('close');
	}
	
	function FAQClose(elmt){
		elmt.css('background','').next(".response").addClass('close');
	}
	
	return {init:init}
}();
