var affiliatesgarage_ad_client = (Math.round((Math.random()*9)+1));
var affiliatesgarage_server_path = 'http://www.affiliatesgarage.com/serve/';
//var affiliatesgarage_server_path = '/affiliatesgarage/serve/';

var ads_url = escape(parent.document.URL);
var affiliatesgarage_hex_cblink = parseHex(affiliatesgarage_hex_border);
var affiliatesgarage_ad_inframe = (top.frames.length);

var affiliatesgarage_ad_whsplit = affiliatesgarage_format.indexOf('x');
var affiliatesgarage_ad_width   = affiliatesgarage_format.substring(0,affiliatesgarage_ad_whsplit);
var affiliatesgarage_ad_height  = affiliatesgarage_format.substring(affiliatesgarage_ad_whsplit+1);

document.write('<div id="ag_' + affiliatesgarage_ad_client + '" style="width:'+ affiliatesgarage_ad_width +'px; height:'+ affiliatesgarage_ad_height +'px; "></div>');

/* ----- main function ------ */
window.onload=function(){

	var p = document.documentElement;
  	var keyword = '';
  	var matchs = new Array;

  	if (p.innerHTML != undefined) {
  		var content = p.innerHTML;
		content = content.replace(/<\S[^<>]*>/gi,'');		

		matchs = content.match(/agkw:([^<>.]*)/i);
    	if (matchs != null) {
	  		keyword = matchs[1].replace(/\n/g,'').replace(/\u2033/g,'"').replace(/\u2019/g,"'");
		} else {
			// extract all words
			//alert(countWords(content));
			content = content.replace(/\n/gi,' ');		
			content = content.replace(/\t/gi,' ');		
			content = content.replace(/[0-9]/g,' ');		
			content = content.replace(/  /gi,' ');		
			keyword = calculateWords(content);
		}
  	}
	affiliatesgarage_kw = keyword;
	document.getElementById('ag_'+ affiliatesgarage_ad_client).innerHTML = '<iframe frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" topmargin=\"0\" bottommargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" scrolling=\"no\" id=\"affiliatesgarage_'+affiliatesgarage_ad_client+'_iframe\" name=\"affiliatesgarage_'+affiliatesgarage_ad_client+'_iframe\" src=\"'+ affiliatesgarage_server_path + 'show_ads.php?url='+ads_url+'&cb_id='+affiliatesgarage_cb_id+'&pdc_id='+affiliatesgarage_pdc_id+'&format='+affiliatesgarage_format+'&border='+affiliatesgarage_hex_border+'&bg='+affiliatesgarage_hex_bg+'&link='+affiliatesgarage_hex_link+'&text='+affiliatesgarage_hex_text+'&cblink='+affiliatesgarage_hex_cblink+'&kw='+affiliatesgarage_kw+'&inframe='+affiliatesgarage_ad_inframe+'" width=\"'+affiliatesgarage_ad_width+'\" height=\"'+affiliatesgarage_ad_height+'\"></iframe>';		
}


/* ----- helper functions ------ */
function parseHex (valHex) {
	
	// RGB
	var red = parseInt ( valHex.substring (1,3),16);
	var grn = parseInt ( valHex.substring (3,5),16);
	var blu = parseInt ( valHex.substring (5,7),16);
	var red = parseInt ( valHex.substring (0,2),16);
	var grn = parseInt ( valHex.substring (2,4),16);
	var blu = parseInt ( valHex.substring (4,6),16);
	
	
	// CMY
	var cya = Math.abs ( Math.ceil ( red / 256 * 100 ) - 100);
	var maj = Math.abs ( Math.ceil ( grn / 256 * 100 ) - 100);
	var yel = Math.abs ( Math.ceil ( blu / 256 * 100 ) - 100);
	
	var cmy = cya+maj+yel;
	var cblink = (cmy>100) ? 'w' : 'b';
	
	return cblink;
}

/* ----- helper functions ------ */
var stopWords = new Array();
stopWords = [
  	'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 
    'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 
    'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 
    'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 
    'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 
    'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 
    'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 
    'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 
    'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 
    'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 
    'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 
    'than', 'too', 'very', 'get', 'according', 'this', 'these', 'yes', 'no', 'yeah', 'nope', 
	'can', 'will', 'every', 'everything', 'any', 'anything', 'going to', 'cannot', 'can\'t', 
	'ago', 'just', 'com', 'find', 'search', 'go', 'via', 'elsewhere', 'links', 'us', 'rants', 
	'week', 'day', 'month', 'year', 'weeks', 'days', 'months', 'years', 'comments', 'trackbacks', 
	'tags', 'nbsp', 'well', 'make',

	'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 
	'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 
	'sunday', 'monday', 'tuesday', 'wednsday', 'thursday', 'friday', 'saturday',
	'sunday', 'monday', 'tue', 'wed', 'thu', 'fri', 'sat',
];


function inArray(value, array) {
	for (var i=0; i<array.length; i++) {
		if (array[i] === value) {
			return true;
		}
	}
}

function cleanWord(word) {
	rv = word.toLowerCase();
	rv = rv.replace(",","");
	rv = rv.replace(".","");
	return rv;
}

function calculateWords(text) {
	var words = text.split(" ");
	
	for (i=0;i<words.length;i++) {
		words[i] = cleanWord(words[i]);
	}
	
	var wordsFrequencies = new Array();
	var uniqueWordCount = 0;

	// Count unique words and place into array
	for (c=0;c<words.length;c++) {
		alreadyCounted=false;
		currentWord = words[c];

		if (currentWord.length > 1 && !inArray(currentWord, stopWords)) {
		
			// make sure it's a word we haven't already counted
			for (e=0;e<wordsFrequencies.length;e++) {
				if (currentWord==wordsFrequencies[e][0]){
					alreadyCounted = true;
				}
			}

			if (!alreadyCounted) {
				// add to array
				wordsFrequencies[uniqueWordCount] = new Array(2);
				wordsFrequencies[uniqueWordCount][0] = currentWord;
				wordsFrequencies[uniqueWordCount][1] = 0;

				// count total instances
				for (d=0;d<words.length;d++) {
					if (words[d]==wordsFrequencies[uniqueWordCount][0]){
						wordsFrequencies[uniqueWordCount][1]++;
					}
				}

				uniqueWordCount++;

			}

		}

	}

	// Sort array
	var sorted = new Array();
	var maxFreq = 0;
	var sortCounter = 0;

	// find the highest word frequency
	for (c=0;c<wordsFrequencies.length;c++) {
		maxFreq = (wordsFrequencies[c][1]>maxFreq) ? wordsFrequencies[c][1] : maxFreq;
	}

	// count down through possible frequencies, adding words to new array if their frequency matches.
	for (c=maxFreq;c>=1;c--) {
		for (d=0;d<wordsFrequencies.length;d++) {
			word = wordsFrequencies[d][0];
			freq = wordsFrequencies[d][1];
			if (freq==c) {
				sorted[sortCounter] = new Array(2);
				sorted[sortCounter][0] = word;
				sorted[sortCounter][1] = freq;
				sortCounter++;
			}
		}
	}


	// create HTML list output
	var reportLength = (sorted.length>15) ? 15 : sorted.length;
	var strReport = '';
	for (c=0;c<reportLength;c++) {
		//strReport += sorted[c][0] + ": " + sorted[c][1] + "<br />";
		strReport += sorted[c][0] + " ";
	}
	return strReport;
	//document.getElementById("report").innerHTML = strReport;
}