// Copyright (C) 2008 Manuel F. <mf@find-the-flow.com>
//
// 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, 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.
var scriptURL="http://userscripts.org/scripts/show/25740";

function TT(ttBgColor,ttFadeinTime,ttFadeoutTime,maxxTTLength,LINKTITLES,ALTTITLES,titleTypes,GM_VERSION){
	this.TT_Version=0.2;
	this.GM_Version=GM_VERSION;
	this.ttBgColor=ttBgColor;
	this.ttFadeinTime=ttFadeinTime;
	this.ttFadeoutTime=ttFadeoutTime;
	this.maxxTTLength=maxxTTLength;
	this.LINKTITLES=LINKTITLES;	
	this.ALTTITLES=ALTTITLES;
	this.allTitles=getElements(document,titleTypes,'title');	// get selected (all) elements with a "title"-attribute!
	this.allLinks=getElements(document,'a','href');		// get all links with a "href"-attribute!
	this.allImages=getElements(document,'img','alt');		// get all images with an "alt"-attribute!
	this.allIframes=getElements(document,'iframe');		// just get all document's iframes (not recursively though)!
	this.checkVersion();
	this.injectTitleTooltips();
}


TT.prototype.checkVersion=function(){
	if(this.GM_Version<this.TT_Version&&window.location!=scriptURL&&!getCookie('TT_noupdate')){
		var update=confirm("New version (Version "+this.TT_Version+") of Advanced Title Tooltips available. Do you like to upgrade?");
		if(update){
			setCookie('TT_noupdate','true',1,'/');	//Block updates for one day
			window.location=scriptURL;
		}else{
			setCookie('TT_noupdate','true',7,'/');	//Block updates for 7 days!
		}
	}
}

TT.prototype.injectTitleTooltips=function(){
	for(var i=0;i<this.allIframes.length;i++){
		try{
			var iFramedocument=this.allIframes[i].contentDocument;
			this.getIframeTitles(iFramedocument);
		}catch(err){/*alert(err);*/ }
	}
	if(this.LINKTITLES){
		for(var i=0;i<this.allLinks.length;i++){
			try{
				var hasImageContent=this.allLinks[i].getElementsByTagName("img").length>0;
				var linkTxt=this.allLinks[i].getAttributeNode('href')?this.allLinks[i].getAttributeNode('href').cloneNode(false).value:null;
				if(!this.allLinks[i].title&&!hasImageContent&&linkTxt){
					this.allLinks[i].setAttribute("title",linkTxt);
					this.allTitles.push(this.allLinks[i]);
				}
			}catch(err){/*alert(err);*/ }
		}
	}
	if(this.ALTTITLES){
		for(var i=0;i<this.allImages.length;i++){
			try{
				var altTxt=this.allImages[i].getAttributeNode('alt')?this.allImages[i].getAttributeNode('alt').cloneNode(false).value:null;
				if(!this.allImages[i].title&&altTxt!=null){
					this.allImages[i].setAttribute("title",altTxt);
					this.allTitles.push(this.allImages[i]);
				}
			}catch(err){/*alert(err);*/ }
		}
	}
	for(var i=0;i<this.allTitles.length;i++){
		try{
			var titleAtt=this.allTitles[i].title;
			if(titleAtt){
				var ttText=this.allTitles[i].getAttributeNode("title").cloneNode(false).value;
				if(this.allTitles[i].title!=''){
					var ttText=this.allTitles[i].getAttributeNode('title').cloneNode(false).value;
					if(ttText.length>=this.maxxTTLength)ttText=ttText.splitHTMLText(this.maxxTTLength);
					this.allTitles[i].setAttribute('tttitle',ttText);
					this.allTitles[i].title="";
				}
				this.allTitles[i].setAttribute("onmouseover","this.setTT();"
				+"Tip(this.getAttribute('tttitle'),FADEIN,"
				+this.ttFadeinTime+",FADEOUT,"
				+this.ttFadeoutTime+",BGCOLOR, '"
				+this.ttBgColor+"',SHADOW,true,SHADOWWIDTH,2);");
				this.allTitles[i].setAttribute("onmouseout","UnTip();");
			}
		}catch(err){/*alert(err);*/ }
	}
}

TT.prototype.getIframeTitles=function(doc){
	var iFrameTitles=getElements(doc,'body/*','title');
	var iFrameLinks=getElements(doc,'a','href');
	var iFrameImages=getElements(doc,'img','alt');
	var iFrameIframes=getElements(doc,'iframe');	// just get all iframe's iframes!
	for(var j=0;j<iFrameTitles.length;j++){
		this.allTitles.push(iFrameTitles[j]);
	}
	if(this.LINKTITLES){
		for(var k=0;k<iFrameLinks.length;k++){
			this.allLinks.push(iFrameLinks[k]);
		}
	}
	if(this.ALTTITLES){
		for(var i=0;i<iFrameImages.length;i++){
			this.allImages.push(iFrameImages[i]);
		}
	}
	for(var i=0;i<iFrameImages.length;i++){
		if(iFrameIframes[i].contentDocument){
			this.getIframeTitles(iFrameIframes[i].contentDocument);
		}
		//alert(iFrameImages[0].contentDocument.body.innerHTML);
	}
}
/* needed for iframe's contentDocument! */
function getElements(doc,nodeName,attType,attName,xPathExpr1,strToFind,xPathExprSuffix){
	var xPathExpr="//";
	if(nodeName)xPathExpr+=nodeName;
	if(attType&&!attName)
		xPathExpr+="[starts-with(@"+attType+",*)]";
	else if(attType&&attName)
		xPathExpr+="[starts-with(@"+attType+",'"+attName+"')]";
	if(xPathExpr1&&strToFind)
		xPathExpr+="[contains("+xPathExpr1+",'"+strToFind+"')]";
	if(xPathExprSuffix)
		xPathExpr+=xPathExprSuffix;
	try{
		var xPathEval=document.evaluate(xPathExpr, doc, null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	}catch(exception){}
	var result=new Array;
	if(xPathEval){
		for (var i = 0; i < xPathEval.snapshotLength; i++) {
			result.push(xPathEval.snapshotItem(i));	// push it!
		}
	}
	return result;
}