
// Utility function for checking if an element (e) is a member of CSS class (c).
function hasClass(e, c) {

// If we passed the function a string, then get the element with that id.
	if (typeof e == "string") e = document.getElementById(e);

// Declare the variable "classes" as the text value of our element's CSS classes.
	var classes = e.className;

// If our element doesn't have any CSS classes assigned to it, return false.
	if (!classes) return false;

// If our element has exactly one class and that class is (c), return true.
	if (classes == c) return true;

// If our element has multiple classes and one of them is (c), return true.
	return e.className.search("\\b" + c + "\\b") != -1;
};



// Function for rendering affiliate links in a way that won't pass PageRank.
function winkelLinks(){

// Declare local variables.
	var theURL, theAnchorText, theTitle;

// Declare the variable "spans" as the collection of all <span> elements.
	var spans = document.getElementsByTagName('span');

// Perform the following steps for every <span> element.
	for (var i = 0; i<spans.length; i++){

// If the <span> element is part of the "affiliate" class...
		if (hasClass(spans[i], 'button')){

// Use the content between the <span> tags as our affiliate link's anchor text.
// Example: <span class="affiliate" title="Affiliate Site">this will be our anchor text</span>
// The content doesn't have to be just text; it can be HTML too.
// Example: <span class="affiliate" title="Affiliate Site"><img src="/banners/affiliate-logo.png" /></span>
			theAnchorText = spans[i].innerHTML;

// Get the value of the <span> element's title attribute, make it lowercase, and remove whitespace
// characters from the beginning and end.
			theTitle = spans[i].title.toLowerCase().replace(/^\s+|\s+$/g,"");

// Check the value of the <span> element's title attribute against the following possibilities.
			switch (theTitle) {

// If the <span> element's title is "thesis" then set our affiliate link's href to
// "http://www.seomofo.com/affiliate/thesis/"
				case 'vrouwenuggsassem': theURL = 'http://www.seomofo.com/affiliate/thesis/'; break;

// If the <span> element's title is "thesis plans" then set our affiliate link's href to
// "http://www.seomofo.com/affiliate/thesis/plans/"
				case 'thesis plans': theURL = 'http://www.seomofo.com/affiliate/thesis/plans/'; break;

// If the <span> element's title doesn't match any of the possibilities I've provided, then make
// my affiliate link do NOTHING when clicked on, because I f***ed up my code somewhere.
				default: theURL = 'javascript: void(0)';
			}

// Insert the new affiliate link into its corresponding <span> element and copy the <span> element's
// CSS classes (all of them) into the affiliate link's <a> tag.
			spans[i].innerHTML = '<a href="' + theURL + '" class="' + spans[i].className + '">' + theAnchorText + '</a>';

// Remove the title attribute from the <span> element, to prevent Firefox from displaying it in a tooltip.
			spans[i].removeAttribute('title');
		}
	}
}



// Call the affiliateLinks function AFTER the document has finished loading.
window.onload = function(){
	winkelLinks();
}

