/* MC definded */

/* affichage des images non redimensionné avec le plugin ui dialog de jquery */

/* avoid iamge resizing during page loading */
$("head").append($("<style type='text/css'>#article_content img { display: none} #article_content img.notresize  { display: block; padding: 0 10px 20px 0}</style>"));
$(function(){

	hash_base_init();
	
	$(".artstep").children("li").contents().filter(function() {
        return this.nodeType == 3 && jQuery.trim(this.data).length > 0;
    }).wrap("<h3></h3>");
	
	$(".artstep img").each(function() { this.title = $(this).parent().find('h3').html(); });
	
	$(".main-content abbr").each(function() { 
		$(this).attr("descr", $(this).attr("title")).removeAttr("title");
	}).click(glossary_show);
	
    /* Affichage des images en taille réelle cliquant dessus*/
    $("#article_content img").not(".notresize")
		.removeAttr( "width" )
		.removeAttr( "height" )
        .css("cursor", "pointer")
        .wrap("<div class='imgwrap'></div>")
        .after("<a class='click2resize'>Agrandir</a>")
        .css("display", "inline")
        .click(image_show);
	
	$(".click2resize").each(function() {
        $(this).click(image_show);
    });

	$('.imgwrap').css('display', 'block').css('margin', '1em');
	
	$('#instantfield').keyup(function() 
	{
		var search   = $('#instantfield').val();
		var language = $('#ajaxlanguage').val();
		var category = $('#searchcategory').val();
				
		if (search.length > 0) { 
			$.ajax({ 
				type:    "POST", 
				url:     "ajaxresponse.php", 
				data:    "search=" + search + "&ajaxlanguage=" + language + "&searchcategory=" + category, 
				success: function(searchresults) 
				{ 
					$(".main-content").empty(); 
					if (searchresults.length > 0)  { 
						$(".main-content").append(searchresults);
					} 
				} 
			});
		}
	});
});

function hash_base_init(){
	var  base = new  RegExp('^'+$('base').attr('href'));
    $("#article_content a").each(function(){
		if (this.href!=''&&this.hash!=''&&this.href.match(base)){
			$(this).click(hash_base_go);
			// this.href=document.location.hash+"/"+this.hash;
		}
	});
}

function hash_base_go(){
	document.location.hash=this.hash;
}

function glossary_show() {

	// Fermeture fenêtre précédente
	if (typeof dial != "undefined") dial.dialog('destroy');
	
	var offset = $(this).offset();
	var pos = [parseInt(offset.left), parseInt(offset.top-$(window).scrollTop())];

	var acronym = $(document.createElement('div')).attr('title', $(this).html()).html($(this).attr('descr'));

	dial = $(acronym).dialog({ position : pos, width: 650 }).click(function(){
		dial.dialog('destroy');
	});;

	$(".ui-dialog").css("background", "#FFFFFF");
	$(".ui-dialog-content").css("padding", "0.5em 1em");
	
	/* Fix bug Opera qui sélectionne  l'ensemble de la fenêtre de dialogue à l'ouverture */
	if(jQuery.browser.opera){
		$(dial).append("<input type='hidden' id='hidden'/>");
		document.getElementById("hidden").focus()
	}
	
	var ecart = $(window).width()-$(window).scrollWidth()||document.body.offsetWidth-document.body.scrollWidth;
	var offset = $(this).offset();
	if(ecart<0) {
		offset.left += ecart-10;
		$(".ui-dialog").css("left", offset.left);
	}
}

function image_show() {

	//Fermeture fenêtre précédente
	if (typeof dial != "undefined") dial.dialog('destroy');
		
	wrap = $(document.createElement('div'));
	wrap.html($(this).parent().html()).children("a").remove();
	var img = wrap.children("img").load(set_dial_size);
	wrap.title = img.attr('title')||img.attr('title');

	/* Ouverture de la fenêtre de dialogue */
	if(!jQuery.browser.mozilla) dial = $(wrap).dialog();

	/* Fix bug Opera qui sélectionne  l'ensemble de la fenêtre de dialogue à l'ouverture */
	if(jQuery.browser.opera){
		$(dial).append("<input type='hidden' id='hidden'/>");
		document.getElementById("hidden").focus()
	}
}

function set_dial_size(){
	/* Correction taille & positionnement de la fenêtre de dialogue */
	if(jQuery.browser.mozilla) dial = wrap.dialog({width: this.width+4});
	else wrap.dialog('option', 'width', this.width+4);
	
	if(jQuery.browser.safari) {
		wrap.dialog('option', 'height', this.height+40);
		$(".ui-dialog-content").css("height", $(this).height()+25);
	}
	
	wrap.dialog('option', 'title', $(this).attr('title'))
	wrap.click(function(){
		dial.dialog('destroy');
	});
	
	var ecart = $(window).width()-$(window).scrollWidth()||document.body.offsetWidth-document.body.scrollWidth;
	var offset = $(this).offset();
	if(ecart<0) {
		offset.left += ecart-10;
		$(".ui-dialog").css("left", offset.left);
	}
}

// Create scrollWidth method
$.fn[ 'scrollWidth' ] = function(val) {
if (!this[0]) return;

return val != undefined ?

// Set the scrollWidth
this.each(function() {
this == window || this == document ?
window.scrollWidth( val ) :
this[ 'scrollWidth' ] = val;
}) :

// Return the scrollWidth
this[0] == window || this[0] == document ?
self[ 'scrollWidth' ] ||
$.boxModel && document.documentElement[ 'scrollWidth' ] ||
document.body[ 'scrollWidth' ] :
this[0][ 'scrollWidth' ];
};