if ( typeof(Bim_Bowie) === "undefined" ) {
	if (!window.jQuery){
		 document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"><\/script>');
		$.noConflict();
	}
	
	//setup bimbowie, should only run once per a page
	//by cory gwin
	 Bim_Bowie = ( function() {
		//var cookieIframe = document.createElement('iframe')
		//cookieIframe.setAttribute('src','')
		//cookieIframe.setAttribute('height','0')
		//cookieIframe.setAttribute('width','0')
		//cookieIframe.setAttribute('border','none')
		//cookieIframe.setAttribute('id','bimcookiesetframe')
		//document.getElementsByTagName("body")[0].append(cookieIframe)
		var paths = ["weather", "news", "sports", "health", "autos", "recipe", "food", "{{ dart_site }}"];
		var predicted = [];
		var sectionpredict= {
			weather:{
				keys:["weather", "forecast", "storm", "hurricane","gail", "blast", "blizzard", "blow", "cloudburst", "cyclone", "disturbance", "downpour", "gale", "gust", "hurricane", "monsoon", "precip", "precipitation", "raining cats and dogs", "snowstorm", "squall", "tempest", "tornado", "twister", "whirlwind", "windstorm", "cancellations"],
				count: 0
			},
			news:{
				keys:["advice", "announcement", "broadcast", "bulletin", "cable", "cognizance", "communication", "communique", "copy", "data", "description", "disclosure", "discovery", "dispatch", "enlightenment", "expose", "eye-opener", "front-page news", "headlines", "hearsay", "intelligence", "itemization", "knowledge", "leak", "lowdown", "message", "narration", "news flash", "particularization", "recital", "recognition", "release", "report", "rumor", "scandal", "scoop", "specification", "statement", "story", "telecast", "telegram", "telling", "the goods", "tidings", "word", "news", "local", "national"],
				count: 0
			},
			sports:{
				keys:["sports", "sport", "basketball", "baseball", "football", "soccer", "golf", "rugby", "cycling", "bicycling", "hockey", "action", "amusement", "athletics", "ball", "disport", "diversion", "exercise", "frolic", "fun", "fun and games", "gaiety", "game", "games", "pastime", "physical activity", "picnic", "play", "pleasure", "recreation", "amateur", "animal", "challenger", "competitor", "contender", "contestant", "games player", "gorilla", "iron person", "jock", "jockey", "muscle person", "player", "professional", "shoulders", "sport", "sportsperson", "superjock"],
				count: 0
			},
			food:{
				keys:["food", "bite", "board", "bread", "cheer", "chow", "comestible", "cookery", "cooking", "cuisine", "diet", "drink", "eatable", "eats", "entree", "fare", "fast food", "feed", "fodder", "foodstuff", "goodies", "grit", "groceries", "grub", "handout", "home cooking", "keep", "larder", "meal", "meat", "menu", "mess", "moveable feast", "nourishment", "nutriment", "nutrition", "pabulum", "provision", "ration", "refreshment", "slop", "snack", "store", "subsistence", "support", "sustenance", "table", "take out", "tuck", "viand", "victual", "vittles", "recipe", "recipes"],
				count: 0
			},
			autos:{
				keys:["auto", "automobile", "bucket", "buggy", "bus", "clunker", "compact", "convertible", "conveyance", "coupe", "gas guzzler", "hardtop", "hatchback", "heap", "jalopy", "jeep", "junker", "limousine", "machine", "motor", "motorcar", "pickup", "ride", "roadster", "sedan", "station wagon", "subcompact", "touring car", "truck", "van", "wagon", "wheels", "wreck", "car"],
				count: 0
			},
			health:{
				keys:["health", "wellness", "doctor", "nurse", "illness", "strength", "fitness", "bloom", "clean bill", "complexion", "constitution", "energy", "eupepsia", "euphoria", "fettle", "fine feather", "fitness", "form", "good condition", "haleness", "hardihood", "hardiness", "healthfulness", "healthiness", "lustiness", "pink*", "prime*", "robustness", "salubriousness", "salubrity", "shape", "soundness", "stamina", "state", "strength", "tone", "tonicity", "top form", "verdure", "vigor", "well-being", "wholeness", "disease", "illness", "infirmity", "sickness"],
				count: 0
			}
		};
		//Convert an array to a object so we can check for presence of string
		function oc(a){
			var o = {};
			for(var i=0;i<a.length;i++) {
			  o[a[i]]='';
			}
			return o;
		}
		
		//section
		function section(){
			var checkthese = {};
			for(heading in ["h1", "h2", "h3"]){
			if(document.getElementsByTagName(heading).length > 0){
				var holder = document.getElementsByTagName(heading).length;
				checkthese.headings = "";
				for( var i=0; i<holder.length; ++i ){
					checkthese.headings += holder[i].innerHTML + " ";
				}
			}
			}
			if(document.title){
				checkthese.titleofPage = document.title;
			}
			checkthese.urlnosplit = window.location.href;
			var urlsplit = window.location.href.split("/");
			var urlset = false;
			var section = "";
			//is it the domain?
			if(checkthese.urlnosplit == "http://" + document.domain + "/"){
				predicted.push("home")
				//return home since we know where we are.
				return predicted;
			}
			//path prediction
			for(i in urlsplit){
				if(typeof(urlsplit[i]) != "undefined"){
					//split the url and check each segement against our set of categories
					var cpath = String(urlsplit[i]).toLowerCase();
					if (cpath in oc(paths) && !urlset){
						section = cpath;
						predicted.push(section);
					}
				}
			}
			//no path predicted, try guessing
			for(key in sectionpredict){
				//loop over all possible section types
				var looplength = sectionpredict[key].keys.length;
				for( var i=0; i<looplength; i++ ){
					//loop over keywords for that section
					var c = String(sectionpredict[key].keys[i]).toLowerCase();
					for(cur in checkthese){
						if(typeof(checkthese[cur]) != "undefined" && typeof(c) != "undefined"){
							//check to see if the key word is in an important field.
							var w = String(checkthese[cur]).toLowerCase();
							if(w.indexOf(c) > 0){
								//Evedience of section type found
								sectionpredict[key].count += 1;	
							}
						}
					}//end check these loop
					for(seg in urlsplit){
						if(typeof(urlsplit[seg]) != "undefined"){
							//split the url and check each segement against our set of categories
							var cpath = String(urlsplit[seg]).toLowerCase();
							if(cpath.indexOf(c) > 0){
								sectionpredict[key].count += 4;
							}
						}
					}//end url split
				}
			}//end key in section predict
			

			var sortable = [];
			for(key in sectionpredict){
				//create arrays in the array so that we can sort out what is the highest level of prediction
				sortable.push([key, sectionpredict[key].count]);
			}
			sortable.sort(function(a, b) {return a[1] - b[1]}).reverse();
			//take the top two results from the sorted array if they are likely matches, likely being three corresponding
			//matches or more.
			if(sortable[0][0] in oc(predicted)){}else{if(sortable[0][1] > 3){
				predicted.push(sortable[0][0]);
			}}
			if(sortable[1][0] in oc(predicted)){}else{ if(sortable[1][1] > 3){
				predicted.push(sortable[1][0]);
			}}
			return predicted;

		}
		
		//demographic from bimreg
		function Bowiedemographics(){
			console.log("demo")
			if(typeof(bimreg) != "undefined" && bimreg.loggedin){
				var demos = {};
				if(bimreg.getuserkey("user_city") != "undefined"){
					demos.user_city = bimreg.getuserkey("city");
				}
				if(bimreg.getuserkey("user_state") != "undefined"){
					demos.user_state = bimreg.getuserkey("state");
				}
				if(bimreg.getuserkey("birth_date") != "undefined"){
					var birthdate = bimreg.getuserkey("birth_date");
					var theDate=new Date();
					demos.user_age = theDate.getFullYear() - birthdate.split("-")[0];	
				}
				Bim_Bowie["demographics"] = demos;
			}
		}
		
		//return values
		var returnValues = {
			section: section()
		};
		if(typeof(bimreg) != "undefined"){
			bimreg.listener.addListener("userLogin",Bowiedemographics);
		}
		return returnValues
	})();
}



if ( typeof(bimcreateDartTag) === "undefined" ) {
	function bimcreateDartTag(settings){
		//Takes an object of settings for the dart tag and returns the js string for said ad.
		//size : 300x250
		//parameters: string paramA=valA;paramB=valB
		//dartname = bi.test.site
		//id = string represnting the ad ad
		//location=string path to page location for sectional targeting
		//helpers
		var heightview = (function() {
			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				myHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				myHeight = document.body.clientHeight;
			}
			return myHeight
		})();
		
		function findPos(obj) {
			try{
				var curleft = curtop = 0;
				if (obj.offsetParent) {
					do {
						curtop += obj.offsetTop;
						} while (obj = obj.offsetParent);
					return curtop;
				}
			}catch(e){
				return false;	
			}
		};
		
		(function(){
			settings.height = parseInt(settings.size.split("x")[1]);
			settings.width = parseInt(settings.size.split("x")[0]);
		})();
		
		//above fold?
		function aboveFold(){
			try{
				var adpos = findPos(ad);
				var foldplace = adpos/heightview;
				var aboveFold = false;
				if(foldplace < 1){
					aboveFold = true;
				}
				
				return aboveFold;
			}catch(e){
				return false;	
			}
		};
		//quartile percent above fold
		function percentAboveFold(){
			try{
				var offset = Math.round(((heightview-ad.offsetTop)/settings.height)*100);
				offset = Math.round(offset/25);
				var percent= offset * 25;
				if(percent > 100){
					percent = 100;
				}
				if(percent < 0){
					percent = 0;
				}
				return percent;
			} catch (e) {
				return 0;	
			}
		};
		
		if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
		var adString = "http://ad.doubleclick.net/adj/"+settings.dartname;
		if(settings.location){
			adString+=settings.location;
		}else{
			adString+=window.location.pathname;
		}
		adString+=";sz="+settings.size;
		if(settings.parameters){
			adString += ";"+settings.parameters;
		}
		if (settings.id && settings.height){
			//find ad position in comparison to fold
			var ad = document.getElementById(settings.id);
			adString += ";abtf="+aboveFold()+";prcntabtf="+percentAboveFold();
		}
		for(key in Bim_Bowie){
			if(Bim_Bowie[key] instanceof Array){
				for (val in Bim_Bowie[key]){
					adString += ";"+encodeURIComponent(Bim_Bowie[key][val])+"=true";
				}
			} else if(typeof(Bim_Bowie[key]) === "object" || typeof(Bim_Bowie[key])==="number"){
				for(innerKey in Bim_Bowie[key])	{
					if (typeof(Bim_Bowie[key][innerKey])==="string" || typeof(Bim_Bowie[key][innerKey])==="number"){
						adString += ";"+encodeURIComponent(innerKey)+"="+encodeURIComponent(Bim_Bowie[key][innerKey]);
					}
				}
			} else if (typeof(Bim_Bowie[key])==="string" || typeof(Bim_Bowie[key])==="number"){
				adString += ";"+encodeURIComponent(key)+"="+encodeURIComponent(Bim_Bowie[key]);
			}
		}
		adString += ";domain=" + encodeURIComponent(document.domain);
		adString += ";ord=" + ord + "?";
		return adString;
	}
}
/**
 * writeCapture.js v1.0.5
 *
 * @author noah <noah.sloan@gmail.com>
 * 
 */
(function($,global) {
	var doc = global.document;
	function doEvil(code) {
		var div = doc.createElement('div');
		doc.body.insertBefore(div,null);
		$.replaceWith(div,'<script type="text/javascript">'+code+'</script>');
	}
	// ensure we have our support functions
	$ = $ || (function(jQuery) {
		/**
		 * @name writeCaptureSupport
		 *
		 * The support functions writeCapture needs.
		 */		
		return {
			/**
			 * Takes an options parameter that must support the following:
			 * {
			 * 	url: url,
			 * 	type: 'GET', // all requests are GET
			 * 	dataType: "script", // it this is set to script, script tag injection is expected, otherwise, treat as plain text
			 * 	async: true/false, // local scripts are loaded synchronously by default
			 * 	success: callback(text,status), // must not pass a truthy 3rd parameter
			 * 	error: callback(xhr,status,error) // must pass truthy 3rd parameter to indicate error
			 * }
			 */
			ajax: jQuery.ajax,
			/**
			 * @param {String Element} selector an Element or selector
			 * @return {Element} the first element matching selector
			 */
			$: function(s) { return jQuery(s)[0]; },
			/**
			 * @param {String jQuery Element} selector the element to replace.
			 * writeCapture only needs the first matched element to be replaced.
			 * @param {String} content the content to replace 
			 * the matched element with. script tags must be evaluated/loaded 
			 * and executed if present.
			 */
			replaceWith: function(selector,content) {
				// jQuery 1.4? has a bug in replaceWith so we can't use it directly
				var el = jQuery(selector)[0];
				var next = el.nextSibling, parent = el.parentNode;

				jQuery(el).remove();

				if ( next ) {
					jQuery(next).before( content );
				} else {
					jQuery(parent).append( content );
				}
			},

			onLoad: function(fn) {
				jQuery(fn);
			},
			
			copyAttrs: function(src,dest) {
				var el = jQuery(dest), attrs = src.attributes;
				for (var i = 0, len = attrs.length; i < len; i++) {
					if(attrs[i] && attrs[i].value) {
						try {
							el.attr(attrs[i].name,attrs[i].value);
						} catch(e) { }
					}
				}
			}
		};
	})(global.jQuery);

	$.copyAttrs = $.copyAttrs || function() {};
	$.onLoad = $.onLoad || function() {
		throw "error: autoAsync cannot be used without jQuery " +
			"or defining writeCaptureSupport.onLoad";
	};

	// utilities
	function each(array,fn) {
		for(var i =0, len = array.length; i < len; i++) { 
			if( fn(array[i]) === false) return; 
		}
	}	
	function isFunction(o) {
		return Object.prototype.toString.call(o) === "[object Function]";
	}
	function isString(o) {
		return Object.prototype.toString.call(o) === "[object String]";
	}	
	function slice(array,start,end) {
		return Array.prototype.slice.call(array,start || 0,end || array && array.length);		
	}
	function any(array,fn) {
		var result = false;
		each(array,check);
		function check(it) {
			return !(result = fn(it));
		}
		return result;
	}
	
	function SubQ(parent) {
		this._queue = [];
		this._children = [];
		this._parent = parent;
		if(parent) parent._addChild(this);
	}
	
	SubQ.prototype = {
		_addChild: function(q) {
			this._children.push(q);
		},
		push: function (task) {
			this._queue.push(task);
			this._bubble('_doRun');
		},
		pause: function() {
			this._bubble('_doPause');
		},
		resume: function() {
			this._bubble('_doResume');
		},
		_bubble: function(name) {
			var root = this;
			while(!root[name]) {
				root = root._parent;
			}
			return root[name]();
		},
		_next: function() {
			if(any(this._children,runNext)) return true;
			function runNext(c) {
				return c._next();
			}
			var task = this._queue.shift();
			if(task) {
				task();
			}
			return !!task;
		}
	};
	
	/**
	 * Provides a task queue for ensuring that scripts are run in order.
	 *
	 * The only public methods are push, pause and resume.
	 */
	function Q(parent) {
		if(parent) {
			return new SubQ(parent);
		}
		SubQ.call(this);
		this.paused = 0;
	}
	
	Q.prototype = (function() {
		function f() {}
		f.prototype = SubQ.prototype;
		return new f();
	})();
	
	Q.prototype._doRun = function() {
		if(!this.running) {
			this.running = true;
			try {
				// just in case there is a bug, always resume 
				// if paused is less than 1
				while(this.paused < 1 && this._next()){}
			} finally {
				this.running = false;
			}
		}
	};
	Q.prototype._doPause= function() {
		this.paused++;
	};
	Q.prototype._doResume = function() {
		this.paused--;
		this._doRun();
	};
	
	// TODO unit tests...
	function MockDocument() { }
	MockDocument.prototype = {
		_html: '',
		open: function( ) { 
			this._opened = true;
			if(this._delegate) {
				this._delegate.open();
			}
		},
		write: function(s) { 
			if(this._closed) return; 
			this._written = true;
			if(this._delegate) {
				this._delegate.write(s);
			} else {
				this._html += s;
			}
		}, 
		writeln: function(s) { 
			this.write(s + '\n');
		}, 
		close: function( ) { 
			this._closed = true;
			if(this._delegate) {
				this._delegate.close();
			}
		}, 
		copyTo: function(d) { 
			this._delegate = d;
			d.foobar = true;
			if(this._opened) {
				d.open();
			}
			if(this._written) {
				d.write(this._html); 
			}
			if(this._closed) {
				d.close();
			}
		}
	};
	
	// test for IE 6/7 issue (issue 6) that prevents us from using call
	var canCall = (function() {
		var f = { f: doc.getElementById };
		try {
			f.f.call(doc,'abc');
			return true;
		} catch(e) {
			return false;
		}
	})();
	
	function unProxy(elements) {
		each(elements,function(it) {
			var real = doc.getElementById(it.id);
			if(!real) {
				logError('<proxyGetElementById - finish>',
					'no element in writen markup with id ' + it.id);
				return;
			}

			each(it.el.childNodes,function(it) {
				real.appendChild(it);
			});

			if(real.contentWindow) {
				// TODO why is the setTimeout necessary?
				global.setTimeout(function() {
					it.el.contentWindow.document.
						copyTo(real.contentWindow.document);
				},1);
			}
			$.copyAttrs(it.el,real);
		});
	}
	
	function getOption(name,options) {
		if(options && options[name] === false) {
			return false;
		}
		return options && options[name] || self[name];
	}
	
	function capture(context,options) {
		var tempEls = [],
			proxy = getOption('proxyGetElementById',options),
			forceLast = getOption('forceLastScriptTag',options),
			writeOnGet = getOption('writeOnGetElementById',options),
			immediate = getOption('immediateWrites', options),
			state = {
				write: doc.write,
				writeln: doc.writeln,
				finish: function() {},
				out: ''
			};
		context.state = state;
		doc.write = immediate ? immediateWrite : replacementWrite;
		doc.writeln = immediate ? immediateWriteln : replacementWriteln;
		if(proxy || writeOnGet) {
			state.getEl = doc.getElementById;
			doc.getElementById = getEl;
			if(writeOnGet) {
				findEl = writeThenGet;
			} else {
				findEl = makeTemp;
				state.finish = function() {
					unProxy(tempEls);
				};
			}
		}
		if(forceLast) {
			state.getByTag = doc.getElementsByTagName;
			doc.getElementsByTagName = function(name) {
				var result = slice(canCall ? state.getByTag.call(doc,name) :
					state.getByTag(name));				
				if(name === 'script') {
					result.push( $.$(context.target) );
				}
				return result;
			};
			var f = state.finish;
			state.finish = function() {
				f();
				doc.getElementsByTagName = state.getByTag;
			};
		}
		function replacementWrite(s) {
			state.out +=  s;
		}
		function replacementWriteln(s) {
			state.out +=  s + '\n';
		}
		function immediateWrite(s) {
			var target = $.$(context.target);
			var div = doc.createElement('div');
			target.parentNode.insertBefore(div,target);
			$.replaceWith(div,sanitize(s));
		}
		function immediateWriteln(s) {
			var target = $.$(context.target);
			var div = doc.createElement('div');
			target.parentNode.insertBefore(div,target);
			$.replaceWith(div,sanitize(s) + '\n');
		}
		function makeTemp(id) {
			var t = doc.createElement('div');
			tempEls.push({id:id,el:t});
			// mock contentWindow in case it's supposed to be an iframe
			t.contentWindow = { document: new MockDocument() };
			return t;
		}
		function writeThenGet(id) {
			var target = $.$(context.target);
			var div = doc.createElement('div');
			target.parentNode.insertBefore(div,target);
			$.replaceWith(div,state.out);
			state.out = '';
			return canCall ? state.getEl.call(doc,id) : 
				state.getEl(id);
		}
		function getEl(id) {
			var result = canCall ? state.getEl.call(doc,id) : 
				state.getEl(id);
			return result || findEl(id);
		}
		return state;
	}
	function uncapture(state) {
		doc.write = state.write;
		doc.writeln = state.writeln;
		if(state.getEl) {
			doc.getElementById = state.getEl;
		}
		return state.out;
	}
	
	function clean(code) {
		// IE will execute inline scripts with <!-- (uncommented) on the first
		// line, but will not eval() them happily
		return code && code.replace(/^\s*<!(\[CDATA\[|--)/,'').replace(/(\]\]|--)>\s*$/,'');
	}
	
	function ignore() {}
	function doLog(code,error) {
		console.error("Error",error,"executing code:",code);
	}
	
	var logError = isFunction(global.console && console.error) ? 
			doLog : ignore;
	
	function captureWrite(code,context,options) {
		var state = capture(context,options);
		try {
			doEvil(clean(code));
		} catch(e) {
			logError(code,e);
		} finally {
			uncapture(state);
		}
		return state;
	}
	
	// copied from jQuery
	function isXDomain(src) {
		var parts = /^(\w+:)?\/\/([^\/?#]+)/.exec(src);
		return parts && ( parts[1] && parts[1] != location.protocol || parts[2] != location.host );
	}
	
	function attrPattern(name) {
		return new RegExp('[\\s\\r\\n]'+name+'=(?:(["\'])([\\s\\S]*?)\\1|([^\\s>]+))','i');
	}
	
	function matchAttr(name) {
		var regex = attrPattern(name);
		return function(tag) {
			var match = regex.exec(tag) || [];
			return match[2] || match[3];
		};
	}

	var SCRIPT_TAGS = /(<script[^>]*>)([\s\S]*?)<\/script>/ig, 
		SCRIPT_2 = /<script[^>]*\/>/ig,
		SRC_REGEX = attrPattern('src'),
		SRC_ATTR = matchAttr('src'),
		TYPE_ATTR = matchAttr('type'),
		LANG_ATTR = matchAttr('language'),
		GLOBAL = "__document_write_ajax_callbacks__",
		DIV_PREFIX = "__document_write_ajax_div-",
		TEMPLATE = "window['"+GLOBAL+"']['%d']();",
		callbacks = global[GLOBAL] = {},
		TEMPLATE_TAG = '<script type="text/javascript">' + TEMPLATE + '</script>',
		global_id = 0;
	function nextId() {
		return (++global_id).toString();
	}
	
	function normalizeOptions(options,callback) {
		var done;
		if(isFunction(options)) {
			done = options;
			options = null;
		}
		options = options || {};
		done = done || options && options.done;
		options.done = callback ? function() {
			callback(done);
		} : done;
		return options;
	}
	
	// The global Q synchronizes all sanitize operations. 
	// The only time this synchronization is really necessary is when two or 
	// more consecutive sanitize operations make async requests. e.g.,
	// sanitize call A requests foo, then sanitize B is called and bar is 
	// requested. document.write was replaced by B, so if A returns first, the 
	// content will be captured by B, then when B returns, document.write will
	// be the original document.write, probably messing up the page. At the 
	// very least, A will get nothing and B will get the wrong content.
	var GLOBAL_Q = new Q();
	
	var debug = [];
	var logDebug = window._debugWriteCapture ? function() {} :
		function (type,src,data) {
		    debug.push({type:type,src:src,data:data});
		};

	var logString = window._debugWriteCapture ? function() {} :
		function () {
			debug.push(arguments);
		};

	function newCallback(fn) {
		var id = nextId();
		callbacks[id] = function() {
			fn();
			delete callbacks[id];
		};
		return id;
	}
	
	function newCallbackTag(fn) {			
		return TEMPLATE_TAG.replace(/%d/,newCallback(fn));
	}	

	/**
	 * Sanitize the given HTML so that the scripts will execute with a modified
	 * document.write that will capture the output and append it in the 
	 * appropriate location.  
	 * 
	 * @param {String} html
	 * @param {Object Function} [options]
	 * @param {Function} [options.done] Called when all the scripts in the 
	 * sanitized HTML have run.
	 * @param {boolean} [options.asyncAll] If true, scripts loaded from the
	 * same domain will be loaded asynchronously. This can improve UI 
	 * responsiveness, but will delay completion of the scripts and may
	 * cause problems with some scripts, so it defaults to false.
	 */
	function sanitize(html,options,parentQ,parentContext) {
		// each HTML fragment has it's own queue
		var queue = parentQ && new Q(parentQ) || GLOBAL_Q;
		options = normalizeOptions(options);
		var done = getOption('done',options);
		var doneHtml = '';
		
		var fixUrls = getOption('fixUrls',options);
		if(!isFunction(fixUrls)) {
			fixUrls = function(src) { return src; };
		}
		
		// if a done callback is passed, append a script to call it
		if(isFunction(done)) {
			// no need to proxy the call to done, so we can append this to the 
			// filtered HTML
			doneHtml = newCallbackTag(function() {
				queue.push(done);
			});
		}
		// for each tag, generate a function to load and eval the code and queue
		// themselves
		return html.replace(SCRIPT_TAGS,proxyTag).replace(SCRIPT_2,proxyBodyless) + doneHtml;
		function proxyBodyless(tag) {
			// hack in a bodyless tag...
			return proxyTag(tag,tag.substring(0,tag.length-2)+'>','');
		}
		function proxyTag(element,openTag,code) {
			var src = SRC_ATTR(openTag),
				type = TYPE_ATTR(openTag) || '',
				lang = LANG_ATTR(openTag) || '',
				isJs = (!type && !lang) || // no type or lang assumes JS
					type.toLowerCase().indexOf('javascript') !== -1 || 
					lang.toLowerCase().indexOf('javascript') !== -1;
			
			logDebug('replace',src,element);
			
			if(!isJs) {
			    return element;
			}
			
			var id = newCallback(queueScript), divId = DIV_PREFIX + id,
				run, context = { target: '#' + divId, parent: parentContext };
			
			function queueScript() {
				queue.push(run);
			}
			
			if(src) {
				// fix for the inline script that writes a script tag with encoded 
				// ampersands hack (more comon than you'd think)
				src = fixUrls(src);
								
				openTag = openTag.replace(SRC_REGEX,'');
				if(isXDomain(src)) {
					// will load async via script tag injection (eval()'d on
					// it's own)
					run = loadXDomain;
				} else {
					// can be loaded then eval()d
					if(getOption('asyncAll',options)) {
						run = loadAsync();
					} else {
						run = loadSync;
					}
				}
			} else {
				// just eval code and be done
				run = runInline;
				                  
			}
			function runInline() {
				captureHtml(code);
			}
			function loadSync() {
				$.ajax({
					url: src,
					type: 'GET',
					dataType: 'text',
					async: false,
					success: function(html) {
						captureHtml(html);
					}	
				});
			}
			function logAjaxError(xhr,status,error) {
				logError("<XHR for "+src+">",error);
				queue.resume();
			}
			function setupResume() {
				return newCallbackTag(function() {
					queue.resume();
				});
			}
			function loadAsync() {
				var ready, scriptText;
				function captureAndResume(script,status) {
					if(!ready) {
						// loaded before queue run, cache text
						scriptText = script;
						return;
					}
					try {
						captureHtml(script, setupResume());
					} catch(e) {
						logError(script,e);
					}
				}
				// start loading the text
				$.ajax({
					url: src,
					type: 'GET',
					dataType: 'text',
					async: true,
					success: captureAndResume,
					error: logAjaxError
				});				
				return function() {
					ready = true;
					if(scriptText) {
						// already loaded, so don't pause the queue and don't resume!
						captureHtml(scriptText);
					} else {
						queue.pause();	
					}
				};
			}
			function loadXDomain(cb) {
				var state = capture(context,options);
				queue.pause(); // pause the queue while the script loads
				logDebug('pause',src);
				
				doXDomainLoad(context.target,src,captureAndResume);		

				function captureAndResume(xhr,st,error) {
					logDebug('out', src, state.out);
					html(uncapture(state), 
						newCallbackTag(state.finish) + setupResume());
					logDebug('resume',src);
				}
			}
			function captureHtml(script, cb) {
				var state = captureWrite(script,context,options);
				cb = newCallbackTag(state.finish) + (cb || '');
				html(state.out,cb);
			}
			function safeOpts(options) {
				var copy = {};
				for(var i in options) {
					if(options.hasOwnProperty(i)) {
						copy[i] = options[i];
					}
				}
				delete copy.done;
				return copy;
			}
			function html(markup,cb) {
			 	$.replaceWith(context.target,sanitize(markup,safeOpts(options),queue,context) + (cb || ''));
			} 
			return '<div style="display: none" id="'+divId+'"></div>' + openTag +
				TEMPLATE.replace(/%d/,id) + '</script>';
		}
	}
    
    function doXDomainLoad(target,url,success) {
    	// TODO what about scripts that fail to load? bad url, etc.?
		var script = document.createElement("script");
		script.src = url;

		target = $.$(target);

		var done = false, parent = target.parentNode;

		// Attach handlers for all browsers
		script.onload = script.onreadystatechange = function(){
			if ( !done && (!this.readyState ||
					this.readyState == "loaded" || this.readyState == "complete") ) {
				done = true;
				success();

				// Handle memory leak in IE
				script.onload = script.onreadystatechange = null;
				parent.removeChild( script );
			}
		};		
		
		parent.insertBefore(script,target);        
    }	
	
	/**
	 * Sanitizes all the given fragments and calls action with the HTML.
	 * The next fragment is not started until the previous fragment
	 * has executed completely.
	 * 
	 * @param {Array} fragments array of objects like this:
	 * {
	 *   html: '<p>My html with a <script...',
	 *   action: function(safeHtml,frag) { doSomethingToInject(safeHtml); },
	 *   options: {} // optional, see #sanitize
	 * }
	 * Where frag is the object.
	 * 
	 * @param {Function} [done] Optional. Called when all fragments are done.
	 */
	function sanitizeSerial(fragments,done) {
		// create a queue for these fragments and make it the parent of each 
		// sanitize call
		var queue = GLOBAL_Q;
		each(fragments, function (f) {
			queue.push(run);
			function run() {
				f.action(sanitize(f.html,f.options,queue),f);
			}
		});
		if(done) {
			queue.push(done);		
		}
	}
	
	function findLastChild(el) {
		var n = el;
		while(n && n.nodeType === 1) {
			el = n;
			n = n.lastChild;
			// last child may not be an element
			while(n && n.nodeType !== 1) {
				n = n.previousSibling;
			}
		}
		return el;
	}
		
	/**
	  * Experimental - automatically captures document.write calls and 
	  * defers them untill after page load.
	  * @param {Function} [done] optional callback for when all the 
	  * captured content has been loaded.
	  */
	function autoCapture(done) {
		var write = doc.write, 
			writeln = doc.writeln,
			currentScript,
			autoQ = [];
		doc.writeln = function(s) {
			doc.write(s+'\n');
		};
		var state;
		doc.write = function(s) {
			var scriptEl = findLastChild(doc.body);
			if(scriptEl !== currentScript) {
				currentScript = scriptEl;
				autoQ.push(state = {
					el: scriptEl,
					out: []
				});					
			}
			state.out.push(s);
		};
		$.onLoad(function() {			
			// for each script, append a div immediately after it, 
			// then replace the div with the sanitized output
			var el, div, out, safe, doneFn;
			done = normalizeOptions(done);
			doneFn = done.done;
			done.done = function() {
				doc.write = write;
				doc.writeln = writeln;
				if(doneFn) doneFn();				
			};
			for(var i = 0, len = autoQ.length; i < len; i++ ) {
				el = autoQ[i].el;
				div = doc.createElement('div');
				el.parentNode.insertBefore( div, el.nextSibling );
				out = autoQ[i].out.join('');
				// only the last snippet gets passed the callback
				safe = len - i === 1 ? sanitize(out,done) : sanitize(out);
				$.replaceWith(div,safe);
			}
		});
	}
	
	function extsrc(cb) {
		var scripts = document.getElementsByTagName('script'),
			s,o, html, q, ext, async, doneCount = 0,
			done = cb ? newCallbackTag(function() {
				if(++doneCount >= exts.length) {
					cb();
				}
			}) : '',
			exts = [];
			
		for(var i = 0, len = scripts.length; i < len; i++) {
			s = scripts[i];
			ext = s.getAttribute('extsrc');
			async = s.getAttribute('asyncsrc');
			if(ext || async) {
				exts.push({ext:ext,async:async,s:s});
			}
		}

		for(i = 0, len = exts.length; i < len; i++) {
			o = exts[i];
			if(o.ext) {
				html = '<script type="text/javascript" src="'+o.ext+'"> </script>';
				$.replaceWith(o.s,sanitize(html) + done);				
			} else if(o.async) {
				html = '<script type="text/javascript" src="'+o.async+'"> </script>';
				$.replaceWith(o.s,sanitize(html,{asyncAll:true}, new Q()) + done);
			}
		}
	}
	
	var name = 'writeCapture';
	var self = global[name] = {
		_original: global[name],
		support: $,
		/**
		 */
		fixUrls: function(src) {
		    return src.replace(/&amp;/g,'&');
		},
		noConflict: function() {
			global[name] = this._original;
			return this;
		},
		debug: debug,
		/**
		 * Enables a fun little hack that replaces document.getElementById and
		 * creates temporary elements for the calling code to use.
		 */
		proxyGetElementById: false,
		// this is only for testing, please don't use these
		_forTest: {
			Q: Q,
			GLOBAL_Q: GLOBAL_Q,
			$: $,
			matchAttr: matchAttr,
			slice: slice,
			capture: capture,
			uncapture: uncapture,
			captureWrite: captureWrite
		},
		replaceWith: function(selector,content,options) {
			$.replaceWith(selector,sanitize(content,options));
		},
		html: function(selector,content,options) {
			var el = $.$(selector);
			el.innerHTML ='<span/>';
			$.replaceWith(el.firstChild,sanitize(content,options));
		},	
		load: function(selector,url,options) {
			$.ajax({
				url: url,
				dataType: 'text',
				type: "GET",
				success: function(content) {
					self.html(selector,content,options);
				}
			});
		},
		extsrc: extsrc,
		autoAsync: autoCapture,
		sanitize: sanitize,
		sanitizeSerial: sanitizeSerial
	};
	
})(this.writeCaptureSupport,this);

/**
 * jquery.writeCapture.js 
 * 
 * Note that this file only provides the jQuery plugin functionality, you still
 * need writeCapture.js. The compressed version will contain both as as single
 * file.
 *
 * @author noah <noah.sloan@gmail.com>
 * 
 */
(function($,wc,noop) {
	// methods that take HTML content (according to API)
	var methods = {
		html: html
	};
	// TODO wrap domManip instead?
	$.each(['append', 'prepend', 'after', 'before', 'wrap', 'wrapAll', 'replaceWith',
		'wrapInner'],function() { methods[this] = makeMethod(this); });
	
	function isString(s) {
		return Object.prototype.toString.call(s) == "[object String]";
	}
	
	function executeMethod(method,content,options,cb) {
		if(arguments.length == 0) return proxyMethods.call(this);
		
		var m = methods[method];
		if(method == 'load') {
			return load.call(this,content,options,cb);
		}
		if(!m) error(method);
		return doEach.call(this,content,options,m);
	}
	
	$.fn.writeCapture = executeMethod;
	
	var PROXIED = '__writeCaptureJsProxied-fghebd__';
	// inherit from the jQuery instance, proxying the HTML injection methods
	// so that the HTML is sanitized
	function proxyMethods() {
		if(this[PROXIED]) return this;
		
		var jq = this;
		function F() {
			var _this = this, sanitizing = false;
			this[PROXIED] = true;
			$.each(methods,function(method) {
				var _super = jq[method];
				if(!_super) return;
				_this[method] = function(content,options,cb) {
					// if it's unsanitized HTML, proxy it
					if(!sanitizing && isString(content)) {
						try {
							sanitizing = true;
							return executeMethod.call(_this,method,content,
								options,cb);
						} finally {
							sanitizing = false;
						}
					} 
					return _super.apply(_this,arguments); // else delegate
				};
			});
			// wrap pushStack so that the new jQuery instance is also wrapped
			this.pushStack = function() {
				return proxyMethods.call(jq.pushStack.apply(_this,arguments));
			};
			this.endCapture = function() { return jq; };
		}
		F.prototype = jq;
		return new F();
	}
	
	function doEach(content,options,action) {
		var done, self = this;
		if(options && options.done) {
			done = options.done;
			delete options.done;
		} else if($.isFunction(options)) {
			done = options;
			options = null;
		}
		wc.sanitizeSerial($.map(this,function(el) {
			return {
				html: content,
				options: options,
				action: function(text) {
					action.call(el,text);
				}
			};
		}),done && function() { done.call(self); } || done);
		return this;
	}
	
	
	function html(safe) {
		$(this).html(safe);
	}
	
	function makeMethod(method) {
		return function(safe) {
			$(this)[method](safe);
		};
	}
	
	function load(url,options,callback) {
		var self = this,  selector, off = url.indexOf(' ');
		if ( off >= 0 ) {
			selector = url.slice(off, url.length);
			url = url.slice(0, off);
		}
		if($.isFunction(callback)) {
			options = options || {};
			options.done = callback;
		}
		return $.ajax({
			url: url,
			type:  options && options.type || "GET",
			dataType: "html",
			data: options && options.params,
			complete: loadCallback(self,options,selector)
		});
	}
	
	function loadCallback(self,options,selector) {
		return function(res,status) {
			if ( status == "success" || status == "notmodified" ) {
				var text = getText(res.responseText,selector);
				doEach.call(self,text,options,html);
			}
		};
	}
	
	var PLACEHOLDER = /jquery-writeCapture-script-placeholder-(\d+)-wc/g;
	function getText(text,selector) {
		if(!selector || !text) return text;
		
		var id = 0, scripts = {};			
		return $('<div/>').append(
			text.replace(/<script(.|\s)*?\/script>/g, function(s) {
				scripts[id] = s;
				return "jquery-writeCapture-script-placeholder-"+(id++)+'-wc';
			})
		).find(selector).html().replace(PLACEHOLDER,function(all,id) {
			return scripts[id];
		});
	}
	
	function error(method) {
		throw "invalid method parameter "+method;
	}
	
	// expose core
	$.writeCapture = wc;
})(jQuery,writeCapture.noConflict());

if(typeof(adSetupLoader) == "undefined"){
	function adSetupLoader(settings){
		settings.id = "natad" + settings.size + "-" + Math.floor(Math.random()*51);
		var adHolder = "<div id='"+settings.id+"'></div>";
		document.write(adHolder);
		var pageloadtimer, adsloaded = false;
		var timerset = false;
		function loadAd(){
			if ( !adsloaded ){
				var adsrc =bimcreateDartTag(settings);
				$("#"+settings["id"]).writeCapture().html('<!-- bim ad source '+ adsrc +' --><script language="JavaScript" src="'+adsrc+'" type="text/javascript"><\/script>',{proxyGetElementById:true});
				adsloaded = true;
			}
			if(timerset){
				clearTimeout(pageloadtimer);
				var timerset = false;
				bimreg.listener.removeListener("regAuth",self);
			}
		}
		if(typeof(bimreg) != "undefined"){
			bimreg.listener.addListener("userLogin",loadAd);
			pageloadtimer = setTimeout(loadAd,3000);
			timerset = true;
		}else{
			$(document).ready(function(){
				loadAd();
			});
		}
	}
}

