$(document).ready(function(){

  //*** You can Customize This   ***/					   
	//storing the mytip wrappers in holders... you can change these and style them in your css
	var mytipOpenTags = '<span class="mytip">';
	var mytipCloseTags = '</span>';
	//You have to keep the closebutton class unless
	var closeButton = '<br/><br/><span class="closebutton"><strong>close</strong></span>';
	//offset the tool tip so it isn't right under the mouse
	var mytipXoffset = 10;	
	var mytipYoffset = 15;
	var mytipCENTERoffset = 15;	
	//quickness of fade
	var fadein = 300;
	var fadeout = 150;
	//there is a flicker of bottom right in IE sometimes
	var myHoverIntent = 333;
//*** END of Customization variables ***/
	
	var allmytips = $("*[rel*='mytip']");
	var mytip;
	var mystack = new Array();
	
//checking for strings
	function check(checked,checkfor){
				if(checked.indexOf(checkfor)>=0){return true;}
				else{return false;}
			}
			
	function setCSS(object,property,value){
				object.css(property,value);
			}
			
//bugs
	var IE=(navigator.userAgent.indexOf("MSIE")>=0);	
	var SAF=(navigator.userAgent.indexOf("Safari")>=0);	
	if(SAF){$("a[rel*='mytip']").hover(function(){$(this).css('text-decoration','none')});}
			 
//adding the actions to the mytip links
	$("a[rel*='mytip']").each(function(){
		//setting up local globals							   
			var oStatus;	
			var element;
			var elementlocation;
			var myDelay;
			var childImg = $(this).find('img');
			var rel=$(this).attr('rel');
			var tip;
			var count=0;
		//check for custom settings
			var posCenter = check(rel,'center');
			var posTop = check(rel,'top');
			var posLeft = check(rel,'left');
			var leaveopen = check(rel,'leaveopen');			
			var heightText = rel.indexOf('height');
			var widthText = rel.indexOf('width');
			var get = rel.indexOf('get');
			
		//set link position to relative if it is static
			if($(this).css('position') == 'static'){
				$(this).css('position','relative');
			}		
				
		//setting the z-index on hover or in order of hover for open 
			function zstack(mylink,zI,mult){
					var myindex = allmytips.index(mylink);
					var mytotal = $("*[rel*='mytip']").length;
					var newtotal=(mytotal-myindex)+zI;
					newtotal*=mult;
					mylink.css('z-index', newtotal);
			}
				
		//get JSON or not?	
			if(get>0){
				var tipLocation = $(this).attr('id');
				tip = tips[tipLocation];
			}else{
				tip = $(this).attr('rev');
			}

		//Add close button or not?
			if(leaveopen>0){
				tip = mytipOpenTags+tip+closeButton+mytipCloseTags;
			}else{
				tip = mytipOpenTags+tip+mytipCloseTags;

			}
			

		//making sure only 1 tip shows up
			if($(this).find('span').length<1){
				$(this).append(tip);
			}	
			var theTip = $(this).find('.mytip');
			var span = $(this).find('span');

		//get height setting TEXT in attribute
			if(heightText>0){
				var ht = heightText + 6;
				var boxHeightText;
				for(h=ht;h<ht+6;h++){
					boxHeightText+=rel.charAt(h);
				}
				boxHeightText = boxHeightText.replace(/\D/g,"");
				theTip.css('height', boxHeightText+'px');
			}
				
		//get width setting TEXT in attribute
			if(widthText>0){
				var wt = widthText + 5;
				var boxWidthText;
				for(w=wt;w<wt+6;w++){
					boxWidthText+=rel.charAt(w);
				}
				boxWidthText = boxWidthText.replace(/\D/g,"");
				theTip.css('width', boxWidthText+'px');
			}
			
			function removeElement(){
				mystack.splice(elementlocation,1);
			}
				
		//make sure span doesn't hyperlink
			span.mouseover(function(){return false;});
			span.click(function(){return false;});
		//make sure close button doesn't hyperlink
				$(this).find('.closebutton').mouseenter(function(){return false});
				$(this).find('.closebutton').click(function(){
					removeElement();
					$(this).parent('.mytip').hide();
					var theParent = $(this).parent().parent();
					zstack(theParent,6000,1);
					oStatus=true;
					count=0;
					return false;
				});

			
		//***Adding all the mouse events
			$(this).bind({
				click: function(){if($(this).attr('href')=='#'){return false}},
			
			mouseenter: function(){
				
				oStatus=true
				var mywidth = $(this).outerWidth();
				var myheight = $(this).outerHeight();					
				element=this;
				
			//adding Element to an array for z-index purposes
				mystack.push(element)
				if(!IE){elementlocation = mystack.indexOf(element)}
				else{
				 for (var i = 0; i < mystack.length; i++) {
					if (mystack[i] == element) {
					elementlocation = i;
					break;}
				  }
				}
			//setting the z-index  of the links in descending order
				zstack($(this),10000,(elementlocation+1)*100);
										

				//making the mytip move with the mouse
				$(this).mousemove(function(e){
					if(oStatus!=false){
					//geting mouse location for accurate tool tip location and other math
						var x = e.pageX - $(this).offset().left;
						var y = e.pageY - $(this).offset().top;
						var right=(mywidth-x)+mytipXoffset;
						var bottom=(myheight-y)+mytipYoffset;
						var spanWidth = span.outerWidth();
						var spanHeight = span.outerHeight();
						var center = spanHeight / 2;
						var centerHeight = parseInt($(this).outerHeight())/2;
						if(childImg.length>0){(centerHeight=childImg.attr('height')/-2)-mytipXoffset}
						
						center = (parseInt(center))*-1;
							if(!posCenter){
								if(posLeft){setCSS(span,'left',x-mytipXoffset-spanWidth+'px');}
								else{setCSS(span,'left',x+mytipXoffset+'px');}
							
								if(posTop){setCSS(span,'top',y-mytipYoffset-spanHeight+'px');}
								else{setCSS(span,'top',y+mytipYoffset+'px');}
							}
							
							if(posCenter) {
								setCSS(span,'top',centerHeight+center+'px');
								//check for left if not default to right
								if(posLeft){setCSS(span,'left',((spanWidth+(mytipCENTERoffset))*-1)+'px');}
								else{setCSS(span,'left',$(this).outerWidth()+mytipCENTERoffset+'px');}
							}
					}//end of oStatus if
				});//end of mousemove
				
			//Time to smake it show
				if(count==0){
					count++;
					myDelay= setTimeout(function(){span.fadeIn(fadein)},myHoverIntent);
				}else{
					span.show();
				}
			},
			
			mouseout: function(){
				clearTimeout(myDelay);
				count=0;
				
				//force leave open tool tips to stay open
				if(leaveopen){
					oStatus=false;
				}
				
				//regular tool tips
				if(!leaveopen) {
					zstack($(this),6000,1);
					removeElement();
					//bye bye my tip... but not forever ( :
					span.fadeOut(fadeout);
				}
			}

 }); }); });


//BLESSINGS,
//Richard Andrew Lee