/*
Copyright (c) 2006 Trinair
Author: Robin van der Rijst
*/

var js_fadeBlock = {
	maxBgOpacity: 60,		//number: maximum opacity of background
	backgroundOpacity: 0,	//number: current opacity of background
	fadeTimerId: null,		//id: currently running fading timer
	fadeInDelay: 40,		//number: millisecond delay in fade-in steps
	fadeOutDelay: 40,		//number: millisecond delay in fade-out steps
	opacityStepIn: 20,		//number: steps with which fade-in is done
	opacityStepOut: 2,		//number: steps with which fade-out is done
	fadingout: false,		//flag to indicate fading out
	hideTimerId: null,		//id: currently running hiding timer
	hideDelay: 500,			//number: millisecond delay before starting fade-out
	content: Object,		//Object: recent block text
	background: Object,		//Object: recent block background
	topShift: 20,			//number: number of pixels to shift block to prevent overlap with origin
	sizeCorrection: 0,		//number: pixels of correction to use to resize background
	available: false,		//Boolean: true if browser supports all required functionality
	
	/* public functions */
	
	init: function(){
		//document.getElementById should be supported
		if(!document.getElementById) return;
		var content = document.getElementById('recent');
		var background = document.getElementById('recentbackground');
		var origin = document.getElementById('recentorigin');
		//content and background should be defined
		if(!content || !background || !origin) return;
		
		//this could be enhanced
		var versionStr = navigator.appVersion;
		var oldIE = /MSIE (4|5).*/;
		if(oldIE.test(versionStr)){
			//old internet explorer so correct for improper box model
			this.sizeCorrection = 0;
		}else{
			this.sizeCorrection = 2;
		}
		
		this.topShift = parseInt(origin.offsetHeight);
		this.content = content;
		this.background = background;
		this.origin = origin;
		this.available = true;
	},
	
	keep: function(){
		if(this.fadingout){
			if(this.backgroundOpacity > 0.5*this.maxBgOpacity){
				this.show();
			}
		}
		else{
			this.show();
		}
	},
	
	show: function(){
		//if not supported, return true so link will be followed if responding to onclick event.
		if(!this.available) return true;
		//currently fading out, start fading back in
		if(this.hideTimerId != null){
			//stop timer - timer might also be fade-in timer but fadeIn will continue
			js_fadeBlock.stopHide();
			js_fadeBlock.startFadeIn();
		}
		else if(this.fadeTimerId != null){
			js_fadeBlock.stopFade();
			js_fadeBlock.startFadeIn();
		}
		//make visible and start fade-in
		else{
			js_fadeBlock.setBackgroundOpacity(0);		//make background totally transparent
			this.content.style.visibility  = 'hidden';	//hide content for now
			this.content.style.display = 'block';		//make blocks display as block 
			this.background.style.display = 'block';	// (not visible because transparant or hidden)
			//position block at same place as origin
			this.background.style.left = parseInt(js_fadeBlock.findPosX(this.origin))+'px';
			this.background.style.top = parseInt(js_fadeBlock.findPosY(this.origin)+this.topShift)+'px';
			this.content.style.left = parseInt(js_fadeBlock.findPosX(this.origin))+'px';
			this.content.style.top = parseInt(js_fadeBlock.findPosY(this.origin)+this.topShift)+'px';
			//background size = content size - border width
			this.background.style.width = (this.content.offsetWidth-this.sizeCorrection)+'px'; 
			this.background.style.height = (this.content.offsetHeight-this.sizeCorrection)+'px';
			js_fadeBlock.startFadeIn();
		}
		
		//return false so link will not be followed if responding to onclick event
		return false;
	},
	
	hide: function(){
		if(!this.available) return false;
		//if fading in, stop and start fading out.
		if(this.fadeTimerId != null){
			js_fadeBlock.stopFade();
		}
		if(this.hideTimerId != null)
			js_fadeBlock.stopHide(); //restart timer
		this.hideTimerId = setTimeout('js_fadeBlock.startFadeOut()', this.hideDelay);
		
	},
	
	
	/* private functions */
	
	stopHide: function(){
		window.clearTimeout(this.hideTimerId);
		this.hideTimerId = null;
	},
	
	startFadeIn: function(){
		this.fadeTimerId = window.setInterval('js_fadeBlock.fadeIn()', this.fadeInDelay);
	},
	
	stopFade: function(){
		window.clearInterval(this.fadeTimerId);
		this.fadeTimerId = null;
	},

	fadeIn: function(){
		var finished = true;
		if(this.backgroundOpacity < this.maxBgOpacity){
			//increase opacity
			newOpacity = this.backgroundOpacity + this.opacityStepIn;
			//make sure backgroundOpacity is never greater than maxBgOpacity
			this.backgroundOpacity = newOpacity <= this.maxBgOpacity ? newOpacity : this.maxBgOpacity; 
			js_fadeBlock.setBackgroundOpacity(this.backgroundOpacity);
			finished = false;
		}
		if(finished){
			js_fadeBlock.showContent();
			js_fadeBlock.stopFade();
		}
	},
	
	showContent: function(){
		this.content.style.visibility = 'visible';
	},
	
	hideContent: function(){
		this.content.style.visibility = 'hidden';
	},
	
	startFadeOut: function(){
		js_fadeBlock.stopHide();
		js_fadeBlock.hideContent();
		this.fadeTimerId = window.setInterval('js_fadeBlock.fadeOut()', this.fadeOutDelay);
		this.fadingout = true;
	},
	
	fadeOut: function(){
		var finished = true;
		if(this.backgroundOpacity > 0){
			//decrease opacity
			newOpacity = this.backgroundOpacity - this.opacityStepOut;
			//make sure backgroundOpacity is never smaller than 0
			this.backgroundOpacity = newOpacity >= 0 ? newOpacity : 0; 
			js_fadeBlock.setBackgroundOpacity(this.backgroundOpacity);
			finished = false;
		}
		if(finished){
			//when fadeout finished, stop timer and hide blocks
			this.background.style.display = 'none';
			this.content.style.display = 'none';
			js_fadeBlock.stopFade();
			js_fadeBlock.stopHide();
			this.fadingout = false;
		}
	},
	
	setBackgroundOpacity: function(opacity){
		var passed = parseInt(opacity);
		passed = passed < 0 ? 0 : (passed > 100 ? 100 : passed); //min 0 max 100
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			this.background.style.filter = "alpha(opacity="+passed+")";
	    } else {
			if(passed < 10)
				passed = "0" + passed;
			this.background.style.opacity = '.'+passed; //css3
			this.background.style.MozOpacity = '.'+passed; //mozilla
			this.background.style.KhtmlOpacity = '.'+passed; //konq / safari
		}
	},

	/* 
	 * findPosX and findPosY are courtesy of Peter-Paul Koch of Quircksmode with
	 * adaptation of Alex Timble and were donated to public domain.
	 * 
	 * http://www.quirksmode.org/js/findpos.html
	 * http://blog.firetree.net/2005/07/04/javascript-find-position
	 */
	findPosX: function(obj){
	    var curleft = 0;
	    if(obj.offsetParent)
	        while(1) 
	        {
	          curleft += obj.offsetLeft;
	          if(!obj.offsetParent)
	            break;
	          obj = obj.offsetParent;
	        }
	    else if(obj.x)
	        curleft += obj.x;
	    return curleft;
	 },

	 findPosY: function(obj) {
	    var curtop = 0;
	    if(obj.offsetParent)
	        while(1)
	        {
	          curtop += obj.offsetTop;
	          if(!obj.offsetParent)
	            break;
	          obj = obj.offsetParent;
	        }
	    else if(obj.y)
	        curtop += obj.y;
	    return curtop;
	 }
};
