
var BLOOMER={ // v1.1
	parent:null,
	stage:null,
	arrow:"",
	verticaloffset:0,
	decayTimers:[],
	edgeoffsetx:0,
	edgeoffsety:0,
	arrowimg:["/_img/bloomer-arrow.gif"],
	arrowheadheight:20,

	captureAnchors:function(){
		var baseElem=null;
		if(arguments.length>0){
			baseElem=$.getById(arguments[0]);
		}
		var bloomAnchors=$.getByClass("bloomer",baseElem);
		
		for(var counter=0;counter<bloomAnchors.length;counter++){
			if(counter==0){
				BLOOMER.parent=bloomAnchors[counter].parentNode;
				BLOOMER.parent.onmouseout =function(e){
					this.blur();
				}

			}
			if(!bloomAnchors[counter].currAnchor){
				var aryFullClass=String(bloomAnchors[counter].className).split(" ");
				var bloomerId="";
				var nextItem=false;
				if(aryFullClass.length>1){
					for(var classItem=0;classItem<aryFullClass.length;classItem++){
						if(nextItem){
							bloomerId=aryFullClass[classItem];
							break;
						}
						if(aryFullClass[classItem]=="bloomer")nextItem=true;
					}
				}
				// alert(bloomAnchors[counter].id+"="+bloomerId);
				
				
				var bloomerActive=false;
				if(String(bloomerId).length>0){
					try{
						if(bloomerData[bloomerId].content){
							bloomAnchors[counter].content=bloomerData[bloomerId].content;
							bloomerActive=true;
						} else {
							bloomAnchors[counter].content="";
						}
					} catch(e){
							bloomAnchors[counter].content="";
					}
					try{
						if(bloomerData[bloomerId].thumb){
							bloomAnchors[counter].thumb=bloomerData[bloomerId].thumb;
							bloomerActive=true;
						} else {
							bloomAnchors[counter].thumb="";
						}
					} catch(e){
							bloomAnchors[counter].thumb="";
					}
					
					if (counter==0){
						prevAnc=bloomAnchors.length-1;
					} else {
						prevAnc=counter-1;
					}
					if (counter==bloomAnchors.length-1){
						nextAnc=0;
					} else {
						nextAnc=counter+1;
					}
					
					bloomAnchors[counter].decayTimer=null;
					bloomAnchors[counter].prevAnchor=prevAnc;
					bloomAnchors[counter].nextAnchor=nextAnc;
					bloomAnchors[counter].currAnchor=counter;
					if(bloomerActive){
						bloomAnchors[counter].onmouseover =function(e){
							this.blur();
							var evtobj=window.event? window.event : e
							BLOOMER.CURRANCHOR=this.currAnchor;
							BLOOMER.CURRANCHOROBJ=this;
							BLOOMER.show(evtobj,this); 
							return false;
							}
			
			
						bloomAnchors[counter].onmouseout =function(e){
							BLOOMER.CURRANCHOR=-1;
							this.blur();
							this.decayTimer=setTimeout(function(){BLOOMER.hide(e);},1000);
							return false;
							}
			
						}
					}
				}
		}
	},
	init:function(){
		// alert("init");
		BLOOMER.stage=GLOBALS.addElement(document.body,"div",{"id":"bloomerstage" });
		BLOOMER.stage.style["display"]="none"; 

		BLOOMER.arrow=GLOBALS.addElement(document.body,"img",{"id":"bloomerarrow" });
		BLOOMER.arrow.setAttribute("src", BLOOMER.arrowimg[0])
		BLOOMER.arrow.style["display"]="none"; 
		BLOOMER.captureAnchors();
	},
	hide:function(e){
		if(BLOOMER.CURRANCHOR<0){
					BLOOMER.arrow.style["display"]="none";
					BLOOMER.stage.style["display"]="none"; 
		}
	},
	getposOffset:function(what, offsettype){
		var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
		var parentEl=what.offsetParent;
		while (parentEl!=null){
			totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
			parentEl=parentEl.offsetParent;
		}
		return totaloffset;
	},

	iecompattest:function(){
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	},

	clearbrowseredge:function(obj, whichedge){
		var ie=document.all
		var ns6=document.getElementById&&!document.all
		return 0;
		if (whichedge=="rightedge"){
			BLOOMER.edgeoffsetx=0
			var windowedge=ie && !window.opera? BLOOMER.iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
			BLOOMER.stage.contentmeasure=BLOOMER.stage.offsetWidth
			if (windowedge-BLOOMER.stage.x < BLOOMER.stage.contentmeasure)
				BLOOMER.edgeoffsetx=BLOOMER.stage.contentmeasure-obj.offsetWidth
			return BLOOMER.edgeoffsetx
		} else{
			BLOOMER.edgeoffsety=0
			var topedge=ie && !window.opera? BLOOMER.iecompattest().scrollTop : window.pageYOffset
			var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
			BLOOMER.stage.contentmeasure=BLOOMER.stage.offsetHeight
			// alert(windowedge-BLOOMER.stage.y+" < " + BLOOMER.stage.contentmeasure);
			if (windowedge-BLOOMER.stage.y < BLOOMER.stage.contentmeasure) //move up?
				BLOOMER.edgeoffsety=BLOOMER.stage.contentmeasure+obj.offsetHeight+(verticaloffset*2)
			return BLOOMER.edgeoffsety
		}
		
	},
	displaytiparrow:function(){ //function to display optional arrow image associated with tooltip
		// BLOOMER.arrow=$.getById("arrowhead")
		// BLOOMER.arrow.src=(BLOOMER.edgeoffsety!=0)? BLOOMER.arrowimg[0] : BLOOMER.arrowimg[1]
		BLOOMER.arrow.src= BLOOMER.arrowimg[0];
		var ieshadowwidth=0;
		
		statusTextArea=$.getById("myTextArea");
		// statusTextArea.value=$.getStyle(BLOOMER.stage,"bottom");
		
		// try
		// {
		// 	ieshadowwidth=(BLOOMER.stage.filters && BLOOMER.stage.filters[0])? BLOOMER.stage.filters[0].Strength : 0
		// }
		// catch(err)
		// {
		// 	ieshadowwidth=5
		//Handle errors here
		// }
		// var ieshadowwidth=0;
		//modify "left" value depending on whether there's no room on right edge of browser to display it, respectively
		BLOOMER.arrow.style.left=(BLOOMER.edgeoffsetx!=0)? parseInt(BLOOMER.stage.style.left)+BLOOMER.stage.offsetWidth-BLOOMER.arrow.offsetWidth+"px" : parseInt(BLOOMER.stage.style.left)+5+"px"
		//modify "top" value depending on whether there's no room on right edge of browser to display it, respectively
		
		// BLOOMER.arrow.style.top=(BLOOMER.edgeoffsety!=0)? parseInt(BLOOMER.stage.style.top)+BLOOMER.stage.offsetHeight-BLOOMER.arrow.offsetHeight-ieshadowwidth+BLOOMER.arrowheadheight+"px" : parseInt(BLOOMER.stage.style.top)-BLOOMER.arrowheadheight+"px"
		
		stageTop=String($.getStyle(BLOOMER.stage,"top")).replace("px","");
		stageHeight=String(BLOOMER.stage.clientHeight).replace("px","");
		
		
		 // BLOOMER.arrow.style.top=parseInt(stageTop)+BLOOMER.stage.offsetHeight-BLOOMER.arrow.offsetHeight-ieshadowwidth+(BLOOMER.arrowheadheight-22)+"px"

		 BLOOMER.arrow.style.top=((parseInt(stageTop)+parseInt(stageHeight))+1)+"px";


	// BLOOMER.arrow.style.top=$.getStyle(BLOOMER.stage,"bottom");
		
		// BLOOMER.arrow.style.visibility="visible"
		BLOOMER.arrow.style["display"]="block";
	},
	sizeStage:function(elem,img){
		var htmlContent="<p>";
		htmlContent+=elem.content+"</p>";
		if(elem.thumb.length>0)htmlContent+="<img src=\""+img.src+"\" height=\""+img.height+"\" width=\""+img.width+"\" align=\"left\" />";
		BLOOMER.stage.innerHTML=htmlContent;
		
		// dropmenuobj=document.getElementById(obj.getAttribute("rel"))
		// showhide(dropmenuobj.style, e);
		BLOOMER.stage.style["display"]="block";
		// statusTextArea.value=(BLOOMER.stage.clientHeight);
		BLOOMER.stage.x=BLOOMER.getposOffset(elem, "left")+60;
		BLOOMER.stage.y=BLOOMER.getposOffset(elem, "top")-(BLOOMER.stage.offsetHeight + elem.offsetHeight)-18;

		BLOOMER.stage.style.left=BLOOMER.stage.x-BLOOMER.clearbrowseredge(elem, "rightedge")+"px";
		BLOOMER.stage.style.top=BLOOMER.stage.y-BLOOMER.clearbrowseredge(elem, "bottomedge")+elem.offsetHeight+"px";

		//  BLOOMER.stage.style.left="200px";
		// BLOOMER.stage.style.top="300px";
		
		// if (enablearrowhead)
		BLOOMER.displaytiparrow()		
	},

	show:function(e,elem){
		if (window.event) event.cancelBubble=true
		else if (e.stopPropagation) e.stopPropagation()
		// if (typeof BLOOMER.stage!="undefined"){
		// 	BLOOMER.stage.style["display"]="none";
		// }

		if(elem.thumb.length>0){
			var tempImage=new Image();
			tempImage.onload=function(){
					BLOOMER.sizeStage(elem,this);
			}
			tempImage.src=elem.thumb;
		} else {
			BLOOMER.sizeStage(elem,this);
		}
	}

};

GLOBALS.addOnload(BLOOMER.init);
