/* COMMON JAVASCRIPT */

/**
  * Get element by id
  * @param string id, the element id to get
  * @return wanted element or false if not founded
*/
if (typeof 'getE' != 'function') {
	function getE(id) {
		if (el = document.getElementById(id)) { 
			return el;
		} else {
			return false;
		}
	}
}

/**
  * Add an event on a given object
  * @param html object obj, the object to add event on
  * @param string evType, the event type to add (click, mouseover, blur, etc.)
  * @param string fcn, the function name to call on event
  * @return boolean true on success, false on failure
  */
function addEvent(obj, evType, fcn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fcn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fcn);
		return r;
	} else {
		return false;
	}
	return true;
}


/* PAGE EVENTS */
// Style for submit button
/*
addEvent(window, 'load', function(){
	var e = getE('newssearchSubmit');
	var c = getE('docsSubmitContinue');
	if(e){
		addEvent(e, 'mouseover', function(){
			e.src = '/img/modules/pnews/validerON.gif';
		});
		addEvent(e, 'mouseout', function(){
			e.src = '/img/modules/pnews/valider.gif';
		});
	}
	if(c){
		addEvent(c, 'mouseover', function(){
			c.src = '/img/modules/pdocs/poursuivreON.gif';
		});
		addEvent(c, 'mouseout', function(){
			c.src = '/img/modules/pdocs/poursuivre.gif';
		});
	}
});
*/
// Event for search form
addEvent(window, 'load', function(){
	var e = getE('q');
	if(e){
		addEvent(e, 'click', function(){
			if(e.value == 'Mots clés'){
				e.value = '';
			}
		});
		addEvent(e, 'blur', function(){
			if(e.value == ''){
				e.value = 'Mots clés';
			}
		});
	}
});
// Event for edition form
addEvent(window, 'load', function(){
	var e = getE('pcarnetsChildren');
	var p = getE('pcarnetsChildrenField');
	if(p){
		p.className = 'childON';
	}
	if(e){
		for(var m=2 ; m<=5 ; m++){
			var box = getE('child'+m);
			box.className = 'child';
		}
		if(e.selectedIndex){
			for(var k=1 ; k<=e.selectedIndex+1 ; k++){
				var box = getE('child'+k);
				box.className = 'childON';
			}
		}
		addEvent(e, 'change', function(){
			if(e.value){
				for(var i=1 ; i<=e.value ; i++){
					var box = getE('child'+i);
					box.className = 'childON';
				}
			}
			for(var j=(parseInt(e.value))+1 ; j<=5 ; j++){
				var box = getE('child'+j);
				if(box.className == 'childON'){
					inputs = box.getElementsByTagName('input');
					inputs[0].value = '';
					cats = box.getElementsByTagName('select');
					cats[0].selectedIndex = '';
					box.className = 'child';
				}
			}
		});
	}
});
// Event for all pages
addEvent(window, 'load', function(){
	// Authentication form
	var form = getE('cms_forms_3');
	if(form){
		tab = form.getElementsByTagName("input");
		if(tab.length){
			for(var i=0 ; i<tab.length ; i++){
				if(tab[i].type == 'text'){
					e = tab[i];
					break;
				}
			}
			if(e){
				addEvent(e, 'click', function(){
					if(e.value == 'Identifiant'){
						e.value = '';
					}
				});
				addEvent(e, 'blur', function(){
					if(e.value == ''){
						e.value = 'Identifiant';
					}
				});
			}
		}
	}
	// Print link
	var print = getE('print');
	if(print){
		print.style.display = 'inline';
		printLinks = print.getElementsByTagName('a');
		if(printLinks[0]){
			addEvent(printLinks[0], 'click', function(){
				window.print();
			});
		}
	}
});


// jQuery
if (typeof jQuery != 'undefined') {
	$(document).ready(function(){
		// colorBox events
		var compareEntreprises = $('.compareEntreprises');
		if(compareEntreprises.length > 0){
			compareEntreprises.colorbox({
				iframe: true,
				width:650,
				height:600,
				title:'Comparatif entreprise'
			});
		}
	});
}
