/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Copyright Cylo.co.uk
Written by Graham Vincent 
Contact :: loudsphiers@gmail.com
Last Updated: 17/06/2008
*/
Tip = new Object();
Tip.image_path = "/img/";
Tip.DELAY = 0;//Show tip straight away = 0
Tip.removeToolTips = function()
{
	if(Tip.currentTip!="" && document.getElementById(Tip.currentTip))
	document.getElementById(Tip.currentTip).parentNode.removeChild(document.getElementById(Tip.currentTip));
}
Tip.currentTip = "";
Tip.timerShowTip = false;
Tip.currentTipID=0;
Tip.currentTipText="";


Tip.autoRunTips = function()
{
	var f = Tip.$$("*");
	for(var x=0;x<f.length;x++)
	{
		
		if(f[x].title!="")
		{
			omo = f[x].onmouseover + "";
			omo=omo.substring(omo.indexOf("{")+1);
			omo=omo.substring(0,omo.lastIndexOf("}"));
			omo=Tip.replaceAll(omo,"function onmouseover(event)");
			omo=Tip.replaceAll(omo,"function anonymous()");
			v = f[x].title;
			v=Tip.replaceAll(Tip.replaceAll(v,"\r","<br/>"),"\n","");
			TEXT = "\""+Tip.replaceAll(v,"'","")+"\"";
			
			if(v=="content")TEXT="Tip.replaceAll(this.value,\"\'\",\"\")";
			
			omo+="Tip.showTip(this,"+TEXT+",0);";
			
			f[x].onmouseover = new Function(omo);
			f[x].title="";
		}
	}
}

Tip.showTip = function(el,tip,loc)
{
	
	if(typeof(el.id)=="undefined" || el.id=="")
	{
		el.id=Tip.assignID();
	}
	if(typeof(loc)=="undefined" || loc=="")
	{
		loc=0;
	}
	if(Tip.replaceAll(tip,"<br/>","")=="")return 0;
	tip = Tip.replaceAll(tip,"'","");
	Tip.currentTipID=el.id;
	Tip.timerShowTip = true;
	setTimeout("Tip.actuallyShowTip('"+el.id+"','"+tip+"','"+loc+"')",Tip.DELAY);
	el.onmouseout = function (){Tip.timerShowTip=false}
}

Tip.actuallyShowTip = function(el,tip,loc)
{
	el = document.getElementById(el);
	el.title="";
	if(Tip.timerShowTip==true && Tip.currentTipID == el.id)
	{
		
		if(Tip.currentTip!="" && document.getElementById(Tip.currentTip))
		document.getElementById(Tip.currentTip).parentNode.removeChild(document.getElementById(Tip.currentTip));
		var brSize = Tip.getBrowserSize();
		var bodyWidth = brSize[0];
		var bodyHeight = brSize[1];
		var topOffset = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		
		x = Tip.findPosX(el) + 6;
		y = Tip.findPosY(el) + 6;
		if(x<(bodyWidth/2))
		{
			if(y<(bodyHeight)/2+topOffset)
			{
				loc=6;
			}else{
				loc=3;
			}
		}else{
			if(y<(bodyHeight)/2+topOffset)
			{
				loc=4;
			}else{
				loc=1;
			}
		}

		w = el.offsetWidth;	
		h = el.offsetHeight;
		tt = document.createElement("div");
		
		tt.id = el.id+"_tt";
		Tip.currentTip=tt.id;
		el.onmouseout = function (){if(document.getElementById(tt.id))document.getElementById(tt.id).parentNode.removeChild(document.getElementById(tt.id));}
		setTimeout("Tip.killTip('"+tt.id+"')","999999");//kill off any rouge tips after 999999 seconds
		tt.innerHTML = tip;
		tt.className = "CyloToolTip";
		
		var attempts = 0;
		
		document.body.appendChild(tt);//Add it to the docuemnt so we can get the width and height of the new tip
		tw = tt.offsetWidth;	
		th = tt.offsetHeight;
		tt.parentNode.removeChild(tt);//remove it again so we can replce it int he correct position. else the flicker accures
		
		tt.style.left = x +'px';
		tt.style.top = y + 'px';

		shoveX=x;shoveY=y;
		loc=Tip.setDef(loc,0);
		
		//loc = 6;
		//7 doesnt work becuase of the mouse over
/*
------------------------------------------------+
|				|			   |				|
|		1		|	(0)	2	   |		3		|
|			+---|--------------|---+			|
|---------------+--------------+----------------|
|			| 7	|	Element    |   |			|
|---------------+--------------+----------------|
|			+---|--------------|---+			|
|		4	    |		5	   |		6		|
|				|			   |				|
+-----------------------------------------------+
*/
		imgSrc = Tip.image_path + "tip" + loc + ".gif";
		imgShoveX = "";
		imgShoveY = "";
		
		imgW=12;
		imgH=12;
		switch(loc)
		{
			case 1: case "1":
				shoveX = x - tw;
				shoveY = y - th - 2;
				imgShoveX = tw - 7 + "px";
				imgShoveY = th - 7 + "px";
			break;
			case 2: case "2":
				shoveX = x + (w/2) - (tw/2);
				shoveY  = y -th - 2;
				imgShoveX = tw/2 + "px";
				imgShoveY = th - 2 + "px";
				imgW=8;
				imgH=8;
			break;
			case 3: case "3":
				shoveX = x + w;
				shoveY = y - th - 2;
				imgShoveX = "-6px";
				imgShoveY = th - 8 + "px";
			break;
			case 4: case "4":
				shoveX = x - tw;
				shoveY=shoveY+(h);
				imgShoveX = tw -8+ "px";
				imgShoveY =  -7 + "px";
			break;
			case 5: case "5":
				shoveX = x + (w/2) - (tw/2);
				shoveY=shoveY+(h);
				imgShoveX = tw/2 + "px";
				imgShoveY =  -8 + "px";
				imgW=8;
				imgH=8;
			break;
			case 6: case "6":
				shoveX = x + w;
				shoveY=shoveY+(h);
				imgShoveX = "-6px";
				imgShoveY = "-6px";
			break;
			case 0:
			default:
				//put it above and inline with left side of elemenet
				shoveY = y - th - 2;
				break;
		}
		PADDING = 25;
		if(loc==6)
		{
			//check that the end width is not of the screen
			if(tw+shoveX > bodyWidth)
			{
				tw = tw - ((tw+shoveX)-bodyWidth)-PADDING;
				tt.style.width = tw + 'px';
				document.body.appendChild(tt);//Add it to the docuemnt so we can get the width and height of the new tip
				th = tt.offsetHeight;
				tt.parentNode.removeChild(tt);//*/
			}
		}

		if(loc==3)
		{
			//check that the end width is not of the screen
			if(tw+shoveX > bodyWidth)
			{
				tw = tw - ((tw+shoveX)-bodyWidth)-PADDING;
				tt.style.width = tw + 'px';
				document.body.appendChild(tt);
				th2 = tt.offsetHeight;
				tt.parentNode.removeChild(tt);
				shoveY=shoveY-(th2-th);
				imgShoveY=Tip.replaceAll(imgShoveY,"px","");
				imgShoveY++;imgShoveY--;
				imgShoveY=imgShoveY+(th2-th)+"px";
			}
		}
		
		if(loc==1)
		{
			//check that the starting width is not of the screen
			if(shoveX < 0)
			{
				posdif = -shoveX
				
				tempw=tw-posdif;
				shoveX = PADDING;
				
				tw = tempw - PADDING;
				document.body.appendChild(tt);
				tt.style.width = tw + 'px';
				
				th2 = tt.offsetHeight;
				tt.parentNode.removeChild(tt);
				shoveY=shoveY-(th2-th);
				
				imgShoveX=Tip.replaceAll(imgShoveX,"px","");
				imgShoveX++;imgShoveX--;
				imgShoveX = tw - 7 + "px";
				imgShoveY=Tip.replaceAll(imgShoveY,"px","");
				imgShoveY++;imgShoveY--;
				imgShoveY=imgShoveY+(th2-th)+"px";
			}
		}

		if(loc==4)
		{
			//check that the starting width is not of the screen
			if(shoveX < 0)
			{
				
				posdif = -shoveX
				
				tempw=tw-posdif;
				shoveX = PADDING;
				
				tw = tempw - PADDING;
				document.body.appendChild(tt);
				tt.style.width = tw + 'px';
				
				th2 = tt.offsetHeight;
				tt.parentNode.removeChild(tt);
				shoveY=shoveY-(th2-th)+h;
				imgShoveY=Tip.replaceAll(imgShoveY,"px","");
				imgShoveY++;imgShoveY--;
				imgShoveY = imgShoveY +"px";
				
				imgShoveX=Tip.replaceAll(imgShoveX+"","px","");
				imgShoveX++;imgShoveX--;
				imgShoveX = (tw - 7) + "px";
				
			}
		}
		tt.style.left = shoveX + 'px';
		tt.style.top = shoveY + 'px';
		
		tt.style.width = tw + 'px';
		tt.zIndex = 9999;
		
		if(loc>0)
		{
			img=document.createElement("img");
			img.src=imgSrc;
			img.style.top=imgShoveY;
			img.style.left=imgShoveX;
			img.style.width=imgW;
			img.style.height=imgH;
			img.id=tt.id+"_img";
			img.className="tt_img_opac";
			tt.appendChild(img);//
		}
		
		document.body.appendChild(tt);//
	}
}	


Tip.killTip = function(id)
{
	if(document.getElementById(id))
	document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}
Tip.findPosX = function(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft - obj.scrollLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

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


Tip.setDef = function(v,d)
{
	if(typeof(d)=="undefined" || d=="")d="";
	if(typeof(v)=="undefined" || v=="")
	return d;
	return v;
}
Tip.assignID = function()
{
	exists = 1;
	while(exists>0)
	{
		var chars = "abcdefghiklmnopqrstuvwxyz";
		var string_length = 15;
		var randomstring = '';
		
		for (var i=0; i<string_length; i++)
		{
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		
		if(document.getElementById(randomstring)){}
		else
		{	
			exists =-1;
			return randomstring;
		}
	}
}

Tip.$ = function()
{
	 //http://javascript.internet.com/snippets/prototype-dollar-function.html
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

Tip.$$ = function(parent)
{
	var tags = new Array("ADDRESS","ACRONYM","APPLET","AREA","A","BASE","BASEFONT","BUTTON","BIG","BLOCKQUOTE","BODY","BR","B","CAPTION","CENTER","CITE","CODE","COL","COLGROUP","DD","DFN","DIR","DIV","DL","DEL","DFN","DT","EM","FONT","FIELDSET","FORM","FRAMESET","H1","H2","H3","H4","H5","H6","HEAD","HR","HTML","I","IFRAME","IMG","INPUT","ISINDEX","I","KBD","LINK","LEGEND","LABEL","LI","MAP","MENU","META","OL","OPTION","OPTGROUP","OBJECT","PARAM","PRE","P","Q","S","SAMP","SPAN","SCRIPT","SELECT","SMALL","STRIKE","STRONG","STYLE","SUB","SUP","TABLE","TBODY","TD","TEXTAREA","TH","TFOOT","THEAD","TITLE","TR","TT","UL","U","VAR");
	arr = new Array();
	for(x=0;x<tags.length;x++)
	{
		els = null;
		if(typeof(parent)=="undefined" || parent=="" || parent=="*")
		{
			els = document.getElementsByTagName(tags[x]);
		}else{
			if(typeof(parent)=="string")
			{
				parent = document.getElementById(parent);
			}
			els = parent.getElementsByTagName(tags[x]);
		}	
		for(i=0;i<els.length;i++)
		{
			arr.push(els[i]);
		}
		
	}
	return arr;
}

////////////////////////////////////
Tip.getBrowserSize = function()
{
	var bodyWidth = document.documentElement.clientWidth;
	var bodyHeight = document.documentElement.clientHeight;
	
	var bodyWidth, bodyHeight; 
	if (self.innerHeight){ // all except Explorer 
	 
	   bodyWidth = self.innerWidth; 
	   bodyHeight = self.innerHeight; 
	}  else if (document.documentElement && document.documentElement.clientHeight) {
	   // Explorer 6 Strict Mode 		 
	   bodyWidth = document.documentElement.clientWidth; 
	   bodyHeight = document.documentElement.clientHeight; 
	} else if (document.body) {// other Explorers 		 
	   bodyWidth = document.body.clientWidth; 
	   bodyHeight = document.body.clientHeight; 
	} 
	
	return [bodyWidth,bodyHeight];		
}


Tip.replaceAll = function(str,from,to)
{ var idx = str.indexOf( from ); while ( idx > -1 ) { str = str.replace( from, to ); idx = str.indexOf( from );}
return str;}

Tip.lTrim = function( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1");}
Tip.rTrim = function( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1");}
Tip.trim = function( value ) { return Tip.lTrim(Tip.rTrim(value));}

