var BlockQTipsConf = {
	offsetLeft : (15)      /* pixel offset */,
	offsetTop  : (0)      /* pixel offset */,
	width      : 15         /* width in em's */, 
	opacity    : "0.85"     /* final opacity of the block */,
	borderColor: "#bbbbbb",
	borderWidth: "1px",
	bgColor    : "#333333"
};

var Effects = {
	fadeIn : function (elem,maxOpac){
		elem.fadeIn = Effects._fadeIn;
		elem.maxOpac = maxOpac;
		elem.curOpac = 0;
		this.cancelCurrent();
		elem.fadeIn();
	},
	fadeOut : function (elem,fadeDoneF){
		elem.fadeOut = Effects._fadeOut;
		elem.fadeOutDone = fadeDoneF;
		this.cancelCurrent();
		elem.fadeOut();
	},
	cancelCurrent : function() {
		clearTimeout(window.evtId);
	},
	_fadeIn : function() {
		if( (+this.curOpac) < (+this.maxOpac) ){
			this.curOpac = (+this.curOpac)+(0.05);
			Effects.setOpacity(this,this.curOpac);
			window.fadeInElem = this;
			window.evtId = setTimeout(function(){this.fadeInElem.fadeIn()},1);
		} else {
			Effects.setOpacity(this,this.maxOpac);
			window.fadeInElem = null;
		}
	},
	_fadeOut : function() {
		if( (+this.curOpac) > 0 ){
			this.curOpac = Math.max(0,(+this.curOpac)-(0.05));
			Effects.setOpacity(this,this.curOpac);
			window.fadeOutElem = this;
			window.evtId = setTimeout(function(){this.fadeOutElem.fadeOut()},1);
		} else if(this.fadeOutDone) {
			this.fadeOutDone();
			window.fadeOutElem = null;
		}
	},
	setOpacity : function (elem,opac){
		elem.style.filter = "alpha(opacity:"+ ((+opac)*100) +")";
		elem.style.KHTMLOpacity  = opac;
		elem.style.WebkitOpacity = opac;
		elem.style.MozOpacity    = opac;
		elem.style.opacity       = opac;
	}
};

var BlockQuoteEvents = {
	posRef : function(){
		return (
			(document.documentElement.scrollTop)?
				document.documentElement : document.body
			);
	},
	showCitation : function(e){
		var citation = this.citation;
		if(window.activeCit == citation){
			clearTimeout(window.hideEvent);
		}
		if(citation.style.display != "block"){
			citation.style.display = "block";
			Effects.fadeIn(citation,BlockQTipsConf.opacity);
			var posRef = ((document.documentElement.scrollTop)?
				document.documentElement : document.body);
			if(!e){
				e = window.event;
			}
			BlockQuoteEvents.moveCitation(e,citation);
		}
	},
	moveCitation : function(e,citation){
		if(e == null){ e = window.event };
		var posx = BlockQTipsConf.offsetLeft;
		var posy = BlockQTipsConf.offsetTop;
		if(e.pageX || e.pageY){
			posx += e.pageX;
			posy += e.pageY;
		} else if(e.clientX || e.clientY) {
			posx += e.clientX + BlockQuoteEvents.posRef().scrollLeft;
			posy += e.clientY + BlockQuoteEvents.posRef().scrollTop;
		}
		citation.style.top  = (posy) + "px";
		citation.style.left = (posx) + "px";
	},
	hideCitation : function(e){
		var citation = this.citation;
		window.activeCit = citation;
		window.hideEvent = setTimeout( function(){
			window.activeCit.style.display = "none";
			}, 1);
	},
	_cleanup : function(){
	}
};

var BlockQTips = {
	activateOn : function(type){
		var tags = document.getElementsByTagName(type);
		for(var i=0;i<tags.length;i++){
			this.buildCitation(tags[i]);
		}
	},
	buildCitation : function(elem) {
		var titleText = elem.getAttribute("title");
		var url = elem.getAttribute("cite");
		elem.removeAttribute("title");
		if(titleText==null || titleText.length==0){
			titleText = "No Info";
		}
		var citation = document.createElement("div");
		var vmlWrapper = document.createElement("v:roundrect");
			vmlWrapper.strokecolor  = BlockQTipsConf.borderColor;
			vmlWrapper.strokeweight = BlockQTipsConf.borderWidth;
			vmlWrapper.fillColor    = BlockQTipsConf.bgColor;
			vmlWrapper.arcSize = "15%";
		if(typeof vmlWrapper.style.behavior != "undefined"){
			/* Cheap hack to determine if client has VML */
			citation = vmlWrapper;
		}
		citation.className = "citation";
		citation.style.width = BlockQTipsConf.width + "em";
		citation.style.position = "absolute";
		
		var title = this.createElem("span","citeTitle");
		title.appendChild(document.createTextNode(titleText));		
		//var citeAnchorSec = this.createElem("div","citeBottom");
		//var anchor = document.createElement("a");
		//anchor.href = url;
		//var urlSize = (BlockQTipsConf.width * 1.5);
		//var urlText = this.makeUrlFit(url,urlSize);
		//anchor.appendChild(document.createTextNode(urlText));
		//anchor.style.fontFamily = "monospace";
		//citeAnchorSec.appendChild(anchor);
		citation.appendChild(title);
		//citation.appendChild(citeAnchorSec);
		
		elem.citation = citation.citation = citation;
		elem.onmouseover = citation.onmouseover = BlockQuoteEvents.showCitation;
		elem.onmouseout  = citation.onmouseout  = BlockQuoteEvents.hideCitation;
		document.getElementsByTagName("body")[0].appendChild(citation);
	},
	createElem : function(tag,className){
		var elem = document.createElement(tag);
		elem.className = className;
		elem.style.display = "block";
		return elem;
	},
//	makeUrlFit : function(url,size){
//		var urlText = url;
//		size -=4;
//		if(url.length>size){
//			urlText =
//				urlText.substring(0,size/2)
//				+ "....."
//				+ urlText.substring(urlText.length-size/2);
//		}
//		return urlText;
//	}
};

