var filled=0;
/////////////////////////// prodigal son
function getIEver()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

var sweetTitles = { 
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	obj : Object,							// @Element: That of which you're hovering over
	tip : Object,							// @Element: The actual toolTip itself
	active : 0,								// @Number: 0: Not Active || 1: Active
	currentBlock : 0,
	show : 0,
	ie_add : 0,
	childText : null,
	init : function(fillnumber) {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		filled=fillnumber;
		addEvent($('panel'),'mousemove',this.tipOver);
		//addEvent($('locate'),'click',this.tipOver);
		//addEvent($('panel'),'mousemove',this.redBox);
		addEvent($('panel'),'mouseout',this.tipOut);
		window.onresize= function () { $('locate').style.display="none";}
		if(getIEver()>=6 && getIEver()<7) { this.ie_add=30; }
	},
	
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;

		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX;
			sweetTitles.yCord = window.event.clientY;
			
			var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
		     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
		     ( navigator.vendor == 'KDE' )
		    if( !badOldBrowser ) {
		      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		        //IE 4, 5 & 6 (in non-standards compliant mode)
		        sweetTitles.xCord += document.body.scrollLeft;
		        sweetTitles.yCord += document.body.scrollTop;
		      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		        //IE 6 (in standards compliant mode)
		        sweetTitles.xCord += document.documentElement.scrollLeft;
		        sweetTitles.yCord += document.documentElement.scrollTop;
		      }
	    	}
			
		}
	},
	tipOut: function() {
			if ( window.tID ) {
				$('panel').style.cursor="default";
				clearTimeout(tID);
			}
	},

	
	
	tipOver : function(e) {
	
		if ( window.tID ) {
			sweetTitles.show=1;		
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}	
	//	$('panel').style.cursor="wait";
		sweetTitles.obj = this;
	//	$('locate').style.cursor="wait";
		tID = window.setTimeout("sweetTitles.tipShow()",100);
		//sweetTitles.show=1;
		sweetTitles.updateXY(e);
		//sweetTitles.tipShow();
		
	},	
	tipOverLong : function(e) {
	
		if ( window.tID ) {
			sweetTitles.show=1;		
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}	
	//	$('panel').style.cursor="wait";
		sweetTitles.obj = this;
	//	$('locate').style.cursor="wait";
		tID = window.setTimeout("sweetTitles.tipShow()",500);
		//sweetTitles.show=1;
		sweetTitles.updateXY(e);
		//sweetTitles.tipShow();
		
	},
	
	tipClear: function (e) {
		sweetTitles.show=0;
	},
	
	tipShow : function() {		
		if (sweetTitles.show) {
			var panel=$('main');
			$('panel').style.cursor="default";
			var bw=10;
			var bh=10;
			var row=80;
	
			var scrX = Number(this.xCord);
			var scrY = Number(this.yCord);
					
			var panelPosX=scrX-panel.offsetLeft-37;
			var panelPosY=scrY-panel.offsetTop;
			
			var brick= (  Math.floor(panelPosY/bh)*row  )+(Math.floor(panelPosX/bw)) +1;
			
			var http_request = null;
		
		    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		        http_request = new XMLHttpRequest();
		        if (http_request.overrideMimeType) {
		            http_request.overrideMimeType('text/xml');
		            // See note below about this line
		        }
		    } else if (window.ActiveXObject) { // IE
		        try {
		            http_request = new ActiveXObject("Msxml2.XMLHTTP");
		        } catch (e) {
		            try {
		                http_request = new ActiveXObject("Microsoft.XMLHTTP");
		            } catch (e) {}
		        }
		    }
		
		    if (!http_request) {
		        alert('Giving up :( Cannot create an XMLHTTP instance');
		        return false;
		    }
		    
			    http_request.onreadystatechange = function() { sweetTitles.tipReply(http_request); };
			    http_request.open('POST', 'brick.php', true);
			    http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			    http_request.send('id='+brick);
			    sweetTitles.currentBlock=brick;

		}
	},
	
	redBox : function (e) {
				sweetTitles.updateXY(e);
			    var scrX = Number(sweetTitles.xCord);
				var scrY = Number(sweetTitles.yCord);
				var tp = parseInt(scrY/10)*10;
				var lt = parseInt(scrX/10)*10;
				if (lt>(832+($('main').offsetLeft))) return false;
				var locate=$('locate'); 
				locate.style.top=tp + 'px';	 locate.style.left=lt+ 'px';
				locate.style.display="";
				locate.style.background="#f00";
		
	},
	
	blockDisplay: function (number, message) {
	
			    var scrX = Number(this.xCord);
				var scrY = Number(this.yCord);
				var tp = parseInt(scrY)+10;
				var lt = parseInt(scrX)+15-42;
				$('locate').style.cursor="pointer";
			
				if(!message) { return false; }
				this.tip.style.visibility='hidden';	
				//alert(message.innerHTML);
				this.tip.innerHTML=message;
				this.tip.className="";
				var scroll = getScrollXY();

				if ( scroll[0] < parseInt(this.tip.offsetWidth+lt) ) {
					this.tip.style.left = parseInt(lt-(this.tip.offsetWidth)-15+86+this.ie_add)+'px';
					this.tip.className="horizontal";
				} else {
					this.tip.style.left = parseInt(lt-15)+'px';

				}

				if ( ( scroll[1] < parseInt(this.tip.offsetHeight+tp) ) && tp>500) {

					this.tip.style.top = parseInt(tp-(this.tip.offsetHeight)-5)+'px';
					if (this.tip.className=="horizontal")
						this.tip.className="vertical shift";
					else
						this.tip.className="vertical";
				} else {
					this.tip.style.top = parseInt(tp-10)+'px';		
				}
				
				this.tip.style.visibility = 'visible';
				this.tip.style.opacity = '1';
				

							
				//this.tipFade(10);
		//addEvent($('b1'),'click',this.tipOver);
		//addEvent($('b2'),'click',this.tipOver);	
		addEvent($('b1'),'mousemove',this.tipOverLong);	
		addEvent($('b2'),'mousemove',this.tipOverLong);	
		addEvent($('block'),'mousemove',this.tipClear);	
	},
	
		blockFind: function (brick) {
			
			if(brick < 1 || brick > 100000) return false;
			
			var panel=$('main');
		
			var bw=10;
			var bh=10;
			var row=80;
			var extra_offset=37;
			var scrX = Number(this.xCord);
			var scrY = Number(this.yCord);
					
			var panelPosX=panel.offsetLeft+10-41+37;
			var panelPosY=panel.offsetTop+15;
			var user= this.tip;

	       	//this.tip.style.visibility='hidden';	
			this.currentBlock=brick;
		    var req = createXMLHttpRequest();
		    req.onreadystatechange = function() {	
					if (req.readyState == 4) {
	       				if (req.status == 200) {
							sweetTitles.tip.innerHTML=sweetTitles.returnData(req);
							window.onresize=function () {sweetTitles.tip.style.visibility='hidden'; $('locate').style.display='none';};
							
							sweetTitles.tip.className="";
			
							tp=(bh*Math.floor((brick-1)/row))+panelPosY;
							lt=(bw*(brick-(Math.floor((brick-1)/row)*row)))+panelPosX;
			
							if (  lt+71  > parseInt($('panel').offsetWidth) ) {
								sweetTitles.tip.style.left = parseInt(lt-(sweetTitles.tip.offsetWidth)-15+sweetTitles.ie_add+86)+'px';
								sweetTitles.tip.className="horizontal";
							} else {
								sweetTitles.tip.style.left = parseInt(lt-15)+'px';
			
							}
							var locate=$('locate'); 
							locate.style.top=tp-15 + 'px';					locate.style.left=lt+21+ 'px';
							locate.style.display="";
							locate.style.background="#f00";
							window.setTimeout("TF.Scroller.anchorScrollId($('locate'))",200);
							
							sweetTitles.tip.style.top = parseInt(tp-10)+'px';		
							sweetTitles.tip.style.visibility = 'visible';	sweetTitles.tip.style.opacity = '1';
							//sweetTitles.tipFade(10);
							addEvent(locate,'click',sweetTitles.tipOver);
							addEvent(locate,'mouseout',sweetTitles.tipOut);
						}
					}
				};
		    req.open('POST', 'brick.php', true);
		    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		    req.send('id='+brick);
 
			return true; 
				
	},
	
	tipReply: function(http_request) {
	
		
		if (http_request.readyState == 4) {
	       	if (http_request.status == 200) {
	       	
				this.blockDisplay(this.currentBlock, sweetTitles.returnData(http_request));
	
			
	       } else {
	           alert('There was a problem with the request.');
	       }
   		}
	},
	
	returnData: function(http_request) {
		
				var txt=http_request.responseText;
				if (txt.substr(0,2) == "/*") {
				  txt = txt.substring(2, txt.length - 2);
				}
				doc = eval("(" + txt + ")");
				http_request = null;
				
				var innerHTML='';
		       	if(doc['id']==this.currentBlock) { 
	           
			   		if(doc['name']=="NULL") { doc['name']=""; } else { doc['name']="<strong>"+doc['name']+"</strong>";}
					if(doc['content']=="NULL") { doc['content']=""; } 
					innerHTML = "<span id='b0'></span><span id='b1'></span><span id='b2'></span><p id='block'><img src='"+doc['img']+"' align='top'><span>"+doc['name']+doc['content']+"</span></p><div></div>";

					return innerHTML;
	          	} 
				return '';
				
	},
	
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 100 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity="+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else { 
			this.tip.style.opacity = '1';
			this.tip.style.filter = "alpha(opacity:100)";
		}
	}
};
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  
  return [ scrOfX+myWidth, scrOfY+myHeight , scrOfX, scrOfY];

}

		var foo;
		
		function findBlock() {
			if($('finder').className=='keyword') {
				
				var req = createXMLHttpRequest();
				req.open("GET", "search.php?string="+$('blockid').value, true);
				req.onreadystatechange = function() {
		
					if (req.readyState == 4 ) {
		       			var txt = req.responseText;
						if (txt.substr(0,2) == "/*") {
						  txt = txt.substring(2, txt.length - 2);
						}
						object = eval("(" + txt + ")");
						if(object['result']==1) {
							$('blockid').style.background="#FFF";
							sweetTitles.blockFind(object['id']);
								
						} else if (object['result'] == 0) {
							$('blockid').style.background="#F77";
						} else {
							$('block_finder').submit();
						}
						req=null;

					}
				}

				req.send(null);
				
				return false;
		
			} else {
				blockid=$('blockid').value;
				if(!isNaN(blockid)) {
					sweetTitles.blockFind(blockid);
				}
				return false;
			}
		}
		
		function switchMode() {
			var f=$('finder');
			if(f.className=='keyword') { f.className=''; $('nextmode').innerHTML='&nbsp;keyword'; $('blockid').style.background="#FFF";}
			else { f.className='keyword'; $('nextmode').innerHTML='find block';	$('blockid').style.background="#FFF"; }		
		}
		
		function timer() {
		
			if(foo==null) { 
				foo = setTimeout('findBlock()',1000)
			}
		}
		function endtimer() {
			clearTimeout(foo);
			foo=null;	
		}
		
		
		function load_well(res) {
				sweetTitles.tip.style.visibility = "hidden";
				var req = createXMLHttpRequest();
				if($('well_content_'+res).innerHTML==$('well_content').innerHTML) {
					req.open("GET", "well.php?id="+res, true);
					req.onreadystatechange = function() {
			
						if (req.readyState == 4 ) {
			       			var txt = req.responseText;
							if (txt.substr(0,2) == "/*") {
							  txt = txt.substring(2, txt.length - 2);
							}
							object = eval("(" + txt + ")");
							if(object['result']==1) {
								$('well_content_'+res).innerHTML = object['output'];
							} else if (object['result'] == 0) {
								$('well_content_'+res).innerHTML = "This well is not yet being built.";
							} else {
								$('well_content_'+res).innerHTML = "This well is not yet being built.";
							}
							req=null;
	
						}
					}
	
					req.send(null);
				}				
				return true;
		}

