function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function URL(url){
    if(url.length==0) eval('throw "Invalid URL ['+url+'];');
    this.url=url;
    this.port=-1;
    this.query=(this.url.indexOf('?')>=0)?this.url.substring(this.url.indexOf('?')+1):'';
    if(this.query.indexOf('#')>=0) this.query=this.query.substring(0,this.query.indexOf('#'));
    this.protocol='';
    this.host='';
    var protocolSepIndex=this.url.indexOf('://');
    if(protocolSepIndex>=0){
        this.protocol=this.url.substring(0,protocolSepIndex).toLowerCase();
        this.host=this.url.substring(protocolSepIndex+3);
        if(this.host.indexOf('/')>=0) this.host=this.host.substring(0,this.host.indexOf('/'));
        var atIndex=this.host.indexOf('@');
        if(atIndex>=0){
            var credentials=this.host.substring(0,atIndex);
            var colonIndex=credentials.indexOf(':');
            if(colonIndex>=0){
                this.username=credentials.substring(0,colonIndex);
                this.password=credentials.substring(colonIndex);
            }else{
                this.username=credentials;
            }
            this.host=this.host.substring(atIndex+1);
        }
        var portColonIndex=this.host.indexOf(':');
        if(portColonIndex>=0){
            this.port=this.host.substring(portColonIndex);
            this.host=this.host.substring(0,portColonIndex);
        }
        this.file=this.url.substring(protocolSepIndex+3);
        this.file=this.file.substring(this.file.indexOf('/'));
    }else{
        this.file=this.url;
    }
    if(this.file.indexOf('?')>=0) this.file=this.file.substring(0, this.file.indexOf('?'));
    var refSepIndex=url.indexOf('#');
    if(refSepIndex>=0){
        this.file=this.file.substring(0,refSepIndex);
        this.reference=this.url.substring(this.url.indexOf('#'));
    }else{
        this.reference='';
    }
    this.path=this.file;
    if(this.query.length>0) this.file+='?'+this.query;
    if(this.reference.length>0) this.file+='#'+this.reference;

    this.getPort=getPort;
    this.getQuery=getQuery;
    this.getProtocol=getProtocol;
    this.getHost=getHost;
    this.getUserName=getUserName;
    this.getPassword=getPassword;
    this.getFile=getFile;
    this.getReference=getReference;
    this.getPath=getPath;
    this.getArgumentValue=getArgumentValue;
    this.getArgumentValues=getArgumentValues;
    this.toString=toString;

    function getPort(){
        return this.port;
    }

    function getQuery(){
        return this.query;
    }

    function getProtocol(){
        return this.protocol;
    }

    function getHost(){
        return this.host;
    }

    function getUserName(){
        return this.username;
    }

    function getPassword(){
        return this.password;
    }

    function getFile(){
        return this.file;
    }

    function getReference(){
        return this.reference;
    }

    function getPath(){
        return this.path;
    }

    function getArgumentValue(key){
        var a=this.getArgumentValues();
        if(a.length<1) return '';
        for(i=0;i<a.length;i++){
            if(a[i][0]==key) return a[i][1];
        }
        return '';
    }

    function getArgumentValues()
    {
        var a=new Array();
        var b=this.query.split('&');
        var c='';
        if(b.length<1)
        {
            return a;
        }
        for(i=0;i<b.length;i++)
        {
            c=b[i].split('=');
            a[i]=new Array(c[0],((c.length==1)?"":c[1]));
        }
        return a;
    }

    function toString(){
        return this.url;
    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////

var baseURL = "http://www.mp4playersblog.com/todb.php";
var baseCookieName = "SmartLEAF_TRACKER";
var baseSiteGUID = "356a192b7913b04c54574d18c28d46e6395428ab";

function addURLParam(sURL, sParamName, sParamValue)
{
    sURL += (sURL.indexOf("?") == -1 ? "?" : "&");
    sURL += encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
    return sURL;
}

function isVirgin()
{
    var result = true;

    if (getCookie(baseCookieName))
    {
        result = false;
    }

    return result;
}

function processGoogle(what)
{
    var url = baseURL;

    url = addURLParam(url, "LEAF_referer", what.toString());
    url = addURLParam(url, "LEAF_engine", "Google");
    url = addURLParam(url, "LEAF_keywords", what.getArgumentValue("q"));

    return url;
}

function processYahoo(what)
{
    var url = baseURL;

    url = addURLParam(url, "LEAF_referer", what.toString());
    url = addURLParam(url, "LEAF_engine", "Yahoo");
    url = addURLParam(url, "LEAF_keywords", what.getArgumentValue("p"));

    return url;
}

function processMSN(what)
{
    var url = baseURL;

    url = addURLParam(url, "LEAF_referer", what.toString());
    url = addURLParam(url, "LEAF_engine", "MSN");
    url = addURLParam(url, "LEAF_keywords", what.getArgumentValue("q"));

    return url;
}

function processLive(what)
{
    var url = baseURL;

    url = addURLParam(url, "LEAF_referer", what.toString());
    url = addURLParam(url, "LEAF_engine", "Live");
    url = addURLParam(url, "LEAF_keywords", what.getArgumentValue("q"));

    return url;
}

function processOther(what)
{
    var url = baseURL;

    url = addURLParam(url, "LEAF_referer", what.toString());

    return url;
}

function processReferer(what)
{
    if (what.length == 0)
    {
        what = "";
        var url = "none";
        var hostName = "";
    }
    else
    {
        var url = new URL(what.toLowerCase());
        var hostName = url.getHost();
    }

    var picURL = "";
    var meHostName = document.domain;

    if (hostName.indexOf("google") != -1)
    {
        picURL = processGoogle(url);
    }
    else if (hostName.indexOf("yahoo") != -1)
    {
        picURL = processYahoo(url);
    }
    else if (hostName.indexOf("msn") != -1)
    {
        picURL = processMSN(url);
    }
    else if (hostName.indexOf("live") != -1)
    {
        picURL = processLive(url);
    }
    else
    {
        picURL = processOther(url);
    }

    picURL = addURLParam(picURL, "LEAF_page", document.location);

    if (isVirgin())
    {
        picURL = addURLParam(picURL, "LEAF_visits", "0"); // il trimit cu 0 pentru ca e prima oara cand intra si trebuie sa marchez chestia asta cumva dupa care ii setez cookie ca a intrat odata, fiindca, evident, a intrat odata :)
        var nrVisits = 1;
    }
    else
    {
        var nrVisits = getCookie(baseCookieName);
        nrVisits++;
        picURL = addURLParam(picURL, "LEAF_visits", nrVisits);
    }

    picURL = addURLParam(picURL, "LEAF_guid", baseSiteGUID);

    var expDate = new Date(new Date().getTime() + (7*24*60*1000)); // teoretic, expira cam in 7 zile :) practic? habar nu am
    setCookie(baseCookieName, nrVisits, expDate, "/", meHostName);

    document.write('<img style="display:none" border=0 hspace=0 vspace=0 width=1 height=1 src="'+picURL+'" alt="" />');
}