/***********************************************************************
 *
 * SOFTWARE
 *  AdSpurt - Keyword Advertising System 
 *
 * PAGE
 *  track.js
 *
 * DESCRIPTION
 *	 Javascript Keyword Ad Parser
 *
 * COPYRIGHT 
 *	(c) 2008, David Ian Bennett/Paul Freedman/Frank Haywood. 
 *	All rights reserved.
 *
 * AUTHOR
 *   Paul Freedman
 *
 ***********************************************************************/

var pca = "";
var pba = "";
var rt = "";
var img = null;
var imgt = null;

Array.prototype.internalCounter = 0;
Array.prototype.put=function(value)
{
    this[this.internalCounter]=value;
    this.internalCounter++;
};

function IQGetPageCoordsObj(obj)
{
    var _3={x:0,y:0};
    do
    {
        _3.x+=obj.offsetLeft;
        _3.y+=obj.offsetTop;
    }while((obj=obj.offsetParent));
    return _3;
}

function fpy(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    }
    else if(obj.y)
    {
        curtop += obj.y;
    }
    return curtop;
}

function getSt()
{
    var st;
    
    if (document.all)
    {
        if (document.documentElement && document.documentElement.scrollTop > 0)
        {
            st = document.documentElement.scrollTop;
        }
        else
        {
            st = document.body.scrollTop;
        }
    }
    else
    {
        st = window.pageYOffset;
    }
    
    //alert ("st="+st);
    return(st);
}

function getSl()
{
    var sl;
    
    if (document.all)
    {
        if (document.documentElement && document.documentElement.scrollLeft > 0)
        {
            sl = document.documentElement.scrollLeft;
        }
        else
        {
            sl = document.body.scrollLeft;
        }
    }
    else
    {
        sl = window.pageXOffset;
    }
    
    //alert ("sl="+sl);
    return(sl);
}

function dy(e)
{
    var py = 0;
	if (!e) var e = window.event;
	if (e.pageY)
	{
		py = e.pageY;
	}
	else if (e.clientY)
	{
		py = e.clientY;
	}
	
	return (py);
}

function act(obj, event, value2, pca, ct)
{
    //alert('fpy-getst='+(fpy(obj)-getSt()) + ',cliy=' + event.clientY + ',dy=' + dy(event) + ',ost='+"+div.offsetTop+"+',val2='+"+value2+"+',lhs='+(event.clientY-(fpy(obj)-getSt())+',rhs='+"+(value2-30)+"));
    var lhs = parseInt(event.clientY-(fpy(obj)-getSt()));
    var rhs = parseInt(value2-30);
    
    //alert (lhs + ", " + rhs);
    return((lhs > rhs) ? pca : ct);
}

var jsLinkParser=
{
    cache:new Array(),style:function()
    {
        this.cssText=false;
        this.cssSingleText=false;
        this.textAlign=false;
        this.verticalAlign=false;
        this.border=false;
        this.borderTop=false;
        this.borderBottom=false;
        this.borderLeft=false;
        this.borderRight=false;
        this.backgroundColor=false;
        this.backgroundImage=false;
        this.backgroundRepeat=false;
        this.backgroundPosition=false;
        this.textAlign=false;
        this.fontSize=false;
        this.font=false;
        this.width=false;
        this.height=false;
        this.position=false;
        this.textDecoration=false;
        this.color=false;
        this.top=false;
        this.left=false;
        this.right=false;
        this.bottom=false;
        this.cursor=false;
        this.zIndex=false;
        this.display=false;
        this.overflow=false;
        this.padding=false;
        this.paddingTop=false;
        this.paddingBottom=false;
        this.paddingLeft=false;
        this.paddingRight=false;
        this.margin=false;
        this.marginTop=false;
        this.marginBottom=false;
        this.marginLeft=false;
        this.marginRight=false;
        this.listStyle=false;
        this.usefloat=false;
        this.scaleUnit="px";
        this.html=function()
        {
            var s="";
            if(this.cssText)
            {
                s+=this.cssText;
            }
//            if(this.cssSingleText)
//            {
//                s+=this.cssSingleText;
//            }

            if(this.verticalAlign)
            {
                s+="vertical-align:"+this.verticalAlign+";";
            }
            if(this.backgroundImage)
            {
                s+="background-image:url("+this.backgroundImage+");";
            }
            if(this.backgroundRepeat)
            {
                s+="background-repeat:"+this.backgroundRepeat+";";
            }
            if(this.backgroundPosition)
            {
                s+="background-position:"+this.backgroundPosition+";";
            }
            if(this.usefloat)
            {
                s+="float:"+this.usefloat+";";
            }
            if(this.listStyle)
            {
                s+="list-style:"+this.listStyle+";";
            }
            if(this.borderTop)
            {
                s+="border-top:"+this.borderTop+";";
            }
            if(this.borderBottom)
            {
                s+="border-bottom:"+this.borderBottom+";";
            }
            if(this.borderLeft)
            {
                s+="border-left:"+this.borderLeft+";";
            }
            if(this.borderRight)
            {
                s+="border-right:"+this.borderRight+";";
            }
            if(this.padding)
            {
                s+="padding:"+this.padding+";";
            }
            if(this.paddingTop)
            {
                s+="padding-top:"+this.paddingTop+this.scaleUnit+";";
            }
            if(this.paddingBottom)
            {
                s+="padding-bottom:"+this.paddingBottom+this.scaleUnit+";";
            }
            if(this.paddingLeft)
            {
                s+="padding-left:"+this.paddingLeft+this.scaleUnit+";";
            }
            if(this.paddingRight)
            {
                s+="padding-right:"+this.paddingRight+this.scaleUnit+";";
            }
            if(this.margin)
            {
                s+="margin:"+this.margin+";";
            }
            if(this.marginTop)
            {
                s+="margin-top:"+this.marginTop+this.scaleUnit+";";
            }
            if(this.marginBottom)
            {
                s+="margin-bottom:"+this.marginBottom+this.scaleUnit+";";
            }
            if(this.marginLeft)
            {
                s+="margin-left:"+this.marginLeft+this.scaleUnit+";";
            }
            if(this.marginRight)
            {
                s+="margin-right:"+this.marginRight+this.scaleUnit+";";
            }
            if(this.display)
            {
                s+="display:"+this.display+";";
            }
            if(this.overflow)
            {
                s+="overflow:"+this.overflow+";";
            }
            if(this.zIndex)
            {
                s+="z-index:"+this.zIndex+";";
            }
            if(this.font)
            {
                s+="font-family:"+this.font+";";
            }
            if(this.border)
            {
                s+="border:"+this.border+";";
            }
            if(this.backgroundColor)
            {
                s+="background-color:"+this.backgroundColor+";";
            }
            if(this.textAlign)
            {
                s+="text-align:"+this.textAlign+";";
            }
            if(this.fontSize)
            {
                s+="font-size:"+this.fontSize+this.scaleUnit+";";
            }
            if(this.width)
            {
                s+="width:"+this.width+this.scaleUnit+";";
            }
            if(this.height)
            {
                s+="height:"+this.height+this.scaleUnit+";";
            }
            if(this.position)
            {
                s+="position:"+this.position+";";
            }
            if(this.textDecoration)
            {
                s+="text-decoration:"+this.textDecoration+";";
            }
            if(this.color)
            {
                s+="color:"+this.color+";";
            }
            if(this.top)
            {
                s+="top:"+this.top+this.scaleUnit+";";
            }
            if(this.left)
            {
                s+="left:"+this.left+this.scaleUnit+";";
            }
            if(this.right)
            {
                s+="right:"+this.right+this.scaleUnit+";";
            }
            if(this.bottom)
            {
                s+="bottom:"+this.bottom+this.scaleUnit+";";
            }
            if(this.cursor)
            {
                s+="cursor:"+this.cursor+";";
            }
            return s;
        };
    },obj:function(_5)
    {
        this.prefix=_5;
        this.relativePath="";
        this.browser=jsLinkParser.browser();
        this.style=new jsLinkParser.style;
        this.popup={parent:this,style:new jsLinkParser.style,title:{style:new jsLinkParser.style},alink:{style:new jsLinkParser.style},cache:[],timer:2000,create:function(_6){var _7=document.createElement("DIV");
        document.body.appendChild(_7);
        var _8 = "";
        if (this.parent.searchKeys.isdoubleunderline[_6] == "1")
        {
            _8+="<div id=\"jsLinkParser_popup_"+this.parent.prefix+"_title\" style=\""+this.parent.popup.title.style.html()+"\">"+this.parent.searchKeys.Title[_6]+"</div>";
            _8+="<div><a target=\"_blank\" style=\""+this.parent.popup.alink.style.html()+"\" id=\"jsLinkParser_popup_"+this.parent.prefix+"_alink\" href=\""+this.parent.searchKeys.ADSSERVERURL[_6]+"stats.php?url="+this.parent.searchKeys.URL[_6]+"&keyword="+this.parent.searchKeys.KeyWord[_6]+"\">"+this.parent.searchKeys.DISPLAYURL[_6]+"</a></div>";
        }
        _7.innerHTML=_8;
        return _7;
    },over:function(e)
    {
        if(!e)
        {
            e=window.event;
        }
        var _a=window.event?e.srcElement:e.target;
        if(!_a)
        {
            return false;
        }
        var _b=jsLinkParser.cache[String(_a.className).split("_")[1]];
        if (_b.searchKeys.isdoubleunderline[parseInt(_a.id)] == "0")
        {
            return;
        }    
        var _c=IQGetPageCoordsObj(_a);
        var _f = 0;
        
        if (document.all)
        {
            if (document.documentElement && document.documentElement.offsetWidth > 0)
            {
                _f = document.documentElement.offsetWidth;
            }
            else
            {
                _f = document.body.clientWidth;
            }
        }
        else if(document.documentElement)
        {
            _f=document.documentElement.clientWidth;
        }
        
        var div=_b.popup.create(_a.id);
        div.style.cssText=_b.popup.style.html();
        div.style.position="absolute";
        //alert (div.style.backgroundImage );
        div.style.backgroundImage = "url('" + rt + "/images/balloon_ad.png')";
        div.id="jsLinkParser_popup_"+_b.prefix+"_"+_a.id;
        var value1=337;//parseInt(div.offsetWidth);
        var value2=195;//parseInt(div.offsetHeight);
        var value3=_c.x+value1+80;
        var value4=_c.y-value2-getSt();
        //alert (value3 + ", " + _f);
        if(value3>_f)
        {
            var x=(_c.x-220);
        }
        else
        {
            var x=(_c.x+20);
        }
        if(value4>0)
        {
            var y=(_c.y-value2);
        }
        if(value4<0)
        {
            var y=(_c.y+20);
        }
        
        div.style.left=x+"px";
        div.style.top=y+"px";
        var pcode = "<div id=\"aslink\" style=\"font-family:verdana,arial;font-size:9px;position:absolute;width:"+(value1-parseInt(div.style.paddingLeft)-parseInt(div.style.paddingRight))+"px;text-align:center;top:"+(value2-35)+"px;\">"+pba+"</div>";
        div.innerHTML+=pcode+"<img src=\""+_b.searchKeys.ADSSERVERURL[parseInt(_a.id)]+"stats.php?type=display&adID="+_b.searchKeys.adID[parseInt(_a.id)]+"&atype="+_b.searchKeys.addType[parseInt(_a.id)]+"&keyword="+_b.searchKeys.KeyWord[parseInt(_a.id)]+"\" width=\"1\" height=\"1\">";
        div.innerHTML+="<img onclick=\"window.open(act(this, event, "+value2+",'"+pca+"', '"+_b.searchKeys.ADSSERVERURL[parseInt(_a.id)]+"stats.php?type=clicks&adID="+_b.searchKeys.adID[parseInt(_a.id)]+"&atype="+_b.searchKeys.addType[parseInt(_a.id)]+"&url="+_b.searchKeys.URL[parseInt(_a.id)]+"&keyword="+_b.searchKeys.KeyWord[parseInt(_a.id)]+"'));\" onmouseout=\"jsLinkParser.cache['"+_b.prefix+"'].popup.out(this)\" src=\""+imgt.src+"\" style=\"position:absolute;top:0px;left:0px;cursor:pointer;\" width=\""+value1+"\" height=\""+value2+"\" />";
        div.onmouseover=function(e)
        {
            if(!e)
            {
                e=window.event;
            }
            var obj=e.srcElement||e.target;
            if(!obj)
            {
                return false;
            }
            e.cancelBubble=true;
            var valueb=(obj.id==""?obj.parentNode.id:obj.id).split("_")[2];
            if(!valueb)
            {
                return false;
            }
            var dom=jsLinkParser.cache[valueb];
            var valued=dom.popup.cache.pop();
            if(valued)
            {
                window.clearInterval(valued.time);
            }
            var arr={time:false,obj:div.firstChild};
            dom.popup.cache.push(arr);
        };
        var valuef=_b.popup.cache.pop();
        if(valuef)
        {
            window.clearInterval(valuef.time);
            if(!valuef.obj.parentNode)
            {
                //alert(1);
            }
            valuef.obj.parentNode.removeChild(valuef.obj);
        }
        var arr={time:window.setTimeout("jsLinkParser.cache['"+_b.prefix+"'].popup.timeoutOut(jsLinkParser.cache['"+_b.prefix+"']);",_b.popup.timer),obj:div};
        _b.popup.cache.push(arr);
    },out:function(obj)
    {
        var div=obj.parentNode;
        div.parentNode.removeChild(div);
    },timeoutOut:function(obj)
    {
        var _24=obj.popup.cache.pop();
        if(_24)
        {
            var div=_24.obj;
            if(div)
            {
                div.parentNode.removeChild(div);
            }
            window.clearInterval(_24.time);
        }
    }
};
this.searchKeys={KeyWord:new Array(),Title:new Array(),URL:new Array(),DISPLAYURL:new Array(),adID:new Array(),addType:new Array(),ADSSERVERURL:new Array(),isdoubleunderline:new Array()};
this.css=function()
{
    var css="<style type=\"text/css\">";
    css+=".cssjsLinkParser_"+this.prefix+"_span {"+this.style.html()+"};";
    css+="</style>";
    return css;
};
this.getRelativePath=function(_27)
{
    var _28=document.getElementsByTagName("SCRIPT");
    if(!_28)
    {
        return false;
    }
    var _29=new RegExp(_27,"gi");
    for(var i=0;i<_28.length;i++)
    {
        if(String(_28.item(i).src).match(_29))
        {
            this.relativePath=_28.item(i).src.replace(_29,"");
        }
    }
};
this.execute=function()
{
    if(this.relativePath=="")
    {
        this.getRelativePath("track.js");
    }
    if(this.create)
    {
        this.create();
    }
    this.create=false;
    this.ajax("mode=getKeyWords");
    jsLinkParser.cache[this.prefix]=this;
    };
    this.create=function()
    {
        document.write(this.css());
    };
    this.getKeyWords=function()
    {
        this.searchWord.execute();
    };
    this.searchWord={parent:this,word:false,root:document.body,keywordarr:false,titlearr:false,linkarr:false,displaylinkarr:false,ADSSERVERURLarr:false,isdoubleunderlinearr:false,execute:function()
    {
        this.keywordarr=this.parent.searchKeys.KeyWord;
        this.titlearr=this.parent.searchKeys.Title;
        this.linkarr=this.parent.searchKeys.URL;
        this.displaylinkarr=this.parent.searchKeys.DISPLAYURL;
        this.ADSSERVERURLarr=this.parent.searchKeys.ADSSERVERURL;
        this.isdoubleunderlinearr=this.parent.searchKeys.isdoubleunderline;
        for(var i=0;i<this.keywordarr.length;i++)
        {
            this.findAndReplace(i);
        }
    },findAndReplace:function(_2c)
    {
        if (img == null)
        {
            img = new Image();
            imgt = new Image();
            img.src = rt + "/images/balloon_ad.png";
            imgt.src = rt + "/images/transparent.gif";
        }
        if(!_2c&&_2c!=0)
        {
            return false;
        }
        var _2d=[window.document.documentElement||window.document.body];
        var _2e=new RegExp("\\b" + this.keywordarr[_2c] + "\\b","gi"),span=window.document.createElement("SPAN");
        var i=0,j,length,result,m,n=0,newNode;span.className="cssjsLinkParser_" + this.parent.prefix + "_span";
        span.id=_2c;
        do
        {
            node=_2d[i];
            if (this.isdoubleunderlinearr[_2c] != "1")
            {
                //span.style.borderBottom = "1px solid";
                span.style.borderBottom = "0px";
                span.style.textDecoration = "underline";
                span.setAttribute("onclick", "window.open('" + this.linkarr[_2c] + "');");
            }
            //alert (node.nodeName + ": " + node.nodeType + " = " + node.nodeValue);
            if(node.nodeType===3)
            {
                var _30=node.parentNode;
                if(_30.nodeName=="A" || _30.nodeName=="STYLE")
                {
                    _2e.lastIndex=0;
                }
                else
                {
                    _2e.lastIndex=0;
                    result=_2e.exec(node.nodeValue);
                    if(result!==null)
                    {
                        length=result[0].length;
                        if(_2e.lastIndex>length)
                        {
                            node.splitText(_2e.lastIndex-length);
                            node=node.nextSibling;
                        }
                        if(node.nodeValue.length>length)
                        {
                            node.splitText(length);
                            _2d[i++]=node.nextSibling;
                        }
                        newNode=span.cloneNode(true);
                        newNode.appendChild(window.document.createTextNode(result[0]));
                        n++;
                        node.parentNode.replaceChild(newNode,node);
                        if(this.parent.browser=="IE")
                        {
                            newNode.attachEvent("onmouseover",this.parent.popup.over);
                        }
                        else
                        {
                            newNode.addEventListener("mouseover",this.parent.popup.over,false);
                        }
                    }
                }
            }
            else
            {
                j=node.childNodes.length;
                while(j)
                {
                    _2d[i++]=node.childNodes.item(--j);
                }
            }
        }
        while(i--);
    }
};
this.ajax=function(_31)
{
    var obj=this;
    var _33=function()
    {
        if(xmlRequestObj.readyState==4&&(xmlRequestObj.status==200||xmlRequestObj.status==304))
        {
        //alert (xmlRequestObj.responseText);
            eval(xmlRequestObj.responseText);
        }
    };
    var xmlRequestObj=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    xmlRequestObj.onreadystatechange=_33;
    xmlRequestObj.open("POST",this.relativePath+"parse.php",true);
    xmlRequestObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    var _35=[window.document.documentElement||window.document.body];
    var i=0,j,length,_33,m,n=0,tempDoc,newNode;
    do
    {
        node=_35[i];
        if(node.nodeType===3)
        {
            tempDoc+=node.nodeValue.replace(/\s*$/g," ");
        }
        else
        {
            j=node.childNodes.length;
            while(j)
            {
                _35[i++]=node.childNodes.item(--j);
            }
        }
    }
    while(i--);
    tempDoc.replace(/\s*/g," ");
    _31+="&tempDoc="+escape(tempDoc);
    //alert(_31);
    xmlRequestObj.send(_31);
};
},browser:function()
{
    var _37=new String(navigator.appName);
    var _38=new String(navigator.appVersion);
    if(_37.match("Microsoft Internet Explorer"))
    {
        return String("IE");
    }
    if(_37.match("Netscape"))
    {
        return String("MOZ");
    }
    if(_37.match("Opera"))
    {
        return String("OPERA");
    }
}
};
