/*
 * 
 * 
 * 
 */
function ws_Athletes() {
	var me = this;
		
	me.divContainerHeader = ".Right-Panel-Header";
	me.divContainer = ".Right-Panel-Container";
	me.divContainerNews = "#Right-Panel-News";
	me.divContainerOneRiot = "#Right-Panel-OneRiot";
	me.divContainerMedals = ".Right-Panel-Medals";
	
	me.displayAthletes;
	me.displayEvents;
	me.displayMedals;
	me.displayWeather = null;
	me.displayNews;
	me.displayTweets;
	
	me.ghelpers = new GEHelpers(ge);
	
	me.athletesData = [];
	me.currentAthlete = null;
	me.currentAthList = {select: null, list: null};
	
	me.cameraView = {lat: null, lng: null, range: null, heading: null, tilt: null};
	
	// athlete events
	me.addEvListener = function(func) {
		google.earth.addEventListener(ge.getView(), 'viewchangeend', me.openAthleteBalloon);
	};
	me.remEvListener = function(func) {
		google.earth.removeEventListener(ge.getView(), 'viewchangeend', me.openAthleteBalloon);		
	};
	// balloon events
	me.addBalEvListener = function(func) {
		google.earth.addEventListener(ge, 'balloonclose', control.onBalloonClose);
	};
	me.remBalEvListener = function(func) {
		google.earth.removeEventListener(ge, 'balloonclose', control.onBalloonClose);
	};
	// OneRiot Events
	me.addAthEvListener = function(placemark) {
		google.earth.addEventListener(placemark, 'click', me.athleteOneRiot);
	};
	me.remAthEvListener = function(placemark) {
		google.earth.removeEventListener(placemark, 'click', me.athleteOneRiot);
	};
	// athletes with twitter feeds
	me.twitters = [1, 5, 8, 20, 28, 29, 33, 35, 44, 55, 66, 72, 84, 87, 96, 104, 120, 128, 129, 130, 165, 167, 171, 177, 179, 183, 184, 195, 211, 220, 228, 233, 235, 280, 297, 298, 306, 312, 324, 328, 331, 344, 354, 356, 365, 369, 389, 403, 406, 407, 412, 418, 422, 429, 435, 455, 458, 462, 479, 484, 490, 498, 516, 528, 531, 532, 542, 545, 546, 548, 561];

	// olympic villages
	var latlng = new google.maps.LatLng(49.271600, -123.107111);
	var opts = {name: 'Vancouver Olympic Village', snippet: 'athletes'};
	me.ghelpers.createPointPlacemark(latlng, opts);

	var latlng = new google.maps.LatLng(50.079240, -123.041695);
	var opts = {name: 'Whistler Olympic Village', snippet: 'athletes'};
	me.ghelpers.createPointPlacemark(latlng, opts);
	
	// event centers
	me.whistlerOlympicPark = {lat: '50.137140', lng: '-123.110657', range: '409.064906', heading: '-52.619437', tilt: '51.108935'};
	me.whistlerCreekside = {lat: '50.09491546995168', lng: '-122.9908981895206', range: '139.3625231565732', heading: '148.2766594802119', tilt: '73.30587857365843'};
	me.whistlerSlidingCentre = {lat: '50.10526973055104', lng: '-122.9410395855618', range: '1711.038887420606', heading: '66.36950621410948', tilt: '60.33232286803114'};
	me.cypressMountain = {lat: '49.39012975726419', lng: '-123.1985264377441', range: '1216.531926883792', heading: '-60.90066733910233', tilt: '69.48641676401468'};
	me.pacificColiseum = {lat: '49.285720', lng: '-123.043797', range: '315.010046', heading: '-80.047090', tilt: '81.125522'};
	me.canadaHockeyPlace = {lat: '49.277828', lng: '-123.108254', range: '127.948493', heading: '-66.771043', tilt: '81.993898'};
	me.ubcThunderbirdArena = {lat: '49.26051571379147', lng: '-123.2420857768011', range: '167.986848943023', heading: '-89.38197579986971', tilt: '83.28901159315456'};
	me.vancouverOlympicCenter = {lat: '49.24371666428624', lng: '-123.1074669094354', range: '205.9246428869867', heading: '-4.716764420932908', tilt: '78.53752201150897'};
	me.richmondOlympicOval = {lat: '49.17709117674778', lng: '-123.148532328444', range: '561.7811356817799', heading: '43.91514587303694', tilt: '88.25829627800977'};

	// countries
	me.Australia = {lat: '-25.27439884185205', lng: '133.7751363968769', range: '5589055.911294127', heading: '-1.694481297163478e-007', tilt: '0'};
	me.Austria = {lat: '47.58615336262997', lng: '13.35897669647956', range: '663230.9198094268', heading: '-0.8746652514786805', tilt: '0'};
	me.Belgium = {lat: '50.50248774053563', lng: '4.47481769839213', range: '246134.6891289356', heading: '0.0001939332082130756', tilt: '0'};
	me.Canada = {lat: '52.33777825693555', lng: '-96.83686595299577', range: '3639447.921879704', heading: '7.726468955372633', tilt: '0'};
	me.China = {lat: '35.86721699273743', lng: '104.2953315878072', range: '5265552.472411026', heading: '-0.02502471185384415', tilt: '0'};
	me.Croatia = {lat: '45.09930430247118', lng: '15.20916610863568', range: '548465.1560310307', heading: '0.0009673292984366653', tilt: '0'};
	me.CzechRepublic = {lat: '49.8168787157199', lng: '15.47292604392914', range: '489210.2977987633', heading: '-2.066788973420867e-006', tilt: '0'};
	me.Denmark = {lat: '56.26392004826541', lng: '9.501784525883231', range: '420711.3992370351', heading: '-3.942777203028313e-007', tilt: '0'};
	me.Estonia = {lat: '58.59527197496993', lng: '25.01360702063903', range: '395507.3144843506', heading: '1.761553072827782e-008', tilt: '0'};
	me.Finland = {lat: '61.92411053130218', lng: '25.74815154383489', range: '1508848.624467987', heading: '4.798390828788126e-007', tilt: '0'};
	me.France = {lat: '46.22763850326418', lng: '2.2137488089689', range: '1056650.597150937', heading: '-1.379427920787567e-007', tilt: '0'};
	me.Georgia = {lat: '42.31541811052521', lng: '43.35685250669935', range: '712845.5244719351', heading: '4.659673095952728e-006', tilt: '0'};
	me.Germany = {lat: '51.16569126459144', lng: '10.45152575047973', range: '1152412.043449905', heading: '-1.943671077291313e-007', tilt: '0'};
	me.Ghana = {lat: '7.946526988380724', lng: '-1.023194027686235', range: '733556.6778775422', heading: '-3.827609136124549e-009', tilt: '0'};
	me.GreatBritain = {lat: '54.06178242615813', lng: '-2.527208062056192', range: '1145133.693126757', heading: '0.6868032355823169', tilt: '0'};
	me.India = {lat: '20.59368388655401', lng: '78.9628794049556', range: '2800853.617437127', heading: '-2.092999956356355e-007', tilt: '0'};
	me.Italy = {lat: '42.37999726276796', lng: '12.68706799098402', range: '1558066.504081906', heading: '0.09605259564126491', tilt: '0'};
	me.Japan = {lat: '35.54913507350086', lng: '137.5716852370696', range: '1571212.263756488', heading: '-0.3719180303637543', tilt: '0'};
	me.Latvia = {lat: '57.4099496515278', lng: '25.05930962354089', range: '602923.3318208857', heading: '0.3843201373026577', tilt: '0'};
	me.Liechtenstein = {lat: '47.16605564493832', lng: '9.555451563646622', range: '36623.26992359812', heading: '-1.080262843591706e-005', tilt: '0'};
	me.Netherlands = {lat: '52.13263298308376', lng: '5.291265955335565', range: '403429.1219339478', heading: '-3.52596331743979e-008', tilt: '0'};
	me.Norway = {lat: '60.9864967443529', lng: '12.51102577726401', range: '2376487.109196671', heading: '3.448533698442665', tilt: '0'};
	me.Poland = {lat: '51.9194380572507', lng: '19.1451362045973', range: '946515.4687682841', heading: '1.610476105250102e-007', tilt: '0'};
	me.Russia = {lat: '60.60287964900736', lng: '105.3187568120375', range: '7674526.815327771', heading: '7.074783388945223e-007', tilt: '0'};
	me.Slovenia = {lat: '46.15124097960729', lng: '14.99546300048592', range: '259651.9093549101', heading: '3.503810356514061e-010', tilt: '0'};
	me.SouthKorea = {lat: '35.90775688229478', lng: '127.766921900596', range: '784560.3327099059', heading: '-5.8298651311478e-008', tilt: '0'};
	me.Sweden = {lat: '60.12816107337655', lng: '18.64350122802576', range: '1592666.160706101', heading: '1.97730619047814e-007', tilt: '0'};
	me.Switzerland = {lat: '46.8181881157299', lng: '8.227512081766291', range: '315588.0419363296', heading: '5.962267885385376e-008', tilt: '0'};
	me.Turkey = {lat: '38.96374435316303', lng: '35.24332208298985', range: '1080231.122586361', heading: '5.218633129684438e-008', tilt: '0'};
	me.Ukraine = {lat: '48.37943283638911', lng: '31.16558003438212', range: '1016521.868602995', heading: '2.570258717242912e-008', tilt: '0'};

    // init
	me.getOneRiot('2010 Olympics');
	$(me.divContainerHeader).html('<div id="header-text">Athletes & Events</div>');
}

ws_Athletes.prototype.getAthletes = function() {
	ge.setBalloon(null);
	
	var me = this;
	me.clearPlacemarks('athletes');
	me.athletesData = [];
	$(me.divContainerMedals).hide();
	$(me.divContainer).html('');
	$(me.divContainer).show();

	resetView();
	
	var country = $("#ath_country").val();
	var sport = $("#ath_sport").val();

	if(country == 'aus') {
		this.cameraView = this.Australia;
	} else if (country == 'aut') {
		this.cameraView = this.Austria;
	} else if (country == 'bel') {
		this.cameraView = this.Belgium;
	} else if (country == 'can') {
		this.cameraView = this.Canada;
	} else if (country == 'chn') {
		this.cameraView = this.China;
	} else if (country == 'cro') {
		this.cameraView = this.Croatia;
	} else if (country == 'cze') {
		this.cameraView = this.CzechRepublic;
	} else if (country == 'den') {
		this.cameraView = this.Denmark;
	} else if (country == 'est') {
		this.cameraView = this.Estonia;
	} else if (country == 'fin') {
		this.cameraView = this.Finland;
	} else if (country == 'fra') {
		this.cameraView = this.France;
	} else if (country == 'geo') {
		this.cameraView = this.Georgia;
	} else if (country == 'ger') {
		this.cameraView = this.Germany;
	} else if (country == 'gha') {
		this.cameraView = this.Ghana;
	} else if (country == 'gbr') {
		this.cameraView = this.GreatBritain;
	} else if (country == 'ind') {
		this.cameraView = this.India;
	} else if (country == 'ita') {
		this.cameraView = this.Italy;
	} else if (country == 'jpn') {
		this.cameraView = this.Japan;
	} else if (country == 'lat') {
		this.cameraView = this.Latvia;
	} else if (country == 'lie') {
		this.cameraView = this.Liechtenstein;
	} else if (country == 'ned') {
		this.cameraView = this.Netherlands;
	} else if (country == 'nor') {
		this.cameraView = this.Norway;
	} else if (country == 'pol') {
		this.cameraView = this.Poland;
	} else if (country == 'rus') {
		this.cameraView = this.Russia;
	} else if (country == 'slo') {
		this.cameraView = this.Slovenia;
	} else if (country == 'kor') {
		this.cameraView = this.SouthKorea;
	} else if (country == 'swe') {
		this.cameraView = this.Sweden;
	} else if (country == 'sui') {
		this.cameraView = this.Switzerland;
	} else if (country == 'tur') {
		this.cameraView = this.Turkey;
	} else if (country == 'ukr') {
		this.cameraView = this.Ukraine;
	} else {
		this.cameraView.lat = 41.848905;
		this.cameraView.lng = -95.907471;
		this.cameraView.range = 5500000;
		this.cameraView.heading = 0;
		this.cameraView.tilt = 45;
	}
	
	moveCamera(this.cameraView.lat, this.cameraView.lng, this.cameraView.range, this.cameraView.heading, this.cameraView.tilt);

	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "athletes", "country": country, "sport": sport}, function(data) {
		var jsonObject = JSON.parse(data);

		var rpHtml = '';
		for(var i = 0; i < jsonObject.length; i++) {
			var latlng = new google.maps.LatLng(jsonObject[i].lat, jsonObject[i].lng);

			var blndesc = me.formatAthleteBalloon(jsonObject[i]);

			var opts = {
					name: jsonObject[i].name, 
					balloon: blndesc,
					snippet: 'athletes', 
					icon: BASE_URL+'images/sports-icons/'+jsonObject[i].sport.replace(' ', '-').toLowerCase()+'.png'
				};
			
			var placemark = me.ghelpers.createPointPlacemark(latlng, opts);
			
			me.athletesData.push({"name": jsonObject[i].name, "placemark": placemark, "balloon": blndesc});
			me.addAthEvListener(placemark);

			rpHtml += me.formatAthlete(jsonObject[i], i);
		}

		me.currentAthList.header = $(me.divContainerHeader).html();
		me.currentAthList.list = rpHtml;
		
		$(me.divContainer).html(rpHtml);
	});
};

ws_Athletes.prototype.getEvents = function(sortDate, sortEvents) {
	var me = this;
	$(me.divContainerMedals).hide();
	$(me.divContainer).html('');
	$(me.divContainer).show();
	
	var sortDate = $("#eve_date").val();
	var sortEvents = $("#eve_event").val();
	
	// lat, lng, range, heading, tilt
	if(sortEvents == "Alpine Skiing") {
		this.cameraView = this.whistlerCreekside;
	} else if(sortEvents == "Biathlon") {
		this.cameraView = this.whistlerOlympicPark;
	} else if(sortEvents == "Bobsleigh") {
		this.cameraView = this.whistlerSlidingCentre;
	} else if(sortEvents == "Cross-Country Skiing") {
		this.cameraView = this.whistlerOlympicPark;
	} else if(sortEvents == "Curling") {
		this.cameraView = this.vancouverOlympicCenter;
	} else if(sortEvents == "Figure Skating") {
		this.cameraView = this.pacificColiseum;
	} else if(sortEvents == "Freestyle Skiing") {
		this.cameraView = this.cypressMountain;
	} else if(sortEvents == "Ice Hockey") {
		this.cameraView = this.ubcThunderbirdArena;
	} else if(sortEvents == "Luge") {
		this.cameraView = this.whistlerSlidingCentre;
	} else if(sortEvents == "Nordic Combined") {
		this.cameraView = this.whistlerOlympicPark;
	} else if(sortEvents == "Short Track Speed Skating") {
		this.cameraView = this.pacificColiseum;
	} else if(sortEvents == "Skeleton") {
		this.cameraView = this.whistlerSlidingCentre;
	} else if(sortEvents == "Ski Jumping") {
		this.cameraView = this.whistlerOlympicPark;
	} else if(sortEvents == "Speed Skating") {
		this.cameraView = this.richmondOlympicOval;
	} else if(sortEvents == "Snowboard") {
		this.cameraView = this.cypressMountain;
	} else {
		this.cameraView.lat = 49.6280;
		this.cameraView.lng = -123.0092;
		this.cameraView.range = 110000;
		this.cameraView.heading = 30;
		this.cameraView.tilt = 70;
	}
	
	moveCamera(this.cameraView.lat, this.cameraView.lng, this.cameraView.range, this.cameraView.heading, this.cameraView.tilt);

	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "events", "dates": sortDate, "event": sortEvents}, function(data) {
		var jsonObject = JSON.parse(data);

		var rpHtml = '';
		for(var i=0; i<jsonObject.length;i++) {
			rpHtml += me.formatEvent(jsonObject[i]);
		}
		$(me.divContainer).html(rpHtml);
	});
};

ws_Athletes.prototype.getMedals = function() {
	var me = this;
	
	$(me.divContainerHeader).html('<div id="header-text">Medal Count</div>');
	$(me.divContainerMedals).show();
	$(me.divContainer).html('');
	$(me.divContainer).hide();
	
	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "medals"}, function(data) {
	});
	
};

ws_Athletes.prototype.getWeather = function() {
	var me = this;
	
	var headerHtml = '<img src="'+BASE_URL+'images/weather/logos/TWClogo_61px.png" />';
	$(me.divContainerHeader).html('<div id="header-text">Weather</div>');
	$(me.divContainerMedals).hide();
	$(me.divContainer).html('');
	$(me.divContainer).show();

	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "weather"}, function(data) {
		var jsonObject = JSON.parse(data);

		var locWeather	= '<div class="ws_weather">'
			+ '<span> Vancouver </span>'
			+ '<div class="ws_weather_temp">' + jsonObject.van_temp + '</div>'
			+ '<div class="ws_weather_desc">' + jsonObject.van_description + '</div>'
			+ '<img src="' + BASE_URL + 'images/weather/icons/31x31/' + jsonObject.van_icon + '.png" />'
			+ '</div>'
			+ '<div class="ws_weather">'
			+ '<span> Whistler </span>'
			+ '<div class="ws_weather_temp">' + jsonObject.whi_temp + '</div>'
			+ '<div class="ws_weather_desc">' + jsonObject.whi_description + '</div>'
			+ '<img src="' + BASE_URL + 'images/weather/icons/31x31/' + jsonObject.whi_icon + '.png" />'
			+ '</div>';
		$(me.divContainer).html(locWeather);

	});

	google.earth.fetchKml(ge, 'http://bbs.keyhole.com/ubb/ubbthreads.php?ubb=download&Number=17068', function(kmlObject) {
		me.kmlLoaded(kmlObject);
		me.displayWeather = kmlObject;
	});
};

ws_Athletes.prototype.getNews = function(result) {
	$(this.divContainerHeader).html('<div id="header-text"><a href="#" onclick="control.getNews(\'http://sports.yahoo.com/oly/rss.xml\');">News</a> and <div class="tv_sched"><a href="#" onclick="control.getTvList();">TV Schedule</a></div></div>');
	$(this.divContainerMedals).hide();
	$(this.divContainer).html('');
	$(this.divContainer).show();
	$(this.divContainer).append('<div id="Right-Panel-News"></div>');
	$(this.divContainerOneRiot).html('');

	if(typeof result == 'string') {
	} else {
		if (result.error || result.entries.length <= 0) {
			$(control.divContainerNews).html('No Results Found');
			return;
		}
	}

	var feed = new google.feeds.Feed(result);
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("Right-Panel-News");
			
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				
				var newsContent = '<div class="news-title"> <a href="'+entry['link']+'" target="_blank">'+entry['title']+'</a> </div>'
								+ '<div class="news-publish">'+entry['publishedDate']+'</div>'
								+ '<div class="news-content">'+entry['contentSnippet']+'</div>';
				
				$(control.divContainerNews).append(newsContent);				
			}
		}
	});
};

ws_Athletes.prototype.athleteOneRiot = function(event) {
	$(control.divContainerMedals).hide();
	$(control.divContainer).html('');
	$(control.divContainer).show();
	control.getOneRiot(event.getTarget().getName());
	control.addBalEvListener();
};

ws_Athletes.prototype.getOneRiot = function(query) {
	var me = this;
	
	$(this.divContainer).append('<div id="Right-Panel-OneRiot"></div>');
	
	$.ajax({
		url: "http://openapi.oneriot.com/search?appId=waterstone01&version=1.1&q="+query,
		dataType: "jsonp",
		jsonpCallback: com_oneriot_search_results
	});
};

function com_oneriot_search_results(data) {
	if(data.totalResults) {
		if(data.featuredResultList) {
			var entry = data.featuredResultList[0];
			var featHtml	= '<div class="oneriot-featured-title"><a href="'+entry.redirectUrl+'" target="_blank">'+entry.title+'</a></div>'
							+ '<div class="oneriot-featured-snippet">'+entry.snippet+'</div>'
							+ '<div style="position: fixed; left: 0; top: 0;"><img src="'+entry.trackingUrl+'" /></div>';
			$(control.divContainerOneRiot).html(featHtml);
		}

		var count=0;
		if(data.resultList.length > 4) {
			count=4;
		} else {
			count=1;
		}

		for(var i=0; i<count; i++) {
			var entry = data.resultList[i];
			var html 	= '<div class="oneriot-title"><a href="'+entry.redirectUrl+'" target="_blank">'+entry.title+'</a></div>'
						+ '<div class="oneriot-snippet">'+entry.snippet+'</div>';
			$(control.divContainerOneRiot).append(html);
		}
	} else {
		$(control.divContainerOneRiot).append("<span> No results found. </span>");		
	}
}

ws_Athletes.prototype.getTvList = function() {
	var me = this;
	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "tv"}, function(data) {
		var jsonObject = JSON.parse(data);
	
		var html = '';
		for(var i=0; i<jsonObject.length; i++) {
			html 	+='<div class="tv-listing">'
					+ '	<div class="tv-listing-date"> <span>Date:</span> '+jsonObject[i].date+'</div>'
					+ '	<div class="tv-listing-channel"> <span>Network:</span> '+jsonObject[i].channel+'</div>'
					+ ' <div class="tv-listing-event"> <span>Event:</span> '+jsonObject[i].event+'</div>'
					+ ' <div class="tv-listing-time"> <span>Time:</span> '+jsonObject[i].time+'</div>'
					+ '</div>';
		}
		$(me.divContainer).html(html);
	});
};

ws_Athletes.prototype.kmlLoaded = function(kmlObject) {
	if(!kmlObject) {
		setTimeout(function() {
			alert('Bad or null KML.');
		}, 0);
		return;
	}
	ge.getFeatures().appendChild(kmlObject);
};

ws_Athletes.prototype.loadEventCenters = function() {
	var latlng = new google.maps.LatLng(49.277756, -123.108775);
	var opts = {name: 'Canada Hockey Place', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(49.242875, -123.108168);
	opts = {name: 'Vancouver Olympic Centre', snippet: 'event_centers'};
	this.createVenue(latlng, opts);

	latlng = new google.maps.LatLng(49.285916, -123.042693);
	opts = {name: 'Pacific Coliseum', snippet: 'event_centers'};
	this.createVenue(latlng, opts);

	latlng = new google.maps.LatLng(49.260400, -123.241975);
	opts = {name: 'UBC Thunderbird Arena', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(50.108759, -122.944515);
	opts = {name: 'Whistler Sliding Centre', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(50.094957, -122.990846);
	opts = {name: 'Whistler Creekside', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(50.137096, -123.114286);
	opts = {name: 'Whistler Olympic Park', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(49.174391, -123.153451);
	opts = {name: 'Richmond Olympic Oval', snippet: 'event_centers'};
	this.createVenue(latlng, opts);
	
	latlng = new google.maps.LatLng(49.391288, -123.205399);
	opts = {name: 'Cypress Mountain', snippet: 'event_centers'};
	this.createVenue(latlng, opts);	
};

ws_Athletes.prototype.findAthlete = function(num) {
	ge.setBalloon(null);
	this.currentAthlete = num;

	var lat = this.athletesData[num].placemark.getGeometry().getLatitude();
	var lng = this.athletesData[num].placemark.getGeometry().getLongitude();
	
	moveCamera(lat, lng, 1000, 45, 45);
	
	this.addEvListener();
	
	$(this.divContainer).html('');
	control.getOneRiot(this.athletesData[this.currentAthlete].name);
	control.addBalEvListener();
};

ws_Athletes.prototype.openAthleteBalloon = function() {	
	var balloon = control.setAthleteBalloon(control.athletesData[control.currentAthlete]);
	ge.setBalloon(balloon);
	
	control.currentAthlete = null;
	control.remEvListener();
};

ws_Athletes.prototype.onBalloonClose = function() {
	control.displayTweets = 0;
	$(control.divContainer).html(control.currentAthList.list);
	$(control.divContainerHeader).html(control.currentAthList.header);	
	$(control.divContainerOneRiot).html('');
	control.remBalEvListener();
};

ws_Athletes.prototype.setAthleteBalloon = function(data) {
	var balloon;
	
	balloon = ge.createHtmlStringBalloon('');
	balloon.setFeature(data.placemark);
	balloon.setMaxWidth(500);
	balloon.setContentString(data.balloon);

	return balloon;
};

ws_Athletes.prototype.formatAthlete = function(obj, num) {
	var html	= '<div class="ws_athlete" onclick="control.findAthlete('+num+');">'
				+ '<div class="ws_athlete_image"><img src="http://www.nbcolympics.com' + obj.image_src + '"/> </div>'
				+ '<div class="ws_athlete_name"> <span>Name:</span> ' + obj.name + '</div>'
				+ '<div class="ws_athlete_sport"> <span>Sport:</span> ' + obj.sport + '</div>'
				+ '<div class="ws_athlete_country"> <span>Country:</span> ' + obj.country + '</div>'
				+ '</div>';
	return html;
};

ws_Athletes.prototype.formatAthleteBalloon = function(data) {
	var twitterHtml = '';
	for(var i=0; i<this.twitters.length; i++) {
		if(data.id == this.twitters[i]) {
			twitterHtml = '<div class="balloon-athlete_twitter"><a href="#" id="tweettog" onclick="control.getTweets('+data.id+', \''+data.twitter_uid+'\')"> Latest tweets </a></div>';
		}
	}
	
	var html 	= '<div class="balloon-container">'
				+ '	<div class="balloon_athlete_name"> <a href="http://www.nbcolympics.com' + data.page_link + '" target="_blank">' + data.name + '</a> </div>'
				+ '	<div class="balloon_athlete_image"> <img src="http://www.nbcolympics.com' + data.image_src + '"/>' + '</div>'
				+ '	<div class="balloon_athlete_country"> <span>Country:</span> ' + data.country + '</div>'
				+ '	<div class="balloon_athlete_sport"> <span>Competition:</span> ' + data.sport + '</div>'
				+ '	<div class="balloon_athlete_birthday"> <span>Birthday:</span> ' + data.birthday + '</div>'
				+ '	<div class="balloon_athlete_birthplace"> <span>Birthplace:</span> ' + data.birthplace + '</div>'
				+ '	<div class="balloon_athlete_hometown"> <span>Hometown:</span> ' + data.hometown + '</div>'				
				+ '	<div class="balloon_athlete_height"> <span>Height:</span> ' + data.height + '</div>'				
				+ twitterHtml
				+ '	<div class="balloon_athlete_bio" style="float:right"> <span>Bio:</span> ' + data.short_bio + '</div>'
				+ '	<div class="balloon_athlete_bronze" style="float:right; margin-right:10px;"> <img src="'+BASE_URL+'images/medals/bronze.png" height="20" width="20" /> ' + data.bronze + '</div>'			
				+ '	<div class="balloon_athlete_silver" style="float:right; margin-right:10px;"> <img src="'+BASE_URL+'images/medals/silver.png" height="20" width="20" /> ' + data.silver + '</div>'			
				+ '	<div class="balloon_athlete_gold" style="float:right; margin-right:10px;"> <img src="'+BASE_URL+'images/medals/gold.png" height="20" width="20" /> ' + data.gold + '</div>'			
				+ '</div>';
	
	return html;
};

ws_Athletes.prototype.getTweets = function(id, tid) {
	var me = this;
	
	if(me.displayTweets) {
		$(me.divContainer).html('');
		$(me.divContainerNews).html('');
		$(me.divContainerOneRiot).html('');

		$(me.divContainer).html(me.currentAthList.list);	
		$(me.divContainerHeader).html(me.currentAthList.header);

		me.displayTweets = 0;
	} else {
		$(me.divContainer).html('');
		$(me.divContainerNews).html('');
		$(me.divContainerOneRiot).html('');
		
		$(me.divContainerHeader).html('<div id="header-text">Tweets <a href="http://twitter.com/'+tid+'" target="_blank">Follow</a></div>');
		
		$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "tweets",  "id": id, "user": tid}, function(data) {
			var jsonObject = JSON.parse(data);
			
			var html = '<div class="tweet-container">';
			for(var i=0; i<jsonObject.length; i++) {
				html +='<div class="tweet">'+jsonObject[i].tweet+'</div>';
			}
				html +='</div>';
			
			$(me.divContainer).html(html);
		});
		
		me.displayTweets = 1;
	}
};

ws_Athletes.prototype.formatEvent = function(obj) {
	var html	= '<div class="ws_event">'
				+ '<div class="ws_event_date"> <span>Date:</span> ' + obj.date + '</div>'
				+ '<div class="ws_event_event"> <span>Event:</span> ' + obj.event + '</div>'
				+ '<div class="ws_event_description"> <span>Description:</span> ' + obj.description + '</div>'
				+ '<div class="ws_event_venue"> <span>Venue:</span> ' + obj.venue + '</div>'
				+ '<div class="ws_event_time"> <span>Time:</span> ' + obj.time.split("-")[0] + '</div>'
				+ '</div>';
				
	return html;
};

ws_Athletes.prototype.formatEventBalloon = function(obj) {
	var html	= '<div class="ws_balloon_event" style="float:left">'
				+ '<div class="ws_balloon_event_date"> <span>Date:</span> ' + obj.date + '</div>'
				+ '<div class="ws_balloon_event_event"> <span>Event:</span> ' + obj.event + '</div>'
				+ '<div class="ws_balloon_event_description"> <span>Description:</span> ' + obj.description + '</div>'
				+ '<div class="ws_balloon_event_venue"> <span>Venue:</span> ' + obj.venue + '</div>'
				+ '<div class="ws_balloon_event_time"> <span>Time:</span> ' + obj.time.split("-")[0] + '</div>'
				+ '</div>';
				
	return html;
};

ws_Athletes.prototype.formatMedals = function(obj) {
	var html	= '<div>'
				+ '</div>';
	return html;
};

ws_Athletes.prototype.clearPlacemarks = function(snippet) {
	var c = ge.getFeatures().getFirstChild();
	while (c) {
		var s = c.getNextSibling();
		if(c.getSnippet() == snippet) {
			ge.getFeatures().removeChild(c);
		}
		c = s;
	}
};

ws_Athletes.prototype.createVenue = function(latlng, opt_opts) {
	var me = this;
	var opts = opt_opts;
	
	$.get(LIB_URL+'db/'+'ajaxDb.php', {"type": "events", "venue": opts.name}, function(data) {
		var jsonObject = JSON.parse(data);
		
		var img;
		if(opts.name == "Canada Hockey Place") {
			img = '<img src="http://www.vancouver2010.com/img/00/18/60/img-2973-web_90imgGalBig-uC.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Vancouver Olympic Centre") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/57/vancouver-olympic-centre---interior_66imgGalBig-bq.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Pacific Coliseum") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/69/pacific-coliseum---front_50imgGalBig-Ub.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "UBC Thunderbird Arena") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/59/ubc-thunderbird-arena---event_58imgGalBig-Ik.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Whistler Sliding Centre") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/65/the-whistler-sliding-centre---luge_34imgGalBig-EV.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Whistler Creekside") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/65/whistler-creekside---aerial-2-closer_20imgGalBig-ym.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Whistler Olympic Park") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/64/whistler-olympic-park---ski-jump-from-top_88imgGalBig-Kr.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Richmond Olympic Oval") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/58/richmond-olympic-oval---front-night_10imgGalBig-qU.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		} else if(opts.name == "Cypress Mountain") {
			img = '<img src="http://www.vancouver2010.com/img/00/11/57/cypress---aerial---whole-moutain_24imgGalBig-vO.jpg" style="float: left; height: 100px; width: 140px; margin-bottom: 15px;"/>';
		}

		var rpHtml 	= '<div class="balloon-header">'+ img +'<span style="float: left; font-weight: bold; font-size: 14px; margin: 20px 0px 0px 10px; width: 180px;">'+opts.name+' Event Schedule</span></div>'
					+ '<div class="balloon-wr"><div class="ws_balloon_event_ticket"> <a href="http://www.vancouver2010.com/olympic-tickets/olympic/"> Buy Tickets </a> </div></div>';
		for(var i=0; i<jsonObject.length;i++) {
			rpHtml += me.formatEventBalloon(jsonObject[i]);
		}
		
		opts.balloon = rpHtml;
		var placemark = me.ghelpers.createPointPlacemark(latlng, opts);
	});	
};

ws_Athletes.prototype.sortAthletes = function() {
	var rpHeader	= '<div class="select-wr">'
					+ '<span> Choose Country: </span>'
					+ '<select id="ath_country" onchange="control.getAthletes();">'
					+ ' <option value="">All Countries</option>'
					+ '	<option value="usa" SELECTED>United States</option>'
					+ '	<option value="can">Canada</option>'
					+ '	<option value="aus">Australia</option>'
					+ '	<option value="aut">Austria</option>'
					+ '	<option value="bel">Belgium</option>'
					+ '	<option value="chn">China</option>'
					+ '	<option value="cro">Croatia</option>'
					+ '	<option value="cze">Czech Republic</option>'
					+ '	<option value="den">Denmark</option>'
					+ '	<option value="est">Estonia</option>'
					+ '	<option value="fin">Finland</option>'
					+ '	<option value="fra">France</option>'
					+ '	<option value="geo">Georgia</option>'
					+ '	<option value="ger">Germany</option>'
					+ '	<option value="gha">Ghana</option>'
					+ '	<option value="gbr">Great Britain</option>'
					+ '	<option value="ind">India</option>'
					+ '	<option value="ita">Italy</option>'
					+ '	<option value="jpn">Japan</option>'
					+ '	<option value="lat">Latvia</option>'
					+ '	<option value="lie">Liechtenstein</option>'
					+ '	<option value="ned">Netherlands</option>'
					+ '	<option value="nor">Norway</option>'
					+ '	<option value="pol">Poland</option>'
					+ '	<option value="rus">Russia</option>'
					+ '	<option value="slo">Slovenia</option>'
					+ '	<option value="kor">South Korea</option>'
					+ '	<option value="swe">Sweden</option>'
					+ '	<option value="sui">Switzerland</option>'
					+ '	<option value="tur">Turkey</option>'
					+ '	<option value="ukr">Ukraine</option>'
					+ '</select>'
					+ '</div>';
	
	rpHeader		+='<div class="select-wr">'
					+ '<span> Choose Sport: </span>'
					+ '<select id="ath_sport" onchange="control.getAthletes();">'
					+ ' <option value="">All Sports</option>'
					+ '	<option value="Alpine Skiing">Alpine Skiing</option>'
					+ '	<option value="Biathlon">Biathlon</option>'
					+ '	<option value="Bobsled">Bobsled</option>'
					+ '	<option value="Cross-Country">Cross-Country</option>'
					+ '	<option value="Curling">Curling</option>'
					+ '	<option value="Figure Skating">Figure Skating</option>'
					+ '	<option value="Freestyle Skiing">Freestyle Skiing</option>'
					+ '	<option value="Hockey">Hockey</option>'
					+ '	<option value="Luge">Luge</option>'
					+ '	<option value="Nordic Combined">Nordic Combined</option>'
					+ '	<option value="Ski Jumping">Ski Jumping</option>'
					+ '	<option value="Short Track">Short Track</option>'
					+ '	<option value="Skeleton">Skeleton</option>'
					+ '	<option value="Snowboarding">Snowboarding</option>'
					+ '	<option value="Speed Skating">Speed Skating</option>'
					+ '</select>'
					+ '</div>';
	
	$(this.divContainerHeader).html(rpHeader);
};

ws_Athletes.prototype.sortEvents = function() {
	var rpHeader	= '<div class="select-wr">'
					+ '<span> Choose Date: </span>'
					+ '<select id="eve_date" onchange="control.getEvents();">'
					+ ' <option value="">All Dates</option>'
					+ '	<option value="Feb. 12">Feb. 12</option>'
					+ '	<option value="Feb. 13">Feb. 13</option>'
					+ '	<option value="Feb. 14">Feb. 14</option>'
					+ '	<option value="Feb. 15">Feb. 15</option>'
					+ '	<option value="Feb. 16">Feb. 16</option>'
					+ '	<option value="Feb. 17">Feb. 17</option>'
					+ '	<option value="Feb. 18">Feb. 18</option>'
					+ '	<option value="Feb. 19">Feb. 19</option>'
					+ '	<option value="Feb. 20">Feb. 20</option>'
					+ '	<option value="Feb. 21">Feb. 21</option>'
					+ '	<option value="Feb. 22">Feb. 22</option>'
					+ '	<option value="Feb. 23">Feb. 23</option>'
					+ '	<option value="Feb. 24">Feb. 24</option>'
					+ '	<option value="Feb. 25">Feb. 25</option>'
					+ '	<option value="Feb. 26">Feb. 26</option>'
					+ '	<option value="Feb. 27">Feb. 27</option>'
					+ '	<option value="Feb. 28">Feb. 28</option>'
					+ '</select>'
					+ '</div>';
	
	rpHeader		+='<div class="select-wr">'
					+ '<span> Choose Event: </span>' 
					+ '<select id="eve_event" onchange="control.getEvents();">'
					+ ' <option value="">All Sports</option>'
					+ '	<option value="Alpine Skiing">Alpine Skiing</option>'
					+ '	<option value="Biathlon">Biathlon</option>'
					+ '	<option value="Bobsleigh">Bobsled</option>'
					+ '	<option value="Cross-Country Skiing">Cross-Country</option>'
					+ '	<option value="Curling">Curling</option>'
					+ '	<option value="Figure Skating">Figure Skating</option>'
					+ '	<option value="Freestyle Skiing">Freestyle Skiing</option>'
					+ '	<option value="Ice Hockey">Hockey</option>'
					+ '	<option value="Luge">Luge</option>'
					+ '	<option value="Nordic Combined">Nordic Combined</option>'
					+ '	<option value="Short Track Speed Skating">Short Track</option>'
					+ '	<option value="Skeleton">Skeleton</option>'
					+ '	<option value="Ski Jumping">Ski Jumping</option>'
					+ '	<option value="Speed Skating">Speed Skating</option>'
					+ '	<option value="Snowboard">Snowboarding</option>'
					+ '</select>'
					+ '</div>';
	
	$(this.divContainerHeader).html(rpHeader);
};

ws_Athletes.prototype.displayOff = function(data) {
	switch(data) {
		case 'athletes':
			// this.remAthEvListener();
			this.clearPlacemarks('athletes');
			this.displayAthletes = null;
			break;
		case 'events':
			this.clearPlacemarks('event_centers');
			this.displayEvents = null;
			break;
		case 'medals':
			this.displayMedals = null;
			$(this.divContainerMedals).hide();
			$(this.divContainer).show();
			break;
		case 'weather':
			ge.getFeatures().removeChild(this.displayWeather);
			this.displayWeather = null;
			break;
		case 'news':
			this.displayNews = null;
			break;
	}	
	
	if(this.displayAthletes || this.displayEvents || this.displayMedals || this.displayWeather || this.displayNews) {
		if(this.displayAthletes) {
			this.sortAthletes();
			this.getAthletes();
		} else if(this.displayEvents) {
			moveCamera(49.6280, -123.0092, 110000, 30, 70);
			this.sortEvents();
			this.getEvents();
		} else if(this.displayMedals) {
			this.getMedals();
		} else if(this.displayWeather) {
			ge.getFeatures().removeChild(this.displayWeather);
			this.displayWeather = null;
			this.getWeather();
		} else if(this.displayNews) {
			this.getNews("http://sports.yahoo.com/oly/rss.xml");
		}
	}
	
	if(!this.displayAthletes && !this.displayEvents && !this.displayMedals && !this.displayWeather && !this.displayNews) {
		$(this.divContainerHeader).html('<div id="header-text">Olympic Real-time</div>');
		$(this.divContainer).html('');
		this.getOneRiot("2010 Olympics");
	}
};

/*
 * 
 * 
 * 
 */
$(document).ready(function() {
	$("#athletes").hover(
			function() {
				if(!control.displayAthletes)
					$(this).attr("src", "images/navigation/left-Athletes/athletes_hov.jpg");				
			},
			function() {
				if(!control.displayAthletes)
					$(this).attr("src", "images/navigation/left-Athletes/athletes.jpg");
			}
		).toggle(
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/athletes_ov.jpg");

			control.displayAthletes = 1;
			control.sortAthletes();
			control.getAthletes();
		},
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/athletes_hov.jpg");
			
			control.displayOff('athletes');
		}
	);
	$("#Events").hover(
			function() {
				if(!control.displayEvents)
					$(this).attr("src", "images/navigation/left-Athletes/events_hov.jpg");				
			},
			function() {
				if(!control.displayEvents)
					$(this).attr("src", "images/navigation/left-Athletes/events.jpg");
			}
		).toggle(
		function() {
			ge.setBalloon(null);
			$(this).attr("src", "images/navigation/left-Athletes/events_ov.jpg");
			
			control.displayEvents = 1;
			control.loadEventCenters();
			control.sortEvents();
			control.getEvents();
//			moveCamera(49.6280, -123.0092, 110000, 30, 70);
		},
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/events_hov.jpg");
			
			control.displayOff('events');
		}
	);
	$("#Medals").hover(
			function() {
				if(!control.displayMedals)
					$(this).attr("src", "images/navigation/left-Athletes/medals_hov.jpg");				
			},
			function() {
				if(!control.displayMedals)
					$(this).attr("src", "images/navigation/left-Athletes/medals.jpg");
			}
		).toggle(
		function() {
			ge.setBalloon(null);
			$(this).attr("src", "images/navigation/left-Athletes/medals_ov.jpg");
			
			control.displayMedals = 1;
			control.getMedals();
		},
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/medals_hov.jpg");

			control.displayOff('medals');
		}
	);
	$("#Weather").hover(
			function() {
				if(!control.displayWeather)
					$(this).attr("src", "images/navigation/left-Athletes/weather_hov.jpg");				
			},
			function() {
				if(!control.displayWeather)
					$(this).attr("src", "images/navigation/left-Athletes/weather.jpg");
			}
		).toggle(
		function() {
			ge.setBalloon(null);
			$(this).attr("src", "images/navigation/left-Athletes/weather_ov.jpg");
			
			control.getWeather();
			control.displayWeather = 1;
		},
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/weather_hov.jpg");
			
			control.displayOff('weather');
		}
	);
	$("#News").hover(
			function() {
				if(!control.displayNews)
					$(this).attr("src", "images/navigation/left-Athletes/news_hov.jpg");				
			},
			function() {
				if(!control.displayNews)
					$(this).attr("src", "images/navigation/left-Athletes/news.jpg");
			}
		).toggle(
		function() {
			ge.setBalloon(null);
			$(this).attr("src", "images/navigation/left-Athletes/news_ov.jpg");

			control.displayNews = 1;
			control.getNews("http://sports.yahoo.com/oly/rss.xml");
		},
		function() {
			$(this).attr("src", "images/navigation/left-Athletes/news_hov.jpg");
			
			control.displayOff('news');
		}
	);
});


