
/*

switched to using images instead of backgrounds...


todo list:
rename button.php
add support for postscript
check in other browsers
work out if setting existing text to blank is good idea
add support for direct cache access...
*/

/*
// mozilla inner text
HTMLElement.prototype.__defineGetter__("innerText", function () {
   var r = this.ownerDocument.createRange();
   r.selectNodeContents(this);
   return r.toString();
});
*/


// the image style php script loaction, not sure if this needs to be absolute
//var imageStyleURL = 'http://lgpremium.gest8.com/dynamic_headers/button.php';
var imageStyleURL = '/dynamic_headers/button.php';
// if this is set and useImageStyleCache is true then the script will attempt to load images
// from the cache first using a hash of the parameters
//var imageStyleCache = 'http://lgpremium.gest8.com/dynamic_headers/cache';
var imageStyleCache = '/dynamic_headers/cache';

// set the caching hash type, can be md5 or sha1
// todo: find out which is faster....
var imageStyleCacheHashType = 'md5';
// turn caching on or off (note: not server side caching)
var useImageStyleCache = true;

// 4sh - added this function to fix PNG image transparency in IE 5.5+
function fixPNG(myImage) // correctly handle PNG transparency in Win IE 5.5 or higher.
   {
    if (window.ie55up)  // this var is set in the main HTML template, in the conditional comments
	 {
		// alert("Fixing image: "+myImage);
	 var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	 var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	 var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' "
	 var imgStyle = "display:inline-block;" + myImage.style.cssText 
	 var strNewHTML = "<span " + imgID + imgClass + imgTitle
	 strNewHTML += " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";"
	 strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	 strNewHTML += "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>" 
	 myImage.outerHTML = strNewHTML
	 }
   }

function imageStyleReplaceHeaders(){
	
	//alert("Image preload array length = "+imagePreloadArray.length);
	// loop over all the header tags.. h1 to h6
	// fixme: can reflect on the style sheet, should be faster
	//for(var i=1; i<7; i++) imageStyleReplaceTags('h'+i);

	//var style = imageStyleSheet;
	if (navigator.appVersion != "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)") {  
		for(tagName in imageStyleSheet){
			var style = imageStyleSheet[tagName];
			if(style) imageStyleReplaceTags(tagName,style);
		
		}
	} 
//else {alert("Imagestyle wont work on this browser....Mac IE");}
}

function imageStyleReplaceTags(tagName,style){

	// check we have the tools needed
	if(!document.getElementsByTagName) return;

	// collect all the matching tags
	tags = document.getElementsByTagName(tagName);

	//alert("Number of tags to replace: "+tags.length);

	// loop over them passing each one to image replace tag
	for(var i=0; i<tags.length; i++) imageStyleReplaceTag(tags[i],style);

}

function imageStyleReplaceTag(tag,style){

	

	// get the tagname in lowercase
	var tagName = tag.nodeName.toLowerCase();

	// check to make sure the tag is in the irss, if not return
	//if(!imageStyleSheet[tagName]) return;

	// get the stylesheet rule that matches this tags name
	//var style = imageStyleSheet[tagName];
	
	// extract the taxt from the tag
	var text = tag.innerHTML;

	// build the url for the image
	var _url = imageStyleURL + '?';
	for(prop in style) _url += prop + '=' + style[prop] + '&';
	if(style['reload']) _url += 'nocache=' + ((new Date()).getTime()) + '&';
	
	//document.all.footer.innerHTML = _url;
	if(false&&style.height){
		// no need to preload first as we know the height....

		// set the height 
		tag.style.height = style.height;

		// set the background style for the tag
		tag.style.background = 'url(' + _url + 'text=' + text + ') no-repeat top left';
		tag.style.backgroundColor = 'transparent';

	}
	else {
		// preload the image using an image object first...

		// create an empty image object in the image preload array
		var id = imagePreloadArray.length;
		var img = imagePreloadArray[id] = new Image();
		
		// save some values in the image object 
		img._id = id;
		img._url = _url
		img._text = text;
		img._tag = tag;

		// start the image loading
		img.src = _url + 'text=' + text;
		
		//alert("Tag #"+id+", image source="+img.src);
//document.write(img.src);
		// if the image is already loaded
		if(img.complete){
			//alert('done :)');
			img.doneLoading = imageLoad;
			img.doneLoading();
		}
		else{
			//alert("Image not completed...");
			// set the onload and onabort handlers
			img.onload = imageLoad;
			img.onabort = imageRollback;
			img.onerror = imageRollback;
			
		}
	}

}

var imagePreloadArray = Array();

var imageLoad = function(){
	
	//alert("loading image");
	
	// if somthing has gone wrong call the image abort handler
	if((this.width<1||this.height<1)&&this.onerror) {
		//alert("something went wrong..");
		this.onerror();}

	// should this be done using innerHTML or by creating an element / replacing an element ?
	// 4sh - added the onload event to fix png images in IE5.5+
	//this._tag.innerHTML = '<img src="'+ this._url + 'text=' + escape(this._text) +'" height="'+this.height+'" width="'+this.width+'" alt="'+this._text+'" onload="fixPNG(this)" />';
	// 4sh - removed the escaped text conversion
	this._tag.innerHTML = '<img src="'+ this._url + 'text=' + this._text +'" height="'+this.height+'" width="'+this.width+'" alt="'+this._text+'" onload="fixPNG(this)" />';

	this._tag.style.display = 'block';
	//4sh added next line
this._tag.style.backgroundColor = 'transparent';

	// set the height of the tag to the height of this image
	//this._tag.style.height = this.height;

	// set the background style for the tag
	//this._tag.style.background = 'url(' + this._url + 'text=' + escape(this._text) + ') no-repeat top left';

	// set the reference to this object to null in the preloader
	imagePreloadArray[this._id] = null;

}

var imageRollback = function(){

	// restore the original text
	//alert("aborting..error: "+errno_str);
	//this._tag.childNodes[0].nodeValue = this._text;
	this._tag.innerHTML = this._text;

	// set the reference to this object to null in the preloader
	imagePreloadArray[this._id] = null; 

}


var checkCachingSpeed = function (){
	// just texting the speed of the caching algorithum
	var text = 'this  is a string here i expect the url to be a similar length to this string here oh yeah about this long is good.';
	var start = (new Date()).getTime();
	for(var i=0;i<1000;i++){
		//hex_md4(text);
		//hex_md5(text);
		hex_sha1(text);
	}
	//alert((new Date()).getTime()-start);
}

// if there is already an onload hander set, wrap it

if(window.onload){
	//alert("detected existing onload event - wrapping it...");
	window._onload = window.onload;
	window.onload = onloadWrapper;
}
// otherwise just set it
else window.onload = imageStyleReplaceHeaders;


//window.onload = imageStyleReplaceHeaders;
