var BdnWeather = new function() {

	this.defaultZip = '04402';
	
	this.init = function() {
		this.zip = BdnWeather.getZip();
		if( this.zip ) {
			BdnWeather.publishLocation( this.zip );
		} else {
			this.zip = BdnWeather.defaultZip;
		}
		BdnWeather.changeWeather( this.zip );
	}
	
	this.publishLocation = function( zip ) {
		jQuery( '#weatherZip' ).val( zip );
	}
	
	this.appealForLocation = function() {
		//jQuery( '#your-location' ).append( '<div id="location-appeal" style="position: absolute; height: 1px; width: 1px;"><div style="position: relative; width: 150px; bottom: 17px; left: 100px;">&larr; We use your location to best provide you local news and weather.</div></div>' );
	}
	
	jQuery( '#weatherZip' ).keyup( function( event ) {
		if( event.keyCode == 13 ) {
			BdnWeather.newZip( jQuery( '#weatherZip' ).val() );
		}
	});
	
	this.changeWeather = function( zip ) {
			var url = 'http://bangordailynews.com/weather/?zip=' + zip;
			jQuery.getJSON( 'http://bangordailynews.com/?feed=weather&zip=' + zip,
				function( data ){
					if( data.todaytext ) {
						var content = '<div id="bdn-weather-widget">';
							content += '<div class="weather-left">';
								content += '<div class="weather-day">';
									content += '<div id="weather-today-link"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">TODAY</a></div>';
									content += '<div class="weather-icon rightBorder" id="weather_today_image"><a href="' + url + '" title="' + data.todaytext + '"><img src="http://static.bangordailynews.com/wp-content/themes/bdn/images/weather/' + data.todayicon + '" alt="' + data.todaytext + '" width="35" height="35" /></a></div>';
									content += '<div class="weather-day-temps">';
										content += '<div class="weather-temp weather-temp-top weather-red" id="weather_today_high">';
										
										if( data.todayhigh ) { 
											content += '<a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.todayhigh + '&deg;</a>';
										}
										content += '&nbsp;</div>';
										content += '<div class="clear-solid"></div>';
										content += '<div class="weather-temp weather-temp-bottom weather-blue" id="weather_today_low"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.todaylow + '&deg;</a></div>';
									content += '</div>';
								content += '</div><!-- .weather-day -->';
								content += '<div class="weather-day">';
									content += '<div id="weather_tomorrow_day"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.tomorrowday + '</a></div>';
									content += '<div class="weather-icon rightBorder" id="weather_tomorrow_image"><a href="' + url + '" title="' + data.tomorrowtext + '"><img src="http://static.bangordailynews.com/wp-content/themes/bdn/images/weather/' + data.tomorrowicon + '" alt="' + data.tomorrowtext + '" width="35" height="35" /></a></div>';
									content += '<div class="weather-day-temps">';
										content += '<div class="weather-temp weather-temp-top weather-red" id="weather_tomorrow_high"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.tomorrowhigh + '&deg;</a></div>';
										content += '<div class="clear-solid"></div>';
										content += '<div class="weather-temp weather-temp-bottom weather-blue" id="weather_tomorrow_low"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.tomorrowlow + '&deg;</a></div>';
									content += '</div>';
								content += '</div><!-- .weather-day -->';
							content += '</div><!-- .weather-left -->';
							
							content += '<div class="weather-right">';
								content += '<div id="aje_1153321"></div>';
							content += '</div><!-- .weather-right -->';
							
							content += '<div class="clear"></div>';
							
							content += '<div class="weather-left">';
								content += '<span id="weather-currently-link"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">CURRENTLY</a></span></span>: <span class="weather-temp weather-red"><span id="weather_current"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">' + data.current + '</a></span>&deg;</span>';
							content += '</div><!-- .weather-left -->';
							
							content += '<div class="weather-right" id="weather-forecast-link"><a href="' + url + '" title="View 7-day forecast for ' + zip + '">7-DAY FORECAST</a></span></div>';
							
							content += '<div class="clear"></div>';
						
						content += '</div><!-- #bdn-weather-widget -->';
						
						jQuery( '#bdn-weather-widget' ).html( content );
					}
				}
			);
		}
	
	this.newZip = function( zip ) {
		jQuery.getJSON( 'http://bangordailynews.com/apps/zip/?919&zip=' + zip,
			function( data ){
				if( data.Zip ) {
					jQuery.cookie( 'bdn_zip', data.Zip, { expires: 999, path: '/', domain: '.bangordailynews.com' });
					jQuery.cookie( 'bdn_city', data.City, { expires: 999, path: '/', domain: '.bangordailynews.com' });
					jQuery.cookie( 'bdn_state', data.State, { expires: 999, path: '/', domain: '.bangordailynews.com' });
					jQuery.cookie( 'bdn_region', data.Region, { expires: 999, path: '/', domain: '.bangordailynews.com' });
					jQuery.cookie( 'bdn_county', data.County, { expires: 999, path: '/', domain: '.bangordailynews.com' });
					jQuery.getJSON( 'http://wp-stats.com/weather.js.php?city=' + data.City, function( data ) { return false; } );
					BdnWeather.changeWeather( data.Zip );
					BdnWeather.publishLocation( data.Zip );
				} else {
					alert( "We're sorry, it doesn't look like we could find that zip code. Please try again" );
					return false;
				}
			});
	}
	
	this.killChromeBar = function() {
		jQuery( '#chrome-box' ).replaceWith( '<div></div>' );
		jQuery( 'html' ).css( 'margin-top', '28px' );
		jQuery( '#wpadminbar' ).css( 'top', '0' );
	}
	
	this.getZip = function() {
		this.zipCookie = jQuery.cookie( 'bdn_zip' );
		this.requestCookie = jQuery.cookie( 'bdn_geo_request' );
		if( !this.zipCookie && !this.requestCookie ) {
			if( navigator.geolocation ) {
				//geolocation supported
				var chromebar = '<div id="chrome-box" style="background-color: #EFEFEF; background-image: -webkit-gradient( linear, left top, left bottom, from( #EFEFEF ), to( #E1E1E1 ) ); background-image: -moz-linear-gradient(bottom,  #EFEFEF,  #E1E1E1); padding: 5px; height: 28px; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 3px; position: fixed; top: 0; left: 0; width: 100%;"><style type="text/css">#wpadminbar{ top: 38px!important; }</style><div class="floatleft"><img src="https://si0.twimg.com/profile_images/1513079818/BDN_512px_reasonably_small.png" height="28" width="28"></div><div class="floatleft" style="padding: 12px 8px;">The Bangor Daily News uses your location to provide local content and weather. <a href="http://bangordailynews.com/2011/09/19/business/bdn-announces-new-mobile-apps-improvements-to-website/">Learn more</a></div><div class="clear"></div>';
				jQuery( chromebar ).appendTo( 'body' );
				jQuery( 'html' ).css( 'margin-top', '66px' );
				navigator.geolocation.getCurrentPosition(function(a) {
					BdnWeather.killChromeBar();
					if (a.coords) {
						var lon = Math.round( a.coords.longitude * 100 ) / 100;
						var lat = Math.round( a.coords.latitude * 100 ) / 100;
						jQuery.getJSON( 'http://bangordailynews.com/apps/zip/?919&lat=' + lat + '&lon=' + lon,
							function( data ){
								if( data.Zip ) {
									jQuery.cookie( 'bdn_zip', data.Zip, { expires: 7, path: '/', domain: '.bangordailynews.com' });
									jQuery.cookie( 'bdn_city', data.City, { expires: 7, path: '/', domain: '.bangordailynews.com' });
									jQuery.cookie( 'bdn_state', data.State, { expires: 7, path: '/', domain: '.bangordailynews.com' });
									jQuery.cookie( 'bdn_region', data.Region, { expires: 7, path: '/', domain: '.bangordailynews.com' });
									jQuery.cookie( 'bdn_county', data.County, { expires: 7, path: '/', domain: '.bangordailynews.com' });
									jQuery.getJSON( 'http://wp-stats.com/weather.js.php?city=' + data.City, function( data ) { return false; } );
									BdnWeather.changeWeather( data.Zip );
									BdnWeather.publishLocation( data.Zip );
									return data.Zip;
								} else {
									BdnWeather.appealForLocation();
									return false;
								}
							});
					} else {
						BdnWeather.appealForLocation();
						return false;
					}
				},
				function(b) {
					if (b.code == 1) {
						jQuery.cookie( 'bdn_geo_request', 'denied', { expires: 30, path: '/', domain: '.bangordailynews.com' });
					}
					BdnWeather.killChromeBar();
					BdnWeather.appealForLocation();
					return false;
				},
				{enableHighAccuracy:true});
			} else {
				BdnWeather.killChromeBar();
				BdnWeather.appealForLocation();	
				return false;
			}
		} else {
			return this.zipCookie;
		}
	}

}
BdnWeather.init();
