
// Make sure webmd object already exists
if (!window.webmd) { webmd = {}; }
if (!webmd.m) { webmd.m = {}; }


// Namespace for module
webmd.m.zipNCCN = {
	
	programURL: "find-a-cancer-center",
	
	// Cancer Centers
	centers: [
		{title: "City of Hope Cancer Center", city: "Duarte, CA", zipCode: "91010", lat: 34.1322536, long: -117.9716433, furl: "city-hope"	},
		{title: "H. Lee Moffitt Cancer Center &amp; Research Institute", city: "Tampa, FL", zipCode: "33612", lat: 28.063417, long: -82.4200549, furl: "h-lee-moffitt" },
		{title: "Dana-Farber/Brigham and Women's Cancer Center", city: "Boston, MA", zipCode: "02114", lat: 42.362429, long: -71.0697789,	furl: "dana-farber-brigham" },
		{title: "Memorial Sloan-Kettering Cancer Center", city: "New York, NY", zipCode: "10065", lat: 40.7646335, long: -73.9553616, furl: "memorial-sloan-kettering" },
		{title: "Robert H. Lurie Comprehensive Cancer Center of Northwestern University", city: "Chicago, IL", zipCode: "60611", lat: 41.8941828, long: -87.622728, furl: "lurie-cancer-center" },
		{title: "Roswell Park Cancer Institute", city: "Buffalo, NY", zipCode: "14263", lat: 42.898527, long: -78.865011, furl: "roswell-park" },
		{title: "UNMC Eppley Cancer Center at The Nebraska Medical Center", city: "Omaha, NE", zipCode: "68198", lat: 41.254244, long: -95.97578, furl: "eppley-cancer-center" },
		{title: "Vanderbilt-Ingram Cancer Center", city: "Nashville, TN", zipCode: "37232", lat: 36.0351198, long: -86.6580622, furl: "vanderbilt-ingram"}
	],
	
	nextClosest: [
		{title: "", city: "", zipCode: ""},
		{title: "", city: "", zipCode: ""},
	],

/*	
	userCoord: ['30030','GA','Dekalb','-84.295044','33.769883','Decatur'],
*/		
	// convert degrees to radians, used in findDistBtwn()
	toRad: function(deci) {
	  return deci * Math.PI / 180;
	},

	// return distance between two points
	findDistBtwn: function(lat1,long1,lat2,long2) {
		var R = 6371; // km
		var dLat = this.toRad(lat2-lat1);
		var dLong = this.toRad(long2-long1); 
		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
				Math.cos(this.toRad(lat1)) * Math.cos(this.toRad(lat2)) * 
				Math.sin(dLong/2) * Math.sin(dLong/2); 
		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		var d = R * c;
		return(d);
	},
	
	// find the closest center
	findClosestCenter: function(zip) {
		$('#nccn_zip_spbox_rdr p').prepend('<img src="http://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/modules/loading_small.gif" />');
/*		// Simple zip code validation
		var valid = "0123456789";
		if (zip.length==5) {
			for (var i=0; i < zip.length; i++) {
				temp = "" + zip.substring(i, i+1);
				if (valid.indexOf(temp) == "-1") {
					alert("Please enter a valid ZIP Code");
					return false;
				}
			}
		} else {
			alert("Please enter a valid ZIP Code");
			return false;
		}
*/

		// record page view
		ctrs('sb-rr-0269-tacv-01_submit');

		// get coordinates of zip code
		$.ajax({
			type: "GET",
			url: "/scapp/zipcode.aspx?code="+zip,
			dataType : "text",
			success: function(data){
				// process response
				if ((data!="") && (data!="Error: invalid zip code")){
					webmd.m.zipNCCN.userCoord=data.split("|");
					$.each(webmd.m.zipNCCN.centers,function(i) {
						webmd.m.zipNCCN.centers[i].distAway = webmd.m.zipNCCN.findDistBtwn(webmd.m.zipNCCN.userCoord[4], webmd.m.zipNCCN.userCoord[3], webmd.m.zipNCCN.centers[i].lat, webmd.m.zipNCCN.centers[i].long);
					});
					
					// Sort
					webmd.m.zipNCCN.centers = webmd.m.zipNCCN.centers.sort(function(a,b){
						return a.distAway - b.distAway;
					});
					
					// go to closest center's url
					document.location= '/' + webmd.m.zipNCCN.programURL + '/' + webmd.m.zipNCCN.centers[0].furl + '?cntr1=' + webmd.m.zipNCCN.centers[1].zipCode + '&cntr2=' + webmd.m.zipNCCN.centers[2].zipCode;
				} else {
					document.location= '/' + webmd.m.zipNCCN.programURL + '/default.htm?error';
				}
			},
			error: function(){
				document.location= '/' + webmd.m.zipNCCN.programURL + '/default.htm?error';
			}
		});
		
		return false;
	},
	
	// display closest cities or error
	showDynamicCopy: function() {
		if (webmd.url.getParam('cntr1')) {
			this.nextClosest[0].zipCode = webmd.url.getParam('cntr1');
			this.nextClosest[1].zipCode = webmd.url.getParam('cntr2');
			$.each(webmd.m.zipNCCN.centers,function(i) {
				if (webmd.m.zipNCCN.centers[i].zipCode == webmd.m.zipNCCN.nextClosest[0].zipCode){
					webmd.m.zipNCCN.nextClosest[0].city = webmd.m.zipNCCN.centers[i].city;
					webmd.m.zipNCCN.nextClosest[0].furl = webmd.m.zipNCCN.centers[i].furl;
					webmd.m.zipNCCN.nextClosest[0].title = webmd.m.zipNCCN.centers[i].title;
				} else if (webmd.m.zipNCCN.centers[i].zipCode == webmd.m.zipNCCN.nextClosest[1].zipCode){
					webmd.m.zipNCCN.nextClosest[1].city = webmd.m.zipNCCN.centers[i].city;
					webmd.m.zipNCCN.nextClosest[1].furl = webmd.m.zipNCCN.centers[i].furl;
					webmd.m.zipNCCN.nextClosest[1].title = webmd.m.zipNCCN.centers[i].title;
				}
			});
			$('.nccn_article_rdr').append(
				"<h5>Other featured NCCN Member Institutions near your ZIP code:</h5><ul class='arrow'><li><a href='" + webmd.m.zipNCCN.nextClosest[0].furl +"'>" + webmd.m.zipNCCN.nextClosest[0].title + "</a><br />" + webmd.m.zipNCCN.nextClosest[0].city + "</li><li><a href='" + webmd.m.zipNCCN.nextClosest[1].furl +"'>" + webmd.m.zipNCCN.nextClosest[1].title + "</a><br />" + webmd.m.zipNCCN.nextClosest[1].city + "</li></ul>");
		} else if (webmd.url.getParam('error')) {
			$("#nccn_error").append("<p>Sorry, your location was not found. Please enter a valid ZIP code in the module to the right, or choose from one of the Featured NCCN Member Institutions listed below.</p>");
		}
	}
};


$(document).ready(function(){
	webmd.m.zipNCCN.showDynamicCopy();
});
