/**
 * $Id: ent_init.js 273 2009-10-15 10:50:12Z Nico $
 */

//////////////////////////////////////////////////////////////////////////////
// PROTOTYPE
//////////////////////////////////////////////////////////////////////////////

// Fire when the dom is loaded : before images
document.observe("dom:loaded", function() {

	// Alternate colors on rows of table 'Liste'
	var aRow = $$("table.tabListe tbody tr");
	var i = 0;
	aRow.each(function(oItem) {
		// Choice of the className
		oItem.addClassName((i%2==0 ? "trPair" : "trImpair"));
		// Add the doubleclick functionnality in order to modify the data
		oItem.ondblclick = function(){
			oItem.descendants().each(function(oChild) {
				if(oChild.tagName == "A" && oChild.rel == "update") document.location.href = oChild.href;
			});
		};

		// Add the click functionnality in order to select / deselect the data
		oItem.onclick = function(){
			oItem.descendants().each(function(oChild) {
				if(oChild.tagName == "INPUT" && oChild.type == "checkbox"){
					oChild.checked = !oChild.checked;
					if(oChild.checked) oItem.addClassName("trSelected").bSelected = true;
					else oItem.removeClassName("trSelected").bSelected = false;
				}
			});

		};

		// Manage the click functionnality
		oItem.descendants().each(function(oChild) {
			// on checkboxes in order to select / deselect the data
			if(oChild.tagName == "INPUT" && oChild.type == "checkbox" && oChild.name == "suppression[]"){
				oChild.onclick = function(event){
					if(!event) event = window.event;
					var target = event.target || event.srcElement;
					// Stop Bubbling
					if (event.stopPropagation) event.stopPropagation();
					event.cancelBubble = true;
					if(this.checked) oItem.addClassName("trSelected").bSelected = true;
					else oItem.removeClassName("trSelected").bSelected = false;
				};
				oChild.ondblclick = function(event){
					if(!event) event = window.event;
					// Stop Bubbling
					if (event.stopPropagation) event.stopPropagation();
					event.cancelBubble = true;
				};
			}
			// on links in order to access the target
			if(oChild.tagName == "A"){
				var tmpOnClick		 = oChild.onclick;
				var tmpOnDblClick	 = oChild.ondblclick;
				oChild.onclick = function(event){
					if(!event) event = window.event;
					var target = (event.target ? event.target : event.srcElement);
					// Stop Bubbling
					if (event.stopPropagation) event.stopPropagation();
					event.cancelBubble = true;
					if(tmpOnClick) tmpOnClick();
				};
				oChild.ondblclick = function(event){
					if(!event) event = window.event;
					// Stop Bubbling
					if (event.stopPropagation) event.stopPropagation();
					event.cancelBubble = true;
					// Prevent click action
					//if (event.preventDefault) event.preventDefault();
					//event.returnValue = false;
					if(tmpOnDblClick) tmpOnDblClick();
				};
			}
		});

		// Add a className when mouseover event fire
		oItem.onmouseover	 = function(){
			if(this.bSelected != true) this.addClassName("trHover");
			else this.removeClassName("trSelected").addClassName("trHover");
		};
		// Remove a className when mouseout event fire
		oItem.onmouseout	 = function(){
			if(this.bSelected != true) this.removeClassName("trHover");
			else this.addClassName("trSelected").removeClassName("trHover");
		};
		// Change the mouse cursor in pointer
		oItem.addClassName("pointer");
		i++;
	});

	// Vérification automatique des formulaires
	var aField = $$("form[rel='autocheck'] input","form[rel='autocheck'] select","form[rel='autocheck'] textarea");
	var i = 0;
	aField.each(function(oItem) {
		var tmpOnBlur = oItem.onblur;
		// Suppression du style 'survol' quand on quitte l'élément
		oItem.onblur		 = function(){
			checkField(this);
			if(tmpOnBlur) tmpOnBlur();
			this.up("div").removeClassName("fieldFocused");
		};
		var tmpOnFocus = oItem.onfocus;
		// Suppression du style 'survol' quand on quitte l'élément
		oItem.onfocus		 = function(){
			if(this.getAttribute("type") != "submit") this.up("div").addClassName("fieldFocused");
			if(tmpOnFocus) tmpOnFocus();
		};
		i++;
	});

	// Masquer toutes les aides de formulaire
	$$("span.help").invoke("hide");

	// Masquer les blocks d'informations
	if($('block_2')) $('block_2').hide().removeClassName("hidden");
	if($('block_3')) $('block_3').hide().removeClassName("hidden");

	// Init the lightbox effects
	new Lightbox();

	// Roll-Over sur les menus
	var aField = $$("div#navigation img");
	var i = 0;
	aField.each(function(oItem) {
		// Roll-Over
		oItem.onmouseover = function(){
			var sSrc = this.src;
			var bActivated = this.getAttribute("bActivated");
			if(bActivated == null){
				if(testerMotif("_off.png$",sSrc)){
					this.src = sSrc.substr(0,sSrc.length-8) + "_on.png";
				}
			}
		};
		// Roll-Out
		oItem.onmouseout = function(){
			var sSrc = this.src;
			var bActivated = this.getAttribute("bActivated");
			if(bActivated == null){
				if(testerMotif("_on.png$",sSrc)){
					this.src = sSrc.substr(0,sSrc.length-7) + "_off.png";
				}
			}
		};
		i++;
	});

	// Désactiver les boutons de formulaire une fois cliqués (Supprimer)
	var aBtn = $$("a.btnDelete");
	aBtn.each(function(oItem) {
		var tmpOnClick = oItem.onclick;
		// Désactivation
		oItem.onclick		 = function(){
			this.startWaiting();
			$$('form#formDeleteUpdate table').each(function(oTable) {
			//$$('form#formDeleteUpdate table tbody').each(function(oTable) {
				oTable.startWaiting('bigBlackWaiting');
			});
			if(tmpOnClick) window.setTimeout(tmpOnClick, 250);
		};
		oItem.goWait	 = function(){
			this.startWaiting();
			$$('form#formDeleteUpdate table').each(function(oTable) {
			//$$('form#formDeleteUpdate table tbody').each(function(oTable) {
				oTable.startWaiting('bigBlackWaiting');
			});
		};
		oItem.dontWait	 = function(){
			window.setTimeout(this.stopWaiting.bind(this), 250);
			$$('form#formDeleteUpdate table').each(function(oTable) {
			//$$('form#formDeleteUpdate table tbody').each(function(oTable) {
				window.setTimeout(oTable.stopWaiting.bind(oTable), 250);
			});

		};
	});

});
