var a_class_display = new Array();
a_class_display[0] = 'display_block';
a_class_display[1] = 'display_none';

// array('url', 'file') : save img en cours d'affichage pour d'autres functions (ex. dl)
var select_activ = new Array();




function f_save_option_select(this_tag, tag) {

	if (!select_activ[tag])
		select_activ[tag] = new Array();

	select_activ[tag]['value'] = this_tag.options[this_tag.selectedIndex].value
		? this_tag.options[this_tag.selectedIndex].value
		: '';

}


function f_class_tag(tag, classname) {

	if (tag && document.getElementById(tag)) {
		
		if (classname == 'displaying')
			classname = a_class_display;

		if (typeof(classname) == "object") {
			document.getElementById(tag).className = document.getElementById(tag).className == classname[1]
				? classname[0]
				: classname[1];
		}
		else
			document.getElementById(tag).className = classname;
	
	}
	
	return false;

}











var a_fix_hidden = new Array();

jQuery.noConflict();
jQuery(function() {
				
	// target blank
	jQuery('a').click(function(){
		if (jQuery(this).hasClass('tgbk'))
			jQuery(this).attr("target", "_blank");
	});

	// popup
	jQuery('[rel^="ppb_rl"]').each(function(){
		if (jQuery(this).children('img').length && jQuery(this).children().length == 1) {
			jQuery(this).attr('href', jQuery(this).children().attr('src'));
			jQuery(this).attr('title', jQuery(this).children().attr('title'));
		}
	});

	jQuery('.ppb').bind('click', function(){
		return f_ppb(this, false, false, false, false, true);
	});

	if (jQuery('#ppb_ctn').html())
		f_ppb(false, jQuery('#ppb_ctn').html(), 'inline');


	// affichage btn up
	if (jQuery(window).scrollTop())
		jQuery('#nvdiv_up_bt').fadeIn();
	
	jQuery(window).scroll(function() {
		if (!a_fix_hidden['nvdiv_up_bt']) {
			if (jQuery(this).scrollTop() != 0)
				jQuery('#nvdiv_up_bt').fadeIn();
			else
				jQuery('#nvdiv_up_bt').fadeOut();
		}
	});
		
	// scroll #ancre
	jQuery('a[href^="#"]').click(function() {
		if (jQuery(this).attr('href') != '#ppb_ctn' && jQuery(this).attr('href').substring(1))
			f_scroll_tag(this);
		return false;
	});
	
	// credit
	jQuery('#ft_crea').removeClass('ft_hv');
	jQuery('#ft_crea .ft_ctt_a').click(function(){
		jQuery('#ft_crea div').slideToggle(200);
		jQuery('#ft_crea .ft_ctt_a').focus();
		return false;
	});
	jQuery('#ft_crea .ft_ctt_a').focusout(function(e){
		if (jQuery('#ft_crea div').css('display') == 'block')
			jQuery('#ft_crea div').fadeOut(200);
	});


});


/* ----------------------------------------------- functions ----------------------------------- */

/* ------------ generique ------------ */

function f_ppb(obj, ctn, type, param, rel, bind) {
// colorbox
// BIND : true pr bind('click') > bizarerie (colorbox lancé avec open = false)

//	var overflow = jQuery('html').css('overflow');
//	jQuery('html').css('overflow', 'hidden');

	if (!type) {
		type = 'media';
		if (obj) {
			if (jQuery(obj).hasClass('ppb_in'))
				type = 'inline';
		}
	}

	if (type != 'inline')
		jQuery('#colorbox').removeClass('ppb_in');

	if (!param)
		var param = new Array();

	var maxWidth = '80%';
	var maxHeight = '80%';
	var opacity = 0.8;

	switch(type) {
		case 'inline' :	maxWidth = 700;
						maxHeight = '80%';
						opacity = 0.5;
						break;
		
		case 'media' :	break;
	}

/*	param['onClosed'] = function() {
		jQuery('html').css('overflow', overflow); jQuery('#ppb_inc').empty();
	};
*/
	param['maxWidth'] = maxWidth;
	param['maxHeight'] = maxHeight;
	param['opacity'] = opacity;
	param['initialWidth'] = 50;
	param['initialHeight'] = 50;
	param['current'] = '{current} /{total}';
	
	onOpen = 'br_rad6' + (type == 'inline' ? ' ppb_in' : '');
	
	if (ctn) {
		param['html'] = ctn;
		
		if (type == 'inline')
			onOpen += ' ppb_notitle';
		else
			jQuery('#colorbox').removeClass('ppb_notitle');
		
		param['onOpen'] = function () {
			jQuery('#colorbox').addClass(onOpen);
			if (param['urlopened'] && !jQuery('#btn_urlopened').length)
				jQuery('<a href="'+param['urlopened']+'" id="btn_urlopened"></a>').appendTo('#cboxContent');
			return false;
		}
		
		jQuery.colorbox(param);
	}
	else {
		
		var obj_s = false;
		
		if (rel && !jQuery('[rel='+rel+']:first').length)
			rel = false;
		
		if (!rel)
			rel = jQuery(obj).attr('rel');
			
		jQuery(rel ? '[rel='+rel+']' : obj).each(function(){



			param['open'] = false;
			param['title'] = '';
			
			param['onClosed'] = rel
				? function(){jQuery('[rel='+rel+']').colorbox.remove();}
				: false;

			if (type == 'inline')
				onOpen += ' ppb_notitle';
			else {
				param['title'] = jQuery(this).attr('title');
				jQuery('#colorbox').removeClass('ppb_notitle');
			}
								
			param['onOpen'] = function () {
				jQuery('#colorbox').addClass(onOpen);
				if (param['urlopened'] && !jQuery('#btn_urlopened').length)
					jQuery('<a href="'+param['urlopened']+'" id="btn_urlopened"></a>').appendTo('#cboxContent');
				return false;
			}


			if (jQuery(this).attr('href') == jQuery(obj).attr('href') || jQuery(this).attr('href') == jQuery(obj).attr('src')) {
				if (rel)
					obj_s = this;
				else if (bind)
					param['open'] = true;
			}

			jQuery(this).colorbox(param);

		});

		if (rel)
			jQuery(obj_s ? obj_s : '[rel='+rel+']:first').colorbox({
				open:true,
			});

	}

	return false;

}

function f_ppb_close() {
	jQuery.colorbox.close();
	return false;
}


function f_display_multiple(tag_close, tag_open, type) {
// tag_close : tag || array(tags)

	if (!type)
		type = 'toggle';

	if (typeof tag_close != 'object')
		tag_close = new Array(tag_close);

	for (tag in tag_close) 
		if (jQuery(tag_close[tag]).css('display') != 'none')
			f_display(tag_close[tag], type);


	if (jQuery(tag_open).css('display') == 'none')
		f_display(tag_open, type);
	
	return false;

}


function f_display(obj, type, disp, fix_hidden, del, func, d) {
// disp = null:toggle||true:open||false:close

	if (jQuery(obj).queue('fx').length)
		return false;


	if (!d)
		d = 300;

	if (!type)
		type = 'fade';

	if (typeof obj == 'string' && fix_hidden)
		a_fix_hidden[obj.substring(1)] = true;

	if (typeof disp == 'undefined')
		disp = null;

	if (jQuery(obj).css('display') != 'none') {
		if (disp === null || !disp)
			switch (type) {
				case 'fade' :	jQuery(obj).stop(true, true).fadeOut(d, function() {
									if (del)
										jQuery(obj).remove();
									if (typeof func == 'function')
										func();
								});
								break;
				case 'toggle' :	jQuery(obj).stop(true, true).slideToggle(d, function() {
									if (del)
										jQuery(obj).remove();
									if (typeof func == 'function')
										func();
								});
								break;
			}
	}
	else if (disp === null || disp)
		switch (type) {
			case 'fade' :	jQuery(obj).stop(true, true).fadeIn(d, function() {
								if (typeof func == 'function')
									func();
							});
							break;
			case 'toggle' :	jQuery(obj).stop(true, true).slideToggle(d, function() {
								if (typeof func == 'function')
									func();
							});
							break;
		}

	
	return false;

}


function f_close_result_box(obj) {
	
	obj_div = jQuery(obj).parents('.result_div');
	obj = obj_div.length ? obj_div : jQuery(obj).parents('.result_box');
	return f_display(jQuery(obj), 'toggle', false, false, true);

}


function f_scroll_tag(obj, tag, v) {
	
	if (!v) {
	
		if (!tag)
			tag = jQuery(obj).attr("href");
		
		if (!jQuery(tag).length)
			return;
		
		v = jQuery(tag).offset().top;
	
	}

	jQuery('body,html').animate({scrollTop:v},400);

}


function f_ajax(param, ajaxcmd, funct, ppb_param) {

	if (!param)
		param = new Array();

	if (!param['cache'])
		param['cache'] = false;
	
	if (!param['type'])
		param['type'] = 'POST';
	
	if (!param['dataType'])
		param['dataType'] = 'xml';
	
	param['url'] = 'includes/index_ajax.php' + (param['url'] ? '?'+param['url'] : '');
	
	if (param['data_PACKAGE']) {
		package = param['data_PACKAGE'];
		delete(param['data_PACKAGE']);
	}
	else
		package = PACKAGE;
	
	param['data'] = 'AJAXPOST_PACKAGE_parent=' + PACKAGE + '&AJAXPOST_PACKAGE=' + package + '&AJAXPOST_result_count_box=' + RESULT_count_box + '&AJAXPOST_dataType=' + param['dataType'] + (param['data'] ? '&'+param['data'] : '');
	if (ajaxcmd)
		param['data'] += '&AJAXPOST_ajaxcmd=' + ajaxcmd;	
	if (ppb_param)
		param['data'] += '&AJAXPOST_ctn_popup=1';
	
	param['success'] = function(data) {

		RESULT_count_box = Number(jQuery(data).find('RESULT_count_box').text());

		// logged
			if (jQuery(data).find('LOGGED').text())
				LOGGED = Number(jQuery(data).find('LOGGED').text());

		// update content_module
			if (jQuery(data).find('content_module').text())
				jQuery('#content_'+package).slideToggle(400, function() {
					jQuery('#content_'+package).html(jQuery(data).find('content_module').text()).slideToggle(400);
				});

		// recall
			if (typeof funct == 'function')
				funct(data);


		// popup
			var ppb_type = 'inline';
			var ppb_ctn = false;

			var param_popup = new Array();
			if (ppb_param) {
				if (ppb_param['urlopened'])
					param_popup['urlopened'] = ppb_param['urlopened'];
				if (ppb_param['type'])
					ppb_type = ppb_param['type'];
			}

			// ctn
				if (jQuery(data).find('ctn_popup').text())
					ppb_ctn = jQuery(data).find('ctn_popup').text();

			// result
				if (jQuery(data).find('popup').text()) {
					jQuery('#ppb_inc').html(jQuery(data).find('popup').text());
					ppb_ctn = jQuery('#ppb_ctn').html();
				}

				if (ppb_ctn)
					f_ppb(false, ppb_ctn, ppb_type, param_popup);
			

	};

	jQuery.ajax(param);
	
	return false;

}


function f_chain_input(tag, chain) {

	data = '';

	jQuery(tag+" :input").each(function(i) {
		
		if ((jQuery(this).attr('type') != 'checkbox' && jQuery(this).attr('type') != 'radio') || jQuery(this).is(':checked'))
			data += '&' + this.name + '=' + this.value;

	});
	
	if (data) {
		if (chain)
			data = chain + data;
		else
			data = data.substring(1);
	}

	return data;

}



function f_check_valueform(tag, a_tags, only_check) {

	var pass = true;
	
	if (a_tags) {
		tag = '';
		for(k in a_tags)
			tag += ','+a_tags[k];
		tag = tag.substring(1);
	}

	jQuery(tag).each(function() {

		if (!jQuery(this).val() || (jQuery(this).get(0).tagName == 'textarea' && !jQuery(this).html())) {
			pass = false;
			if (!only_check) {
				jQuery(this).addClass('fld_ntf');
				jQuery(this).focus();
			}
		}
		else if (!only_check)
			jQuery(this).removeClass('fld_ntf');

	});
	
	return pass;

}


function f_repository(url) {

	var r = '';

	var c = url.split("/").length - 1;
	for(i = 0; i < c; i++)
		r += '../';
	
	return r;

}
