GEL.namespace("bus"); 

GEL.bus.Behavior= (function(){ 
	var RevSciProvider= (function(){

	   var _CFG= GEL.namespace("config.revsci"); 
	   var 	_CSID= _CFG.csid || 
			"J06575", 
		_GW_SCRIPT= _CFG.gwscript || 
			"http://js.revsci.net/gateway/gw.js",
		_HEAD= document.getElementsByTagName("HEAD")[0],
		_SECTION_MAP= { 
			life: false, 
			money: false, 
			news: false, 
			tech: false, 
			travel: false, 
			video: false, 
			umbrella: false
		}
	; 
		_GW_SCRIPT+="?csid=" + _CSID; 
	
	return function(options){ 
		options= options || {}; 
		var 
			_self= this, 
			_url= 'scriptUrl' in options ? 
				options.scriptUrl : _GW_SCRIPT, 
			_attachEl= 'attachEl' in options ? 
				options.attachEl : _HEAD,
			_rsiO= null, 
			_fetcher= new GEL.remoting.Fetcher.DocumentOverwriter({
				attachTo: _attachEl, 
				url: _url
			}), 
			_ssts= {section: null, subsection: null,topic: null, subtopic: null}, 
			_user= {},
			_PROP_MAP= { 
				zip: function(){ _user.zipcode= GEL.env.user.zip; }, 
				yob: function(){ _user.age= GEL.env.user.age }, 
				gender: _user, 
				section: _ssts, 
				subsection: _ssts,
				topic: _ssts, 
				subtopic: _ssts 
			}
		; 
		this.setCategory= function(){ 
			var _category= this.getCategory(arguments[0]); 
			_rsiO.DM_cat(_category); 
			return this; 
		}
		function getSSTSArray(ssts){ 
			ssts= ssts || _ssts; 
			var _a= "section,subsection,topic,subtopic".split(","),
				_l= []
			; 
			for (var i=0;i<_a.length;i++){
				if(_a[i] in ssts && ssts[_a[i]]) 
					_l.push(ssts[_a[i]]); 
				else 
					break; 
			}
			return _l; 
		}
		this.getCategory= function(ssts){
			ssts= ssts || _ssts; 
			var 
				_cat= "newspaper > ", 
				sstsList= ssts instanceof Array ? 
					ssts : getSSTSArray(ssts)
			; 
			if (sstsList[0] in _SECTION_MAP)
				_cat += sstsList.join(" > "); 
			else 
				_cat += 'other'; 
			return _cat; 
		}
		this.setProperties= function(a){ 
			for(var k in a) 
				_rsiO.DM_addToLoc(k,a[k]); 
		}
		this.submit= function(){ 
			this.setCategory(arguments[0]); 
			this.setProperties(_user); 
			_rsiO.DM_tag(); 
			return this; 
		}
		this.setAttribute= function(attr,value){ 
			if(!(attr in _PROP_MAP)) return this; 
			if(
				typeof _PROP_MAP[attr] == 'object'
			) 
				_PROP_MAP[attr][attr]= value;
			else if (typeof _PROP_MAP[attr] == 'function') 
				_PROP_MAP[attr](value,attr); 
			return this; 
		}
		this.loadLibrary= function(){ 
			_fetcher.on("ready", initRS); 
			_fetcher.attach(); 
			function initRS(){ 
				this.unsubscribe("ready", arguments.callee); 
				var 
					_rawsegs= window['rsinetsegs'],
					_segs= [],
					_segRe= new RegExp('^'+_CSID+'_', 'i'),
					_event= {rawsegments: [], segments: []}
				; 
				if(typeof window[_CSID] == 'undefined'){ 
					_self.fire("error", _self, { 
						message: "Failed to load revsci provider libraries " + 
							"from url: " + _url
						}
					); 
				}else if (typeof _rawsegs!= 'undefined') { 
					for(var i=0;i<rsinetsegs.length;i++){
						var a= rsinetsegs[i]; 
						_segs.push(a.replace(_segRe,''));  
					}
					_event.rawsegments= _rawsegs; 
					_event.segments= _segs; 
				}
				_rsiO= window[_CSID]; 
				_rsiO._rsiia= "gif";
				_self.fire("segmentsReceived", _self, _event); 
				return _self; 
			} 
		}
	}; 
	})(); 
	GEL.extend(RevSciProvider, GEL.event.Publisher); 
	return function(){ 
		var 
			_self= this, 
			_UAC= GEL.namespace("config.userauth"); 
			_provider= new RevSciProvider() ,
			_properties= {}, 
			_needsTrack= false, 
			_providerReady= false, 
			_cookieSetUrl= (function(){ 
				if(
					typeof GDN != 'undefined' && 
					typeof GDN.RegUrl == 'function'
				) 
					return GDN.RegUrl( "q=5&Segment="); 
				else  
					return [ 
						window.location.protocol, '', 
						_UAC.host, 
						'GCION.ashx?q=5&Segment='
					].join("/"); 
			})()
		; 
		this.trackLink= new Function(); 
		_provider.on("segmentsReceived", onProviderReady); 
		this.insertCategory= function(ssts,sect,value){
			var _a= []; 
			if(!ssts instanceof Array) 
				ssts= [ ssts ];
			for(var i=0;i<ssts.length;i++) {
				if(ssts[i]) _a.push(ssts[i]); 
				else break; 
			}
			_provider.insertCategory(_a,sect,value);
		}
		this.setAttribute= function(key,value){ 
			_properties[key]= value; 
			_provider.setAttribute(key,value); 
		}
		this.init= function(){ 
			_provider.loadLibrary(); 
		}
		this.trackView= function(){ 
			if(_providerReady) 
				_provider.submit(arguments[0]); 
			else{ 
				_needsTrack= true; 
			}
		}

		function onProviderReady(type, event){ 
		/* 
			This is the business logic driving whether or not 
		     we set the cookie and does not belong here 
		*/
			var 
				_segs= event.segments, 
				_raw= event.rawsegments,
				v= ''
			;
			if(
				_segs.length > 0
				&& !parseInt(GEL.env.sessioncache.getValue("ubt"), 10)
			) {  
				setGciSegments(_raw); 
				GEL.env.sessioncache.setValue("ubt", 1); 
				GEL.env.persistentcache.setValue(
					"segments", 
					_segs.join("|")
				); 
			}
			_providerReady= true; 
			if(_needsTrack) 
				_self.trackView(); 
		}
		function setGciSegments(segs){ 
			var 
				_url= getCookieUrl(escape(segs.join("|"))), 
				_img= new Image()
			;
			_img.src= _url; 
			return true; 
		}
		function getCookieUrl(segs){
			if(
				typeof GDN != 'undefined' && 
				typeof GDN.RegUrl == 'function'
			) 
				return GDN.RegUrl( "q=5&Segment=" + segs ); 
			else  
				return [ 
					window.location.protocol, '', 
					_UAC.host, 
					'GCION.ashx?q=5&Segment=' + segs
				].join("/"); 
		}
	}
})(); 
GEL.bus.PageTrack= (function(){ 
	var SiteCatalystProvider= (function(){ 
		var _PROP_MAP= {
			custom1: 'prop1',
			custom2: 'prop2',
			custom3: 'prop3',
			custom4: 'prop4',
			custom5: 'prop5',
			custom6: 'prop6',
			custom7: ['prop7','eVar5'],
			custom8: 'prop8',
			contenttype: 'prop16',
			section: 'prop17',
			subsection: 'prop18',
			topic: 'prop19',
			subtopic: 'prop20',
			videourl: 'prop21',
			location: 'prop23',
			marketname: ['eVar1', 'prop25'],
			syndication: 'prop26',
			gcionid: 'prop27',
			version: 'prop28',
			status: 'prop29',
			zip: 'prop30',
			yob: 'prop31',
			gender: 'prop32',
			occupation: 'prop33',
			industry: 'prop34',
			country: 'prop35',
			site: 'prop36',
			datecreated: 'prop37',
			zaged: 'prop38',
			useraction: "prop41",
			galleryTitle: "prop43",
			picno: "prop45", 
			segments: 'prop48',
			division: 'prop50',
			v5: 'eVar5'
		},
		_CFG= GEL.namespace("config.omniture"),
		_W= window, 
		_PROP_REGEX= new RegExp("p([0-9]+)", "i")
		;
		return function(options){ 
			options= options || {}; 
			var 
				_self= this, 
				_attributes= {},
				_account= 
					_CFG.account || 
					"gpaper107,gntbcstglobal,__default",
				_omScode
			; 
			if(
				typeof _CFG.s_code == 'object' || 
				typeof window.s_gi == 'function'
			) {
				this.s= window['s'] = _omScode= _CFG.s_code || new s_gi(_account);
			}else { 
				GEL.log("No s_code?"); 
				this.s= _omScode= { 
					tl: new Function(), 
					t: new Function()
				}
			}
			this.init= function(){ 
				initSiteCat(); 
				initPlugins(); 
			}
			this.setAttribute= function(key,value){ 
				var _key= key; 
				if(_key in _PROP_MAP){ 
					_key= _PROP_MAP[_key]; 
				}else if (_PROP_REGEX.test(_key)){
					_key= _key.replace(_PROP_REGEX, "prop$1"); 
				}
				if(_key instanceof Array) 
					for(var i=0;i<_key.length;i++) 
						_omScode[_key[i]]= value; 
				else 
					_omScode[_key]= value; 	
			}
			this.getAttribute= function(key){ 
				var _key= key; 
				if(_key in _PROP_MAP)
					_key= _PROP_MAP[_key]; 
				return _omScode[_key]; 
			}
			this.trackView= function(){ 
				_omScode.t();
			}
			this.trackLink= function(type){ 
				if(!type){ 
					GEL.log("WARNING: You must provide a link TYPE when tracking links using SiteCatalyst"); 
					return; 
				}
				var 
					_s= _omScode, 
					_trackVars= _omScode.linkTrackVars, 
					_trackEvents= _omScode.linkTrackEvents
				;
				_s.linkTrackVars= "prop41,events"; 
				_s.linkTrackEvents= _s.events= "event1"; 
				_s.prop41= type; 
				_s.tl(_W,'o',type); 
				_s.linkTrackVars= _trackVars; 
				_s.linkTrackEvents= _s.events= _trackEvents; 
				delete _s['prop41']; 
				return this; 
			}
			function initSiteCat(){ 
				var _t= ''; 
				for(var o in _CFG){
					_t= typeof _CFG[o]; 	
					if(
						_t == 'string'
						|| _t == 'boolean' 
						|| _t == 'number'
					){
						_omScode[o] = _CFG[o]; 
					}
				}
			}
			function initPlugins(){
				for(var p in _CFG.plugins){ 
					_omScode[p]= _CFG.plugins[p];
				}
				_omScode.doPlugins= doPlugins; 

			}
			function doPlugins(){ 
				if('s_prePlugins' in _W && typeof _W['s_prePlugins'] == 'function') 
					_W['s_prePlugins'](_omScode); 
				if('s_postPlugins' in _W && typeof _W['s_postPlugins'] == 'function')
					_W['s_postPlugins'](_omScode); 
				_omScode.campaign= _omScode.getQueryParam('source'); 
				var 
					_tc= _omScode.campaign,  
					_pn= _omScode.pageName, 
					_curr= _omScode.c_r("tc")
				;
				if(_tc){
					_self.setAttribute("syndication",  _tc +": "+_pn); 
					_omScode.c_w("tc", _tc, 0); 
				}
				if(_tc & _curr!=_tc) {
					_self.setAttribute("syndication", _tc + " : " + _pn); 
					_omScode.c_w("tc", _tc, 0); 
				}
				if(_curr && !_tc) 	
					_self.setAttribute("syndication", _pn);  
					
				_omScode.events=s.events&&s.events.search(/event3/)==-1?s.events+',event3':'event3';
			}
		}
	})(), 
	CheckMateProvider= (function(){ 
		var 
			_CFG= GEL.namespace("config.checkmate"),
			_SECTION_MAP= { 
				umbrella: "Home", 
				money: "Business", 
				business: "Business", 
				entertainment: "Entertainment", 
				life: "Entertainment", 
				news: "News", 
				sports: "Sports", 
				travel: "Travel"
			},
			_SERVER= _CFG.server || 
				"q1.checkm8.com", 
			_PROFILE= _CFG.profile || "", 
			_SCRIPT_LIB= _CFG.scriptlib || 
				"http://q1digital.checkm8.com/adam/cm8adam_1_call.js",
			_DOMAIN= _CFG.domain ||
				(function(){ 
					var _a= window.location.hostname.split("."); 
					return _a[_a.length - 2]
				})()
		; 
		return function(options){ 
			var _section= ''; 
			this.init= function(options){
			}
			this.setAttribute= function(key,value){ 
				if(key.toLowerCase() != 'section') return this; 
				value= value.toLowerCase(); 
				if(value in _SECTION_MAP)
					_section= _DOMAIN + '.' + _SECTION_MAP[value]; 
				return this; 
			}
			this.trackView= function(){ 
				var 
					_u= _SCRIPT_LIB,
					_ow= new GEL.remoting.Fetcher.
						DocumentOverwriter({
							url: _u
						}); 
				window["CM8Server"]= _SERVER; 
				window["CM8Cat"]= _section; 
				window["CM8Profile"]= _PROFILE; 
				_ow.attach(); 
			}
			this.getAttribute= function(key){ 
				if(key.toLowerCase() === 'section') 
					return _section; 
				else 
					return null; 
			}
			this.trackLink= new Function();  
		}
	})(),
	_PROVIDER_MAP= { 
		checkm8: CheckMateProvider, 
		bt: GEL.bus.Behavior, 
		sitecat: SiteCatalystProvider
	}, 
	_DEFAULT_PROVIDERS= [],
	_CFG= GEL.namespace("config.bus")
	;

	return function(options){ 
		var 
			_self= this,
			_PROPS= GEL.config.bus.properties
		;
		options= options || {}; 
		this.providers= {}; 
		this.init= function(provider){ 
			var 
				_p= '',
				_providers= 
					provider ||
					options.providers || 
					options.provider ||  
					[],
				_return= [],
				_provider= null
			; 
			if(typeof _providers=='string') 
				_providers= [_providers]; 
			else if(_providers instanceof Array && _providers.length <= 0) 
				_providers= _DEFAULT_PROVIDERS; 
			else if(typeof _providers == 'undefined') 
				_providers= _DEFAULT_PROVIDERS; 
			for(var i=0;i<_providers.length;i++){ 
				_p= _providers[i]; 
				_provider= this.addProvider(_p); 
				if(!_provider){
					GEL.log("Unable to add provider " + _p); 
					continue; 
				}	
				_provider.init(); 
				_return.push(_provider); 
			}

			this.setPageInfo(GEL.thepage.pageinfo.ssts); 
			this.setUserInfo(); 
			setConfigAttributes(); 
			return _return; 
		}
		this.addProvider= function(p){ 
			if(p in this.providers) return this.providers[p]; 
			if(!(p in _PROVIDER_MAP)) return null; 
			this.providers[p]= 
				new _PROVIDER_MAP[p](); 
			return this.providers[p];
		}
		function setConfigAttributes(){ 
			for (var _a in _PROPS){ 
				if(_PROPS[_a])
					_self.setAttribute(_a, _PROPS[_a]); 
			}
		}

	}; 
})(); 
GEL.bus.PageTrack.prototype= { 
	setAttribute: function(key,value){ 
		for (var p in this.providers) {
			this.providers[p].setAttribute(key,value); 
		}
	},
	setEvent: function(eventName){ 
		for (var p in this.providers) {
			this.providers[p].setEvent(eventName); 
		}
		
	},
	trackView: function(){ 
		for (var p in this.providers){ 
			this.providers[p].trackView(); 
		}
	},
	trackLink: function(type){ 
		for (var p in this.providers){ 
			this.providers[p].trackLink(type); 
		}
	},
	setPageInfo: function(ssts){ 
		/* Only set SSTS info if we have a value */
		ssts= ssts || GEL.thepage.pageinfo.ssts; 	
		var _p= "section,subsection,topic,subtopic".split(","); 
		for (var i=0;i<_p.length;i++){ 
			if(_p[i] in ssts && ssts[_p[i]])
				this.setAttribute(_p[i], ssts[_p[i]]); 
		}
		return this; 
	},
	setUserInfo: function(user){
		user= user || GEL.env.user; 
		var _p= { 
			segments: user.segments || "no segments",  
			zip: user.zip, 
			yob: user.yob, 
			gender: user.gender, 
			occupation: user.occupation, 
			industry: user.industry, 
			country: user.country, 
			site: user.site,
			gcionid: user.uid, 
			version: user.getValue("version"), 
			datecreated: user.getValue("datecreated"), 
			status: user.getValue("status") ? user.getValue("status") : 1,
			zaged:(function(){ 
				var _s= user.getValue("status"); 
				if(_s && _s > 1) return true; 
				else return false; 
			})()
		};
		for (var a in _p)
			this.setAttribute(a, _p[a]); 
	}
};


