UrbanSweep = function() {
	return {
	
		lastPosition        : null,    // Last scroll top position before expansion
		shimSize            : null,    // Holds the last known document width/height
		shimInterval        : null,    // Save the interval id	
				
		chooseSpot: function(e) {
	
			if (e.selectedIndex) {
				/* from dropdown */
				value = e[e.selectedIndex].value;
				$.scrollTo('#newAnchor', 333);
			} else {
				/* from left menu click */
				value = e.id;
				UrbanSweep.updateSelect(e.id);
			}
					
			if (value == 'NEW') {
				UrbanSweep.newSpot();
			} 
			
			if (value == '') {
				$('#spotdetails').addClass('hidden');
			}
			
			if (spots[value]) {
				UrbanSweep.resetClues();
				$('#name').attr('value', spots[value].name);
				$('#code').attr('value', spots[value].code);
				$('#origCode').attr('value', spots[value].code);
				$('#submit').attr('value', 'Save Changes');
				$('#spotdetails').removeClass('hidden');
				$('#delete').removeClass('hidden');
			}
			
			/* populate the clues */
			if (clues[value]) {
				for(var i=0; i<clues[value].length; i++) {
					
					if (!$('#clue' + i).length > 0) {
						$('#clues').append('<textarea name="clue'+i+'" id="clue'+i+'" onFocus="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120);"  onKeyUp="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120);"></textarea>');
					}
					$('#clue' + i).attr('value', clues[value][i].clue);
				}
			}

		},
		
		newSpot: function() {
			$('#spotdetails').removeClass('hidden');
			UrbanSweep.resetClues();
			UrbanSweep.updateSelect('NEW');
			$('#spotid').addClass('hidden');
			$('#name').attr('value', '');
			$('#code').attr('value', randomCode);
			$('#clue0').attr('value', '');
			$('#origCode').attr('value', '');
			$('#submit').attr('value', 'Add Spot');
			$('#delete').addClass('hidden');
		},
		
		updateSelect: function(value) {
			for(i=0; i < $('#spotid')[0].length; i++) {
				if ($('#spotid')[0][i].value == value) {
					$('#spotid')[0][i].selected = true;
				}
			}
		},
		
		resetClues: function() {
			$('#clues textarea').remove();	
			$('#clues').append('<textarea name="clue0" id="clue0" onFocus="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120);"  onKeyUp="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120);"></textarea>');		},
		
		
		addClue: function() {
			count = $('#clues textarea').length;
			$('#clues').append('<textarea name="clue'+count+'" id="clue'+count+'"  onFocus="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120);" onKeyUp="UrbanSweep.checkCount(this, $(\'#clueCount\') ,120)"></textarea>');		
		},
	
		
		checkCount: function(textarea, span, max) {
			span.html(' (' + textarea.value.length + ' characters used.)');
			if (textarea.value.length > max) {
				span.addClass('over');
			} else {
				span.removeClass('over');
			}
		},

		
		confirmDelete: function(msg) {
			var del = confirm(msg);
			
			if (del) {
				return true;
			}
			
			return false;
		},
		
		
		checkAll: function() {
			$('input.player').each( function f() {
				this.checked = true;
			});
			return false;
		},
		
		checkAllWinners: function() {
			UrbanSweep.unCheckAll();
			$('input.win').each( function f() {
				this.checked = true;
			});
			return false;
		},	
		
		checkAllNonWinners: function() {
			UrbanSweep.unCheckAll();
			$('input.nowin').each( function f() {
				this.checked = true;
			});
			return false;
		},			


		unCheckAll: function() {
			$('input.player').each( function f() {
				this.checked = false;
			});
			return false;
		},
		
		exportSelected: function() {
			var values = '';
			$('input.player').each( function f() {
				if(this.checked) {
					values = values + this.value + '|';
				}
			});

			$('#ids').attr('value', values);
			$('#csv').submit();

			return false;
		},
		
		sendMessage: function() {
			var values = '';
			$('input.player').each( function f() {
				if(this.checked) {
					values = values + this.value + '|';
				}
			});

			$('#sendids').attr('value', values);
			$('#send').submit();

			return false;
		},		
		
		renderChart: function(chart, data, unit) {
			
    		$.plot(chart, 
    			[{ data: data }], 
    			{ 
    				xaxis: 
    				{ 
    					mode: "time", 
    					tickFormatter: function(val, axis) {
							var d = new Date(val);
		
							if ( unit === 'hour' ) {
								var hours = d.getHours();
		
								if (hours === 0) {
									return '12am';
								} else if (hours === 12) {
									return '12pm';
								} else if (hours > 12) {
									return (hours - 12) + 'pm';
								} else {
									return hours + 'am';
								}
							} else {
								return (d.getUTCMonth() + 1) + "/" + d.getUTCDate();
							}
						}, 
    					minTickSize:["1", unit] 
    				}, 
    				yaxis:{
    					min: 0, 
    					minTickSize:1, 
    					tickDecimals:0, 
    					autoscaleSkipPointsOutside: true
    				}, 
    				lines: {show: true, fill:false, fillColor: '#333', shadowSize:0},
    				shadowSize: 0,
    				colors: ["#9f1f63"]
    			});		
		},

		
		initImageExpand : function() {
			
			// Find the images
			
			$('a.expand').click(function(e) {
				
				// Verify the element
				
				var img;
				if (e.target.tagName == 'IMG') {
					img = $(e.target);
				} else {
					img = $(e.target).find('img');
					if (img.size() === 0) {
						return;
					}
				}
				e.preventDefault();
				
				// Find the big version of it
				
				var src = img.attr('src').split('/');
				src[src.length - 1] = src[src.length - 1].replace('Small.', 'Big.');
				src = src.join('/');
				
				// Load the image
				
				UrbanSweep.lastPosition = $(window).scrollTop();
			
				UrbanSweep.shimShow();
					
				var newImg = $(new Image());
				newImg.load(function(e) {
					
					UrbanSweep.shimHideLoading();
					$('#content').css('display', 'none');
					$('#secondaryContent div.content').empty().append(this);
					
					$('#secondaryContent').css('display', 'block');
					setTimeout(function() {
						$.scrollTo(0, 333);
					}, 250);
				});
				newImg.attr('src', src);
			});
			
		},
		
		// Shim
		
		shimShow : function() {
			$('#shim p').css({
				'left' : (($(window).width() - 86) / 2) + 'px',
				'top' : (($(window).height() - 33) / 2) + $(window).scrollTop() + 'px',
				'display' : 'block'
			});

			UrbanSweep.shimSize = $(window).width() + ',' + $(document).height();
			$('#shim').css({
				'width' : $(window).width() + 'px',
				'height' : $(document).height() + 'px',
				'display' : 'block'
			}).animate({ 'opacity' : 1 }, 333, function(e) {
				$('#shim').css({
					'width' : $(window).width() + 'px'
				});
			});

			UrbanSweep.shimInterval = window.setInterval(UrbanSweep.shimResize, 500);
		},
		
		shimHide : function() {
			window.clearInterval(UrbanSweep.shimInterval);
			$('#shim').animate({ 'opacity' : 0 }, 333, function(e) {
				$('#shim').css('display', 'none');
			});
		},
		
		shimHideLoading : function() {
			$('#shim p').css('display', 'none');
		},
		
		shimResize : function() {
			if (UrbanSweep.shimSize != $(window).width() + ',' + $(document).height()) {
				$('#shim').css({
					'width' : $(window).width() + 'px',
					'height' : $(document).height() + 'px'
				});
				UrbanSweep.shimSize = $(window).width() + ',' + $(document).height();
			}
		},
		
		initLayout : function() {
			
			UrbanSweep.initImageExpand();
			
			// Init Secondary Content close
			
			$('#secondaryContent p a').click(function(e) {
				e.preventDefault();
				$('#secondaryContent').css('display', 'none');
				$('#content').css('display', 'block');
				UrbanSweep.shimHide();
				if (UrbanSweep.lastPosition !== null) {
					window.setTimeout(function() {
						$.scrollTo(UrbanSweep.lastPosition, 333);
						UrbanSweep.lastPosition = null;
					}, 350);
				}
			});
		}
		
		
				
		
		
	}

}();