var Local = {

    onCommentReply: function(id) {
		Comments.openReply(id);
	},

    onCancelReply: function(id) {
		Comments.closeReply();
	}

};


var wt = {}; // This is the setup for the webtools namespace

(function() { // This is where jQuery code can safely go without foobarring prototype
	var $ = jQuery
	
	$(document).ready(function() { // These functions get called on DOM ready
		$('a').click(function() { $(this).blur() }) // prevents outlines on links for IE
		wt.hero() // Crossfades hero shots if applicable
		wt.nav()
		if($('html#home').size() || $('div.program-overview').size()) {
			wt.maps.setup(document.getElementById('g_map'), wt.maps.custom)
		}
		
		wt.lightbox();
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		
	})
	
	wt.formDefaults = function() { // Swaps default text form input values to blank on focus and back to default text on blur
		$('input:text,textarea').each(function() {
			var elem = $(this)
			var defaultText = elem.val()
			elem.focus(function(){
				if(elem.val() == defaultText) {
					elem.val('')
				}
			})
			elem.blur(function(){
				if(elem.val() == '') {
					elem.val(defaultText)
				}
			})
		})
	}
    
    wt.nav = function() {
    	$('#hd ul.navigation li.subnav').each(
    		function() {
    			$(this).find('ul').addClass('subnav').appendTo($(this).prev('li'))
    			$(this).remove()
    		}
    	)
    	$('#hd ul li.nav-news ul').remove()
    	$('#hd ul.subnav li:first-child').addClass('first')
    	$('#hd ul.subnav li:last-child').addClass('last')
    	$('#hd ul.navigation > li').hover(
    		function() {
    			if(($(this).offset().left - $(this).parent().offset().left) + $(this).find('ul.subnav').width() > $(this).parent().width()) {
    				$(this).find('ul.subnav').css({
    					left: 'auto',
    					right: '0px'
    				})
    			}
    			$(this).addClass('hover').find('ul.subnav').show()
    		},
    		function() {
    			$(this).removeClass('hover').find('ul.subnav').hide()
    		}
    	)
    }
    

    wt.lightbox = function(content) {
    	if(typeof content == 'undefined') { var content = $('body') }
	
    	var links = content.find('a[rel=lightbox],a.thickbox')
	
    	if(!links.size()) { return; }

    	if(!$('#overlay').size()) { $('body').append($(jQuery('<div id="overlay" style="display: none"></div>'))) }
    	if(!$('#popup').size()) { $('body').append($(jQuery('<div id="popup" style="display: none"></div>'))) }
    	if(!$('#popup-target').size()) { $('#popup').append($(jQuery('<div id="popup-target"></div>'))); }
	
    	wt.lightbox.overlay = $('#overlay')
    	wt.lightbox.popup = $('#popup')
    	wt.lightbox.overlay.width($('body').width()).height($('body').height() > $('html').height() ? $('body').height() : $('html').height())
    	wt.lightbox.popup.target = $('#popup-target')
    	if(!wt.lightbox.popup.find('a[href=#close]').size()) {
    		wt.lightbox.popup.close = $(jQuery('<a href="#close" class="close">&times;</a>'));
    		wt.lightbox.popup.append(wt.lightbox.popup.close)
    		wt.lightbox.popup.close.click(function(c) {
    			c.preventDefault()
    			wt.lightbox.popup.fadeOut('fast',function() {
    				wt.lightbox.popup.target.html('')
    			})
    			wt.lightbox.overlay.hide()
    		});
    	}
	
    	wt.lightbox.overlay.click(function() {
    		wt.lightbox.overlay.hide()
    		wt.lightbox.popup.fadeOut('fast')
    	});
	
    	links.each(function() {
    		var link = $(this)
    		link.click(function(c) {
    			c.preventDefault()
    			var href = link.attr('href')
    			wt.lightbox.popup.removeClass('html').removeClass('image')
    			wt.lightbox.overlay.removeClass('html').removeClass('image')
    			var isImage = href.match(/(\.png|\.jpg|\.gif|\.bmp|\.jpeg)/) != null;
    			if(isImage) {
    				wt.lightbox.popup.addClass('image')
    				wt.lightbox.overlay.addClass('image')
    				var img = new Image()
    				wt.lightbox.overlay.show()
    				wt.lightbox.popup.target.empty()
    				img.onload = function() {
    					$(img).appendTo(wt.lightbox.popup.target)
    					var padding = {'x': parseInt(wt.lightbox.popup.css('padding-left'),10) + parseInt(wt.lightbox.popup.css('padding-right'),10), 'y': parseInt(wt.lightbox.popup.css('padding-top'),10) + parseInt(wt.lightbox.popup.css('padding-bottom'),10)}
    					wt.lightbox.popup.width(img.width + padding.x).height(img.height + padding.y).css({'margin-left': (0 - Math.round((img.width + padding.x) / 2)) + 'px', 'margin-top': (0 - parseInt((img.height + padding.y) / 2)) + 'px'})
    					wt.lightbox.popup.fadeIn()
    				}
    				img.src = href
    			} else {
    				wt.lightbox.popup.addClass('html')
    				wt.lightbox.overlay.addClass('html')
    				$.ajax({
    					'async': false,
    					'beforeSend': function() {
    						wt.lightbox.overlay.show()
    						return true;
    					},
    					'dataType': 'html',
    					'success': function(html) {
    						html = html.replace(/<(script|link|meta)[^>]+>(.+?<\/(script)>)?/gi,'')
    						wt.lightbox.popup.target.html(html)
    						wt.lightbox.popup.fadeIn('normal', function() {
	    						wt.maps.setupPop(wt.lightbox.popup.find('#pop_g_map'), function() { wt.maps.custom(true); })
							})
    					},
    					'url': href
    				})
    			}
    		});
    	});
    }


	wt.hero = function() { // Heroshot crossfader; set options below this function

		if(!$('#imageFadeContainer').size()) {
			return
		}
		
		wt.hero.interval = null
		
		wt.hero.container = $('#imageFadeContainer')

		var fade = wt.hero.container.find('input[name=fadevalue]')
		wt.hero.fade = fade.val() * 1000
		fade.remove()
		
		var dur = wt.hero.container.find('input[name=showvalue]')
		wt.hero.dur = dur.val() * 1000
		dur.remove()
		
		if(wt.hero.useForeground) {
			wt.hero.container.append(
				$(jQuery('<div />')).attr({id: 'hero-foreground'})
			)
		}
			
		if(wt.hero.useCaptions) {
			if(wt.hero.container.children('a:first-child').size()) {
				var firstCaption = wt.hero.container.children('a:first-child').children('img').attr('alt')
			} else if(wt.hero.container.children('img:first-child').size()) {
				var firstCaption = wt.hero.container.children('img:first-child').attr('alt')
			} else {
				var firstCaption = ''
			}

			wt.hero.container.append(
				$(jQuery('<div />'))
					.attr({id: 'hero-caption'})
					.append(
						$(jQuery('<span />'))
							.attr({id: 'caption-holder'})
							.show()
							.text(firstCaption)
					)
			)
		}

		if(wt.hero.container.children('a,img').size() < 2) { // Less than two images, we don't need to xfade or add controls
			return
		}
		
		if(wt.hero.useControls) {
			wt.hero.container.append(
					$(jQuery('<div />'))
						.attr({id: 'hero-controls'})
						.append(
							$(jQuery('<ul />'))
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a />'))
												.attr({
													title: 'Previous Photo',
													id: 'hero-previous',
													href: '#previous-photo'
												})
												.click(function(c){
													c.preventDefault()
													clearInterval(wt.hero.interval)
													wt.hero.rotate('prev')
													$('#hero-pause').hide()
													$('#hero-play').show()
												})
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a />'))
												.attr({
													title: 'Pause Photos',
													id: 'hero-pause',
													href: '#pause-photos'
												})
												.click(function(c){
													c.preventDefault()
													clearInterval(wt.hero.interval)
													$('#hero-pause').hide()
													$('#hero-play').show()
												})
												.css({display: 'block'})
												.show()
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a />'))
												.attr({
													title: 'Play Photos',
													id: 'hero-play',
													href: '#play-photos'
												})
												.click(function(c){
													c.preventDefault()
													wt.hero.interval = setInterval(function(){
														wt.hero.rotate('next')
													}, wt.hero.dur + wt.hero.fade)
													$('#hero-pause').show()
													$('#hero-play').hide()
												})
												.css({display: 'block'})
												.hide()
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a />'))
												.attr({
													title: 'Next Photo',
													id: 'hero-next',
													href: '#next-photo'
												})
												.click(function(c){
													c.preventDefault()
													clearInterval(wt.hero.interval)
													wt.hero.rotate('next')
													$('#hero-pause').hide()
													$('#hero-play').show()
												})
										)
								)
						)
				)
		}
			
			
		wt.hero.container.children('a:first-child,img:first-child').attr({current: 'current'})
		wt.hero.container.children('a:not(:first-child),img:not(:first-child)').hide()
		if(jQuery.browser.safari) {
			wt.hero.container.children('a:not(:first-child),img:not(:first-child)').css({display: 'none'})
		}

		wt.hero.interval = setInterval(function() {
			wt.hero.rotate('next')
		}, wt.hero.dur + wt.hero.fade)

		wt.hero.rotate = function(dir) {
			if(typeof dir == 'undefined') {
				var dir = 'next'
			}
			var images = wt.hero.container.children('a,img')
			var current = wt.hero.container.children('a[current],img[current]')
			if(dir == 'next') {
				if(current.next('a,img').size()) {
					var to = current.next('a,img')
				} else {
					var to = $(images[0])
				}
			} else {
				if(current.prev('a,img').size()) {
					var to = current.prev('a,img')
				} else {
					var to = $(images[images.size() - 1])
				}
			}

			current.removeAttr('current').fadeOut(wt.hero.fade)
			
			if(wt.hero.useCaptions) {
				wt.hero.container.find('#caption-holder').fadeOut(wt.hero.fade / 2, function(){
					wt.hero.container.find('#caption-holder').text(to.find('img').size() ? to.find('img').attr('alt') : to.attr('alt'))
					wt.hero.container.find('#caption-holder').fadeIn(wt.hero.fade / 2)
				})
			}

			if(wt.hero.useForeground) {
				if(to.href != 'undefined') {
					wt.hero.container.find('#hero-foreground').bind('click',function() {
						window.location = to.href
					})
				} else {
					wt.hero.container.find('#hero-foreground').unbind('click',function() {
						window.location = to.href
					})
				}
			}
			to.attr({current: 'current'}).fadeIn(wt.hero.fade)
		}
	}

	// Heroshot options
	wt.hero.useForeground = true
	wt.hero.useControls = true
	wt.hero.useCaptions = true


	wt.maps = {}

	wt.maps.setup = function(container, callback) {
		if(typeof container != 'object' || typeof document.getElementById('g_map') == 'undefined') { return }
		document.onunload = "GUnload()";
		wt.maps.map = new google.maps.Map2(document.getElementById('g_map')); // Set up the map
		wt.maps.map.addMapType(G_PHYSICAL_MAP) // Allow the map to be the 'terrain' view
		!$('html#home').size() ? wt.maps.map.addControl(new google.maps.SmallMapControl()) : null // Use the simple pan/zoom controls
		!$('html#home').size() ? wt.maps.map.enableScrollWheelZoom() : null // Allow zooming via mouse scrollwheel
		wt.maps.map.setCenter(new google.maps.LatLng(1,1),1) // Init the map and set it to show the whole world
		wt.maps.map.bounds = new google.maps.LatLngBounds() // Set the initial map boundaries
		wt.maps.geocoder = new google.maps.ClientGeocoder() // Init the geocoder
		wt.maps.manager = new google.maps.MarkerManager(wt.maps.map) // Start the marker manager
		
		if(typeof callback == 'function') { callback(); }
	}

	wt.maps.setupPop = function(container, callback) {
		if(typeof container != 'object' || typeof document.getElementById('pop_g_map') == 'undefined') { return }
		document.onunload = "GUnload()";
		wt.maps.map = new google.maps.Map2(document.getElementById('pop_g_map')); // Set up the map
		wt.maps.map.addMapType(G_PHYSICAL_MAP) // Allow the map to be the 'terrain' view
		!$('html#home').size() ? wt.maps.map.addControl(new google.maps.SmallMapControl()) : null // Use the simple pan/zoom controls
		!$('html#home').size() ? wt.maps.map.enableScrollWheelZoom() : null // Allow zooming via mouse scrollwheel
		wt.maps.map.setCenter(new google.maps.LatLng(1,1),1) // Init the map and set it to show the whole world
		wt.maps.map.bounds = new google.maps.LatLngBounds() // Set the initial map boundaries
		wt.maps.geocoder = new google.maps.ClientGeocoder() // Init the geocoder
		wt.maps.manager = new google.maps.MarkerManager(wt.maps.map) // Start the marker manager
		
		if(typeof callback == 'function') { callback(); }
	}
	
	wt.maps.custom = function(ajax) {
		var zoom = $('html#home').size() ? 12 : 13;
		var lat = $('html#home').size() ? 35.037267 : 35.037267
		var lon = $('html#home').size() ? -85.28 : -85.2835
		wt.maps.map.setCenter(new google.maps.LatLng(lat,lon),zoom) // Show me downtown Chattanooga!
		wt.maps.cohort1 = {'border': '#a733ff', 'fill': '#a733ff'}
		wt.maps.cohort2 = {'border': '#e150cb', 'fill': '#e150cb'}
		wt.maps.borderSize = 1

		var legend = $(jQuery('<ul class="map-legend">')).append($(jQuery('<li class="cohort1" style="background: ' + wt.maps.cohort1.fill +';color: ' + wt.maps.cohort1.border + ';border: ' + wt.maps.borderSize + 'px solid ' + wt.maps.cohort1.border + ';">Cohort 1</li>'))).append($(jQuery('<li class="cohort2" style="background: ' + wt.maps.cohort2.fill +';color: ' + wt.maps.cohort2.border + ';border: ' + wt.maps.borderSize + 'px solid ' + wt.maps.cohort2.border + ';">Cohort 2</li>'))).append($(jQuery('<li id="legend" style="border: ' + wt.maps.borderSize + 'px solid white;"></li>')))
		
		if(!$('html#home').size() && !$('ul.map-legend').size()) {
			$('#g_map').after(legend);
		}

		wt.maps.mlking = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.046880019512436, -85.3041923046112), // top left
				new google.maps.LatLng(35.04127594795207, -85.28921484947205), // top right
				new google.maps.LatLng(35.03920287110193, -85.29030919075012), // bottom right
				new google.maps.LatLng(35.03986169359938, -85.29231548309326),
				new google.maps.LatLng(35.042514498426584, -85.29939651489258),
				new google.maps.LatLng(35.042681393863504, -85.30009388923645),
				new google.maps.LatLng(35.04475438245104, -85.30555486679077), // bottom left
				new google.maps.LatLng(35.046326656511965, -85.30467510223389),
				new google.maps.LatLng(35.04663408086393, -85.30433177947998),
				new google.maps.LatLng(35.046880019512436, -85.3041923046112) // close the box
			],
			wt.maps.cohort1.border, wt.maps.borderSize, 1, wt.maps.cohort1.fill, 0.7, {'geodesic': true}
		)
		wt.maps.mlking['name'] = 'M. L. King'
		wt.maps.map.addOverlay(wt.maps.mlking)

		wt.maps.bushtown = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.05125409034132, -85.27669429779053), // top left
				new google.maps.LatLng(35.04847860267857, -85.2680253982544), // top right
				new google.maps.LatLng(35.03589989424211, -85.27467727661133), // bottom right
				new google.maps.LatLng(35.04064348940409, -85.28750896453857), // bottom left
				new google.maps.LatLng(35.05125409034132, -85.27669429779053) // close the box
			],
			wt.maps.cohort1.border, wt.maps.borderSize, 1, wt.maps.cohort1.fill, 0.7, {'geodesic': true}
		)
		wt.maps.bushtown['name'] = 'Bushtown'
		wt.maps.map.addOverlay(wt.maps.bushtown)

		wt.maps.highlandPark = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.03893934061551, -85.28287410736084), // top left
				new google.maps.LatLng(35.03486329419248, -85.2718448638916), // top right
				new google.maps.LatLng(35.02511158234408, -85.27716636657715), // bottom right
				new google.maps.LatLng(35.02897726496665, -85.28815269470215), // bottom left
				new google.maps.LatLng(35.03893934061551, -85.28287410736084) // close the box
			],
			wt.maps.cohort1.border, wt.maps.borderSize, 1, wt.maps.cohort1.fill, 0.7, {'geodesic': true}
		)
		wt.maps.highlandPark['name'] = 'Highland Park'
		wt.maps.map.addOverlay(wt.maps.highlandPark)

		wt.maps.southside = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.03751626130178, -85.31132698059082), // top left
				new google.maps.LatLng(35.03259678387291, -85.29793739318848), // top right
				new google.maps.LatLng(35.02823928512995, -85.30051231384277), // bottom right
				new google.maps.LatLng(35.03336986422377, -85.31364440917969), // bottom left
				new google.maps.LatLng(35.03751626130178, -85.31132698059082) // close the box
			],
			wt.maps.cohort1.border, wt.maps.borderSize, 1, wt.maps.cohort1.fill, 0.7, {'geodesic': true}
		)
		wt.maps.southside['name'] = 'Southside'
		wt.maps.map.addOverlay(wt.maps.southside)

		wt.maps.orchardKnob = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.04085430946408, -85.27188777923584), // top left
				new google.maps.LatLng(35.038992046780784, -85.26703834533691), // top right
				new google.maps.LatLng(35.037200018088505, -85.26733875274658),
				new google.maps.LatLng(35.03632155829659, -85.26798248291016),
				new google.maps.LatLng(35.033826680993776, -85.26918411254883), // bottom right
				new google.maps.LatLng(35.035829616688154, -85.27463436126709), // bottom left
				new google.maps.LatLng(35.04085430946408, -85.27188777923584) // close the box
			],
			wt.maps.cohort2.border, wt.maps.borderSize, 1, wt.maps.cohort2.fill, 0.7, {'geodesic': true}
		)
		wt.maps.orchardKnob['name'] = 'Orchard Knob'
		wt.maps.map.addOverlay(wt.maps.orchardKnob)

		wt.maps.churchville = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.04586112600508, -85.2693772315979), // top left
				new google.maps.LatLng(35.044016545084844, -85.26442050933838), // top right
				new google.maps.LatLng(35.04359492074073, -85.26452779769897),
				new google.maps.LatLng(35.038992046780784, -85.26703834533691), // bottom right
				new google.maps.LatLng(35.04085430946408, -85.27188777923584), // bottom left
				new google.maps.LatLng(35.04586112600508, -85.2693772315979) // close the box
			],
			wt.maps.cohort2.border, wt.maps.borderSize, 1, wt.maps.cohort2.fill, 0.7, {'geodesic': true}
		)
		wt.maps.churchville['name'] = 'Churchville'
		wt.maps.map.addOverlay(wt.maps.churchville)

		wt.maps.glenwood = new google.maps.Polygon(
			[
				new google.maps.LatLng(35.044016545084844, -85.26442050933838),
				new google.maps.LatLng(35.04285707290244, -85.26137351989746),
				new google.maps.LatLng(35.050551444153164, -85.25712490081787),
				new google.maps.LatLng(35.04967312791716, -85.25450706481934),
				new google.maps.LatLng(35.04661651378926, -85.25394916534424),
				new google.maps.LatLng(35.04285707290244, -85.25347709655762),
				new google.maps.LatLng(35.03892177188621, -85.25369167327881),
				new google.maps.LatLng(35.03533767211819, -85.25519371032715),
				new google.maps.LatLng(35.030523486829, -85.25991439819336),
				new google.maps.LatLng(35.033826680993776, -85.26918411254883),
				new google.maps.LatLng(35.03632155829659, -85.26798248291016),
				new google.maps.LatLng(35.037200018088505, -85.26733875274658),
				new google.maps.LatLng(35.038992046780784, -85.26703834533691),
				new google.maps.LatLng(35.044016545084844, -85.26442050933838) // close the box
			],
			wt.maps.cohort2.border, wt.maps.borderSize, 1, wt.maps.cohort2.fill, 0.7, {'geodesic': true}
		)
		wt.maps.glenwood['name'] = 'Glenwood'
		wt.maps.map.addOverlay(wt.maps.glenwood)

		google.maps.Event.addListener(wt.maps.map, 'click', function(overlay,latLng) {
			(overlay != null && typeof overlay.name != 'undefined') ? $('#legend').html(overlay.name) : $('#legend').html('')
	//		typeof latLng != 'undefined' ? alert(latLng) : null
		})

		if(!$('html#home').size()) {
			$('div.page-content a[href*=#]').each(function() {
				var hash = this.href.substr(this.href.indexOf('#') + 1)
				if(typeof wt.maps[hash] != 'undefined') {
					var mapObject = wt.maps[hash]
					var currentFill = {'color': mapObject.color, 'fill': mapObject.fill, 'opactiy': mapObject.opacity}
					$(this).click(function(c) {
						c.preventDefault()
						var newFill = {'color': '#70ace0', 'fill': true, 'opacity': 0.7}
						mapObject.setFillStyle(newFill)
						setTimeout(function() {
							mapObject.setFillStyle(currentFill)
						}, 1500)
					});
				}
			});
		}
	}





})();