var itx_x;
var itx_y;

function itx_mouse(e) {
	var IE=(navigator.appName!='Netscape');
	if (!IE) {
		itx_x = e.clientX;
		itx_y = e.clientY;
	}
	else
	{
		itx_x = event.clientX + document.body.scrollLeft;
		itx_y = event.clientY + document.body.scrollTop; 
	}
}
/*
function getPositionX(obj){
    leftValue= 0;
    while(obj){
	leftValue+= obj.offsetLeft;
	obj= obj.offsetParent;
    }
    return leftValue;
}
function getPositionY(obj){
    var topValue= 0;
    while(obj){
	topValue+= obj.offsetTop;
	obj= obj.offsetParent;
    }
	return topValue;
}
*/

function intext(src){
	var box = src.lastChild;
	src.className = 'itx-intext over hidden';
	var itx_wscreen = 0;
	var itx_hscreen = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		itx_wscreen = window.innerWidth; itx_hscreen = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
		itx_wscreen = document.documentElement.clientWidth; itx_hscreen = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		itx_wscreen = document.body.clientWidth; itx_hscreen = document.body.clientHeight;
	}
	box.style.width = itx_width + 8;
	src.style.position = 'relative';
	var itx_cordX = src.offsetLeft;
	var itx_cordY = src.offsetTop;
	var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox/') > -1;  
	/*if (is_firefox ) 
	{
			var itx_cordX = getPositionX(src);
			var itx_cordY = getPositionY(src);
	}
		else
	{
			var itx_cordX = src.offsetLeft;
			var itx_cordY = src.offsetTop;
	}
	*/
	var itx_cordX = src.offsetLeft;
	var itx_cordY = src.offsetTop;
	src.style.position = '';
	if (itx_wscreen-itx_x < box.offsetWidth && itx_hscreen-itx_y < box.offsetHeight) 
			{
			box.style.top = itx_cordY -box.offsetHeight +"px";
			box.style.left = itx_cordX -box.offsetWidth +src.offsetWidth +"px";
			}
		else if (itx_wscreen-itx_x < box.offsetWidth)
				{
				box.style.top = itx_cordY +src.offsetHeight +"px";
				box.style.left = itx_cordX -box.offsetWidth +src.offsetWidth +"px";
				}
				else if (itx_hscreen-itx_y < box.offsetHeight)
					{
					box.style.top = itx_cordY -box.offsetHeight +"px";
					box.style.left = itx_cordX +"px";
					}
				else
					{
					box.style.top = itx_cordY +src.offsetHeight +"px";
					box.style.left = itx_cordX +"px";
					}
	src.className = 'itx-intext over';
}
function intextClose(src){
	src.className = 'itx-intext';	
}


//---------------------------------------------------------------------------------------------------------------
// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
var itx_ua = navigator.userAgent.toLowerCase();
if (itx_ua.indexOf(" chrome/") >= 0 || itx_ua.indexOf(" firefox/") >= 0 || itx_ua.indexOf(' gecko/') >= 0) {
	var itxStringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var itxStringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}


var itx_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function itx_encode64(input) {
	var output = new itxStringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		output.append(itx_keyStr.charAt(enc1) + itx_keyStr.charAt(enc2) + itx_keyStr.charAt(enc3) + itx_keyStr.charAt(enc4));
   }
   
   return output.toString();
}

function itx_decode64(input) {
	var output = new itxStringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	while (i < input.length) {
		enc1 = itx_keyStr.indexOf(input.charAt(i++));
		enc2 = itx_keyStr.indexOf(input.charAt(i++));
		enc3 = itx_keyStr.indexOf(input.charAt(i++));
		enc4 = itx_keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output.append(String.fromCharCode(chr1));

		if (enc3 != 64) {
			output.append(String.fromCharCode(chr2));
		}
		if (enc4 != 64) {
			output.append(String.fromCharCode(chr3));
		}
	}

	return output.toString();
}

function itxGetElementsByClassName(class_name) {
    var docList = document.all || document.getElementsByTagName('*');
    var matchArray = new Array();

	/*Create a regular expression object for class*/
    var re1 = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
    for (var i = 0; i < docList.length; i++) {
        if (re1.test(docList[i].className))  {
            matchArray[matchArray.length] = docList[i];
        }
	}

    return matchArray;
}//eof cstmGetElementsByClassName


var itx_global_counter = 0;
var itx_found = false;
var itx_replace_counter = 0;


function itx_replaceKeywords()
{
	var targetElements = new Array();
	
	var itx_unencoded_ad = itx_decode64(itx_ad);
	itx_unencoded_ad = itx_unencoded_ad.replace("#PROFILE#", itx_profile);
	itx_unencoded_ad = itx_unencoded_ad.replace("#AFFILIATE#", itx_source);
		
	if(itx_replace_class_name != "")
	{
		if(!document.getElementsByClassName)
		{
			targetElements = itxGetElementsByClassName(itx_replace_class_name);
		}
		else
		{
			targetElements = document.getElementsByClassName(itx_replace_class_name);
		}
	}
	else if(itx_replace_id != "")
	{
		targetElement = document.getElementById(itx_replace_id);
		if(targetElement != null)
		{
			targetElements[0] = targetElement;
		}
	}
	
	
	for(var i = 0; i < targetElements.length; i++)
	{
		for(var j = 0; j < itx_keywords.length; j++)
		{
			itx_counter = 0;
			itx_replace_counter = 0;
			itx_found = false;
			if(itx_global_counter < itx_max)
			{
				itx_findAndCount(itx_keywords[j], targetElements[i]);
				itx_findAndReplace(itx_keywords[j], itx_decode64(itx_pre1) + '$&' + itx_decode64(itx_pre2) + itx_unencoded_ad + itx_decode64(itx_post), targetElements[i]);
				if(itx_found == true)
				{
					itx_global_counter++;
				}
				
			}
			else
			{
				return;
			}
		}
	}
	
}

function itx_findAndReplace(searchText, replacement, searchNode) {
		
    if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'i') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
	
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1  &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
			regex.test(currentNode.data);
            continue;
        }
		else
		{
			itx_replace_counter++;
		}
		
        var parent = currentNode.parentNode,
            frag = (function(){
                var html;
				if(itx_replace_counter < itx_counter)
				{
					html = currentNode.data,
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
					wrap.innerHTML = html;
					wrap.innerHTML = "&nbsp;" + html;
				}
				else
				{
					html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
					wrap.innerHTML = "&nbsp;" + html;
				}
                
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}


function itx_findAndCount(searchText, searchNode) {
		
   
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'i') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
	
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1  &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
			regex.test(currentNode.data);
            continue;
        }
		else
		{
			itx_counter++;
			itx_found = true;
		}
		
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data,
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML =  html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}



function itx_onload()
{
    // Insert your code here to initialize your program
    // Then, this little bit calls the old onload function
	
	itx_replaceKeywords();
    
    if (itx_old_onload != null)
    {
       itx_old_onload();
    }
}

itx_old_onload = window.onload;
window.onload = itx_onload;
document.onmousemove=itx_mouse;

	



