/**
 * Static Plugs Library
 *
 * Provides the code required to display the static plug libraries that use javascript to
 * generate the links. This should work for roughly 90% of linking codes you encounter out there!
 *
 * @author kostjan
 * @version $Id$
 */


// define variables in advance to avoid warnings
var plug_id = false;
var plugContent = false;
var reg = false;

var new_href = false;
var new_onclick = false;
var new_image = false;

var tokens = false
var new_title = false
var new_description = false;

// loop through found static plugs
for (var static_plug_id in static_plugs) {

	// dismiss possible prototyped functions
	if (!isNaN(static_plug_id)) {

		// try to catch the exceptions, just in case... wont work with old IE tho
		try {

			// reset the variables so we dont get false positives (redundant image, etc)
			new_href = false;
			new_onclick = false;
			new_image = false;
			
			tokens = false;
			new_title = false
			new_description = false;

			plug_id = static_plugs[static_plug_id];

			// fetch the script's output... if none found, pass to the next one
			plugContent = document.getElementById(plug_id);

			if (!plugContent) {
				continue;
			}

			// fetch placeholders (note - validate laters)
			holder_image = document.getElementById('img_loc_' + static_plug_id);
			holder_image_link = document.getElementById('img_href_' + static_plug_id);
			holder_link = document.getElementById('link_href_' + static_plug_id);
			holder_description = document.getElementById('link_description_' + static_plug_id);
	
			plugContent = plugContent.innerHTML;

			// try and see if any links in an a tag are found
			reg = new RegExp('<a.*\ href=["\']([^"\'>]+)[^>]+>', 'i');

			if (plugContent.search(reg) > 0) {
				// replacing &amps as they screw with adbrite
				new_href = RegExp.$1.replace(/&amp;/gi, '&');
			}

			// now, try and see if it has any onlcicks instead

			// I dont want to spend hours designing regexes, so I did it the KISS way...
			// these regex are not sophisticated, but should catch 95% of cases...
			reg = new RegExp('onclick="([^"]+)"', 'i');

			if (plugContent.search(reg) > 0) {
				new_onclick = RegExp.$1;
			} else {
				reg = new RegExp("onclick='([^']+)'", 'i');
				if (plugContent.search(reg) > 0) {
					new_onclick = RegExp.$1;
				}
			}

			// Append the visit counter and the return false (if javascript action)
			if (holder_link && holder_link.href) {
				if (new_onclick) {
					new_onclick += ' countPlugVisit("' +holder_link.href+ '"); return false;';
				} else {
					new_onclick = ' countPlugVisit("' + holder_link.href + '");';
				}
			}

			// finally, locate image (if any)
			reg = new RegExp('<img.*\ src=["\']([^"\'>]+)[^>]+>', 'i');

			if (plugContent.search(reg) > 0) {
				new_image = RegExp.$1;
			}

			
			// now parse the text and title.. axiom - title is shorter than description text...
			
			// does not contain style or scripts tht are outof line
			reg = new RegExp('<script[^>]*>[^<]+</script>', 'gi');
			plugContent = plugContent.replace(reg, '');
			
			reg = new RegExp('<style[^>]*>[^<]+</style>', 'gi');
			plugContent = plugContent.replace(reg, '')			
			
			// ...and contains neither contain other tags
			reg = new RegExp('<[^>]+>', 'g');
			plugContent = plugContent.replace(reg, '%%%%');
			
			// split on the predefined token
			tokens = plugContent.split('%%%%');
			
			
			// now identify the unecessary tokens (if any)
			reg = new RegExp('[a-z]{1}', 'gi');
			
			for (var i=0; i < tokens.length; i++) {
				
				// if array entry is not a specified token
				if (tokens[i].search(reg) > -1) {
					
					// if title is not set, we set it first - otherwise it's description
					if (!new_title) {
						new_title = tokens[i];
					} else {
						new_description = tokens[i];
					}
			
					// we asume that title is shorter than description, so we do the following rook
					if (new_title && new_description) {
						
						if (new_title.length > new_description.length) {
							reg = new_title;
							new_title = new_description;
							new_description = reg;
						}
						
						// no point looping further
						break;			
					}
						
				} 
				
			}
			
			// now substitute document's links and images
			if (holder_image && new_image) {
				
				// adengage and other tracking stats fix.. add others if necessary
				if (new_image.search(/stats/) == -1) {
					holder_image.src = new_image;
				}

			}

			if (holder_image_link && new_href) {
				
				if (document.all) {
					holder_image_link.attachEvent('onmouseover', new Function("window.status='http://www.friendlyjerk.com/visit.php?id=1337'; return true;"));
					holder_image_link.attachEvent('onmouseout', new Function("window.status=' '; return true;"));
				} else {
					holder_image_link.setAttribute('onmouseover', "window.status='http://www.friendlyjerk.com/visit.php?id=1337'; return true;");
					holder_image_link.setAttribute('onmouseout', "window.status=' '; return true;");
				}
				
				holder_image_link.href = new_href;
			}

			if (holder_link && new_href) {
				
				if (document.all) {
					holder_link.attachEvent('onmouseover', new Function("window.status='http://www.friendlyjerk.com/visit.php?id=1337'; return true;"));
					holder_link.attachEvent('onmouseout', new Function("window.status=' '; return true;"));
				} else {
					holder_link.setAttribute('onmouseover', "window.status='http://www.friendlyjerk.com/visit.php?id=1337'; return true;");
					holder_link.setAttribute('onmouseout', "window.status=' '; return true;");
				}				
				
				holder_link.href = new_href;
			}

			if (holder_image_link && new_onclick) {

				if (document.all) {
					holder_image_link.attachEvent('onclick', new Function(new_onclick));
				} else {
					holder_image_link.setAttribute('onClick', new_onclick);
				}
			}

			if (holder_link && new_onclick) {
				if (document.all) {
					holder_link.attachEvent('onclick', new Function(new_onclick));
				} else {
					holder_link.setAttribute('onClick', new_onclick);
				}
			}
			
			if (holder_link && new_title) {
				holder_link.innerHTML = new_title;
			}
			
			if (holder_description && new_description) {
				holder_description.innerHTML = new_description;	
			}
			
			

			// et voila

		} catch (e) {
			// silent, we dont want to crap out
			alert(e.message);
		}

	}
}

/**
 * Since the visit script is no longer
 * invoked when users click on js-based static plugs, we
 * need to log the clicks nevertheless
 */
function countPlugVisit(link) {
	
	// if no body, nothing can be done...
	if (!document.body) {
		return false;
	}
	
	// create an iframe and hit the visited page...
	var iframe = document.createElement('IFRAME');
	iframe.style.display = 'none';
	iframe.src = link;
	
	document.body.appendChild(iframe);
}