var SEARCH_DEF_VAL = "Chercher..."; // Default Value
var SEARCH_AUTOSAVE_URL = "info.marie-jaell"; // Autosave URL
var SEARCH_NUM_RESULTS = 5; // Number of Results

function prepareLinks(inElmt) {
	$("a[target='_blank']",inElmt).addClass('pop');
	
	var reg1 = new RegExp("^[a-z]+://","i");
	var reg2 = new Array("www.marie-jaell.info");
	
	var reg3 = new RegExp("mailto:","i");
	
	var reg4 = new RegExp("\.pdf$","i");
	
	$('a:not(.pop, .ext, .mel, .pdf, .rss)',inElmt).each(function() {
		if ( this.href.match(reg4) ) {
			$(this).addClass('pdf');
		} else if ( this.href.match(reg1) ) {
			var me = this;
			var ext = true;
			$.each( reg2, function(i, n) {
				var domain = new RegExp("://"+n,"i");
				if ( me.href.match(domain) ) ext = false;
			});
			if (ext) $(me).addClass('ext');
		} else if ( this.href.match(reg3) ) {
			$(this).addClass('mel');
		}
	});
	$('a.pop',inElmt).click(function() {window.open(this.href); return false;});
	// thickbox addon
	$('a.thickbox').css("position","relative").append('<img class="tb_icon" src="/libs/thickbox/expand.png" alt="" style="position:absolute; top:0; left:0; width:48px; height:48px; display:none;">');
	$('a.thickbox').mouseover(function() {$('.tb_icon', this).css("display","block");})
			.mouseout(function() {$('.tb_icon', this).css("display","none");});
}

$(document).ready(function(){
	if(jQuery.browser.safari) {
		$("input.search").each(function() {$(this).replaceWith("<input type=\"search\" placeholder=\""+SEARCH_DEF_VAL+"\" autosave=\""+SEARCH_AUTOSAVE_URL+"\" results=\""+SEARCH_NUM_RESULTS+"\" value=\""+this.value+"\" name=\""+this.name+"\" id=\""+this.id+"\" />")});
	} else {
		$("input.search").focus(function() { if (this.value==SEARCH_DEF_VAL) this.value = ''; })
			.blur(function() { if (this.value=='') this.value = SEARCH_DEF_VAL; })
			.each(function() { if (this.value=='') this.value = SEARCH_DEF_VAL; });
	}

	prepareLinks($('#page')[0]);
	
	 if(typeof(hlsr_query) != 'undefined'){ /* va avec hlsr functions pour surligner les recherches locales + Google + Yahoo + Lycos */
	 	var insensitive = 1;
		for (i in hlsr_query){
			var search = hlsr_query[i];
			var span_class = 'hilite term-' + i;
			search = search.replace(/([-.*+?^${}()|[\]\/\\])/g,'\\$1');
			search = search.replace(/___([^_]+)___/g,'[\$1]');
			var regex = new RegExp('(<[^>]*>)|(\\b'+ search +')', insensitive ? 'ig' : 'g');
			$('.hilitable').each(function(){
				$(this).html(
					$(this).html().replace(
						regex, function(a, b, c){
							return (a.charAt(0) == '<') ? a : '<span class=\"'+ span_class +'\">' + c + '</span>';
						}));
			});
		}
	}
	
	var pub = {
		waiting:false,
		todo:null,
		show:function() {
			pub.todo='show';
			if (!pub.waiting) {
				pub.waiting = true;
				setTimeout(function(){
					if (pub.todo == 'show') $('#pub').fadeTo(100, 1.0, function(){pub.waiting=false;if (pub.todo == 'hide') pub.hide();});
					else pub.waiting = false;
				}, 50);
			}
		},
		hide:function() {
			pub.todo='hide';
			if (!pub.waiting) {
				pub.waiting = true;
				setTimeout(function(){
					if (pub.todo == 'hide') $('#pub').fadeTo(100, 0.5, function(){pub.waiting=false;if (pub.todo == 'show') pub.show();});
					else pub.waiting = false;
				}, 50);
			}
		}
	};
	pub.hide();
	$('#pub').mouseover(pub.show).mouseout(pub.hide);
	
 });