/* cnn personalization module js
 ========================================================================= */

var MainLocalObj = (typeof Class == "object") ? Class.create() : {};
MainLocalObj = {

	//declare vars
	data: {
		weatherLoc: {
			locCode: '',
			zip: '',
			name: ''
		},
		sports: {},
		stockSymbols: [],
		prevStockSymbols: []
	},
	
	//initialize modules
	init: function() {
	
		//environment
		this.internationalUser = (location.hostname.indexOf('edition.') > -1) ? true : false;
		this.isProfilePage = (location.pathname.indexOf('profile') > -1) ? true : false;
		
		//cookies
		this.allCookies = allCookies;
		this.defaultExpanded = this.allCookies["pm.defaultExpanded"] || null;
		this.defaultExpandedProfile = this.allCookies["pm.defaultExpandedProfile"] || null;
		
		//is market open?
		if (!this.internationalUser && this.defaultExpanded === null) {
			if (cnnCurrDay !== 'Sat' && cnnCurrDay !== 'Sun' && cnnCurrHour >= 9 && cnnCurrHour <= 18) {
				this.defaultExpanded = 4;
			}
		} else if (this.defaultExpanded === null || (this.internationalUser && this.defaultExpanded > 3)) {
			this.defaultExpanded = 1;
		}
		
		//expiry date and path for saving data
		MainLocalObj.expDate = new Date(1640926800000); //Fri Dec 31 2021 00:00:00 GMT-0500
		this.msUrl = 'http://audience.cnn.com/services/cnn/user.api';
		
		//instantiate storage manager
		this.StorageManagerInstance = StorageManager.getInstance();
		
		//force CookieStorage for IE by removing IEStorage
		//IEStorage was not working for WS (Nov 09)
		var desiredStores = [];
		var availableStores = this.StorageManagerInstance.availableStores;
		availableStores.each(function(val) {
			if (val.name !== 'IEStorage') {
				desiredStores.push(val);
			}
		});
		this.StorageManagerInstance.availableStores = desiredStores;
		
		//get storage
		this.StorageManager = this.StorageManagerInstance.getStorage();
		this.StorageManager.load();
		
		//instantiate CSI manager
		this.CSIManager = CSIManager.getInstance();
		
		//logged in/out logic
		if (ms_isLoggedIn() === true) {
			var cnnUserName = (!this.allCookies.firstName || this.allCookies.firstName === 'undefined') ? this.allCookies.displayname : this.allCookies.firstName;
			$('pmLoggedIn').show();
			$('pmLoggedOff').hide();
			$('pmUserName').update('Hi, ' + cnnUserName);
			$('avatarImg').src = 'http://avatar.cnn.com/people/' + this.allCookies.displayname + '/avatar/35.png';
			$('avatarImg').alt = cnnUserName;
		} else {
			$('pmLoggedIn').hide();
			$('pmLoggedOff').show();
		}
		
		//accordion
		this.accordion = $('pmSlidebox');
		this.options = {
			toggleClass: "accordion-toggle",
			toggleActive: "accordion-toggle-active",
			contentClass: "accordion-content"
		};
		this.contents = this.accordion.select('div.' + this.options.contentClass);
		this.isAnimating = false;
		this.maxHeight = 0;
		this.current = this.defaultExpanded ? this.contents[this.defaultExpanded - 1] : this.contents[0];
		this.toExpand = null;
		this.checkMaxHeight();
		var clickHandler = this.clickHandler.bindAsEventListener(this);
		this.accordion.observe('click', clickHandler);
		
		//load data
		this.loadDefaultData();
		
	},
	
	initMod: function(currentId) {
		//follow on functions
		switch (currentId) {
			case 'pmWeather':
			case 'pmWeatherIntl':
				if (typeof MainLocalObj.Weather.isInitialized === 'undefined') {
					MainLocalObj.Weather.init();
				}
			break;
			case 'pmSports':
				if (typeof MainLocalObj.Sports.isInitialized === 'undefined') {
					MainLocalObj.Sports.init();
				}
			break;
			case 'pmMarkets':
				if (typeof MainLocalObj.Markets.isInitialized === 'undefined') {
					MainLocalObj.Markets.init();
				}
			break;
		}
	},
	
	setUserSpecificData: function(flag) {
		this.data.userSpecificData = flag;
	},
	
	setInternationalUser: function(flag) {
		this.internationalUser = flag;
	},
	
	setLocationZip: function(type, zip) {
		this.data.weatherLoc.zip = zip;
	},
	
	setLocationName: function(type, name) {
		this.data.weatherLoc.name = name;
	},
	
	setLocationLocCode: function(type, code) {
		if (type == 'weather') {
			this.data.weatherLoc.locCode = code;
		}
	},
	
	parseMSData: function(obj) {
		msQueueManager.requestReceived(); // lets iJax know it can process the next request
		if (typeof obj.status !== 'undefined' && obj.status === 'success' && obj.data !== '') {
			this.data = obj.data.evalJSON(true);
			this.data.lastMemberServicesFetch = new Date().getTime();
			this.StorageManager.put('localData', this.data, MainLocalObj.expDate);
			this.StorageManager.save();
		} else if (obj.data == '') {
			this.loadDefaultData(true);
		} else {
			this.data = this.storedData;
		}
		this.saveWeatherCookie();
		this.initialShowHide();
		this.initMod(this.current.id);
	},
	
	parseWeatherCookie: function() {
	
		var lwpCookie = this.allCookies["lwp.weather"] || null;
		var lwpLocCode = false;
		var lwpZip = false;
		
		//parse weather cookie, latest or default value is ours, otherwise used for weather page
		if (lwpCookie) {
			var locationArr = unescape(lwpCookie).split('|');
			var weatherLocParse = locationArr[0];
			if (lwpCookie.indexOf('~') == -1) {
				weatherLocParse = lwpCookie.replace('|', '~');
			}
			var lwpDataArr = locationArr[0].split('~');
			
			return lwpDataArr;
			
		} else {
			return [false, false];
		}
		
	},
	
	loadDefaultData: function(forceDefaultWeather) {
	
		if (typeof forceDefaultWeather === 'undefined') {
			forceDefaultWeather = false;
		}
		
		this.storedData = false;
		
		//parse weather cookie for latest search
		var weatherCookie = this.parseWeatherCookie();
		var lwpLocCode = weatherCookie[0];
		var lwpZip = weatherCookie[1];
		
		//user is logged in
		if (ms_isLoggedIn() && !forceDefaultWeather) {
		
			//any local data?
			if (this.StorageManager.contains('localData')) {
				this.storedData = this.StorageManager.get('localData');
			}
			
			//greater than 30 mins since last fetch?
			function compareFetchDates(fetchDate) {
				if (fetchDate === null || typeof fetchDate === 'undefined' || (new Date().getTime() - fetchDate) > 1800000) {
					return true;
				} else {
					return false;
				}
			}
			
			//if data hasn't been fetched in the last 30 mins then sync-up
			if (compareFetchDates(this.storedData.lastMemberServicesFetch)) {
				var queueItem = new ms_QueueItem(this.msUrl, 'get');
				queueItem.addParam('action', 'getData');
				queueItem.addParam('name', 'teams');
				queueItem.addParam('callback', 'MainLocalObj.parseMSData');
				var queueItemObj = queueItem.getQueueItem();
				msQueueManager.addRequest(queueItemObj);
			} else if (this.storedData) {
				this.data = this.storedData;
				this.initialShowHide();
				this.initMod(this.current.id);
				//set weather cookie to local storage
				this.saveWeatherCookie();
			}
			
			this.data.userSpecificData = true;
			
		} else if (lwpZip) {
			if (this.checkZip(lwpZip)) {
				this.data.userSpecificData = true;
				this.setLocationZip('all', lwpZip);
				this.setLocationLocCode('all', lwpLocCode);
			} else {
				this.Weather.requestInternationalCityLookup(lwpZip, lwpLocCode, 'all');
			}
			this.initialShowHide();
			this.initMod(this.current.id);
		} else if (!this.internationalUser) {
			var randomCityData = [{
				name: 'Welcome, NC',
				zip: '27374'
			}, {
				name: 'Cool, CA',
				zip: '95614'
			}, {
				name: 'Truth or Consequences, NM',
				zip: '87901'
			}, {
				name: 'Okay, OK',
				zip: '74446'
			}, {
				name: 'Ideal, GA',
				zip: '31041'
			}, {
				name: 'Success, MO',
				zip: '65570'
			}, {
				name: 'North, SC',
				zip: '29112'
			}, {
				name: 'Earth, TX',
				zip: '79031'
			}, {
				name: 'Odd, WV',
				zip: '25902'
			}];
			
			var randNum = Math.floor(Math.random() * randomCityData.length);
			var randomObj = randomCityData[randNum];
			this.setLocationZip('all', randomObj.zip);
			this.setLocationName('all', randomObj.name);
			this.initialShowHide();
			this.initMod(this.current.id);
		} else if (this.internationalUser) {
			this.setLocationZip('all', '336736767676');
			this.setLocationName('all', 'London, England');
			this.initialShowHide();
			this.initMod(this.current.id);
		} else {
			this.initialShowHide();
			this.initMod(this.current.id);
		}
		
	},
	
	saveWeatherCookie: function() {
	
		var lwpCookie = this.allCookies["lwp.weather"] || null;
		var newVal = '';
		if (this.data) {
			newVal = this.data.weatherLoc.locCode + '~' + this.data.weatherLoc.zip;
		}
		var cookieValue;
		var weatherCookie = '';
		
		//saves last lookups in cookie for weather page
		if (lwpCookie) {
			var locationArr = unescape(lwpCookie).split('|');
			locationArr.unshift(newVal);
			for (var i = 0; i < locationArr.length; i++) {
				if (locationArr[i] !== newVal && (weatherCookie.indexOf(locationArr[i]) === -1) || i === 0) {
					weatherCookie += locationArr[i];
					if (i < (locationArr.length - 1)) {
						weatherCookie += "|";
					}
				}
			}
		} else {
			weatherCookie = newVal;
		}
		
		CNN_setCookie('lwp.weather', weatherCookie, 24 * 30 * 12, '/', document.domain);
		
	},
	
	saveData: function(suppressWeather) {
	
		var queueItem;
		var queueItemObj;
		
		if (typeof suppressWeather === 'undefined') {
			suppressWeather = false;
		}
		
		//cookie used in other parts of the site
		this.saveWeatherCookie();
		
		//used in footer
		if (typeof cnnWeather === "object" && suppressWeather !== true) {
			cnnWeather.init();
		}
		
		if (ms_isLoggedIn()) {
		
			//save data to local storage
			this.StorageManager.put('localData', this.data, MainLocalObj.expDate);
			this.StorageManager.save();
			
			//sync up with member services
			queueItem = new ms_QueueItem(this.msUrl, 'get');
			queueItem.addParam('action', 'setData');
			queueItem.addParam('name', 'teams');
			queueItem.addParam('data', Object.toJSON(this.data));
			queueItem.addParam('callback', 'MainLocalObj.monitorMSSave');
			queueItemObj = queueItem.getQueueItem();
			msQueueManager.addRequest(queueItemObj);
			
		}
	},
	
	monitorMSSave: function(obj) {
		if (typeof obj.status !== 'undefined' && obj.status === 'success') {
			msQueueManager.requestReceived();
		}
	},
		
	trimWS: function(str) {
		str = str.replace(/,/g, ' ');
		str = str.replace(/^\s*(\S+)*\s*$/, "$1");
		str = str.replace(/(\s{1})\s*/g, "$1");
		return str;
	},
	
	urlEncode: function(str) {
		str = this.trimWS(str);
		str = str.replace(/st\./i, 'saint');
		str = str.replace(/mt\./i, 'mount');
		str = escape(str);
		return str;
	},
	
	loadJsFile: function(filename) {
		var fileref = document.createElement('script')
		fileref.setAttribute("type", "text/javascript")
		fileref.setAttribute("src", filename)
		document.getElementsByTagName("head")[0].appendChild(fileref)
	},
	
	ucwords: function(str) {
		return (str + '').replace(/^(.)|\s(.)/g, function($1) {
			return $1.toUpperCase();
		});
	},
	
	daysInMonth: function(month, year) {
		var dd = new Date(year, month, 0);
		return dd.getDate();
	},
	
	urlDecode: function(str) {
		return unescape(str);
	},
	
	checkZip: function(str) {
		str = this.trimWS(str.toString());
		var bool = ((str.match(/^\d{5}$/) !== null || str.match(/^\d{12}$/) !== null) && str != '00000' ? true : false);
		return bool;
	},
	
	expand: function(el) {
		this.toExpand = el.next('div.' + this.options.contentClass);
		if (this.isProfilePage || (this.current != this.toExpand)) {
			this.toExpand.show();
			this.animate();
		}
	},
	
	checkMaxHeight: function() {
		for (var i = 0; i < this.contents.length; i++) {
			if (this.contents[i].getHeight() > this.maxHeight) {
				this.maxHeight = this.contents[i].getHeight();
			}
		}
	},
	
	clickHandler: function(e) {
		var el = e.element();
		if (el.hasClassName('accTitle')) {
			el = e.element().up();
		} // account for clicking on the text
		if (el.hasClassName('accArrow')) {
			el = e.element().up();
		} // account for clicking on the text
		if (el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
			this.expand(el);
		}
		
		//set cookie for stickyness on homepage
		if (!this.isProfilePage) {
			for (var i = 0; i < this.contents.length; i++) {
				if (this.contents[i].id === el.id.replace('Toggle', '')) {
					CNN_setCookie('pm.defaultExpanded', (i + 1), 24 * 30 * 12, '/', document.domain);
				}
			}
		}
		
	},
	
	initialShowHide: function() {
		for (var i = 0; i < this.contents.length; i++) {
		
			//either on the profile page with cookie, or on homepage as normal
			if ((!this.isProfilePage && this.contents[i] === this.current) || (this.isProfilePage && this.defaultExpandedProfile === null && this.contents[i] === this.current) || (this.isProfilePage && this.defaultExpandedProfile !== null && this.defaultExpandedProfile.indexOf(i + 1) > -1)) {
				this.contents[i].show();
				this.contents[i].setStyle({
					height: '194px'
				});
				
				//on the profile page and has cookie.
				if (this.isProfilePage && this.defaultExpandedProfile !== null) {
					this.initMod(this.contents[i].id);
				}
				
				this.contents[i].previous('div.' + this.options.toggleClass).addClassName(this.options.toggleActive);
				
			} else {
				this.contents[i].hide();
				this.contents[i].setStyle({
					height: '0px'
				});
			}
		}
	},
	
	animate: function() {
		var effects = [];
		
		if (this.isProfilePage && this.toExpand.getHeight() > 0) {
			options = {
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal,
				scaleX: false,
				scaleY: true
			};
			
			effects.push(new Effect.Scale(this.toExpand, 0, options));
		} else {
			var options = {
				sync: true,
				scaleFrom: 0,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal,
				scaleMode: {
					originalHeight: this.maxHeight,
					originalWidth: this.accordion.getWidth()
				},
				scaleX: false,
				scaleY: true
			};
			
			effects.push(new Effect.Scale(this.toExpand, 100, options));
		}
		
		//close current item, non-profile
		if (!this.isProfilePage) {
			options = {
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal,
				scaleX: false,
				scaleY: true
			};
			
			effects.push(new Effect.Scale(this.current, 0, options));
		}
		
		var myDuration = 0.75;
		
		new Effect.Parallel(effects, {
			duration: myDuration,
			fps: 35,
			queue: {
				position: 'end',
				scope: 'accordion'
			},
			beforeStart: function() {
				this.isAnimating = true;
				if (this.isProfilePage && this.toExpand.getHeight() > 0) {
					this.toExpand.previous('div.' + this.options.toggleClass).removeClassName(this.options.toggleActive);
				} else {
					this.toExpand.previous('div.' + this.options.toggleClass).addClassName(this.options.toggleActive);
				}
				this.current.previous('div.' + this.options.toggleClass).removeClassName(this.options.toggleActive);
				this.toExpand.setStyle({
					visibility: 'hidden'
				});
			}.bind(this),
			afterFinish: function() {
				if (this.current.getHeight() === 0) {
					this.current.hide();
				}
				this.toExpand.setStyle({
					visibility: 'visible'
				});
				if (this.toExpand.getHeight() > 0) {
					this.toExpand.setStyle({
						height: this.maxHeight + "px"
					});
				}
				this.current = this.toExpand;
				this.isAnimating = false;
				this.initMod(this.current.id);
				
				//set pipe-delimited cookie for stickyness on profile page
				if (this.isProfilePage) {
					var defaultExpandedProfileCookie = ''
					for (var i = 0; i < this.contents.length; i++) {
						if (this.contents[i].getHeight() > 0) {
							defaultExpandedProfileCookie += (i + 1) + '|';
						}
					}
					defaultExpandedProfileCookie = defaultExpandedProfileCookie.substr(0, defaultExpandedProfileCookie.length - 1);
					CNN_setCookie('pm.defaultExpandedProfile', defaultExpandedProfileCookie, 24 * 30 * 12, '/', document.domain);
				}
				
			}.bind(this)
		});
	}
};

MainLocalObj.Weather = {

	omnitureStr: "var s=s_gi(s_account);s.linkTrackVars='events,products';s.linkTrackEvents='event2';s.events='event2';s.products=';Topix:Local;;;event2=1;';void(s.tl(this,'o','Topix Local Clickthrough'));",
	
	init: function() {
	
		if (typeof MainLocalObj.Weather.isInitialized === 'undefined') {
			MainLocalObj.Weather.requestLocalAll();
		}
		MainLocalObj.Weather.isInitialized = true;
	},
	
	inputFocus: function(e) {
		if (e.value === "Enter a U.S./Intl city or ZIP code") {
			e.value = ''
		}
		e.removeClassName('pmWeatherHollow');
	},
	
	inputBlur: function(e) {
		if (e.value === '') {
			e.value = "Enter a U.S./Intl city or ZIP code";
			e.addClassName('pmWeatherHollow');
		}
	},
	
	requestLocalAll: function() {
		var configObj = MainLocalObj.data;
		MainLocalObj.Weather.displayElements();
		MainLocalObj.Weather.requestLocalWeather(configObj);
		//no local news for international users for launch, perhaps post launch
		if (!MainLocalObj.internationalUser) {
			MainLocalObj.Weather.requestLocalNews(configObj);
		}
	},
	
	localUpdateData: function(name, zip, code, type) {
		MainLocalObj.setLocationZip(type, zip);
		MainLocalObj.setLocationName(type, name);
		MainLocalObj.setLocationLocCode(type, code);
		MainLocalObj.setUserSpecificData(true);
		MainLocalObj.saveData();
		MainLocalObj.Weather.requestLocalAll();
	},
	
	requestLocalWeather: function(configObj) {
		var weatherUrl = 'http://svcs.cnn.com/weather/getForecast';
		var weatherArgs = 'mode=json_html&zipCode=' + configObj.weatherLoc.zip;
		if (configObj.weatherLoc.locCode) {
			weatherArgs += '&locCode=' + configObj.weatherLoc.locCode;
		}
		if (MainLocalObj.internationalUser || MainLocalObj.allCookies["default.temp.units"] == "true") {
			weatherArgs += '&celcius=true';
		}
		var callObj = {
			url: weatherUrl,
			args: weatherArgs,
			domId: false,
			funcObj: MainLocalObj.Weather.updateLocalWeather,
			breakCache: true
		};
		MainLocalObj.CSIManager.callObject(callObj, 'requestLocalWeather');
	},
	
	requestLocalNews: function(configObj) {
		var newsUrl = 'http://local.cnn.com/local/cnn/json';
		if (MainLocalObj.checkZip(configObj.weatherLoc.zip)) {
			//hack for Chicago/Topix bug
			if (configObj.weatherLoc.zip == '60290') {
				configObj.weatherLoc.zip = '60612';
			}
			var newsArgs = 'q=' + configObj.weatherLoc.zip;
		} else {
			var newsArgs = 'q=' + MainLocalObj.urlEncode(configObj.weatherLoc.name);
		}
		
		var callObj = {
			url: newsUrl,
			args: newsArgs,
			domId: false,
			funcObj: MainLocalObj.Weather.updateLocalNews,
			breakCache: true
		};
		MainLocalObj.CSIManager.callObject(callObj, 'requestLocalNews');
	},
	
	displayElements: function(state) {
		if (typeof state === 'undefined') {
			state = false;
		}
		switch (state) {
			case 'changeLoc':
				
				$('cnnGetLocalBox').show();
				if ($('changeLocLink')) {
					$('changeLocLink').hide();
				}
				$('pmLocResultsContainer').hide();
				
				if (!MainLocalObj.internationalUser) {
					$('pmWeatherHeadlines').show();
					//show only first two headlines
					$('pmWeatherHeadlines').select('li').each(function(val, index) {
						if (index > 1) {
							val.hide();
						}
					});
				}
				
				MainLocalObj.Weather.inputBlur($('weatherLoc'));
				
			break;
			case 'intlChangeLoc':
				$('cnnGetLocalBox').show();
				$('changeLocLink').hide();
				$('pmLocResultsContainer').hide();
				$('pmWeatherTom').hide();
				MainLocalObj.Weather.inputBlur($('weatherLoc'));
			break;
			case 'displayResults':
				$('cnnGetLocalBox').show();
				if (!MainLocalObj.internationalUser && !MainLocalObj.isProfilePage) {
					$('pmWeatherHeadlines').hide();
				}
				$('changeLocLink').hide();
				$('pmLocResultsContainer').show();
			break;
			default:
				if (!MainLocalObj.data.userSpecificData) {
					MainLocalObj.Weather.displayElements('changeLoc');
				} else if (MainLocalObj.data.userSpecificData) {
					$('cnnGetLocalBox').hide();
					$('pmLocResultsContainer').hide();
					
					if ($('changeLocLink')) {
						$('changeLocLink').show();
					}
					if ($('pmWeatherTom')) {
						$('pmWeatherTom').show();
					}
					
					//ensure all headlines are showing
					if (!MainLocalObj.internationalUser) {
						$('pmWeatherHeadlines').show();
						$('pmWeatherHeadlines').select('li').each(function(val) {
							val.show();
						});
					} else {
						if ($('curConditionsWeatherDay')) {
							$('curConditionsWeatherDay').show();
						}
					}
				}
				
			break;
		}
	},
	
	updateLocalWeather: function(obj) {
	
		if (!obj[0].invalid) {
		
			var weatherLink = 'http://weather.cnn.com/weather/';
			if (MainLocalObj.internationalUser) {
				weatherLink = 'http://weather.edition.cnn.com/weather/intl/';
			}
			weatherLink += 'forecast.jsp?zipCode=' + MainLocalObj.data.weatherLoc.zip;
			
			if (MainLocalObj.data.weatherLoc.locCode) {
				weatherLink += '&locCode=' + MainLocalObj.data.weatherLoc.locCode;
			}
			var locStr = MainLocalObj.data.weatherLoc.name;
			var detailsToday = '';
			var detailsTomorrow = '';
			
			//set state of change location
			if (MainLocalObj.internationalUser) {
				var state = 'intlChangeLoc';
			} else {
				var state = 'changeLoc';
			}
			
			var degScale = (MainLocalObj.internationalUser || MainLocalObj.allCookies["default.temp.units"] == "true" ? 'C' : 'F');
			
			if (obj.length > -1) {
				locStr = obj[0].location.city;
				if (obj[0].location.stateOrCountry) {
					locStr += ', ' + obj[0].location.stateOrCountry;
				}
			}
			
			for (var counter = 0; counter < obj.length; counter++) {
				if (obj[counter] && obj[counter].forecast && obj[counter].forecast.days) {
					if (obj[counter].forecast.days.length > -1) {
					
						var today = obj[counter].forecast.days[0];
						var curConditions = obj[counter].currentConditions;
						
						var locStr = obj[0].location.city;
						if (obj[0].location.stateOrCountry) {
							locStr += ', ' + obj[0].location.stateOrCountry;
						}
						
						detailsToday += '<p id="pmSelectedWeather">' +
						'	<span>' +
						locStr +
						'</span><span id="changeLocLink">';
						
						if (MainLocalObj.data.userSpecificData) {
							detailsToday += ' <span style="color: #999;">&nbsp;(</span><a id="pmEditWeatherLoc" href="javascript:MainLocalObj.Weather.displayElements(\'' +
							state +
							'\')">Edit location</a><span style="color: #999;">)</span>';
						}
						
						detailsToday += '</span></p>';
						
						var tempFontAdjust = '';
						if (curConditions.temperature.length > 2) {
							tempFontAdjust = ' style="font-size: 24px;"';
						}
						
						var feelsLikeFontAdjust = '';
						if (curConditions.feelsLikeTemperature.length > 2) {
							feelsLikeFontAdjust = ' style="font-size: 9px;"';
						}
						
						if (!MainLocalObj.isProfilePage || MainLocalObj.internationalUser) {
							detailsToday += '<div class="pmWrapper">' +
							'	<p id="curConditionsWeatherDay" class="weatherDay">Current conditions</p>' +
							'	<div id="pmWeatherIcon">' +
							'		<a href="' +
							weatherLink +
							'"><img class="cnn_ie6png" src="http://i.cdn.turner.com/cnn/.element/img/3.0/weather/03/' +
							today.icon.replace(/gif/, "png") +
							'" alt="your weather"></a>' +
							'	</div>' +
							'	<div id="pmCurrentWeather">' +
							'		<div id="pmCurrTemp"' +
							tempFontAdjust +
							'>' +
							'			<span id="pmCurrTempNum">' +
							curConditions.temperature +
							'</span>&deg;' +
							'		</div>' +
							'	</div>' +
							'	<div id="pmWeatherDetails">' +
							'		<p id="pmWeatherDesc">' +
							curConditions.description +
							'		</p>' +
							'		<p id="pmWeatherHiLo">' +
							'			Hi&nbsp;<span id="pmHiTemp">' +
							today.high +
							'</span>&deg;&nbsp;&nbsp;<span style="color: #999; font-size: 10px;">|</span>&nbsp;&nbsp;Lo&nbsp;<span id="pmLoTemp">' +
							today.low +
							'</span>&deg;' +
							'		</p>' +
							'	</div>' +
							'	<div id="pmMoreWeather">' +
							'		<a id="pm10DayBtn" href="' +
							weatherLink +
							'">&nbsp;</a>' +
							'		<p' +
							feelsLikeFontAdjust +
							'>' +
							'			Feels like&nbsp;<span id="pmFeelTemp">' +
							curConditions.feelsLikeTemperature +
							'</span>&deg;' +
							'		</p>' +
							'	</div>' +
							'</div>';
						}
					}
					
					if (obj[counter].forecast.days.length > 0 && MainLocalObj.internationalUser && MainLocalObj.data.userSpecificData) {
					
						var tomorrow = obj[counter].forecast.days[1];
						
						detailsTomorrow += '<p class="weatherDay">Tomorrow</p>' +
						'<div class="pmWrapper">' +
						'	<div id="pmWeatherIcon">' +
						'		<img class="cnn_ie6png" src="http://i.cdn.turner.com/cnn/.element/img/3.0/weather/03/' +
						tomorrow.icon.replace(/gif/, "png") +
						'" alt="your weather">' +
						'	</div>' +
						'	<div id="pmCurrentWeather">' +
						'	</div>' +
						'	<div id="pmWeatherDetails">' +
						'		<p id="pmWeatherDesc">' +
						tomorrow.description +
						'</p>' +
						'		<p id="pmWeatherHiLo">' +
						'			Hi&nbsp;<span id="pmHiTemp">' +
						tomorrow.high +
						'</span>&deg;&nbsp;&nbsp;<span style="color: #999; font-size: 10px;">|</span>&nbsp;&nbsp;Lo&nbsp;<span id="pmLoTemp">' +
						tomorrow.low +
						'</span>&deg;' +
						'		</p>' +
						'	</div>' +
						'</div>';
						
						$('pmWeatherTom').update(detailsTomorrow);
						$('pmWeatherTom').show();
						
					}
				}
			}
			
			$('pmWeatherTab').update(detailsToday);
			$('pmWeatherTab').show();
			
		} else {
			MainLocalObj.Weather.displayElements('changeLoc'); //shouldn't happen
		}
		
		return detailsToday;
	},
	
	updateLocalNews: function(obj) {
	
		var ret = '';
		var max = 3;
		var chgStr = '';
		var displayStr = '';
		if (MainLocalObj.data.userSpecificData) {
			if (!MainLocalObj.internationalUser && MainLocalObj.isProfilePage) {
				max = 5;
			} else if (!MainLocalObj.internationalUser) {
				max = 3;
			}
		}
		for (var counter = 0; counter < obj.length; counter++) {
			var resultSet = obj[counter].ResultSet;
			if (parseInt(resultSet.statusCode) != 200 || resultSet.Result.length < 1) {
				ret += '<li><span class="pmWeatherHollow">We didn\'t find any headlines for that location. Please try widening your search to a larger area. <a href="javascript:MainLocalObj.Weather.displayElements(\'changeLoc\')">Edit your location</a> </span></li>';
			} else {
				if (resultSet.country == "United States") {
					displayStr = resultSet.city + ', ' + resultSet.state;
				} else {
					displayStr = resultSet.country;
				}
				var result = resultSet.Result;
				for (var i = 0; i < result.length; i++) {
					if (i < max) {
						ret += '<li>' +
						'<a target="_blank" href="' +
						result[i].link +
						'" ';
						if (!MainLocalObj.internationalUser) {
							ret += 'onclick="' + MainLocalObj.Weather.omnitureStr + '" ';
						}
						ret += '><span class="pmHLBullet">&bull;</span>' + result[i].headline + '</a>' +
						'<p><a target="_blank" href="' +
						result[i].sourceurl +
						'">' +
						result[i].source +
						'</a></p>' +
						'</li>';
					}
				}
			}
		}
		
		var htmlStr = 'From:&nbsp;<a onclick="' + MainLocalObj.Weather.omnitureStr + '" href="http://www.topix.com/redir/loc=prss-cnnlogo/http://www.topix.com/" id="pmHeadlineSource" target="_blank">Topix.com</a>';
		
		$('pmWeatherHeadlinesList').update(ret);
		$('pmInfoSource').update(htmlStr);
		
		$('pmWeatherHeadlines').show();
		MainLocalObj.Weather.displayElements();
		
		return ret;
		
	},
	
	requestInternationalCityLookup: function(zip, loc, type) {
		var weatherUrl = 'http://svcs.cnn.com/weather/getForecast';
		var weatherArgs = 'mode=json_html&zipCode=' + zip;
		if (loc) {
			weatherArgs += '&locCode=' + loc;
		}
		if (MainLocalObj.internationalUser) {
			weatherArgs += '&celcius=true';
		}
		var callObj = {
			url: weatherUrl,
			args: weatherArgs,
			domId: false,
			funcObj: MainLocalObj.Weather.updateInternationalCityData,
			breakCache: true
		};
		MainLocalObj.CSIManager.callObject(callObj, 'requestInternationalCityLookup');
	},
	
	updateInternationalCityData: function(obj, type) {
	
		var locCode = '';
		var zip = '';
		var locationName = '';
		if (obj && (obj.length > -1) && obj[0].location && obj[0].location.city) {
			locCode = obj[0].location.locCode;
			locationName = obj[0].location.city;
			if (obj[0].location.stateOrCountry) {
				locationName += ', ' + obj[0].location.stateOrCountry;
			}
			zip = obj[0].location.zip;
		}
		
		if (zip && locationName) {
			MainLocalObj.setLocationZip(type, zip);
			MainLocalObj.setLocationName(type, locationName);
			MainLocalObj.setLocationLocCode(type, locCode);
			MainLocalObj.setUserSpecificData(true);
			MainLocalObj.Weather.requestLocalAll();
		}
		
	},
	
	checkInput: function(inputMode, value) {
	
		//First remove any html/script tags
		value = value.replace(/<[^>]*?>/g, '');
		var qryArg = value.toUpperCase();
		
		var validatorUrl = 'http://weather.cnn.com/weather/citySearch';
		var validatorArgs = 'search_term=' + MainLocalObj.urlEncode(qryArg) + '&mode=json_html&filter=true';
		
		var callObj = {
			url: validatorUrl,
			args: validatorArgs,
			domId: MainLocalObj.urlEncode(inputMode + '|' + value),
			funcObj: MainLocalObj.Weather.updateValidationData,
			breakCache: true
		};
		MainLocalObj.CSIManager.callObject(callObj, 'checkInput');
	},
	
	updateValidationData: function(obj, idString) {
	
		var rawData = MainLocalObj.urlDecode(idString).split('|');
		var cleanValue = MainLocalObj.trimWS(rawData[1]);
		//Capitalize, buffer with spaces to match on whole words
		var preparedValue = ' ' + cleanValue.toUpperCase() + ' ';
		
		var locationObj = '';
		
		if (obj.length > 1) { // There are multiple matches. Weed out the one we want.
			var done = obj.length;
			var exactMatch = false;
			var noMatch = true;
			
			var possibleLocations = [];
			
			if (done > 50) {
				done = 50;
			} // Max cap of 50
			for (var i = 0; i < done; i++) {
			
				var match = obj[i].city + ', ' + obj[i].stateOrCountry;
				var objZip = obj[i].zip.toString();
				var objLocCode = obj[i].locCode;
				var newLocationObj = {};
				
				var preparedMatch = ' ' + MainLocalObj.trimWS(match.toUpperCase()) + ' ';
				
				if ((MainLocalObj.checkZip(cleanValue) && cleanValue == objZip) || (preparedValue == preparedMatch)) {
					newLocationObj.zip = objZip;
					newLocationObj.locCode = objLocCode;
					newLocationObj.name = match;
					possibleLocations.push(newLocationObj);
					noMatch = false;
					exactMatch = newLocationObj;
					i = done;
				} else {
					if (preparedMatch.indexOf(preparedValue) != -1) {
						newLocationObj.zip = objZip;
						newLocationObj.locCode = objLocCode;
						newLocationObj.name = match;
						possibleLocations.push(newLocationObj);
						noMatch = false;
					}
				}
			}//end for loop
			if (noMatch) {
				MainLocalObj.Weather.displayNoMatch(rawData[0], cleanValue);
			} else if (possibleLocations.length == 1 || exactMatch) {
				if (!exactMatch) {
					exactMatch = possibleLocations[0];
				}
				locationObj = {};
				locationObj.name = exactMatch.city + ', ' + exactMatch.stateOrCountry;
				locationObj.zip = exactMatch.zip.toString();
				locationObj.locCode = exactMatch.locCode;
				
				MainLocalObj.Weather.localUpdateData(locationObj.name, locationObj.zip, locationObj.locCode, rawData[0]);
				MainLocalObj.Weather.displayElements(rawData[0]);
			} else {
				// We have a bunch of possible locations.
				MainLocalObj.Weather.displayMultipleMatches(possibleLocations, MainLocalObj.urlDecode(rawData[1]), rawData[0]);
			}
		} else if ((obj.length == 1) && (obj[0] && obj[0].locCode && obj[0].locCode != '')) {
			var tmpObj = obj[0];
			locationObj = {};
			locationObj.name = tmpObj.city + ', ' + tmpObj.stateOrCountry;
			locationObj.zip = tmpObj.zip.toString();
			locationObj.locCode = tmpObj.locCode;
			
			MainLocalObj.Weather.localUpdateData(locationObj.name, locationObj.zip, locationObj.locCode, rawData[0]);
			MainLocalObj.Weather.displayElements(rawData[0]);
		} else {
			MainLocalObj.Weather.displayNoMatch(rawData[0], cleanValue);
		}
		return '';
	},
	
	displayMultipleMatches: function(obj, origVal, type) {
	
		var matches = [];
		var domesticMatches = [];
		var intlMatches = [];
		obj.each(function(val) {
			if (val.zip.length > 5) {
				intlMatches.push(val);
			} else {
				domesticMatches.push(val);
			}
		});
		
		matches = intlMatches.concat(domesticMatches);
		
		var val = $F('weatherLoc');
		var container = 'pmLocResultsContainer';
		
		var htmlStr = '<p id="pmWeatherResultMeta">We Found ' + matches.length + ' Results for &quot;<span id="resultLoc">' + origVal + '</span>&quot;</p>';
		htmlStr += '<ul id="pmWeatherResult">';
		
		for (var j = 0; j < matches.length; j++) {
			htmlStr += '<li><a href="javascript:void(0)" onclick="MainLocalObj.Weather.resetSearch(\'' + type + '\');' +
			'MainLocalObj.Weather.localUpdateData(\'' +
			matches[j].name +
			'\',\'' +
			matches[j].zip +
			'\',\'' +
			matches[j].locCode +
			'\',\'' +
			type +
			'\');"><span class="pmHLBullet">&bull;</span>' +
			matches[j].name +
			'</a></li>';
		}
		htmlStr += '</ul>';
		MainLocalObj.Weather.displayElements('displayResults');
		Element.update(container, htmlStr);
	},
	
	displayNoMatch: function(type, val) {
	
		var container = 'pmLocResultsContainer';
		var htmlStr = '<p id="pmWeatherResultMeta">We didn\'t find results  for &quot;<span id="resultLoc">' + val + '</span>&quot;</p>' +
		'<ul id="pmWeatherResult">' +
		'	<li><span class="pmHLBullet">&bull;</span><span>Check the spelling of your city name</span></li>' +
		'	<li><span class="pmHLBullet">&bull;</span><span>Make sure the U.S. ZIP code is accurate</span></li>' +
		'	<li><span class="pmHLBullet">&bull;</span><span>Use USPS for U.S. ZIP codes / city name</span></li>' +
		'</ul>';
		
		MainLocalObj.Weather.displayElements('displayResults');
		Element.update(container, htmlStr);
	},
	
	resetSearch: function(type) {
	
		var fieldName = 'weatherLoc';
		var container = 'pmLocResultsContainer';
		
		Element.update(container, '');
		$(fieldName).value = 'Enter a U.S./Intl city or ZIP code';
	}
};

MainLocalObj.Sports = {
	init: function() {
		
		MainLocalObj.Sports.leagues = {
			'NFL': {'sport': 'football', 'acronym': 'nfl', 'startMonth': 8, 'endMonth': 1},
			'NCAAF': {'sport': 'football', 'acronym': 'ncaa', 'startMonth': 8, 'endMonth': 1},
			'NCAAB': {'sport': 'basketball', 'acronym': 'ncaa', 'startMonth': 11, 'endMonth': 4},
			'MLB': {'sport': 'baseball', 'acronym': 'mlb', 'startMonth': 3, 'endMonth': 11},
			'NHL': {'sport': 'hockey', 'acronym': 'nhl', 'startMonth': 9, 'endMonth': 4},
			'NBA': {'sport': 'basketball', 'acronym': 'nba', 'startMonth': 10, 'endMonth': 4}
		};
		
		MainLocalObj.Sports.teamOverlayAreas = $('choseTeamsOverlayBox').select('.pmTeamInfo');
		
		//clear out if it's a refresh
		$('sportBtns').update('');
		$('pmScores').update('');
		$('pmNoGamesHeadlines').update('');
		
		//only add eventHandler once
		if (typeof MainLocalObj.Sports.isInitialized === 'undefined') {
			MainLocalObj.Sports.loadData();
		}
		
		MainLocalObj.Sports.maxGames = 2;
		MainLocalObj.Sports.isInitialized = true;
			
	},
	
	loadData: function() {

		var lastLeagueCookie = MainLocalObj.allCookies["pm.sports.lastLeague"] || null;
		var leaguesInSeason = [];
		
		//create array of leagues in season
		for (var i in MainLocalObj.Sports.leagues) {

			if (typeof MainLocalObj.Sports.leagues[i] === 'object') {

				var league = MainLocalObj.Sports.leagues[i];
				var curMonth = cnnCurrTime.getMonth()+1;
				var endYear;
				var startYear;
				
				if (league.endMonth < league.startMonth && league.startMonth > curMonth) {
					startYear = (cnnCurrTime.getFullYear() - 1);
				} else {
					startYear = cnnCurrTime.getFullYear();				
				}
				
				if (league.endMonth < league.startMonth && curMonth > league.endMonth) {
					endYear = (cnnCurrTime.getFullYear() + 1);
				} else {
					endYear = cnnCurrTime.getFullYear();
				}
				
				var startDate = new Date(league.startMonth + '/01/' + startYear);
				var endDate = new Date(league.endMonth + '/' + MainLocalObj.daysInMonth(league.endMonth, endYear) + '/' + endYear);
				
				if (cnnCurrTime >= startDate && cnnCurrTime <= endDate) {
					leaguesInSeason.push(i);
				}
			}
		}
		
		var y = 0;
		var leagueTabStyle = '';
		if (leaguesInSeason.length === 6) {
			leagueTabStyle = 'margin-right: 1px';
		}
		leaguesInSeason.each(function(league) {
											
			//check in season
			//add button with obj
			var a = new Element('a', {
				'id': league + 'btn',
				'style': leagueTabStyle,
				href: 'javascript: MainLocalObj.Sports.loadLeague(\'' + league + '\')'
			}).update('<span>' + league + '</span>');
			$('sportBtns').insert({
				bottom: a
			});
			
			//load the first leage in season
			if (y === 0 && lastLeagueCookie === null) {
				MainLocalObj.Sports.loadLeague(league);
			}

			y++;					
		});

		if (lastLeagueCookie !== null) {
			MainLocalObj.Sports.loadLeague(lastLeagueCookie);
		}
		
	},
	
	loadLeague: function(link) {
	
		//adding stickyness
		CNN_setCookie('pm.sports.lastLeague', link, 24 * 30 * 12, '/', document.domain);
		
		//clear out HTML
		$('pmScores').update('');
		
		//setup click listener for overlay trigger
		$('pmSportsChooseBtn').stopObserving('click');
		$('pmSportsChooseBtn').observe('click', function() {
			MainLocalObj.Sports.toggleSportsOverlay(link);
		});
		
		var obj = MainLocalObj.Sports.leagues[link];
		MainLocalObj.Sports.displayElements(false, link);
		var callObj = {
			url: '/.element/ssi/auto/3.0/sect/MAIN/sports/' + obj.sport + '/' + obj.acronym + '/scoreboards/games.html',
			args: 'domains=cnn.com|turner.com',
			domId: false,
			funcObj: function(obj) {
				MainLocalObj.Sports.loadGames(obj, link);
			},
			breakCache: false
		};
		MainLocalObj.CSIManager.callObject(callObj, 'requestLeague' + obj.acronym);
		
	},
	
	loadGames: function(obj, link, team1, team2) {
	
		var league = MainLocalObj.Sports.leagues[obj[0].sport];
		var callObj;
		var team1 = false;
		var team2 = false;
		var linkLower = link.toLowerCase();
		var userGames = [];
		var team1Games = [];
		var team2Games = [];
		var nonUserGames = [];
		var x = 0;
		var y = 0;
		var z = 0;
		
		//update text strings and links
		$('moreLeagueLink').href = 'http://sportsillustrated.cnn.com/' + league.sport + '/' + league.acronym + '?xid=cnnwidget';
		$('moreLeagueLink').update('<span>More ' + obj[0].sport + '</span>');
		$('pmSlidebox').select('.leagueChoose').each(function(val) {
			val.update(obj[0].sport);
		});
		
		//clear out
		$('pmScores').update('');
		$('pmNoGamesHeadlines').update('');
		
		//get any saved teams
		if (typeof MainLocalObj.data.sports[linkLower] !== 'undefined') {
			if (typeof MainLocalObj.data.sports[linkLower].team1 !== 'undefined' && MainLocalObj.data.sports[linkLower].team1 !== '') {
				team1 = MainLocalObj.data.sports[linkLower].team1;
			}
			if (typeof MainLocalObj.data.sports[linkLower].team2 !== 'undefined' && MainLocalObj.data.sports[linkLower].team2 !== '') {
				team2 = MainLocalObj.data.sports[linkLower].team2;
			}
		}
		
		//set choose / edits
		var chooseEdits = $('pmContainer').select('.pmOverlayChooseEdit');
		if (team1 || team2) {
			chooseEdits.each(function(val) {
				val.update('Edit');
			});
		} else {
			chooseEdits.each(function(val) {
				val.update('Choose');
			});
		}
		
		if (obj[0].games.length || team1 || team2) {
		
			$('pmScores').show();
			$('pmNoGames').hide();
			
			//first check for games by our saved teams
			for (var i in obj[0].games) {
				val = obj[0].games[i];
				if (typeof val !== 'object') {
					continue;
				}
				if ((team1 && (val.home.short_name == team1 || val.visitor.short_name == team1)) || (team2 && (val.home.short_name == team2 || val.visitor.short_name == team2))) {
					if (team1 && (val.home.short_name == team1 || val.visitor.short_name == team1)) {
						team1Games[x] = val;
						x++;
					} else if (team2 && (val.home.short_name == team2 || val.visitor.short_name == team2)) {
						team2Games[y] = val;
						y++;
					}
				} else {
					nonUserGames[z] = val;
					z++;
				}
			}
			
			//concat teams arrays
			userGames = team1Games.concat(team2Games);
			
			//check if individual teams have games, if not pop empty team in array
			for (var i = 1; i <= MainLocalObj.Sports.maxGames; i++) {
				if (typeof MainLocalObj.data.sports[linkLower] !== 'undefined' && MainLocalObj.data.sports[linkLower]['team' + i] !== '') {
					if (eval('team' + i + 'Games').length === 0) {
						userGames.push([{
							sport: {
								type: link,
								game: {
									state: false,
									home: {
										three_letter_abrv: false,
										four_letter_abrv: false,
										short_name: MainLocalObj.data.sports[linkLower]['team' + i],
										full_name: MainLocalObj.data.sports[linkLower]['team' + i + 'Name'],
										score: ''
									},
									visitor: {
										three_letter_abrv: false,
										four_letter_abrv: false,
										short_name: false,
										full_name: false,
										rank: false,
										score: ''
									}
								}
							}
						}]);
					}
				}
			}
			
			//concat, trim up array
			displayGames = userGames.concat(nonUserGames);
			displayGames.splice(MainLocalObj.Sports.maxGames, displayGames.length - MainLocalObj.Sports.maxGames);
			
			//left this is loop as I originally wrote it, in case we add more spaces for games later.
			displayGames.each(function(val, index) {
				//testing for val[0] only exists in the 'nogames' version
				if (typeof val[0] != 'undefined') {
					MainLocalObj.Sports.populateScoreBoard(val);
				} else {
					var splitUrl = val.url.split('/').reverse();					
				
					//workaround for NCAAB, if happens more than once I'll work into config above
					if (league.sport === 'basketball' && league.acronym === 'ncaa') {
						var acronym = league.acronym + '/men';
					} else {
						var acronym = league.acronym;
					}
				
					callObj = {
						url: '/.element/ssi/auto/3.0/sect/MAIN/sports/' + league.sport + '/' + acronym + '/' + splitUrl[3] + '/' + splitUrl[2] + '/' + splitUrl[1] + '/' + val.id + '.html',
						args: 'domains=cnn.com|turner.com',
						domId: false,
						funcObj: function(obj) {
							MainLocalObj.Sports.populateScoreBoard(obj, val.home.short_name, val.visitor.short_name, splitUrl);
						},
						breakCache: true
					};
					MainLocalObj.CSIManager.callObject(callObj, 'requestGame' + val.id);
				}
			});
		} else {
			$('pmScores').hide();
			$('pmNoGames').show();
			callObj = {
				url: '/.element/ssi/auto/3.0/sect/MAIN/sports/headlines/' + obj[0].sport.toLowerCase() + '_csi.html',
				args: 'domains=cnn.com|turner.com',
				domId: 'pmNoGamesHeadlines',
				funcObj: false,
				breakCache: true
			};
			MainLocalObj.CSIManager.callObject(callObj, 'requestHeadlines' + league.acronym);
		}
	},
	
	populateScoreBoard: function(obj, homeShortName, visitorShortName, splitUrl) {
	
		if (typeof homeShortName === 'undefined') {
			homeShortName = obj[0].sport.game.home.short_name;
		}
		if (typeof visitorShortName === 'undefined') {
			visitorShortName = obj[0].sport.game.visitor.short_name;
		}
		if (typeof splitUrl === 'undefined') {
			splitUrl = null;
		}
		
		function formatTruncated(str, state, league) {
			str = MainLocalObj.trimWS(str);
			if ((typeof state !== 'boolean' && str.length > 8) || (typeof state !== 'boolean' && league == 'NCAAF')) {
				str = str.substr(0, 8) + '...';
			} else {
				var words = str.split(' ');
				if (words[1].length < 4 && typeof words[2] != 'undefined' && words[2].length < 4 && words.length > 3) {
					str = words[0] + ' ' + words[1] + ' ' + MainLocalObj.trimWS(words[2]) + '...';
				} else if ((words[1].length >= 4 || typeof words[2] != 'undefined' && words[2].length >= 4) && words.length > 2) {
					str = words[0] + ' ' + MainLocalObj.trimWS(words[1]) + '...';
				}
			}
			return str;
		}
		
		function createLink(game, type, splitUrl) {
		
			var hrefPRE;
			var hrefIP;
			var type;
			var str;
			var hrefTemplate = false;
			var href = false;
			var link = '';
			var gameDat;
			
			//choose link format
			switch (type) {
				case 'NFL':
					hrefPRE = '/football/nfl/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/football/nfl/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_boxscore.html';
				break;
				case 'NCAAF':
					hrefPRE = '/football/ncaa/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/football/ncaa/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_boxscore.html';
				break;
				case 'MLB':
					hrefPRE = '/baseball/mlb/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/baseball/mlb/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_gameflash.html';
				break;
				case 'NHL':
					hrefPRE = '/hockey/nhl/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/hockey/nhl/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_boxscore.html';
				break;
				case 'NBA':
					hrefPRE = '/basketball/nba/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/basketball/nba/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_boxscore.html';
			 	break;
				case 'NCAAB':
					hrefPRE = '/basketball/ncaa/men/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_preview.html';
					hrefIP = '/basketball/ncaa/men/gameflash/#{yyyy}/#{mm}/#{dd}/#{gameId}_boxscore.html';
				break;
			}
			
			//choose link template
			switch (game.state) {
				case 'PRE':
					str = 'Upcoming<br>' + game.time;
					hrefTemplate = new Template(hrefPRE);
				break;
				case 'IP':
					str = game.status;
					if (str === 'F') {
						str = 'Final';
					}
					hrefTemplate = new Template(hrefIP);
				break;
				default:
					str = 'No scheduled games<br>in next 24 hours';
				break;
			}
			
			//parse template
			if (hrefTemplate) {
				//setup href data
				if (splitUrl === null) {
					splitUrl = game.url.split('/').reverse();
				}
				var hrefData = {
					yyyy: splitUrl[3],
					mm: splitUrl[2],
					dd: splitUrl[1],
					gameId: game.id
				};
				href = hrefTemplate.evaluate(hrefData);
			}
			
			if (href) {
				var title = str.replace(/(<([^>]+)>)/ig, ' ');
				link += '<a href="http://sportsillustrated.cnn.com' + href + '?xid=cnnwidget' + '" title="' + title + '">';
			}
			link += str;
			if (href) {
				link += '</a>';
			}
			
			return link;
		}
		
		var league = MainLocalObj.Sports.leagues[obj[0].sport.type];
		
		//workaround for team link NCAAB anomaly
		if (league.acronym === 'ncaa' && league.sport === 'basketball') {
			var leagueAcronym = league.acronym + '/men';
		} else {
			var leagueAcronym = league.acronym;
		}
		
		var game = obj[0].sport.game;
		
		var scoreBoard = '' +
		'<li class="top">' +
		'	<div id="pmGame1" class="pmGame">' +
		'		<div class="pmScoreSide">' +
		'			<span class="top">' +
		'				<div id="pmGame1Team1" class="pmGameTeam">' +
		'					<a title="' +
		game.home.full_name +
		'" href="http://sportsillustrated.cnn.com/' +
		league.sport +
		'/' +
		leagueAcronym +
		'/teams/' +
		homeShortName +
		'?xid=cnnwidget">';
		
		if (typeof game.home.rank != 'undefined' && game.home.rank != '') {
			scoreBoard += '(' + game.home.rank + ')&nbsp;';
		}
		
		if (typeof game.home.four_letter_abrv !== 'undefined' && game.home.four_letter_abrv) {
			scoreBoard += game.home.four_letter_abrv.toUpperCase();
		} else if (typeof game.home.three_letter_abrv !== 'undefined' && game.home.three_letter_abrv) {
			scoreBoard += game.home.three_letter_abrv.toUpperCase();
		} else {
			scoreBoard += formatTruncated(MainLocalObj.trimWS(game.home.full_name), game.state, obj[0].sport.type);
		}
		
		scoreBoard += '</a>' +
		'				</div>' +
		'				<div id="pmGame1Score1" class="pmGameScore">' +
		game.home.score +
		'</div>' +
		'			</span>' +
		'			<span>' +
		'				<div id="pmGame1Team2" class="pmGameTeam">' +
		'					<a title="' +
		game.visitor.full_name +
		'" href="http://sportsillustrated.cnn.com/' +
		league.sport +
		'/' +
		leagueAcronym +
		'/teams/' +
		visitorShortName +
		'?xid=cnnwidget">';
		
		if (typeof game.visitor.rank != 'undefined' && game.visitor.rank != '') {
			scoreBoard += '(' + game.visitor.rank + ')&nbsp;';
		}
		
		if (typeof game.visitor.four_letter_abrv !== 'undefined' && game.visitor.four_letter_abrv) {
			scoreBoard += game.visitor.four_letter_abrv.toUpperCase();
		} else if (typeof game.visitor.three_letter_abrv !== 'undefined' && game.visitor.three_letter_abrv) {
			scoreBoard += game.visitor.three_letter_abrv.toUpperCase();
		} else if (game.visitor.full_name) {
			scoreBoard += formatTruncated(MainLocalObj.trimWS(game.visitor.full_name), game.state, obj[0].sport.type);
		}
		
		scoreBoard += '</a>' +
		'				</div>' +
		'				<div id="pmGame1Score2" class="pmGameScore">' +
		game.visitor.score +
		'</div>' +
		'			</span>' +
		'		</div>' +
		'		<div id="pmGame1TimeSide" class="pmTimeSide">' +
		'			<p>';
		
		scoreBoard += createLink(game, obj[0].sport.type, splitUrl);
		
		scoreBoard += '			</p>' +
		'		</div>' +
		'	</div>' +
		'</li>';
		
		$('pmScores').innerHTML += scoreBoard;
		
	},
	
	displayElements: function(state, league) {
	
		if (typeof state === 'undefined') {
			state = false;
		}
		if (typeof league === 'undefined') {
			league = false;
		}
		
		//sort out buttons and listeners
		$('sportBtns').select('a').each(function(val) {
			//setup highlights
			if (val.id == league + 'btn') {
				val.addClassName('pmOn');
			} else {
				val.removeClassName('pmOn');
			}
		});
	},
	
	populateTeamOverlay: function(obj, league) {
	
		var team1 = '';
		var team2 = '';
		var selected = '';
		var selectedTeam = false;
		var hasTeam;
		
		//two team areas
		MainLocalObj.Sports.teamOverlayAreas.each(function(val, index) {
		
			var itemNum = (index + 1);
			val.update('');
			
			if (typeof MainLocalObj.data.sports[league] !== 'undefined') {
				if (MainLocalObj.data.sports[league]['team' + itemNum] !== 'undefined' && MainLocalObj.data.sports[league]['team' + itemNum] !== '') {
					eval('team' + itemNum + ' = MainLocalObj.data.sports[league].team' + itemNum);
					hasTeam = true;
				} else {
					hasTeam = false;
				}
			}
			
			var teamDataStr = '<form name="chooseTeam' + index + '" class="chooseTeam" action="">' +
			'	<select name="team' +
			index +
			'"class="pmTeamChoose">';
			
			//allow blank in second option
			teamDataStr += '	<option></option>';
			
			for (var y in obj) {
				team = obj[y];
				if (typeof team !== 'object') {
					continue;
				}
				if (eval('team' + itemNum + ' == team.TEAM_URL_NM')) {
					selected = ' selected="selected"';
					selectedTeam = team;
				} else {
					selected = '';
				}
				
				if (itemNum > 1 && team1 === team.TEAM_URL_NM) {
					//skip
				} else {
					teamDataStr += '	<option' + selected + ' value="' + team.TEAM_URL_NM + '">' + team.TEAM_DSPLY_NM + '</option>';
				}
			}
			teamDataStr += '</select>' +
			'</form>' +
			'<div class="pmTeamNameRow">' +
			'	<div class="pmTeamName">';
			
			if (selectedTeam) {
				teamDataStr += selectedTeam.TEAM_DSPLY_NM;
			}
			
			teamDataStr += '</div>' +
			'	<div class="pmEditTeam"><a href="javascript: MainLocalObj.Sports.changeSavedTeam(' +
			index +
			')" title="change">change</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript: MainLocalObj.Sports.deleteSavedTeam(\'' +
			league +
			'\', \'' +
			selectedTeam.TEAM_URL_NM +
			'\')" title="delete">delete</a></div>' +
			'</div>';
			
			//update div
			val.update(teamDataStr);
			
			//show/hide logic
			if (hasTeam) {
				val.select('form.chooseTeam')[0].hide();
				val.select('div.pmTeamNameRow')[0].show();
			} else {
				val.select('form.chooseTeam')[0].show();
				val.select('div.pmTeamNameRow')[0].hide();
			}
			
		});
		
	},
	
	saveTeams: function(league) {
		var chosenTeam;
		var chosenSelect;
		MainLocalObj.Sports.teamOverlayAreas.each(function(val, index) {
			delete chosenTeam;
			chosenSelect = val.select('select.pmTeamChoose')[0];
			chosenTeam = chosenSelect.options[chosenSelect.selectedIndex].value;
			chosenTeamName = chosenSelect.options[chosenSelect.selectedIndex].text;
			if (typeof MainLocalObj.data.sports[league] === 'undefined') {
				MainLocalObj.data.sports[league] = {};
			}
			MainLocalObj.data.sports[league]['team' + (index + 1)] = chosenTeam;
			MainLocalObj.data.sports[league]['team' + (index + 1) + 'Name'] = chosenTeamName;
		});
		MainLocalObj.saveData();
		MainLocalObj.Sports.toggleSportsOverlay();
		MainLocalObj.Sports.loadLeague(league.toUpperCase());
	},
	
	changeSavedTeam: function(i) {
		MainLocalObj.Sports.teamOverlayAreas[i].select('form.chooseTeam')[0].show();
		MainLocalObj.Sports.teamOverlayAreas[i].select('div.pmTeamNameRow')[0].hide();
	},
	
	deleteSavedTeam: function(league, team) {
	
		if (typeof MainLocalObj.data.sports[league].team1 !== 'undefined' && MainLocalObj.data.sports[league].team1 === team) {
			MainLocalObj.data.sports[league].team1 = '';
		}
		
		if (typeof MainLocalObj.data.sports[league].team2 !== 'undefined' && MainLocalObj.data.sports[league].team2 === team) {
			MainLocalObj.data.sports[league].team2 = '';
		}
		
		//save updated data
		MainLocalObj.saveData();
		
		//reload overlay
		MainLocalObj.Sports.toggleSportsOverlay();
		MainLocalObj.Sports.toggleSportsOverlay(league);
		
	},
	
	toggleSportsOverlay: function(league) {
	
		if (typeof league === 'undefined') {
			league = '';
		}
		
		//init overlay
		var choseTeamsOverlayBox = $('choseTeamsOverlayBox');
		var is_set = choseTeamsOverlayBox.getStyle('display');
		var doc_height = document.viewport.getHeight() + "px";
		var doc_width = document.viewport.getWidth() + "px";
		var posSource = $('pmContainer');
		var browser = navigator.appName;
		var old_ie = false;
		if (browser == "Microsoft Internet Explorer") { // clonePosition has a bug in IE7 and below
			var ua = navigator.userAgent;
			var msie = ua.indexOf('MSIE');
			var ie = msie + 5; // to grab the actual # after "MSIE"
			var version_string = ua.charAt(ie);
			ie_ver_num = parseFloat(version_string);
			
			if (ie_ver_num <= 7) {
				old_ie = true;
			}
		}
		
		if (is_set === "none") {
		
			if (ms_isLoggedIn() !== true) {
				showOverlay('profile_signin_overlay');
			} else {
			
				var saveTeamsBtn = choseTeamsOverlayBox.select('.pmSaveBtn')[0];
				saveTeamsBtn.stopObserving('click');
				saveTeamsBtn.href = 'javascript: void(0);';
				saveTeamsBtn.observe('click', function() {
					MainLocalObj.Sports.saveTeams(league.toLowerCase());
				});
				
				//load team data
				callObj = {
					url: '/.element/ssi/www/sect/3.0/MAIN/sports/teams/' + league.toLowerCase() + '.html',
					args: 'domains=cnn.com|turner.com',
					domId: 'false',
					funcObj: function(obj) {
						MainLocalObj.Sports.populateTeamOverlay(obj, league.toLowerCase());
					},
					breakCache: true
				};
				MainLocalObj.CSIManager.callObject(callObj, 'requestTeamData' + league);
				
				//continue setting up overlay
				$('choseTeamsOverlay').setStyle({
					display: 'block',
					height: doc_height,
					width: doc_width,
					opacity: 0.5
				});
				choseTeamsOverlayBox.setStyle({
					display: 'block'
				});
				//choseTeamsOverlayBox.absolutize();
				if (old_ie == true) {
					choseTeamsOverlayBox.setStyle({
						margin: '0 0 0 -20px',
						top: '1px'
					}); // end of accounting for the IE clone position Bug
				} else {
					choseTeamsOverlayBox.clonePosition(posSource, {
						setHeight: false
					});
					choseTeamsOverlayBox.setStyle({
						margin: '0 0 0 1px',
						top: '1px'
					});
				}
			}
		} else if (is_set == "block") {
			$('choseTeamsOverlay').setStyle({
				display: 'none'
			});
			choseTeamsOverlayBox.setStyle({
				display: 'none'
			});
		} else {
			return false;
		}
	}
};

MainLocalObj.Markets = {
	init: function() {
	
		var adTag;
		
		this.defaultMsg = 'Enter Symbol';
		this.errorMsg = 'No match. Please enter a new symbol.';
		
		if (MainLocalObj.internationalUser) {
			//set adTag
			adTag = '/cnnintl_adspaces/3.0/homepage/spon.88x31_worldbiz.ad';
		} else {
			//download JS for quote lookup on demand
			//MainLocalObj.loadJsFile('/.element/js/3.0/service.js');
			//set adTag
			adTag = '/cnn_adspaces/3.0/homepage/spon2.126x31.ad';
		}
		
		//load in ad
		new Ajax.Updater('moneySponsor', adTag, {
			method: 'get',
			evalScripts: true
		});
		
		//setup buttons
		MainLocalObj.Markets.togBtns = $('pmMarkets').select('a.toggle');
		var lastIndexCookie = MainLocalObj.allCookies["pm.markets.lastIndex"] || null;
		if (lastIndexCookie !== null && $(lastIndexCookie)) {
			if (lastIndexCookie === 'myQuotesBtn' && !ms_isLoggedIn()) {
				var lastIndexCookie = 'defIndexBtn';
			}
			MainLocalObj.Markets.setActiveIndex($(lastIndexCookie), MainLocalObj.Markets.togBtns);
		}
		MainLocalObj.Markets.togBtns.each(function(val) {
			val.observe('click', function() {
				MainLocalObj.Markets.setActiveIndex(val, MainLocalObj.Markets.togBtns);
			});
		});
			
		if (typeof MainLocalObj.Markets.isInitialized === 'undefined') {
			if (!MainLocalObj.internationalUser) {
				MainLocalObj.Markets.fetchStockQuotes();
			}
			if (window.SymbolComplete != null) {
				// Parameters: textBox id, display area id, # results in div (1-20), delay, callback, use iframe hack to cover controls in IE
				window.SymbolComplete.SetupDynamicScript('searchQuote', 'myContainer', 10, .050, MainLocalObj.Markets.formatResult, false);
				window.SymbolComplete.DisableAutoHighlight(); // don't autohightlight first item in list.
			}
		}

		if (!MainLocalObj.internationalUser) {
			var searchQuote = $('searchQuote');
			searchQuote.addClassName('pmWeatherHollow');
			searchQuote.value = 'Enter Symbol';
			searchQuote.observe('focus', function() {
				MainLocalObj.Markets.inputFocus(searchQuote);
			});
			searchQuote.observe('blur', function() {
				MainLocalObj.Markets.inputBlur(searchQuote);
			});
		}
		
		MainLocalObj.Markets.isInitialized = true;
	},
	setActiveIndex: function(val, togBtns) {
		togBtns.each(function(v) {
		
			var marketDiv;
						
			//two workarounds to find corresponding div
			if (v.id === 'myQuotesBtn') {
				marketDiv = 'pmMyQuotes';
			} else if (v.id === 'defIndexBtn') {
				marketDiv = 'pmDefaultIndecies';
			} else {
				marketDiv = v.id.replace('Btn', '');
			}
			
			if (v.id === val.id) {
				v.removeClassName('togOff').addClassName('togOn');
				$(marketDiv).removeClassName('pmOff').addClassName('pmOn');
				CNN_setCookie('pm.markets.lastIndex', val.id, 24 * 30 * 12, '/', document.domain);
			} else {
				v.removeClassName('togOn').addClassName('togOff');
				$(marketDiv).removeClassName('pmOn').addClassName('pmOff');
			}
			
		});
		
		//make a backup
		MainLocalObj.data.prevStockSymbols = MainLocalObj.data.stockSymbols.slice();
		
		if ($('searchQuote')) {
			if ($('searchQuote').hasClassName('errorColor')) {
				$('searchQuote').removeClassName('errorColor');
			}
			$('searchQuote').value = '';
		}
		
		if ($('symbLookupError')) {
			$('symbLookupError').update('');
		}
		
		if (!MainLocalObj.internationalUser) {
			MainLocalObj.Markets.inputBlur($('searchQuote'));
		}
	},
	
	formatResult: function(query, selectionValue, symbol, companyName, countryCode) {
	
		selectionValue.setValue(symbol);
		
		//Eliminate Country from query text when only using US symbols.
		if (query.indexOf(':') == 2) {
			if (query.length > 3) {
				query = query.substring(3);
			} else {
				query = '';
			}
		}
		
		var scRegExp = new RegExp('\\b(' + query + ')(.*)\\b', 'i'); //Find part to bold
		if (symbol.match(scRegExp)) {
			symbol = symbol.replace(scRegExp, '<span style="font-weight:bold">$1</span>$2');
		} else {
			companyName = companyName.replace(scRegExp, '<span style="font-weight:bold">$1</span>$2');
		}
		
		var aMarkup = ['<table class="resulttable"><tr><td class="col1">', symbol, '</td><td class="col2">', companyName, '</td></tr></table>'];
		
		return (aMarkup.join(''));
		
	},
		
	displayElements: function(state) {
		if (typeof state === 'undefined') {
			state = false;
		}
	},

	lookupStockSymbol: function() {
				
		//grab symbol
		var symbol = $F('searchQuote');
		
		if (symbol !== '') {

			//swap tab
			MainLocalObj.Markets.setActiveIndex($('myQuotesBtn'), MainLocalObj.Markets.togBtns);

			//convert to upper
			symbol = symbol.toUpperCase();
			
			//shouldn't be possible, but just in case
			if (typeof MainLocalObj.data.stockSymbols !== 'object') {
				MainLocalObj.data.stockSymbols = [];
			}
			
			//if exists, remove
			if (MainLocalObj.data.stockSymbols.indexOf(symbol) != '-1') {
				MainLocalObj.data.stockSymbols = MainLocalObj.data.stockSymbols.without(symbol);
			}

			//push symbol onto array
			MainLocalObj.data.stockSymbols.push(symbol);
			MainLocalObj.data.stockSymbols.reverse();
			MainLocalObj.data.stockSymbols = MainLocalObj.data.stockSymbols.slice(0,3);
			MainLocalObj.data.stockSymbols.reverse();					
			MainLocalObj.saveData();
			MainLocalObj.Markets.fetchStockQuotes(symbol);			

		}
	},
	
	fetchStockQuotes: function(symbol) {
				
		//resets
		if (typeof symbol === 'undefined') {
			symbol = false;
		}	
		$('pmMyQuotes').update('');
		if ($('searchQuote')) {
			$('searchQuote').blur();
			if (!$('pmMyQuotesTmp')) {
				var div = new Element('div', {
					'id': 'pmMyQuotesTmp',
					'style': 'position: absolute; left: -9999px;'
				});
				$('pmIndecies').insert({
					bottom: div
				});
			}
		}
									
		var stockSymbols;
		if (typeof MainLocalObj.data.stockSymbols === 'object' && MainLocalObj.data.stockSymbols.length > 0) {
			stockSymbols = MainLocalObj.data.stockSymbols.join();
		} else {
			MainLocalObj.data.stockSymbols = [];
			stockSymbols = '';
		}
						
		if (MainLocalObj.data.stockSymbols.length > 0) {
		
			var callObj = {
				url: 'http://stocks.cnn.com/custom/cnn-com/htmlinc-market-lookup.asp',
				args: 'symb=' + stockSymbols,
				domId: 'pmMyQuotes',
				funcObj: false,
				breakCache: true
			};
			MainLocalObj.CSIManager.callObject(callObj, 'requestMyQuotes');
			
			//work around until we can do callbacks on HTML requests
			var checkReady = setInterval(function() {
				if ($('pmMyQuotes').innerHTML !== '') {
					if ($('symbLookupError').innerHTML != '') {
					
						//set error decoration
						$('searchQuote').addClassName('errorColor');
						$('searchQuote').value = MainLocalObj.Markets.errorMsg;
						
						//remove latest search, restore last good results
						if ($('pmMyQuotesTmp').innerHTML !== '') {
							$('pmMyQuotes').select('ul')[0].remove();
							var lastSearch = $('pmMyQuotesTmp').innerHTML;
							$('pmMyQuotes').insert({
								bottom: lastSearch
							});
						}
						
						//atttempt to reload last good set of stock symbols
						if (MainLocalObj.data.prevStockSymbols.length > 0) {
							MainLocalObj.data.stockSymbols = MainLocalObj.data.prevStockSymbols.without(symbol);
						} else {
							MainLocalObj.data.stockSymbols = MainLocalObj.data.stockSymbols.without(symbol)
						}
						
						MainLocalObj.saveData(true);
						MainLocalObj.Markets.fixBorders();
						
					} else {
						MainLocalObj.Markets.fixBorders();
						MainLocalObj.Markets.syncBackup();
					}
					
					clearInterval(checkReady);
				}
			}, 1);
		} else {
			MainLocalObj.Markets.inputBlur($('searchQuote'));		
		}
	},

	inputFocus: function(e) {
		if (e.value === MainLocalObj.Markets.defaultMsg || e.value === MainLocalObj.Markets.errorMsg) {
			if ($('searchQuote').hasClassName('errorColor')) {
				$('searchQuote').removeClassName('errorColor');
			}
			e.value = ''
		}
		e.removeClassName('pmWeatherHollow');
	},
	
	inputBlur: function(e) {
		if (e.value === '') {
			e.value = MainLocalObj.Markets.defaultMsg;
			e.addClassName('pmWeatherHollow');
		}
	},

	fixBorders: function() {
		var quotes = $('pmMyQuotes').select('li');
		if (quotes.length) {
			quotes[quotes.length - 1].addClassName('last');
		}
	},
	
	syncBackup: function() {
		$('pmMyQuotesTmp').update($('pmMyQuotes').innerHTML);
		$('pmMyQuotesTmp').select('#symbLookupError').each(function(val) {
			val.remove();
		});
	},

	removeQuote: function(item) {
		var lookup = $('pmMyQuotes').select('li.market-' + item)[0];
		lookup.remove();
		
		//fix borders
		MainLocalObj.Markets.fixBorders();
				
		//delete from data
		MainLocalObj.data.stockSymbols.reverse();
		MainLocalObj.data.stockSymbols = MainLocalObj.data.stockSymbols.without(MainLocalObj.data.stockSymbols[item-1]);		
		MainLocalObj.data.prevStockSymbols = MainLocalObj.data.stockSymbols.splice();
		MainLocalObj.data.stockSymbols.reverse();
		MainLocalObj.saveData();

		//create backup
		MainLocalObj.Markets.syncBackup();

	}
};
