
// doc ready
jQuery(function($) {
	$(".lettrine p:first-child").each(function() {
    	var text = $(this).html();
		$(this).html(text.replace(/^([A-Za-z0-9])/g,'<span class="first-letter">$1</span>'));
	});
	label2value();
	scrollToTop();
	mediaTooltip();
	
});
   

// hide labels.labelAsValue and set their texts to the inputs they are for 
this.mediaTooltip = function(){	

	var el = $(document.createElement('p'))
	$("ul.media").after(el)
	
	$("ul.media a").each(function(){		
		$(this).hover(function(){	
			el.text($(this).text())
		}, function(){
			el.text('')
		});	
	});
};

// hide labels.labelAsValue and set their texts to the inputs they are for 
this.label2value = function(){	

	var inactive   = "input-inactive";
	var active     = "input-active";
	var focused    = "input-focused";
    
	$("label.labelAsValue").each(function(){		
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || ($(obj).attr("type") == "password") || (obj.tagName.toLowerCase() == "textarea")){			
			$(obj).addClass(inactive);			
			var text = $(this).text().replace(/\s+$/,'').replace(/^\s+/,'');
			$(this).css("display","none");			
			$(obj).val(text);
			$(obj).focus(function(){	
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);								  
				if($(this).val() == text) $(this).val("");
			});	
			$(obj).blur(function(){	
				$(this).removeClass(focused);													 
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);		
				};				
			});				
		};	
	});
};

this.scrollToTop = function () {

	var el = $(('<p class="top"><a href="#top">Haut de page</a></p>'))
	$(".container").prepend(el)
	
	var scroll_timer;
	var displayed = false;
	var $window = $(window);
	var top = $(document.body).children(0).position().top;
	
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { // use a timer for performance
			if($window.scrollTop() <= top) // hide if at the top of the page
			{
				displayed = false;
				el.fadeOut(500);
			}
			else if(displayed == false) // show if scrolling down
			{
				displayed = true;
				el.stop(true, true).show().click(function () { el.fadeOut(500); });
			}
		}, 100);
	});
};

