var hop;

var placeholder;
if (!placeholder) { placeholder = "xxxxx"; }

// JVPlus settings
var JVPlus = {
   hide : false,     // Hide links?

   owner : "",       // Default ID
   ratio : 0,

   enabled : true,

   setLinks : function(linkList) {
      if (linkList == undefined) {
         linkList = document.links;
      }

      for (var i=0;i<linkList.length;i++) {
         if (!JVPlus.cloakAffiliateLink(linkList[i])) {
            JVPlus.cloakNormalLink(linkList[i]);
         }
      }
   },

   brand : function(layer, customHop) {
      if (customHop == undefined) {
         JVPlus.findHop();

         if (hop) {
            Cookie.set("hop", hop);
            JVPlus.setLinks(document.links);
         }
      }

      if (layer != undefined) {
         if (brandLayer = document.getElementById(layer)) {
            cobrand(brandLayer, customHop);
            return;
         }
      }

      if (brandLayer = document.getElementById("jvplus-rebrand")) {
         cobrand(brandLayer);
      }
      if (brandLayer = document.getElementsByClassName("jvplus-rebrand")) {
         cobrand(brandLayer);
      }

      JVPlus.setLinks(document.links);
   },

   findHop : function() {
      // Check for an HTML layer named "hop"
      hop = undefined;

      if (!hop) {
         if (hopLayer = document.getElementById("hop")) {
            if (hopLayer.value) {
               hop = hopLayer.value;

               if (!hopLayer.onclick) {
                  hopLayer.onclick = function() {
                     if (hopLayer.value == placeholder) {
                        hopLayer.value = '';
                     }
                  }
               }
            }
            else if (hopLayer.innerHTML) { hop = hopLayer.innerHTML; }
         }
      }

      if (hop == placeholder) { hop = undefined; }

      // Check the query string
      if (!hop) { hop = getParameter("hop"); }
      if (!hop) { hop = getParameter("e"); }
      if (!hop) { hop = getParameter("id"); } // paydotcom

      // Check for ?hop=something in the referrer
      if (!hop) { hop = getParameter("hop", document.referrer); }
      if (!hop) { hop = getParameter("e", document.referrer); }
      if (!hop) { hop = getParameter("id"); } // paydotcom

      // Check the referrer
      if (!hop) { hop = getClaim(); }

      // Check the cookie
      if (!hop) { hop = Cookie.get("hop"); }

      // Check the easyClickmate cookie
      if (!hop) {
         var cbnn = Cookie.get("cbnn");

         if (cbnn) {
            hop = cbnn.replace(/_.*$/, "");
         }
      }

      // Remove trailing slash from hoplink
      if (hop) { hop = hop.replace(/\/$/, ''); }

      JVPlus.ratio = parseFloat(JVPlus.ratio);

      if (JVPlus.owner != "") {
         if (!hop) { hop = JVPlus.owner; }
         else if (JVPlus.ratio > 0) {
            // Ratio'd affiliate IDs:
            // Decide whether to give credit to the referrer or to the site owner
            JVPlus.ratio = Math.min(JVPlus.ratio, 100);
            JVPlus.ratio = Math.max(JVPlus.ratio, 0);

            var realRatio;

            if (JVPlus.ratio > 1) { realRatio = JVPlus.ratio / 100; }
            else { realRatio = JVPlus.ratio; }

            var rand = Math.random();
            if (rand > realRatio) { hop = JVPlus.owner; }
         }
      }

      if (hop && hop.match(',')) {
         var hopList = hop.split(',');
         var hopIndex = Math.floor(Math.random() * hopList.length);
         hop = hopList[hopIndex];
      }
   },

   cloakAffiliateLink : function(linkObject) {
      var newURL;

      if (newURL = lookup(linkObject.href)) {
         // If "hop" is "undefined", rebrand regular links but not affiliate links.
         if (newURL.match(placeholder) && hop == undefined) { return false; }

         if (JVPlus.hide) {
            linkObject.onclick = function() { window.location = matchLink(this); return false; };
            return true;
         }
         else {
            if (newURL = newURL.replace(placeholder, hop, 'g')) {
               linkObject.href = newURL;
               return true;
            }
         }
      }
      return false;
   },

   cloakNormalLink : function(linkObject) {
      var match;
      if (match = linkObject.href.match(/^(.*)#(.*)#/)) {
         var realLink = match[1];
         var cloakedLink = '/' + match[2];

         linkObject.href = cloakedLink;
         linkObject.onclick = function() { document.location = realLink; return false; }
      }
   },

   registry : { }
};

var cloak = new Object();
var claim = new Object();
var rebrand = new Object();

// JV Plus 1.60 - by Robert Plank - http://www.jvplus.com
// Add cloaked links here, one on each line.
// Place xxxxx in the URL where you want the affiliate ID to be substituted.

cloak['example.com'] = 'http://xxxxx.example.com';

cloak['phpinabox.com'] = 'http://xxxxx.phpinabox.com';
cloak['blackhatphp.com'] = 'http://xxxxx.blackhatphp.com';
cloak['lightningphp.com'] = 'http://xxxxx.lightningphp.com';
cloak['impactphp.com'] = 'http://xxxxx.impactphp.com';
cloak['pushbuttonphp.com'] = 'http://xxxxx.pushbuttonphp.com';
cloak['speakthegeek.com'] = 'http://xxxxx.speakthegeek.com';
cloak['superchargedphp.com'] = 'http://xxxxx.superchargedphp.com';

cloak['fullblastphp.com'] = 'http://xxxxx.fullblastphp.com';
cloak['jvplus.com'] = 'http://xxxxx.jvplus.com';
cloak['actionpopup.com'] = 'http://xxxxx.actionpopup.com';
cloak['topsecretphp.com'] = 'http://xxxxx.topsecretphp.com';
cloak['phponcrack.com'] = 'http://xxxxx.phponcrack.com';
cloak['javascriptoncrack.com'] = 'http://xxxxx.javascriptoncrack.com';
cloak['phprevealed.com'] = 'http://xxxxx.phprevealed.com';
cloak['websitesoncrack.com'] = 'http://xxxxx.websitesoncrack.com';
cloak['softwaresecretsexplained.com'] = 'http://xxxxx.softwaresecretsexplained.com';

cloak['salespagetactics.com/volume1'] = 'http://xxxxx.salespagetactics.com/volume1';
cloak['salespagetactics.com/volume2'] = 'http://xxxxx.salespagetactics.com/volume2';
cloak['salespagetactics.com/volume3'] = 'http://xxxxx.salespagetactics.com/volume3';
cloak['salespagetactics.com'] = 'http://xxxxx.salespagetactics.com';

cloak['simplejavascript.com'] = 'http://xxxxx.simplejavascript.com';
cloak['sonicphp.com'] = 'http://xxxxx.sonicphp.com';
cloak['phpdecoded.com'] = 'http://xxxxx.phpdecoded.com';
cloak['phpuncensored.com'] = 'http://xxxxx.phpuncensored.com';

cloak['simplephp.com/volume1'] = 'http://xxxxx.simplephp.com/volume1';
cloak['simplephp.com/volume2'] = 'http://xxxxx.simplephp.com/volume2';
cloak['simplephp.com/volume3'] = 'http://xxxxx.simplephp.com/volume3';
cloak['simplephp.com'] = 'http://xxxxx.simplephp.com';
cloak['fiveminutephp.com'] = 'http://xxxxx.fiveminutephp.com';
cloak['fiveminutecopywriting.com'] = 'http://xxxxx.fiveminutecopywriting.com';
cloak['dailyseminar.com'] = 'http://xxxxx.dailyseminar.com';
cloak['membershipsitesoncrack.com'] = 'http://xxxxx.membershipsitesoncrack.com';
cloak['timemanagementoncrack.com'] = 'http://xxxxx.timemanagementoncrack.com';

cloak['wordpressoncrack.com/advanced'] = 'http://xxxxx.wordpressoncrack.com/advanced';
cloak['wordpressoncrack.com/seo'] = 'http://xxxxx.wordpressoncrack.com/seo';
cloak['wordpressoncrack.com/techie'] = 'http://xxxxx.wordpressoncrack.com/techie';
cloak['wordpressoncrack.com'] = 'http://xxxxx.wordpressoncrack.com';

cloak['jquerytactics.com'] = 'http://xxxxx.jquerytactics.com';
cloak['readytogophp.com'] = 'http://xxxxx.readytogophp.com';
cloak['hypnoticphp.com'] = 'http://xxxxx.hypnoticphp.com';
cloak['wordpresscrusher.com'] = 'http://xxxxx.wordpresscrusher.com';

cloak['fastfoodcopywriting.com'] = 'http://xxxxx.fastfoodcopywriting.com';
cloak['paydotcom.net'] = 'http://paydotcom.net/?affiliate=285616';
cloak['clickbank.com'] = 'http://simplephp.reseller.hop.clickbank.net';

// Add claimed traffic sources
// For example, claim all traffic from "stevenschwartzman.com" to the affiliate ID "stevenss"

claim['stevenschwartzman.com'] = 'stevenss';
claim['TOPIC_ID=223429'] = 'bookmoney';

rebrand['stevenss'] = "Brought to you by Robert Plank and Steven Schwartzman!";
rebrand['jmhonline'] = "Brought to you by Robert Plank and Big Jason Henderson!";
rebrand['drsuccess'] = "Brought to you by Robert Plank and Michel Fortin!";
// Cookie functions
var Cookie = {

   set : function(name, value, days) {
      // Default to a 1 year cookie
      if (days == undefined) { days = 365; }

      // Format date string
      var date = new Date();
      date.setTime(date.getTime() + (days * 86400000));

      // Set cookie name, value, and expiration date
      document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
   },

   get : function(name) {
      // Find the cookie's value in the document cookie string
      var results = document.cookie.match(
         new RegExp("(?:^|; )" + name + "=" + "(.*?)(?:$|;)")
      );

      // Return the value if a match was found, undefined otherwise
      if (results && results.length > 1) return results[1];
      return undefined;
   },

   clear : function(name) {
      // Erase a cookie
      setCookie(name, "", -1);
   }

};

document.getElementsByClassName = function(clsName){
   var retVal = new Array();
   var elements = document.getElementsByTagName("*");

   for(var i = 0;i < elements.length;i++) {
      if (elements[i].className.indexOf(" ") >= 0) {
         var classes = elements[i].className.split(" ");
         for(var j = 0;j < classes.length;j++){
             if(classes[j] == clsName) { retVal.push(elements[i]); }
         }
      }
      else if(elements[i].className == clsName) {
         retVal.push(elements[i]);
      }
   }
   return retVal;
};

// Procedural code
JVPlus.brand();

var shareLayer;
if (shareLayer = document.getElementById("jvplus-share")) {
   shareLayer.innerHTML = '<form action="#" onsubmit="return false"><p><b>Share This Site ... And Make Money!</b></p><p>Refer people to this site with your <a target="_blank" href="http://simplephp.reseller.hop.clickbank.net">ClickBank affiliate ID</a> and get paid when they make a purchase.</p><p align="center">   <label><b>ClickBank ID:</b> <input type="text" id="jvplus-affiliates" size="9"  onkeyup="JVPlus.brand(\'jvplus-url\', \'jvplus-affiliates\')" /></label><br />   <textarea id="jvplus-url" cols="30" rows="2" wrap="hard" onclick="this.focus(); this.select();">http://xxxxx.robertplank.com</textarea><br />   <a target="_blank" href="http://xxxxx.jvplus.com">Powered by JV Plus</a></p></form>';
}

document.write('<style type="text/css">');
document.write('<' + '!--');
document.write('#jvplus-share *, #jvplus-share a {   font-size:11px;}#jvplus-share a {   font-weight:bold;   text-decoration:underline;}#jvplus-share {   padding:5px;   /*   border:solid 1px;   background-color:white;   color:black;   */}#jvplus-url {	font-size:10px;}');
document.write('--' + '>');
document.write('</style>');

function cobrand(layer, customHopLayer) {
   var localHop;

   if (hopObject = document.getElementById(customHopLayer)) {
      localHop = hopObject.value;
   }
   else {
      localHop = hop;
   }

   if (layer.length && layer.length > 0) {
      var i;
      for (i=0;i<layer.length;i++) {
         cobrand(layer[i]);
      }
      return;
   }

   //var updateText = layer.id + ": href = " + layer.href + ", src = " + layer.src + ", value = " + layer.value;
   //alert("localHop = " + localHop);

   // Edit the HREF attribute if this is a link
   if (layer.id == "localHop") { /* give up */ }
   else if (layer.href) {
      layer.href = layer.href.replace(placeholder, localHop, 'g');
   }
   // Edit the SRC attribute if this is an iframe
   else if (layer.src) {
      layer.src = layer.src.replace(placeholder, localHop, 'g');
   }
   // Edit the VALUE attribute if this is a form element
   else if (layer.value) {
      var layerID = layer.id;

      if (!JVPlus.registry[layerID]) {
         JVPlus.registry[layerID] = layer.value;
      }

      if (localHop) {
         layer.value = JVPlus.registry[layerID].replace(placeholder, localHop, 'g');
      }
      else {
         layer.value = JVPlus.registry[layerID];
      }
   }
   // Otherwise, edit the innerHTML attribute if this is a text layer
   else if (layer.innerHTML) {
      var rebrandText = null;
      if (!rebrandText) { rebrandText = rebrand[localHop]; }
      if (!rebrandText) { rebrandText = getClaim(document.referrer, rebrand); }
      if (rebrandText) { layer.innerHTML = rebrandText; }
   }
}

function lookup(url) {
   for (name in cloak) {
      // Find out if the link matches any of our non-affiliate links
      //var re = new RegExp(name + "/?$");
      var re = new RegExp(name);
      if (url.match(re)) { return cloak[name]; }
   }
   return null;
}

function matchLink(obj) {
   var realURL;

   if (realURL = lookup(obj.href)) {
      newURL = realURL.replace(placeholder, hop, 'g');
      return newURL;
   }
   return true;
}

function getParameter(parameter, url) {
   if (url == undefined) {
      url = window.location.search.substring(0);
   }
   else {
      url = url.substring(0);
   }

   if (url == "") { return undefined; }

   var results = url.match(
      new RegExp("(?:&|\\?)" + parameter + "=(.*?)(?:&|$)", "")
   );

   if (results && results != null && results.length >= 1) {
      return unescape(results[1]);
   }
   return undefined;
}

function getClaim(refer, haystack) {
   if (haystack == undefined) {
      haystack = claim;
   }
   if (refer == undefined) {
      refer = document.referrer;
   }

   for (claimURL in haystack) {
      if (refer.match(claimURL)) {
         var affiliateID = haystack[claimURL];
         return affiliateID;
      }
   }
}