
/** ############ */

var scrollTiTask = scPresSizeMgr = scOnLoads[scOnLoads.length] = {
	//Largeur mini de la view
	fMinWidth : 500,
	// ratio de marge max par rapport à la largeur du container
	fMaxMargin : .3,
	// ratio d'espace libre à placer au dessus du contenu
	fTopSpace : .3,
	fScrollTopHeight : "10px",
	fScrollBottomHeight : "10px",
	//Refs noeuds
	fContainerPath : "ide:container",
	fMainZPath : "ide:mainZ",
	fBtnUpPath : "ide:scrollTop",
	fBtnDownPath : "ide:scrollBottom",
	//Classes
	fClassOffUp : "btnOff",
	fClassOffDown : "btnOff",
	//Ratio de l'écran par rapport à la taille "normale" 800x600
	fRatioNormalScreen : 1,
	//
	fSpeed : 0,
	execTask : function(){
		try {
			if(this.fSpeed == 0) return false;
			scPresMgr.getCurrView().scrollTop += this.fSpeed;
			return true;
		}catch(e){
			this.fSpeed = 0;
			return false;
		}
	},
	step : function(pPx) {
		try { scPresMgr.getCurrView().scrollTop += pPx; }catch(e){}
	},
	getRatioNormalScreen : function(){
		return this.fRatioNormalScreen;
	},
	getMainContentWidth : function() {
		return this.fCt.clientWidth;
	},
	getMainContentHeight : function() {
		return this.fCt.clientHeight;
	},
	xReset : function(){
		var vView = scPresMgr.getCurrView();
		vView.style.top="0px";
		vView.style.left="0px";
		vView.style.right="0px";
		vView.style.bottom="0px";
	},
	xRedraw : function(){
	try{
		var vView = scPresMgr.getCurrView();
		var vScrollTop = vView.scrollTop;
		var vH = this.fCt.clientHeight;
		var vContentH = scSiLib.getContentHeight(vView);
		//console.log("vH:::"+vH+"  - vContentH:::"+vContentH);
		if(vContentH < vH) {
			var vW = this.fCt.clientWidth;
			if(vW > this.fMinWidth) {
				var vMargin = Math.round(Math.min( vW * this.fMaxMargin * (1 - vContentH / vH), (vW - this.fMinWidth)/2) ) + "px";
				//console.log("vMargin:::"+vMargin);
				vView.style.left = vMargin;
				vView.style.right = vMargin;
				vH = this.fCt.clientHeight;
				vContentH = scSiLib.getContentHeight(vView);
			}
		}
		if(vContentH < vH) {
			vView.style.top = Math.round( (vH-vContentH) * this.fTopSpace)+"px";
		}
		if(vContentH > vH) {
			//On laisse l'espace pour les scrollTop 
			vView.style.top = this.fScrollTopHeight;
			vView.style.bottom = this.fScrollBottomHeight;
		}
		this.xCheckBtn();
	}catch(e){alert(e);}
	},
	xDefFontSize : function(){
		this.fRatioNormalScreen = Math.sqrt(this.fCt.offsetHeight / 600 * this.fCt.offsetWidth / 800);
		//console.log("this.fRatioNormalScreen ::::"+this.fRatioNormalScreen );
		this.fMainZ.style.fontSize = Math.round(this.fRatioNormalScreen * 18)+"px";
		this.fNeedResizeFont = false;
	},
	xCheckBtn : function(){
		var vView = scPresMgr.getCurrView();
		var vScrollTop = vView.scrollTop;
		var vBtnUpOff = this.fBtnUp.className.indexOf(this.fClassOffUp);
		if(vScrollTop <= 0) {
			if(vBtnUpOff < 0) this.fBtnUp.className+= " "+this.fClassOffUp;
		} else {
			if(vBtnUpOff >= 0) this.fBtnUp.className = this.fBtnUp.className.substring(0, vBtnUpOff);
		}
		
		var vContentH = scSiLib.getContentHeight(vView);
		var vBtnDownOff = this.fBtnDown.className.indexOf(this.fClassOffDown);
		if( vContentH - vScrollTop <= vView.offsetHeight){
			if(vBtnDownOff < 0) this.fBtnDown.className+= " "+this.fClassOffDown;
		} else {
			if(vBtnDownOff >=0) this.fBtnDown.className = this.fBtnDown.className.substring(0, vBtnDownOff);
		}
	},
	onResizedAnc : function(pOwnerNode, pEvent) {
		if(scPresMgr.fEditMode) {
			this.fNeedResizeFont = true;
			return;
		}
		if(pEvent.phase==1) {
			this.xReset();
			this.xDefFontSize();
		} else {
			this.xRedraw();
		}
	},
	onResizedDes : function(pOwnerNode, pEvent) {
		if(scPresMgr.fEditMode) return;
		if(pEvent.resizedNode == pOwnerNode) return;
		if(pEvent.phase==1) {
			this.xReset(); 
			if(this.fNeedResizeFont) this.xDefFontSize();
		} else {
			this.xRedraw();
		}
	},
	ruleSortKey : "presSize",
	onScroll : function(){scrollTiTask.xCheckBtn();},
	onLoad : function(){
		var vListView = scPresMgr.listViews();
		for(var i = 0, l=vListView.length; i<l; i++) {
			var vView = vListView[i];
			vView.onscroll = this.onScroll;
			try {
				if(scCoLib.isIE) vView.onmousewheel = function(){scrollTiTask.step(Math.round(-event.wheelDelta/30))};
				else vView.addEventListener('DOMMouseScroll', function(pEvent){scrollTiTask.step(pEvent.detail*2)}, false);
				
			}catch(e){}
		}
		this.fCt = scPaLib.findNode(this.fContainerPath);
		this.fMainZ = scPaLib.findNode(this.fMainZPath);
		this.fBtnUp = scPaLib.findNode(this.fBtnUpPath);
		this.fBtnDown = scPaLib.findNode(this.fBtnDownPath);
		scSiLib.addRule(this.fCt, this);
		this.xDefFontSize();
		this.xRedraw();
	},
	onLoadSortKey : "presSize"
}
