
// Define the global variables
var LB_uid_338d9c = 'c85abc51';
var LB_excl_338d9c = 'http://zonakaoz.org,http://www.zonakaoz.org';
var LB_type_338d9c = 3;
var LB_targ_338d9c = 1;
var LB_freq_338d9c = 0;

// Add a startsWith() function to the string object
String.prototype.startsWith_c04d18 = function(e) {

    // Process either an array of items or single item
    if (typeof e == "object")
    {
        // Return true if any items in the array are found 
        for (_i = 0; _i < e.length; _i++)
        {
            if (this.toLowerCase().indexOf(e[_i].toLowerCase()) == 0)
                return true;
        }
        return false;
    }
    else
    {
        return (this.toLowerCase().indexOf(e.toLowerCase()) == 0);
    }
};

// Add a matchesWith() function to the string object
String.prototype.matchesWith_c04d18 = function(e) {

    // Process either an array of items or single item
    if (typeof e == "object")
    {
        // Return true if any items in the array are found 
        for (_i = 0; _i < e.length; _i++)
        {
            if (this.toLowerCase().indexOf(e[_i].toLowerCase()) > -1)
                return true;
        }
        return false;
    }
    else
    {
        return (this.toLowerCase().indexOf(e.toLowerCase()) > -1);
    }
};

function addHandler_c04d18(obj, eventName, handler)
{
    if (obj.addEventListener)
        obj.addEventListener(eventName, function(e) { eval(handler) }, false);
    else if (obj.attachEvent)
        obj.attachEvent("on" + eventName, function(e) { eval(handler) });
    else
        obj["on" + eventName] = function(e) { eval(handler) };
}

function formatExcl_c04d18(exclusions)
{
    var excl_array = exclusions.split(",");
    var star_pos = -1;
    
    for (i = 0; i < excl_array.length; i++)
    {
        star_pos = excl_array[i].indexOf("*");

        if (star_pos > -1) {
            excl_array[i] = excl_array[i].substring(star_pos+1);
        }
    }
    
    return excl_array;
}

function convertToUnicode_c04d18(source) {
    result = '';
    for (i = 0; i < source.length; i++) {
        result += '&#' + source.charCodeAt(i);
    }
    return result;
}

function convertToHex_c04d18(num) {
    var hex = '';
    for (i = 0; i < num.length; i++) {
        if (num.charCodeAt(i).toString(16).toUpperCase().length < 2) {
            hex += "%0" + num.charCodeAt(i).toString(16).toUpperCase();
        } else {
            hex += "%" + num.charCodeAt(i).toString(16).toUpperCase();
        }
    }
    return hex;
}

function createCookie_c04d18(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie_c04d18(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function retrieveClicks_c04d18()
{
    var cookieName = 'lb_ctl';
    var totalCount = readCookie_c04d18(cookieName);
    
    if (totalCount != null)
        totalCount = parseInt(totalCount);
    else
	    totalCount = 0;

    return totalCount;
}

function IncrementClick_c04d18() {
    var cookieName = 'lb_ctl';
    var totalCount = readCookie_c04d18(cookieName);

    if (totalCount != null)
        createCookie_c04d18(cookieName, parseInt(totalCount) + 1, 1);
    else
        createCookie_c04d18(cookieName, 1, 1);
}

// Init the Loader
function initLoader_c04d18()
{
    if (LB_uid_338d9c.length == 0)
    {
        if (typeof lb_params != "undefined" && lb_params[0] != null)
            LB_uid_338d9c = lb_params[0];
        else if (typeof uid != "undefined")
            LB_uid_338d9c = uid;
        
        if (LB_uid_338d9c.length != 0)
            document.write('<SCR' + 'IPT type="text/javascript" src="http://www.linkbucks.com/WebServices/jsParseLinks.aspx?id=' + LB_uid_338d9c + '&random=' + Math.floor(89999999 * Math.random() + 10000000) + '&millis=' + new Date().getTime() + '"></SCR' + 'IPT>');
    }
    else if (LB_type_338d9c != 0)
    {
        // Check for link freqency cap then attach to the load event
        if (LB_freq_338d9c == 0 || retrieveClicks_c04d18() < LB_freq_338d9c)
        {
            onLinksLoad(0,0);
            addHandler_c04d18(window, "load", "processLinks_c04d18()");
        }
    }
}

// Determine if links are finished loading by counting interval between new links
function onLinksLoad(count, tries)
{
    var links = document.getElementsByTagName("A").length;

    if (links != count)
        tries = 0
    else
        tries++;

    if (tries <= 5)
        setTimeout(function() { onLinksLoad(links, tries) }, 100);
    else
        processLinks_c04d18();
}

// Process the links on the page
function processLinks_c04d18()
{
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    
    var excls = formatExcl_c04d18(LB_excl_338d9c);
    var excls2 = new Array(LB_uid_338d9c, convertToUnicode_c04d18(LB_uid_338d9c), convertToHex_c04d18(LB_uid_338d9c));

    var links = document.getElementsByTagName("A");
    var linksrc = "";
    
    var mouseEvent = (LB_type_338d9c == 3) ? "click" : "mousedown";

    // Loop through all the links on the page
    for (i = 0; i < links.length; i++)
    {
        linksrc = links[i].href;
               
        // Only update fully qualified http links
        if (!linksrc.startsWith_c04d18(new Array("http://", "https://")))
            continue;
            
        // Only update links not on the exclusion list
        if (excls[0].length > 0 && linksrc.matchesWith_c04d18(excls))
            continue;

        // Only update links not containing a link id
        if (excls2[0].length > 0 && linksrc.matchesWith_c04d18(excls2))
            continue;
                                
        addHandler_c04d18(links[i], mouseEvent, "linkClicked_c04d18(obj, " + LB_targ_338d9c + ", " + LB_type_338d9c + ", \"" + LB_uid_338d9c + "\")");

    }

}

function linkClicked_c04d18(obj_338d9c, target_338d9c, type_338d9c, uid_338d9c)
{

    // Check the frequency cap each time a link is clicked
    if (LB_freq_338d9c != 0 && retrieveClicks_c04d18() >= LB_freq_338d9c)
        return;
        
    var excls = formatExcl_c04d18(LB_excl_338d9c);
    var excls2 = new Array(LB_uid_338d9c, convertToUnicode_c04d18(LB_uid_338d9c), convertToHex_c04d18(LB_uid_338d9c));

    // Only update fully qualified http links
    if (!obj_338d9c.href.startsWith_c04d18(new Array("http://", "https://")))
        return;
        
    // Only update links not on the exclusion list
    if (excls[0].length > 0 && obj_338d9c.href.matchesWith_c04d18(excls))
        return;

    // Only update links not containing a link id
    if (excls2[0].length > 0 && obj_338d9c.href.matchesWith_c04d18(excls2))
        return;            
        
    if (target_338d9c == 1 || type_338d9c == 3)
        obj_338d9c.target = '_top';
    else if (target_338d9c == 2)
        obj_338d9c.target = '_blank';

    // Code section for popup types
    if (type_338d9c == 3) {

        var pLoaded = false;
        
        if (window.SymRealWinOpen) { open = SymRealWinOpen; }
        if (window.NS_ActualOpen) { open = NS_ActualOpen; }

        var popURL = "http://fa413ca8.linkbucks.com/url/?lurl=http://647467ff.linkbucks.com/url/?lurl=http://c6cf32df.linkbucks.com/url/http://" + uid_338d9c + ".linkbucks.com";
        var pxLeft = 2000;
        var pxTop = 2000;
        var popWidth = 1;
        var popHeight = 1;

       

        var day = new Date();
        var day_id = day.getTime();

        pLoaded = open(popURL, 'LPv2_' + day_id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,top=' + pxTop + ',left=' + pxLeft + ',width=' + popWidth + ',height=' + popHeight);

        if (pLoaded)
        {   
            // Focus the pop window in front of the page
            pLoaded.focus();
       }
         
    } // Code section for regular links
    else {

        var linkURL = "http://" + uid_338d9c + ".linkbucks.com/url/" + obj_338d9c.href;
        var linkName = obj_338d9c.innerHTML;
    
        obj_338d9c.href = linkURL;
        
        if (obj_338d9c.innerHTML != linkName)
            obj_338d9c.innerHTML = linkName;
    }

    IncrementClick_c04d18();

}

initLoader_c04d18();
