/* gestion de menu js */

function menu_rollover_on(menu, image_on)
{
	if (jQuery('#' + menu + '_img')) {
		jQuery('#' + menu + '_img').attr('src', image_on);
	}
	if (jQuery('#' + menu)) {
		jQuery('#' + menu).attr('class', 'menu_item_sel');
	}
}

function menu_rollover_off(menu, image_off)
{
	if (jQuery('#' + menu + '_img')) {
		jQuery('#' + menu + '_img').attr('src', image_off);
	}
	if (jQuery('#' + menu)) {
		jQuery('#' + menu).attr('class', 'menu_item');
	}
}

function menu_rollover_show(menu, image_on)
{
	menu_rollover_on(menu, image_on);
	if (jQuery('#' + menu + '_block')) {
		jQuery('#' + menu + '_block').css('display', 'block');
	}
}

function menu_rollover_hide(menu, image_on)
{
	menu_rollover_off(menu, image_on);
	if (jQuery('#' + menu + '_block')) {
		jQuery('#' + menu + '_block').css('display', 'none');
	}
}

/* générique */

function page_inner_size()
{
	var ret = { width : 0, height : 0 };
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		ret.width = jQuery(document).width();
		ret.height = jQuery(document).height();
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE
		ret.width = document.documentElement.clientWidth;
		ret.height = document.documentElement.clientHeight;
	}
	return ret;
}

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 date_iso(no_sec)
{	
	var date = new Date();
	
	var month = (date.getMonth() + 1).toString();
	
	var day = date.getDate().toString();
	
	var hours = date.getHours().toString();
	var min = date.getMinutes().toString();
	var sec = date.getSeconds().toString();
	
	if (month.length == 1) { month  = '0' + month; }
	if (day.length == 1) { day = '0' + day; }
	
	if (hours.length == 1) { hours = '0' + hours; }
	if (min.length == 1) { min = '0' + min; }
	if (sec.length == 1) { sec  = '0' + sec; }
	
	var date_time = date.getFullYear() + '-' + month  + '-' + day  + ' ' + hours + ':' + min
	if (no_sec == null) { date_time = date_time + ':' + sec; }
	
	return date_time;
}

function date_iso_verify_input(input, e, no_sec, no_time)
{
	var text_ar = jQuery(input).val().split('');
	var text_ok = [];
	var i;
	var caret_pos = jQuery(input).caret().start;
	
	var length = 19; 
	if (no_sec != null) { length = 16; }
	if (no_time != null) { length = 10; }
	
	for (i=0;i<length;i++)
	{
		if (i==0)
		{
			if (parseInt(text_ar[i]) >= 0 && parseInt(text_ar[i]) <= 9) { text_ok[i] = parseInt(text_ar[i]); }
			else { text_ok[i] = '2'; }
		}
		else if (i==10) { text_ok[i] = ' '; }
		else if (i==4 || i==7) { text_ok[i] = '-'; }
		else if (i==13 || i==16) { text_ok[i] = ':'; }
		else
		{
			if (parseInt(text_ar[i]) >= 0 && parseInt(text_ar[i]) <= 9) { text_ok[i] = parseInt(text_ar[i]); }
			else { text_ok[i] = '0'; }
		}
	}
	jQuery(input).val(text_ok.join(''));
	
	var smart_caret = 0;
	if ( e.which != 37 && caret_pos < length && (caret_pos == 10 || caret_pos == 4 || caret_pos == 7 || caret_pos == 13 || caret_pos == 16)) { smart_caret = (caret_pos + 1); }
	else if ( (e.which == 37 || e.which == 8) && caret_pos > 0 ) 
	{ 
		smart_caret = caret_pos - 1 
		if (smart_caret == 10 || smart_caret == 4 || smart_caret == 7 || smart_caret == 13 || smart_caret == 16) { smart_caret = smart_caret - 1; }
	}
	else if ( e.which == 37 && caret_pos == 0 ) { smart_caret = length - 1; }
	else if (caret_pos >= length) { smart_caret = 0; }
	else { smart_caret = caret_pos; }
	
	jQuery(input).caret(smart_caret, smart_caret + 1); 
	return false;
}

function date_iso_set_one_char_sel(input, no_sec, no_time)
{
	var length = 19; 
	if (no_sec != null) { length = 16; }
	if (no_time != null) { length = 10; }
	var caret_pos = jQuery(input).caret().start;
	
	if (caret_pos == 10 || caret_pos == 4 || caret_pos == 7 || caret_pos == 13 || (length != 16 && caret_pos == 16)) { caret_pos = caret_pos + 1; }
	else if (caret_pos >= length) { caret_pos = 0; }
	jQuery(input).caret(caret_pos, caret_pos + 1); 
}

function fixed_in_block(fixed,relative)
{
	var place_it = function() {
        var scrollPos = jQuery(window).scrollTop();
        var position = jQuery(relative).offset();
        var maxTop = jQuery(relative).innerHeight()-jQuery(fixed).outerHeight(true);
        var realTop = (scrollPos - position.top);
        
        if (realTop <= 0)
        	jQuery(fixed).css('top', '0px');
        else if(realTop < maxTop)
	        jQuery(fixed).css('top', realTop);
	    else
	    	jQuery(fixed).css('top', maxTop);
    }
	place_it();
    jQuery(window).scroll(place_it);
}

function resume_class_element_height(element_class_name, max_height, setCssHeight)
{ 
	var item_orig_text, item_text, chomped_height, compl, txt;
 		
	$jQuery('#' + "." + element_class_name).each(function (i, item)
	{	
		item_orig_text = item.get('html');
		
		compl = '';
		chomped_height = false;
		
		control_item_name = item.attr('id') + '_ctrl';
		overflow_item_name = item.attr('id') + '_ovfl';
		
		if (max_height != null && setCssHeight != null) { compl = ' height: ' + max_height + 'px; ';  }
		
		item.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 = jQuery('#' + control_item_name);
		overflow_item = jQuery('#' + 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.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.html(item_text);
			}
		}
   });
}

function go_url(url, target, restore_scroll)
{
	if (restore_scroll != null)
	{
		var ori_url = url;
		var scroll = f_scrollTop();
		exp_has_get_parameters = /^([^\?]+)(\?[^\?\&]+)/
		if (res = exp_has_get_parameters.exec(url))
		{
			exp_test_scroll = /scroll=/i;
			var done = 0;
			var base  = res[1];
			var first  = res[2]
			
			other_values = url.split('&');
			if (other_values[0]) { other_values[0] = ''; }
			
			url = base;
			
			if (!exp_test_scroll.test(first)) { url = url + first; }
			else { url = url + '?scroll=' + scroll; done = 1; }
			
			for (i in other_values)
			{
				var value = other_values[i];
				if (value != '')
				{
					if (!exp_test_scroll.test(value)) { url = url + '&' + value; }
					else { url = url + '&scroll=' + scroll; done = 1; }
				}
			}
			
			if (done == 0) {
				url = url + '&scroll=' + scroll;
			}
			
		}
		else
		{
			url = url + '?scroll=' + scroll;
		}
	}
	
	if (target == null || target == '')
	{
		document.location.href = url;	
	}
	else
	{
		var newWindow = window.open(url, target);
		newWindow.focus();
	}
	
}

function affiche_cache_el(el){
	if (jQuery('#' + el)) {
		if (jQuery('#' + el).attr('visibility') == 'visible') {
			cache_el(el);
		}
		else {
			affiche_el(el);
		}
	} 
}

function affiche_el(el)
{
	if (jQuery('#' + el)) {
		jQuery('#' + el).css('visibility', 'visible');
	}
}

function cache_el(el)
{
	if (jQuery('#' + el)) {
		jQuery('#' + el).css('visibility', 'hidden');
	}
}


function gotoPage(page, target)
{
    if(target!=null && target.toLowerCase()=='_blank') {
        window.open(page);
    } else {
		window.location.href = page;
    }
}

function sendMail(mail)
{
	window.open('mailto:'+mail+'?subject=Contact Kenny Racing');
}

function container_reload(selector, unique_id)
{
	url = '/common/core/lib/containers/Containers/ajax_all.php?mode_all=reload';
	jQuery(selector).hicone_container_load(url, {}, unique_id, { 
		success: function (e) {
			jQuery(e).trigger('container_reload');
		}
	});
}

function container_show_popup(container_unique_id, container_reload_css_type_id, id_popup)
{
	var data = { 'unique_id' : container_unique_id, 'container_reload_css_type_id' : container_reload_css_type_id, 'id_popup' : id_popup };
	display_popup('main_popup_container', 'show_container_popup', data);
}

function url_show_popup(url, default_height, id_popup)
{
	var data = { 'url' : url, 'default_height' : default_height, 'id_popup' : id_popup };
	display_popup('main_popup_container', 'show_popup_url', data);
}

function container_replace(selector, unique_id, replace_unique_id)
{					
	url = '/common/core/lib/containers/Containers/ajax_all.php?mode_all=replace';
	jQuery('.' + selector).hicone_container_load(url, { 'replace_unique_id' : replace_unique_id }, unique_id, { 
		success: function (e) {
			jQuery(e).trigger('container_replace');
		}
	});
}

function container_replace_data(selector, unique_id, replace_unique_id)
{					
	url = '/common/core/lib/containers/Containers/ajax_all.php?mode_all=replace_data';
	jQuery('.' + selector).hicone_container_load(url, { 'replace_unique_id' : replace_unique_id }, unique_id, { 
		success: function (e) {
			jQuery(e).trigger('container_replace_data');
		}
	});
}

function container_session_set(selector, unique_id, s_key, s_value)
{
	if (s_key != null && s_value != null)
	{	
		url = '/common/core/lib/containers/Containers/ajax_all.php' + 
				'?mode_all=set_session&ajax=1&unique_id=';
		
		if(typeof(selector) == 'string') selector = jQuery('.' + selector);
		
		selector.each(function (i, it_sel) {
			var it = jQuery(it_sel);
			var cur_unique_id = it.attr('data-unique-id');
			if (cur_unique_id != null && (cur_unique_id == unique_id))
			{
				var request = jQuery.ajax({
					global: false,
					success : function (data, textStatus, XMLHttpRequest) {
						it.trigger('container_session_set', [s_key, s_value, data]);
					},
					url: (url + cur_unique_id),
					type: 'post',
					data : { 'session_key' : s_key, 'session_value' : s_value }
				});
			}
		});
	}
}

function container_session_get(selector, unique_id, key)
{
	if (key != null)
	{
		url = '/common/core/lib/containers/Containers/ajax_all.php' + 
				'?mode_all=get_session&ajax=1&unique_id=';
		var return_data;

		if(typeof(selector) == 'string') selector = jQuery('.' + selector);
		
		selector.each(function (i, it_sel) {
			var it = jQuery(it_sel);
			var cur_unique_id = it.attr('data-unique-id');
			if (cur_unique_id != null && (cur_unique_id == unique_id))
			{
				return_data = jQuery.ajax({
					async: false,
					global: false,
					url: (url + cur_unique_id),
					type: 'post',
					data : { 'session_key' : key }
				}).responseText;
			}
		});
		
		return return_data;
	}
}

function containers_keep_alive()
{
	if (global_keep_alive == null) { container_start_keep_alive(); }
	
	var ka_unique_ids = {};
	jQuery('.ka').each(function (i, item_sel) {
		var item = jQuery(item_sel);
		var unique_id = item.attr('data-unique-id');
		if (unique_id != null) { ka_unique_ids[unique_id] = unique_id; }
	});
	
	var page_load_id = jQuery('meta[name="page_load_id"]').attr('content');
	
	var request = jQuery.ajax({
		global: false,
		success : function (data, textStatus, XMLHttpRequest) {
			if (global_keep_alive == null) { container_start_keep_alive(); }
			jQuery(document).trigger('containers_keep_alive', data);
		},
		error : function (jqXHR, textStatus, errorThrown) {
			window.setTimeout('containers_keep_alive()', 5000);
		},
		url: "/common/core/composants/ajax_keep_alive/ajax_keep_alive.php",
		type: 'post',
		data : { "unique_ids" : ka_unique_ids, 'page_load_id' : page_load_id }
	});
}


var global_keep_alive;

function container_start_keep_alive()
{
	global_keep_alive = window.setInterval('containers_keep_alive()', 60000);
}

jQuery(document).ready(function () {   
	container_start_keep_alive();
});

/* gestion de volets */

var volets_timer = null;
var volets_targets = null;
var volets_action = null;
var volets_step_count = null;

function volets_go_target(item, other, target_item, target_other, steps, time_per_step)
{
	if (volets_timer) { window.clearInterval(volets_timer); }
	
	volets_targets = new Array();
	volets_action = new Array();
	volets_step_count = 0;
	
	jQuery(other).each(function (i, e) {
		if (e == item) { volets_targets[i] = target_item; }
		else { volets_targets[i] = target_other; }
		volets_action[i] = (parseInt(jQuery(e).css('width')) - volets_targets[i]) / steps;
	});
	
	volets_timer = window.setInterval(function () {
		if (volets_step_count < steps) {
			jQuery(other).each(function (i, e) {
				if (volets_action[i] && e != item) {
					jQuery(e).css('width', parseInt(jQuery(e).css('width')) - volets_action[i] + 'px');
				}
			});
			var item_index = volets_targets.indexOf(target_item);
			jQuery(item).css('width', parseInt(jQuery(item).css('width')) - volets_action[item_index] - 1 + 'px');
			volets_step_count++;
		}
		else {
			jQuery(other).each(function (i, e) {
				if (volets_targets[i]) {
					jQuery(e).css('width', volets_targets[i] + 'px');
				}
			});
		}
	}, time_per_step);
}
