main = function() {
	return {
		
		curSlide : 1,
		maxSlides : null,
		slides : [
			['Orange signs in Pittsburgh advertise RouteShout TXT codes to riders.', 0],
			['Ohio State Students use RouteShout to TXT for their upcoming bus.', -500],
			['The FREE RouteShout iPhone App supports more transit agencies than any other app!', -1000]
		],
		keyword : null,
		positioned : false,
		geo : null,

		// Methods

		init : function() {},
		
		initHomepage : function() {
			main.maxSlides = main.slides.length;
			window.setInterval(main.rotateSlide, 5000);
		},
		
		rotateSlide : function() {
			$('#photoSlide').animate({'opacity' : 0}, 333, function() {
				main.curSlide++;
				if (main.curSlide > main.maxSlides) {
					main.curSlide = 1;
				}
				var slide = main.slides[main.curSlide - 1];
				$('#photoSlide .photo').css('background-position', slide[1] + 'px 0');
				$('#photoSlide p.description').html(slide[0]);
				$('#photoSlide').animate({'opacity' : 1}, 333);
			});
		},
		
		initTimesCreate : function() {
			// Attempt to auto-locate

			if (navigator.geolocation) {
				$('#content p.location').html('Attempting to locate you...');
				navigator.geolocation.getCurrentPosition(main.gotLocation, function(msg) {
					$('#content p.location').html('Could not geolocate. Please enter your location below');
				}, {'timeout' : 8000});
			} else {
				$('#content p.location').html('Please enter your location below');
			}
			
			// Select button
			
			$('#content .grid-1 button.action-select').live('click', function(e) {
				e.preventDefault();
				$(this).html('Saving...').attr('disabled', true);
				var agencystop = $(this).attr('id').replace('stop_', '').split('_');
				var params = {
					'agency' : agencystop[0],
					'stop' : agencystop[1],
					'keyword' : main.keyword
				};
				
				$.getJSON(main.saveStickerKeyword, params, function(data, status) {
					if (status == 'success' && !data.error) {
						$(this).parents('p').html('<strong>Saved!</strong>');
						window.location.reload();
					} else {
						if (data.error) {
							alert(data.errorInfo);
						} else {
							alert('An unknown error occurred. Please try again later');
						}
					}
				});
			});
			
			// Address search
			
			$('#btnSearch').click(function(e) {
				e.preventDefault();
				$(this).blur().html('Searching...').attr('disabled', true);
				
				if ($('#address').val() === '') {
					$('#address').focus();
					return;
				}
				
				if (main.geo === null) {
					main.geo = new google.maps.Geocoder();
				}
				
				main.geo.geocode({'address':$('#address').val()}, function(data, status) {
					if (status == 'OK' && data.length > 0 && data[0].geometry && data[0].geometry.location) {
						main.positioned = true;
						main.stopLookup(data[0].geometry.location.lat(),data[0].geometry.location.lng());
					} else {
						alert("Couldn't find the location. Please try again");
						$('#btnSearch').html('Search address').attr('disabled', false);
						$('#address').focus();
					}
				});
			});
		},
		
		gotLocation : function(position) {
			var lat = position.coords.latitude;
			var lon = position.coords.longitude;
			if (main.positioned === false) {
				main.stopLookup(lat, lon);
			}
		},
		
		stopLookup : function (lat, lon) {
			var params = {
				'lat' : lat,
				'lon' : lon
			};

			$.getJSON(main.apiGetStops, params, function(data, status) {
				if (status == 'success' && !data.error && data.html) {
					$('#btnSearch').html('Search address').attr('disabled', false);
					$('#stops').html(data.html);
				}
			});
		},
		
		initMainApi : function() {
			$('div.example p.control button').click(function(e) {
				$(this).blur();
				var parentDiv = $(this).parents('div.example');
				if ($(parentDiv).hasClass('open')) {
					$(parentDiv).removeClass('open').addClass('close');
					$(parentDiv).find('p.control button').html('Show example');
				} else {
					$(parentDiv).removeClass('close').addClass('open');
					$(parentDiv).find('p.control button').html('Hide example');
				}
			});
		}

	};
}();
