
//console.profile();

var isIE6 = (jQuery.browser.msie !== undefined && /^6/.test(jQuery.browser.version));
var isIE7 = (jQuery.browser.msie !== undefined && /^7/.test(jQuery.browser.version));
var isIE8 = (jQuery.browser.msie !== undefined && /^8/.test(jQuery.browser.version));

var flags = ( function () {
		
	var delay = 3000,
		duration = 600,
		current = 0,
		loaded = 0,
		timer = null,
		img = [];
	
	var flags = ["cn.gif", "fr.gif", "mo.gif", "sw.gif", "uk.gif"];
				
	function load() {
		
		var i = 0;
		
		for (i = 0; i < flags.length; i++) {
			
			img[i] = new Image();
			img[i].src = Registry.url.base + "public/themes/defaut/medias/communs/flags/" + flags[i];
			
			if (!img[i].complete) {
				img[i].onload = onLoad;
			}
			else{
				onLoad();
			}
		}	
	}
	
	function onLoad() {
		
		loaded++;			
		if (loaded === flags.length - 1) {				
			start();
		}
	}
	
	function start() {			
		timer = window.setTimeout(hide, delay);			
	}

	function show() {
		$('#languages').animate({
			opacity : "1"
		}, duration, onShow);
	}

	function hide() {
		$('#languages').animate({
			opacity : "0"
		}, duration, onHide);
	}

	function onShow() {
		timer = window.setTimeout(hide, delay);
	}

	function onHide() {
		change();
		show();
	}

	function change(){
		
		var next = current + 1;
		if (next >= flags.length) {
			next = 0;				
		}
		current = next;
		$('#languages').empty();
		$('#languages').append(img[current]);
	}
	
	if (!isIE6){
		load();
	}

}());

var recherche = ( function () {
	
	if ($("form#form-recherche").length > 0) {

		$("form#form-recherche input#recherche").focus( function () {
			if ($(this).val() === Registry.form.default_search_term) {
				$(this).val('');
			}
		});
		
		$("form#form-recherche input#recherche").blur( function () {
			if ($(this).val() === "") {
				$(this).val(Registry.form.default_search_term);
			}
		});
	}
}());

var identification = ( function () {

	if ($("form#form-identification").length > 0) {

		$("form#form-identification input#login").focus( function () {
			if ($(this).val() === Registry.form.default_identification_login) {
				$(this).val('');
			}
		});
		$("form#form-identification input#pwd").focus( function () {
			if ($(this).val() === Registry.form.default_identification_password) {
				$(this).val('');
			}
		});
		$("form#form-identification input#login").blur( function () {
			if ($(this).val() === "") {
				$(this).val(Registry.form.default_identification_login);
			}
		});
		$("form#form-identification input#pwd").blur( function () {
			if ($(this).val() === "") {
				$(this).val(Registry.form.default_identification_password);
			}
		});
	}
}());

// menu
var Menu = function (element_id) {

	var timer,
		isOver = false,
		isOpen = false,
		duration = 200,
		hOpen = 0,
		hClose = 0;

	if (element_id === undefined) {
		return;
	}

	if ($('#' + element_id).length === 0 || $('#ss-' + element_id).length === 0) {
		return;
	}

	function onOpen() {
		
	}

	function open() {
		if (!isOpen && isOver) {		
			$('#ss-' + element_id).css("visibility", "visible");
			$('#ss-' + element_id).animate({
				height : hOpen + "px"
			}, duration, onOpen);
			isOpen = true;		
		}
	}

	function onClose() {
		
		$('#ss-' + element_id).css("visibility", "hidden");
	}

	function close() {
		
		if (isOpen && !isOver) {
			$('#ss-' + element_id).animate({
				height : hClose + "px"
			}, duration, onClose);
			isOpen = false;
		}
	}

	function overHandler() {
		
		closeOthers(element_id);
		
		$('#ss-' + element_id).css("zIndex", "7000");
		isOver = true;
		clearTimeout(timer);
		open();
	}

	function outHandler() {
		
		$('#ss-' + element_id).css("zIndex", "6000");
		isOver = false;
		timer = window.setTimeout( function () {
			close();
		}, 100);
	}

	function init() {
		
		var left = (isIE7 || isIE6) ? $('#' + element_id).position().left + "px" : "auto";

		$('#ss-' + element_id).css("left", left).css("height", "0").css("visibility", "hidden");
		
		if ($('#ss-' + element_id).hasClass('last')){
			$('#ss-' + element_id).css("marginLeft", "-110px");
		}				
		$('#' + element_id).bind("mouseover", overHandler);
		$('#ss-' + element_id).bind("mouseover", overHandler);

		$('#' + element_id).bind("mouseout", outHandler);
		$('#ss-' + element_id).bind("mouseout", outHandler);

		$('#ss-' + element_id + ' > li').each( function () {
			hOpen += $(this).outerHeight();
		});

	}

	init();
	
	return {
		open : open,
		close : close,
		element_id : element_id
	};
};

var i,
	me,
	lis = $("li.li-menu"),
	itemMenus = $("a.menu"),
	menus = [];

for (i = 0; i < lis.length; i++) {
	$(lis[i]).removeClass("li-menu");
}	
for (i = 0; i < itemMenus.length; i++) {		
	me = new Menu(itemMenus[i].id);	
	menus.push(me);		
}

itemMenus = $("a.currentMenu");
for (i = 0; i < itemMenus.length; i++) {
	
	me = new Menu(itemMenus[i].id);		

	menus.push(me);		
}

function closeOthers(current_element_id){
	var m;
		i = 0;
		l = menus.length;	
	for (i = 0; i < l; i++) {		
		m = menus[i];		
		if ((typeof m.close === "function") && (m.element_id !== current_element_id)) {
			m.close();
		}
	}	
}
// languages
$("a.iframe").fancybox({		
	'opacity'        : true,
	'width'          : 500,
	'height'         : 436,
	'scrolling'      : 'no',
	'centerOnScroll' : true,
	'overlayShow'	 : true,
	'overlayOpacity' : 0.7,
	'overlayColor'   : '#000'
});

$("#info-bulle").css("visibility", "hidden").css("display", "none");			
$("a.iframe").bind("mouseover", function(e){
	
	var target = e.currentTarget,
		p = $(target).offset(),
		x = p.left + $(target).width(),  
		y = p.top;  
			
	$("#info-bulle")
		.css("left", x + "px")
		.css("top", y + "px")
		.css("visibility", "visible")
		.css("display", "block");
});
$("a.iframe").bind("mouseout", function(e){
	$("#info-bulle")
		.css("visibility", "hidden")
		.css("display", "none");
});

var actualites = ( function() {		
	var id = news || 0;
	if (!isNaN(id) && id !== 0) {
		$.scrollTo( "#actu_" + id, 800);
	}	
}());

//console.profileEnd();

