/*!
 * (v) Compact labels plugin
 * Takes one option: labelOpacity [default: true] set to false to disable label opacity change on empty input focus
 */
 function temoigner(){
		if(document.getElementById('temoigner').style.display=="block"){
		document.getElementById('temoigner').style.display="none";
		}else{
		 document.getElementById('temoigner').style.display= "block";
		}
	}
(function($){$.fn.compactize=function(options){var defaults={labelOpacity:true};options=$.extend(defaults,options);return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){if(options.labelOpacity){if(input.val()===''){label.css('opacity','0.5');}}else{label.hide();}});if(options.labelOpacity){input.keydown(function(){label.hide();label.css('opacity',1);});} input.blur(function(){if(input.val()===''){label.show();} if(options.labelOpacity){label.css('opacity',1);}});window.setTimeout(function(){label.toggle(input.val()==='');},50);});};})(jQuery);

/*!
 * (v) hrefID jQuery extention
 * returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*!
 * Scripts
 *
 */ 
jQuery(function($) {
 
	var bodyClassSwitcher = function(el){

		var 
			body = $('body'),
			curClass = body.attr('class').split(' '),
			bodyClass = $(el).attr('class').match(/body-[a-z]/);

		$.each(curClass, function(index, className){
			if (className.match(/body-[a-z]/)) {
				body.removeClass(className);
			}
		});
							
		//body class change
		
		if (bodyClass !== null) {
			body.addClass(bodyClass[0]);
		}		
	};
	
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			labels : function(){
				$('label.c').compactize();
			}
		},
		ui : {
			featured : function(){
				
				var wrap = $('#featured-home');
				if (wrap.length === 0) {
					return;
				}
				
				var defaults = {
					autorotate : true,
					autorotateInterval : 5000
				};
				
				var options = arguments[0] || {};
				options = $.extend({}, defaults, options);
				
				var 
					timeout,
					slides = wrap.find('div.item'),
					nav = $('<ul class="nav" />'),
					current = 0,
					triggers,
					autorotate;
					
				var move = function(pos) {
					slides.hide();
					if ($.browser.msie) {
						slides.eq(pos).show();	
					}
					else {
						slides.eq(pos).fadeIn();
					}
					
					current = pos;
					triggers.removeClass('active');
					triggers.eq(pos).addClass('active');
					
					bodyClassSwitcher(slides.eq(current));
					
					if (options.autorotate) {
						autorotate();
					}
				};
				
				autorotate = function(){
					clearTimeout(timeout);

					timeout = setTimeout(function(){
						var pos = current + 1;
						if (pos === slides.length) {
							pos = 0;
						}

						move(pos);
					}, options.autorotateInterval);
				};
				
				slides.each(function(index, item){
					var trigger = $('<a href="#">' + (index + 1) + '</a>');
					trigger.data('index',index);
					nav.append($('<li />').append(trigger));
				});

				triggers = nav.find('a');
				
				triggers.click(function(e){
					e.preventDefault();
					move($(this).data('index'));
				});
				
				wrap.append(nav);
				
				move(current);				
				/*
				wrap.mouseenter(function(){
					clearTimeout(timeout);
				}).mouseleave(function(){
					autorotate();
				});	
*/				
			},
			tabs : function(){
				$('div.tabs-a').each(function(e){
					var 
						current,
						triggers = $(this).find('ul.nav a'),
						tabs = $(this).find('div.tab');
						
						
					$('.testimonials').click(function(e){	
					
						e.preventDefault();
						var 
							id = $(this).hrefId(),
							currentTab = $(id);
							
						if (current === id) {
							return;
						}
											
						tabs.hide();
						current = id;
						if ($.browser.msie) {
							currentTab.show();	
						}
						else {
							currentTab.fadeIn('fast');
						}
						
						bodyClassSwitcher(currentTab);
						
						triggers.removeClass('active');				 
						$('.onglet_testimonials').addClass('active');
					});
					
					
					
					
					
					triggers.click(function(e){
						e.preventDefault();
						var 
							id = $(this).hrefId(),
							currentTab = $(id);
						
						if (current === id) {
							return;
						}
											
						tabs.hide();
						current = id;
						if ($.browser.msie) {
							currentTab.show();	
						}
						else {
							currentTab.fadeIn('fast');
						}
						
						bodyClassSwitcher(currentTab);
						
						triggers.removeClass('active');
						$(this).addClass('active');
												
					});
					
				});
			}
		},
		fixes : {
			featured : function(){
				$('div.featured div.wrap div.item').click(function(e){
					e.preventDefault();
					window.location = $(this).find('a').attr('href');
				});
			},
			testimonials : function(){
				$('ol.testimonials-b li').each(function(index, el){
					$(this).append('<span class="no">'+(index+1)+'.</span>');
				});
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.labels();
	Engine.fixes.featured();
	Engine.fixes.testimonials();
	
	Engine.ui.featured({
		autorotate : true,
		autorotateInterval : 5000
	});
	
	Engine.ui.tabs();
});
