window.onload = function() {
	tinyScrolling.init(); bloginfo.init();
};

/* Tiny Scrolling - a smooth navigation between internal links and their destinations
by Marco Rosella - http://www.centralscrutinizer.it/en/design/js-php/tiny-scrolling
based on the works by Travis Beckham and Brian McAllister.
                v0.4 - November 25, 2006
				
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA				
*/

var tinyScrolling = {
	speed : 50,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 150,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedY:0,
	init: function() {
			var lnks = document.getElementsByTagName('a');   
			for(var i = 0, lnk; lnk = lnks[i]; i++) {   
				if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
				('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
				lnk.onclick = tinyScrolling.initScroll;   		
				}   
			}
	},
	getTarget: function(target) {
		while(target.tagName.toLowerCase() != 'a')
			target = target.parentNode;
		return target;
	},
	getElementYpos: function(el){
			var y = 0;
			while(el.offsetParent){  
				y += el.offsetTop    
				el = el.offsetParent;
			}	return y;
	},		
	getScrollTop: function(){
			if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			else return window.pageYOffset;   
	},	
	getWindowHeight: function(){
			if (window.innerHeight)	return window.innerHeight; 
			if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	},
	getDocumentHeight: function(){
			if (document.height) return document.height;
			if(document.body.offsetHeight) return document.body.offsetHeight;
	},
	initScroll: function(e) {
			var targ;  
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;   
   			targ = tinyScrolling.getTarget(targ);
			tinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
			tinyScrolling.currentBlock = document.getElementById(tinyScrolling.hash);   
			if(!tinyScrolling.currentBlock) return;
			tinyScrolling.requestedY = tinyScrolling.getElementYpos(tinyScrolling.currentBlock); 
			tinyScrolling.scroll();  
			return false;
	},
	scroll: function(){
			var top  = tinyScrolling.getScrollTop();
			if(tinyScrolling.requestedY > top) {  
				var endDistance = Math.round((tinyScrolling.getDocumentHeight() - (top + tinyScrolling.getWindowHeight())) / tinyScrolling.brakeK);
				endDistance = Math.min(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK), endDistance);
				var offset = Math.max(2, Math.min(endDistance, tinyScrolling.maxStep));
			} else { var offset = - Math.min(Math.abs(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK)), tinyScrolling.maxStep);
			} window.scrollTo(0, top + offset);  
			if(Math.abs(top-tinyScrolling.requestedY) <= 1 || tinyScrolling.getScrollTop() == top) {
				window.scrollTo(0, tinyScrolling.requestedY);
				if(!document.all || window.opera) location.hash = tinyScrolling.hash;
				tinyScrolling.hash = null;
			} else 	setTimeout(tinyScrolling.scroll,tinyScrolling.speed);
	}	
}


/* Cooltip
yet another tooltip/nice titles version
by Marco Rosella - http://lab.centralscrutinizer.it/cooltip
   v0.1 - January 16, 2006

Based on the works by Stuart Langridge, Paul McLanahan, Peter Janes, Brad Choate, Dunstan Orchard, Ethan Marcotte, Mark Wubben, Alessandro Fulciniti */
  
var cooltip = {
	element : 'container',   	 
	init: function() { 
		var links,i,h;
		if(!document.getElementById || !document.getElementsByTagName) return;
		this.addCss();
		h=document.createElement("span");
		h.id="btc";
		h.setAttribute("id","btc");
		h.style.position="absolute";
		document.getElementsByTagName("body")[0].appendChild(h);
		if(this.element==null) links=document.getElementsByTagName("a");
		else links=document.getElementById(this.element).getElementsByTagName("a");
		/*var lnks = document.getElementsByTagName('a');   
			for(var i = 0, lnk; lnk = lnks[i]; i++) {   
				lnk.onclick = tinyScrolling.initScroll;   		
				}   
			}
*/			
		for(i=0;i<links.length;i++){
		    this.prepare(links[i]);
		}
	}, 
	prepare: function(el){
		var tooltip,t,b,s,l;
		t = el.getAttribute("title");
		if(t == null || t.length == 0) t = "";
		el.removeAttribute("title");
		tooltip=this.createEl("span","tooltip");
		s = this.createEl("span","top");
		s.appendChild(document.createTextNode(t));
		tooltip.appendChild(s);
		b = this.createEl("b","bottom");
		l = el.getAttribute("href");
		if (l == '#') l = '';
		if(l.length>30) l = l.substr(0,27)+"...";
		b.appendChild(document.createTextNode(l));
		tooltip.appendChild(b);
		this.setOpacity(tooltip);
		el.tooltip = tooltip;
		el.onmouseover = this.showCooltip;
		el.onmouseout = this.hideCooltip;
		el.onmousemove = this.Locate;
	},
	showCooltip: function(e){
		document.getElementById("btc").appendChild(this.tooltip);
		//this.Locate(e);
	},
	hideCooltip: function(e){
		var d=document.getElementById("btc");
		if(d.childNodes.length>0) d.removeChild(d.firstChild);
	},
	setOpacity: function(el){
		el.style.filter="alpha(opacity:90)";
		el.style.KHTMLOpacity="0.90";
		el.style.MozOpacity="0.90";
		el.style.opacity="0.90";
	},
	createEl: function(t,c){
		var x=document.createElement(t);
		x.className = c;
		x.style.display = "block";
		return(x);
	},
	addCss: function(){
		var l=this.createEl("link");
		l.setAttribute("type","text/css");
		l.setAttribute("rel","stylesheet");
		l.setAttribute("href","http://localhost/lab/wp-content/themes/lab/js/bt.css");
		l.setAttribute("media","screen");
		document.getElementsByTagName("head")[0].appendChild(l);
	},
	Locate: function(e){
		var posx = 0,posy = 0;
		if(e == null) e = window.event;
		if(e.pageX || e.pageY){
		    posx=e.pageX; posy = e.pageY;
	    }
		else if(e.clientX || e.clientY){
	    if(document.documentElement.scrollTop){
	        posx=e.clientX+document.documentElement.scrollLeft;
	        posy=e.clientY+document.documentElement.scrollTop;
	        }
	    else{
	        posx=e.clientX+document.body.scrollLeft;
	        posy=e.clientY+document.body.scrollTop;
	        }
	    }
		document.getElementById("btc").style.top = (posy+10)+"px";
		document.getElementById("btc").style.left = (posx-20)+"px";
	}
}


/* Technorati's Blog Info v0.4
an experiment by Marco Rosella - November 2005->November 2006
*/

var bloginfo = {
	init : function() {
		if(document.getElementById('bifrm')) {
			var bifrm = document.getElementById('bifrm');
			bifrm.onsubmit = function() {
				bloginfo.getInfo();	
				return false;
			} 
		} else {
		return }
	},
	getInfo : function() {
		if (window.XMLHttpRequest) { 
		xmlhttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) { 
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		document.getElementById('results').innerHTML ='';		
		document.getElementById('loading').innerHTML = '<img src="http://lab.centralscrutinizer.it/wp-content/themes/lab/images/bimg/loader.gif"/>';
		xmlhttp.open('GET', 'http://lab.centralscrutinizer.it/wp-content/themes/lab/bloginfo.php?url='+escape(document.getElementById('blogUrl').value), true);
		xmlhttp.onreadystatechange = this.handle;
		xmlhttp.send(null);
	},
	handle : function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText.indexOf('invalid') == -1) {
				var resp = xmlhttp.responseXML;
			    var u = document.createElement('ul');
				function tag(tagname,child) { 
					if(child) {
					var res = (resp.getElementsByTagName(tagname)[0] && resp.getElementsByTagName(tagname)[0].firstChild) ? resp.getElementsByTagName(tagname)[0].firstChild.nodeValue : 'not indexed'; 
					} else { var res = resp.getElementsByTagName(tagname)[0] ? true : false; }
					return res;
				}
			    document.getElementById('results').appendChild(u);
				var name = tag('name') ? tag('name',true) : 'not indexed';
			    var url = tag('url') ? '<a href=\"'+ tag('url',true)+'">'+tag('url',true)+'</a>' : 'not indexed';				
			    var author = tag('author') ? tag('firstname',true) +' '+ tag('lastname',true) : 'not indexed';
				var inboundlinks = tag('inboundlinks') ? tag('inboundlinks',true) : 'not indexed';
				var inboundblogs = tag('inboundblogs') ? tag('inboundblogs',true) : 'not indexed';
			    var rank = tag('rank') ? tag('rank',true) : 'not indexed';
			    var rssurl = tag('rssurl') ? '<a href=\"'+tag('rssurl',true)+'">'+tag('rssurl',true)+'</a>' : 'not indexed';
			    var atomurl = tag('atomurl') ? '<a href=\"'+tag('atomurl',true)+'">'+tag('atomurl',true)+'</a>' : 'not indexed';
				var lastupdate = tag('lastupdate') ? tag('lastupdate',true) : 'not indexed';
			    var foafurl = tag('foafurl') ? '<a href=\"'+tag('foafurl',true)+'">'+tag('foafurl',true)+'</a>' : 'not indexed';
				u.innerHTML =
				'<li><strong>Blog name:</strong> '+name+'</li>'+
				'<li><strong>URL:</strong> '+url+'</a></li>'+
				'<li><strong>Author:</strong> '+author+'</a></li>'+
				'<li><strong>Inbound links:</strong> '+inboundlinks+'</li>'+
				'<li><strong>Inbound blogs:</strong> '+inboundblogs+'</li>'+
				'<li><strong>Technorati rank:</strong> '+rank+'</li>'+
				'<li><strong>URL RSS:</strong> '+rssurl+'</li>'+
				'<li><strong>URL Atom:</strong> '+atomurl+'</li>'+
				'<li><strong>URL FOAF:</strong> '+foafurl+'</li>'+
				'<li><strong>Last update:</strong> '+lastupdate+'</li>';
			    document.getElementById('loading').innerHTML = 'The results:';
			}
		}
	}	
}

