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
		if(jQuery.browser.msie){
			$('ul.navigation li').each(function(){
				$(this).mouseover(function(){
					$(this).addClass('hover')
				})
				$(this).mouseout(function(){
					$(this).removeClass('hover')
				})
			})
		}
		wt.navAjax();
		wt.hero() // Crossfades hero shots if applicable
		function disclaim(){
			$("#main h1").after("<div id=\"help-disclaimer\">The helping professionals listed here have indicated to MCR Foundation that it is assumed, ascribe to our mission and philosophy. They have indicated experience and expertise in eating disorders. MCR does not determine or warrant the competence of any therapist listed here. Use of this website to locate a helping professional is voluntary and will not result in any liability against MCR Foundation.   MCR Foundation is not liable for damages to any user of this service for the voluntary selection of a helping professional, for the services provided by anyone listed, or for any other damages which may occur. MCR provides this list for information purposes only.<br /><br />Please indicate your understanding and acceptance of the terms and conditions of this MCR Foundation service by clicking the \"I Accept\" button.<br /><input type=\"button\" value=\"I Accept\" /></div>")
		}
		if(window.location.toString().indexOf('/get-help-for')>-1){
			if(document.cookie.indexOf('help_disclaimer=')<0){
				disclaim();
				$("#help-disclaimer input").click(function(){
					accept_terms()
					$("#help-disclaimer").css('display','none')
					$("div.help-blurb").css('display','block')
				})
			}else{
				$("div.help-blurb").css('display','block')
			}
		}
		function accept_terms(){
			var days=12000;
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie='help_disclaimer=agree; expires='+expires+'; path=/';
		}
	})
	
	$(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.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
	j=$;
	wt.lightbox = function() {
		if(!j('#overlay').size()) {
			wt.lightbox.overlay = j(jQuery('<div id="overlay" style="display: none"></div>'))
			wt.lightbox.overlay.width(j(window).width()).height(j(window).height())
			if(jQuery.browser.msie) {
				j(document).scroll(function() {
					wt.lightbox.overlay.css({
						'top': document.scrollTop
					})
				})
			}
			j(window).resize(function() {
				wt.lightbox.overlay.width(j(window).width()).height(j(window).height())
			})
			j('body').append(wt.lightbox.overlay)
			wt.lightbox.overlay.open = function(callback) {
				if(jQuery.browser.msie) {
					wt.lightbox.overlay.width(document.documentElement.clientWidth)
					wt.lightbox.overlay.height(document.documentElement.clientHeight)
					wt.lightbox.overlay.css({'display': 'block'})
				} else {
					wt.lightbox.overlay.fadeIn('normal')
				}
				if(typeof callback == 'function') {
					callback()
				}
			}
			wt.lightbox.overlay.close = function(callback) {
				if(jQuery.browser.msie) {
					wt.lightbox.overlay.css({'display': 'none'})
				} else {
					wt.lightbox.overlay.fadeOut('fast')
				}
				if(typeof callback == 'function') {
					callback()
				}
			}
			wt.lightbox.overlay.click(function() {
				wt.lightbox.overlay.close()
				wt.lightbox.popup.close()
			})
		}
		if(!j('#popup').size()) {
			wt.lightbox.popup = j(jQuery('<div id="popup" style="display: none"></div>'))
			if(jQuery.browser.msie) {
				j(document).scroll(function() {
					wt.lightbox.popup.css({
						'top': document.scrollTop + parseInt(wt.lightbox.popup.css('top'),10)
					})
				})
			}

			j('body').append(wt.lightbox.popup)
			wt.lightbox.popup.open = function(callback) {
				wt.lightbox.popup.find('div.page-content > :last-child').css({ 'margin-bottom': 0 })
				var height = wt.lightbox.popup.height()
				wt.lightbox.popup.slideDown(
					'normal',
					function() {
						if(typeof callback == 'function') {
							callback()
						}
					}
				)
				if(height + parseInt(wt.lightbox.popup.css('top'), 10) > j('body').height()) {
					wt.lightbox.popup.css({'position': 'absolute'})
					if(jQuery.browser.msie) {
						wt.lightbox.overlay.height(height + parseInt(wt.lightbox.popup.css('top'), 10))
					}
					j('body').css({
						'height': height + parseInt(wt.lightbox.popup.css('top'), 10)
					})
				}
			}
			wt.lightbox.popup.close = function(callback) {
				if(jQuery.browser.msie && jQuery.browser.version < 7) {
					wt.lightbox.popup.hide()
				} else {
					wt.lightbox.popup.fadeOut('normal')
				}
				j('body').height('')
				wt.lightbox.popup.css({ 'position': '' })
				if(typeof callback == 'function') {
					callback()
				}
			}
			wt.lightbox.popup.closeButton = j(jQuery('<div class="close"></div>'))
			wt.lightbox.popup.closeButton.click(function() {
				wt.lightbox.overlay.close()
				wt.lightbox.popup.close()
			})
			wt.lightbox.popup.ajaxComplete(function() {
				wt.lightbox.popup.append(wt.lightbox.popup.closeButton.clone(true))
			})
		}
		
	}
	
	wt.navAjax = function() {
		j('ul#directors li a, ul#trustees li a').each(function() {
			var item = j(this)
			var wrapper = 'div#content'
			item.click(function(c) {
				c.preventDefault()
				if(typeof wt.lightbox.popup == 'undefined') {
					wt.lightbox()
				}
				wt.lightbox.popup.load(
					item.attr('href') + ' ' + wrapper,
					{},
					function() {
						wt.lightbox.overlay.open()
						wt.lightbox.popup.open()
					}
				)
			})
		})
	}
})();