String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }
var oHelper = [];

jQuery('a, area').live('click', function(){
	var strOmniture, tree, link;
	
	if ( this.href.indexOf('doubleclick.net') != -1 ) {
		document.cookie = 'omni_PromoCode=; expires=Thu, 01-Jan-70 00:00:01 GMT; domain=scholastic.' + document.domain.split('.').pop() + '; path=/';					
	}
	else {
		tree = jQuery(this).parents().map(function(){
			if ( this.id ) {
				return this.id;
			}
		}).get().join('/');		
		
		oHelper[tree] = 1;				
		
		if ( this.tagName == 'A' ) {		
			
			if ( jQuery(this).children().length ) {				
				
				var hasText = false, text = '';
				
				for ( var i = 0; i < this.childNodes.length; i++ ) {
					if ( this.childNodes[i].nodeType == 1 && this.childNodes[i].nodeName != 'IMG' ) {
						hasText = true;
						text += this.childNodes[i].firstChild.nodeValue;
					}
					if ( this.childNodes[i].nodeType == 3 && (this.childNodes[i].nodeValue).trim() != '') { hasText = true; text += this.childNodes[i].nodeValue; }
				}
								
				if ( hasText ) {
					link = '[text][' + text.trim() + ']';
				}
				else {
					var tmp = jQuery(this).children(':first').attr('alt') || 'n/a';
					link =  '[img][' + tmp + ']';
				}
				/*
				if ( this.firstChild.nodeValue != null ) {
					link = '[text][' + jQuery(this).text() + ']';
				}
				else {
					var tmp = jQuery(this).children(':first').attr('alt') || 'n/a';
					link =  '[img][' + tmp + ']';
				}
				*/				
			}
			else {
				link = '[text][' + jQuery(this).text() + ']';
			}			
			
			/*
			if ( jQuery(this).children().length > 0 ) {
				if ( jQuery(this).children(':first').is('img') ) {					
					var tmp = jQuery(this).children(':first').attr('alt') || 'n/a';
					link =  '[img][' + tmp + ']';
				}
				else {
					link = '[text][' + jQuery(this).children(':first').text() + ']';
				}
			}
			else {
				link = '[text][' + jQuery(this).text() + ']';
			}
			*/
		}
		else {
			var t = jQuery(this).attr('alt') || 'n/a';
			link = '[imgMap][' + t + ']';
		}				
		
		var that = this;
		var containerID = tree.split('/')[0],
			pos,
			hm = jQuery('#' + containerID + ' ' + this.tagName).each(function(index){
				if ( that === this ) {
					pos = index + 1;				
					return false;
				}
			}).length;		
					
		tree = tree.split('/');
		var tempTree = tree;
		for (var i = tempTree.length - 1; i >= 0; i--) {
			if ( tempTree[i] != 'schlTweens' && tempTree[i] != 'schlKidsWrapper' && tempTree[i] != 'schlPageWrapper' ) {
				tree.pop();
			}
			else break;
		}
		tree = tree.join('/');
				
		tree = '[' + tree + ']';

		strOmniture = '[' + location.pathname + ']';
		strOmniture += tree;
		strOmniture += '[' + pos + '_' + hm + ']' + link;			
		
		if ( readCookie('omni_PromoCode') != null ) document.cookie = 'omni_PromoCode=; expires=Thu, 01-Jan-70 00:00:01 GMT; domain=scholastic.' + document.domain.split('.').pop() + '; path=/';
			
		document.cookie = 'omni_PromoCode=' + strOmniture + '; domain=scholastic.' + document.domain.split('.').pop() + '; path=/';
	}
	
});

