var ProgramGuideModule = function() {
	
	var $U = CBC.UTIL;
	var $ = $U.getElementsById;
	var _me;
	var _config = new Object();
	var _queryString;
	var _query = new Object();
	var _cookieExpiryDays = 365;
	var _timer;
	var _loadingImg;
	var _dataDiv;
	var _locationsDiv;
	var _connection;
	var _selectedLocationKey;
	var _selectedRow;
	var _locationsUrl = '/olympics/LocationList.do?mediaType=television';
	
	return {
		init: function(initObj) {
			_me = this;
			_config = initObj;
			_dataDiv = YAHOO.util.Dom.getElementsByClassName('data', 'div', $(_config.div))[0];
			_locationsDiv = YAHOO.util.Dom.getElementsByClassName('locations', 'div', $(_config.div))[0];
			_loadingImg = YAHOO.util.Dom.getElementsByClassName('loading', 'img', $(_config.div))[0];
			_selectedLocationKey = $U.readCookie('pgTvLocation');
			this.processQueryString();
			this.loadLocations();
			this.loadData();
			if (_config.mode == 'videopage') this.initVideo();
		},
		
		processQueryString: function() {
			_queryString = window.location.hash.substr(1);
			_query = this.queryStringToObj(_queryString);
		},
		
		initRefresh: function() {
			if (_timer) clearTimeout(_timer);
			_timer = setTimeout(this.loadData, _config.refreshTime * 1000);
		},
		
		initHover: function() {
			var rows = _dataDiv.getElementsByTagName("tr");
			for(var i=0;i<rows.length;i++) {
			  var row = rows[i];
			  if (row.className.indexOf("onair") >= 0) {
				  row.onmouseover = this.mouseOver;
				  row.onmouseout = this.mouseOut;
			  }
			}
		},
		mouseOver: function() {
			YAHOO.util.Dom.addClass(this, 'over');
		},
		mouseOut: function() {
			YAHOO.util.Dom.removeClass(this, 'over');
		},
		
		loadLocations: function() {
			_connection = YAHOO.util.Connect.asyncRequest('GET', _locationsUrl, {success:_me.displayLocations, failure:_me.fail, scope:_me, timeout:15000});
		},
		displayLocations: function(o) {
			if (o.responseText != undefined) {
				// add the dropdown to the dom
				_locationsDiv.innerHTML = '<select>'+o.responseText+'</select>';
				var sel = _locationsDiv.getElementsByTagName("select")[0];
				YAHOO.util.Event.addListener(sel, "change", _me.changeLocation, sel, _me);
				// if user has a location cookie set, select it in the dropdown
				if (_selectedLocationKey != null) {
					var opts = sel.options;
					for(var i=0;i<opts.length;i++) {
						if (opts[i].value == _selectedLocationKey) {
							sel.selectedIndex = i;
							break;
						}
					}
				}
			}
		},
		
		loadData: function() {
			YAHOO.util.Dom.addClass(_loadingImg, 'active');
			var dataUrl = _config.dataUrl;
			if (_selectedLocationKey) dataUrl+= "locationKey=" + _selectedLocationKey;
			_connection = YAHOO.util.Connect.asyncRequest('GET', dataUrl, {success:_me.displayData, failure:_me.fail, scope:_me, timeout:15000});
		},
		displayData: function(o) {
			if (o.responseText != undefined) {
				_dataDiv.innerHTML = o.responseText;
			}
			YAHOO.util.Dom.removeClass(_loadingImg, 'active'); 
			this.initRefresh();
			this.initHover();
			this.activateRow();
		},		
		
		
		changeLocation: function(e, targ) {
			clearTimeout(_timer);
			var selectedValue = targ.options[targ.selectedIndex].value;
			$U.createCookie('pgTvLocation',selectedValue,_cookieExpiryDays);
			_selectedLocationKey = selectedValue;
			this.loadData();
		},
		
		initVideo: function() {
			this.processQueryString();
			if (_queryString) {
				if (_selectedRow) YAHOO.util.Dom.removeClass(_selectedRow, 'active');
				this.activateRow();
				$('videoframe').src = "/olympics/livevideo/player.html?" +_queryString;
			}
		},
		
		activateRow: function() {
			if (_query) {
				var requestedNetwork = _query.n;
				_selectedRow = YAHOO.util.Dom.getElementsByClassName(requestedNetwork, 'tr', _dataDiv)[0];
				YAHOO.util.Dom.addClass(_selectedRow, 'active');
			}
		},
		deActivateRow: function() {
			YAHOO.util.Dom.removeClass(_selectedRow, 'active');
			_query = null;
		},
		
		
		watchNow: function(targ, network, program, episode, datetime, stream) {			
			var query = "n="+network+"&p="+program+"&e="+episode+"&dt="+datetime+"&s="+stream;
			window.location = "/olympics/livevideo/#"+query;
			if (_config.mode == 'videopage') this.initVideo();
			return false;
		},
		
		
		fail: function(o) {
			YAHOO.util.Dom.removeClass(_loadingImg, 'active'); 
			// failure
		},
		
		
		
		
		/* UTILITIES */
		
		queryStringToObj: function(qs) {
			var pairs = qs.split(/[;&]/);
			var obj = new Object();
			for (var i=0; i<pairs.length; i++) {
				var pair = pairs[i].split('=');
				var key = unescape(pair[0]);
				var val = unescape(pair[1]).replace(/\+/g, ' ');
				if (key.length > 0 && val.length > 0) {
					obj[key] = val;
				}
			}
			return obj;
		}

	}
}
