/* spécifique */

window.addEvent('domready', function () {
	/* cache les menus deroulants */
	$$('.menu_item_block').setStyle('display', 'none');
	/* precharge les images du menu */
	//init_menu_images();
	/* precharge le fond */
	var myImages = new Asset.images(['/common/images_site/fond.jpg'], {
	    onComplete: function(){
			if($('background'))
			{
				$('background').setStyle('opacity', '0');
				$('background').setStyle('background', 'url(\'/common/images_site/fond.jpg\') no-repeat 50% 0px');
				$('background').set('tween', {duration: 25000});
				$('background').tween('opacity', '0%', '100%');
			}
	    }
	});
});

function init_images()
{
    MM_preloadImages('/images/header/<?=$CFG->lang?>/societe_sel.png');
	MM_preloadImages('/images/header/<?=$CFG->lang?>/actualite_sel.png');
	MM_preloadImages('/images/header/<?=$CFG->lang?>/produits_sel.png');
	MM_preloadImages('/images/header/<?=$CFG->lang?>/revendeurs_sel.png');
	MM_preloadImages('/images/header/<?=$CFG->lang?>/contacts_sel.png');
	MM_preloadImages('/images/header/<?=$CFG->lang?>/competition.png');
}

function menu_rollover_on(menu, image_on){
	if ($(menu + '_img')) {
		$(menu + '_img').setProperty('src', image_on);
	}
	if ($(menu)) {
		$(menu).setProperty('class', 'menu_item_sel');
	}
}

function menu_rollover_off(menu, image_off){
	if ($(menu + '_img')) {
		$(menu + '_img').setProperty('src', image_off);
	}
	if ($(menu)) {
		$(menu).setProperty('class', 'menu_item');
	}
}

function menu_rollover_show(menu, image_on){
	menu_rollover_on(menu, image_on);
	if ($(menu + '_block')) {
		$(menu + '_block').setStyle('display', 'block');
	}
}

function menu_rollover_hide(menu, image_on){
	menu_rollover_off(menu, image_on);
	if ($(menu + '_block')) {
		$(menu + '_block').setStyle('display', 'none');
	}
}

/* générique */


function addslashes (str) {
    return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}


function clear_info_text(element, check_text)
{ 
	if (check_text == null || check_text == element.value)
	{ 
		element.value = ''; 
	} 
}

function set_info_text(element, info_text)
{
	if (element.value == '')
	{
		element.value = info_text;
	}
}

function f_scrollLeft() {
        return f_filterResults (
                window.pageXOffset ? window.pageXOffset : 0,
                document.documentElement ? document.documentElement.scrollLeft : 0,
                document.body ? document.body.scrollLeft : 0
        );
}


function f_scrollTop() {
        return f_filterResults (
                window.pageYOffset ? window.pageYOffset : 0,
                document.documentElement ? document.documentElement.scrollTop : 0,
                document.body ? document.body.scrollTop : 0
        );
}

function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientWidth() {
        return f_filterResults (
                window.innerWidth ? window.innerWidth : 0,
                document.documentElement ? document.documentElement.clientWidth : 0,
                document.body ? document.body.clientWidth : 0
        );
}

function f_clientHeight() {
        return f_filterResults (
                window.innerHeight ? window.innerHeight : 0,
                document.documentElement ? document.documentElement.clientHeight : 0,
                document.body ? document.body.clientHeight : 0
        );
}

function resume_class_element_height(element_class_name, max_height, setCssHeight)
{ 
	var item_orig_text, item_text, chomped_height, compl, txt;
 		
	$$("." + element_class_name).each(function (item, i)
	{	
		item_orig_text = item.get('html');
		
		compl = '';
		chomped_height = false;
		
		control_item_name = item.getProperty('id') + '_ctrl';
		overflow_item_name = item.getProperty('id') + '_ovfl';
		
		if (max_height != null && setCssHeight != null) { compl = ' height: ' + max_height + 'px; ';  }
		
		item.set('html', '<div style="overflow: hidden; ' + compl + '" id="' + overflow_item_name + '"><div style="display: block;" id="' + control_item_name + '">' + 
			item_orig_text + '</div></div>'); 
		
		control_item = $(control_item_name);
		overflow_item = $(overflow_item_name);
		
		if (max_height != null)
		{
			while (control_item.offsetHeight >= max_height) 
			{
			   chomped_height = true;
			   item_text = String(control_item.get('text'));
			   txt=item_text.substring(0, (item_text.length - 10))
			   control_item.set('html', txt);
			   if ((item_text.length - 10) <= 0) { break; }
			}
			if (chomped_height)
			{
			   item_text = control_item.get('text');
			   item_text = item_text.substring(0, item_text.length - 3);
			   item_text = item_text.replace(/\s+[^\s]+\s*$/, '...');
			   control_item.set('html', item_text);
			}
		}
   });
}

function go_url(url, target)
{
	if (target == null || target == '')
	{
		document.location.href = url;	
	}
	else
	{
		var newWindow = window.open(url, target);
		newWindow.focus();
	}
	
}

function affiche_cache_el(el){
	if ($(el)) {
		if ($(el).getProperty('visibility') == 'visible') {
			cache_el(el);
		}
		else {
			affiche_el(el);
		}
	} 
}

function affiche_el(el)
{
	if ($(el)) {
		$(el).setStyle('visibility', 'visible');
	}
}

function cache_el(el)
{
	if ($(el)) {
		$(el).setStyle('visibility', 'hidden');
	}
}