if (!PIMENTECH.util.CommonEventManager) {
	alert('PIMENTECH.util.AjaxFrames ERROR : looks like PIMENTECH.util.CommonEventManager is not available.\ncommon_event_manager.js must be included.');
} else if (!PIMENTECH.util.AjaxFrames) {

	PIMENTECH.util.AjaxFrames = function() {
		/**
		 * Gives HTML frames behaviour to Div
		 */
	}
	
	PIMENTECH.util.AjaxFrames.prototype.init_frame = function (frame_id) {
		/**
		 * Specifies that div identified with frame_id should be managed 
		 * by AjaxFrames
		 */
		cem.addListener(document.getElementById(frame_id), "click", function (e) { 
			var target, target_elem, input;
			var click_target = cem.getEventTarget(e);

			while (click_target.tagName != 'A' && click_target.tagName != 'INPUT' && click_target.id != frame_id) {
				// stop on link or when container Element is reached
				click_target = click_target.parentNode;
			}
			
			if (click_target.tagName != 'A' && click_target.tagName != 'INPUT') { // Element is not a link (should be the div container)
				return false;
			} 
			else { 
				if (click_target.tagName == 'A') {// Element is a link
					var href = click_target.getAttribute('href');
					if (href && href.indexOf('javascript:') != 0 && href != '' && href.slice(0,1) != '#') {
						if (typeof(click_target.target) == 'string' && click_target.target.length > 0) {
							target = click_target.target; // another target is specified
						}
						else {
							target = frame_id; // use original frame as target
						}
						target_elem = document.getElementById(target);
						if (target_elem == null) {
							return false;
						}
						cem.preventDefault(e); // prevents the browser to open the link in the whole window (normal behaviour)
						jsload(target, href);
						target_elem.setAttribute('src', href);
					}
				} else { // Element is a input FIELD
					target = click_target.getAttribute('target');
					target_elem = document.getElementById(target);
					if (click_target.getAttribute('type') == 'submit' && target !== null) { // only for input submit
						// prevents the browser to open the link in the whole window (normal behaviour)
						cem.preventDefault(e); 
						var form = click_target.form;
						var method = form.method.toLowerCase() || 'post';
						var url = form.action || window.location;
						var withfile = method == 'post' && form.enctype == 'multipart/form-data';
						for (var i=0;i<form.length;i++) {
							input = form[i];
							if (input != click_target && input.getAttribute('type') == 'submit') {
								input.disabled=true; // Multi submit buttons hack 
							}
						}
						
						var obj_submitform = new PIMENTECH.util.LoadHTML('obj_submitform', target, url);

						var anim = new YAHOO.util.Anim(target_elem, {opacity: {to: 1.0}}, 1, YAHOO.util.Easing.easeBoth);
						YAHOO.util.Dom.setStyle(target_elem, 'opacity', 1);

						if (withfile) {
							obj_submitform.setSilence();
							obj_submitform.callback = {
								upload: function(o) { alert('upload OK'); }, // doesn't work
								failure: function(o) { alert('upload NOK'); }, // doesn't work
								timeout: 3000
							};
						}
						obj_submitform.afterPost = function() { 
							for (var i=0;i<form.length;i++) {
								input = form[i];
								if (input.disabled == true) {
									input.disabled = false;
								}
							}
							YAHOO.util.Dom.setStyle(target_elem, 'opacity', 0.5);
							YAHOO.util.Dom.setStyle(target_elem, 'background', 'none');

							anim.animate();
						};
						obj_submitform[method](form.name, url, withfile);
						target_elem.setAttribute('src', url);
					}
				}
			}
		});
	}

	PIMENTECH.util.AjaxFrames.prototype.unframe = function (frame_id) {
		alert('PIMENTECH.util.AjaxFrames.prototype.unframe : to be implemented'); // TODO : implement that !
	}
	
	function loadAjaxFrames() {
		/* *
		 * Ajax frames initialisation 
		 */
		var ajax_frames = new PIMENTECH.util.AjaxFrames();
		var has_src;
		var div = document.getElementsByTagName("div");
		for (var i=0; i < div.length; i++) {
			if (div[i].attributes["src"] !== undefined) {
				if (!div[i].getAttribute('id')) {
					// !!! Insuffisant en cas d'imbrication de blocs
					div[i].setAttribute('id', 'ajax_frame_' + i); 
				}
				ajax_frames.init_frame(div[i].getAttribute('id'));
				if (div[i].getAttribute('src') != "") {
					div[i].reload = function () { jsload(this, this.getAttribute("src")); }
					jsload(div[i].getAttribute('id'), div[i].getAttribute('src'));
				}
			}
		}
	}
}

