/******************************
 decorations declarations
******************************/
var decoration = new Class({
	initialize : function(){
	
		this.appendCorners("div.blockSimple", "div.blockInside" 
						, {tag : "span" , desc : { 'html' : '<span class="tl"></span><span class="tr"></span>' , 'class' : "topCorners"}}
						, {tag : "span" , desc : { 'html' : '<span class="bl"></span><span class="br"></span>' , 'class' : "bottomCorners"}});
		
		/*
		this.appendCorners("div.blockShadowSimple,div.blockTabSub div.blockCtn", "div.blockInside" 
				, {'html' : '<span class="sideL"></span><span class="sideR"></span><span class="sideT"><span class="cornerRight"></span></span>'}
				, {tag : "span" , desc : { 'html' : '<span class="cornerRight"></span>' , 'class' : "sideB"}});
		*/
		var pageTopSpan = $$('span.pageTop');
		if (pageTopSpan == null || pageTopSpan == "") {
			this.appendCorners("div#page", "div#pageInside" 
					, {tag : "span" , desc : { 'html' : '<span class="cornerRight"></span>' , 'class' : "pageTop"}}
					, {tag : "span" , desc : { 'html' : '<span class="cornerRight"></span>' , 'class' : "pageBottom"}});
		}
	},

	appendCorners : function(psElmSelector, psBodySelector, poTopCorner, poFooterCorner){
		$(document.body).getElements(psElmSelector).each(function(poElm){
			
			if(!poTopCorner.html){
				var loNode = new Element(poTopCorner.tag, poTopCorner.desc);
				poElm.insertBefore( loNode ,poElm.firstChild);
			}else{
				var loNode = new Element("div", poTopCorner);
				while(loNode.lastChild)
					poElm.insertBefore( loNode.removeChild(loNode.lastChild),poElm.firstChild);
			}
			
			var loNode = new Element(poFooterCorner.tag, poFooterCorner.desc);
			
			var loBody = poElm.getElements(psBodySelector);
			if(loBody && loBody.nextSibling){
				poElm.insertBefore( loNode ,loBody.nextSibling);
			}else{
				poElm.appendChild(loNode);
			}		
		});
	}
});

/******************************
* Tips layer manager
******************************/
var tipsMgr = new Class({
	initialize : function(){
		this.tipsElm = $("tips");
		if(this.tipsElm){
			this.isOver = false;
			this.tips = new Array();
			this.tipsCtn = $("tipsContainer");
			
			//sroucheray 23/02/2010
			this.shimTip = new IframeShim(this.tipsCtn, {
							    display: false,
							    className: 'myFloatingDivShimClass'
							});
			$$("a.tips").each(function(loA){
				var aTip = new tip(loA , this);
				this.tips.push(aTip);
				}.bind(this));
		}
	},

	getTips : function (poTip){
		this.tipsCtn.setProperty("html", poTip.content);
		this.tipsElm.setStyle("visibility" , "visible");
		this.shimTip.show();
	},
	
	letTips : function (){
		this.shimTip.hide();
		this.tipsCtn.setProperty("html", "");
		this.tipsElm.setStyle("visibility" , "hidden");
	},
	
	positionate : function (poTip){
		this.tipsElm.setStyle("left" , poTip.btn.getPosition().x + poTip.btn.offsetWidth+ 20);
		this.tipsElm.setStyle("top" , poTip.btn.getPosition().y - poTip.btn.offsetHeight);
	}
	
});


/******************************
* Tips layer
******************************/
var tip = new Class({
	initialize : function(loElm, loMgr){
		this.mgr = loMgr;
		this.btn = loElm;
		var loTagLink = $(this.btn.href.split("#")[1]);
		if(loTagLink){
			this.content = loTagLink.getProperty("html");
			
			// If the Help system is disabled, we change the class to doubleDotted2 to remove the underlining
			if(!gbHelpSystemFlag && !this.btn.hasClass("tipsFixed")) {
				if(this.btn.hasClass("doubleDotted")){
					this.btn.removeClass("doubleDotted");
					this.btn.add("doubleDotted2");
				}
				
				if(!this.btn.hasClass("second")) {
					var loParent = this.btn.getParent();
					// We put the same color as the parent element
					if(loParent.currentStyle) {
						// Solution for IE
						this.btn.style.color = loParent.currentStyle.color;
					} else if(document.defaultView) {
						// Solution for Firefox
						this.btn.style.color = document.defaultView.getComputedStyle(loParent, '').getPropertyValue("color");
					}
				}
			}
			
			this.btn.addEvent('mouseover', function(){
				// We show the help only if the help system is enabled
				// or if it is something which cannot be disabled (class=tipsFixed)
				if(gbHelpSystemFlag || (this.btn.hasClass("tipsFixed"))) {
					
					if(!this.mgr.isOver){ 
						this.mgr.TO = setTimeout(function(){
								this.mgr.getTips(this);
								this.mgr.isOver =  true;
							}.bind(this), 500);
					}
				}
			}.bind(this));
			
			this.btn.addEvent('mouseout', function() {
				if(this.TO) {
					clearTimeout(this.TO);
					this.TO = null;
				}
				setTimeout(function(){
						this.isOver = false;
						this.letTips();
				}.bind(this), 10);
			}.bind(this.mgr));
			
			
			this.btn.addEvent('mousemove', function() {this.mgr.positionate(this);}.bind(this));
		}
	} 
});

/******************************
* Column resizing
******************************/
var colResize = new Class({
	/**
	 * Constrcutor
	 */
	initialize : function(poOptions){
		this.options = $extend({
			columnDesc			: [{ idcol : 'main'},
			          			   { idcol : 'rightCol' , rowClass : 'rightColInside'}],
			colIdContainer		: 'content'			
		}, poOptions || {});
		
		this.resizing = false;
		
		//get column container
		this.colsContainer = $(this.options.colIdContainer);
		if (!this.colsContainer) return;
		
		//get column
		this.cols = new Array();
		this.lastHeights = new Array();
		this.options.columnDesc.each(function(loColDesc) {
			var loCol = $(loColDesc.idcol);
			if(loCol){
				this.cols.push({ col : loCol , 
								 rows : !loColDesc.rowClass? null : loCol.getElements('div.' + loColDesc.rowClass)});
			}
		}.bind(this));
		
		//check size
		this.checker();		 
	},
	
	/**
	 * check if size has change
	 */
	checker : function() {
		if(this.resizing) return;
		if(this.cssIsDisabled())  return;
		var loColsHeight = this.cols.map(function(loCol) {
				return loCol.col ? loCol.col.offsetHeight : 0;
		});
		
		if (this.lastHeights.join('') != loColsHeight.join('')) {
			this.resize();
			this.lastHeights = loColsHeight;
		}
	},
	
	/**
	 * Resize column
	 */
	resize : function() {
		
		//lock sizing
		this.resizing = true;

		//compute new size
		var lnContainerHeight = this.colsContainer.scrollHeight;
		this.cols.each(function(poCol) {
			if (poCol.col) {
				if (poCol.col.offsetHeight != lnContainerHeight) {
					this.resizeBlocks(poCol.rows || poCol.col , lnContainerHeight - poCol.col.offsetHeight);
				}
			}
		}.bind(this));
		
		//unlock sizing
		this.resizing = false;
	},
	
	/**
	 * Resize column rows
	 * @param poRows rows
	 * @param pnSizeToAdd delta pixel for the rows 
	 */
	resizeBlocks : function(poRows, pnSizeToAdd) {
		var lnNbRow = poRows.length;
		if (poRows.length == 0) return;
		var loRows = poRows instanceof Array ? poRows : [poRows];
		lnNbRow = loRows.length;
		var lnSize = parseInt(pnSizeToAdd/lnNbRow, 10);
		loRows.each(function(loRow) {
			loRow.setStyle("height", loRow.getStyle("height").toInt() + lnSize);
		});
		loRows.getLast().setStyle("height",  parseInt(loRows.getLast().getStyle("height" ,10) + (pnSizeToAdd%lnNbRow)));
	},
	
	/**
	 * disable resize by css
	 */
	cssIsDisabled : function() {
		if(!this.cssCheckElement) {
			this.cssCheckElement = new Element("span", { 'id' : 'cssChecker',
														 'styles' : { 'background-color' : 'red', 'height' : '0px' , 'overflow': 'hidden'}});
			$(document.body).appendChild(this.cssCheckElement);
		};
		return this.cssCheckElement.getStyle("background-color") != 'red';
	}
	
});

/** 
 * blocksResize : alignement des blocs en hauteur 
 */
var blocksResize = new Class({
	/**
	 * Constrcutor
	 */
	initialize: function(poOptions) {
		this.options = $extend({
			nodeName:				'div',
			className:				'line'
		}, poOptions || {});
		
		//lines detections
		var loLines = $(document.body).getElements(this.options.nodeName + "." + this.options.className);
		for(var lnI = 0, lnL = loLines.length;  lnI < lnL; lnI++){
			this.resize(loLines[lnI]);
		}
	},

	/**
	 * Resizing block
	 * @param poParentBlock line containing the blocks to resize
	 */
	resize : function(poParentBlock) {//body block resize
		var lnLineHeight = poParentBlock.clientHeight;
		//detect block
		var loBlocks = poParentBlock.getElements(".unit"); 
		$each(loBlocks, function(poUnit){ //for each block define the body height
			var lnBlockHeight = poUnit.clientHeight;
			var loChildBlocks = poUnit.getElements(".unit");
			$each(loChildBlocks, function(poChildUnit){ loBlocks.erase(poChildUnit); });
			var loBody = poUnit.getElement(".body");
			var lnBodyHeight = loBody.clientHeight + (lnLineHeight - lnBlockHeight);
			loBody.setStyle('height', lnBodyHeight);
		});
	}
});

// Ininitalization events
window.addEvent("domready", function(){new decoration(); new tipsMgr();});
window.addEvent("load", function(){new blocksResize(); new colResize();});