var Local = {

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

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

};

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

wt.browserSucks = (jQuery.browser.msie && jQuery.browser.version < 7);

(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.intro();
		wt.backgrounds();
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		$(".close_button").each(function() {
			var button = $(this);
			var originalWidth = button.parents('.info').width();
			button.click(function(){ 
				if($(".close_button").hasClass("closed")){
					$(".info").animate({width:originalWidth},500);
					$(".info").children(':not(p.slide)').animate({width:originalWidth,'display':'block'},500);
				} else {
					$(".info-wrapper").height($(".info-wrapper").height())
					$(".info").animate({width:0},500);
					$(".info").children(':not(p.slide)').animate({width:0,'display':'none'},500);
				}
				$(".close_button").toggleClass("closed");
			});
	
		});

	})
	
	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 = false
	wt.hero.useControls = false
	wt.hero.useCaptions = false
	
	wt.intro = function() {
		if(!$('html#home').size()) { return; }
		if($.cookie('intro') == null) {
			$.cookie('intro','true');
			$('body').height($('body').height());
			var logoPosition = $('#hd img.logo').offset();
			$('#wrapper').css({'display':'none'});
			$(window).load(function() {
				setTimeout(function(){
					var logoWrap = $(jQuery('<div id="intro-logo"></div>'));
					logoWrap.css({
						'width': '421px',
						'height': '63px',
						'position': 'absolute',
						'top': Math.round(($('body').height() / 2) - 31.5) + 'px',
						'left': Math.round(($('body').width() / 2) - 210.5) + 'px'
					}).appendTo($('body'));
					var step1 = $(jQuery('<img src="/images/logo-step-1.png" style="display: none; position: absolute; top: 0; left: 0; z-index: 1" />'));
					var step2 = $(jQuery('<img src="/images/logo-step-2.png" style="display: none; position: absolute; top: 0; left: 0; z-index: 2" />'));
					var step3 = $(jQuery('<img src="/images/logo-step-3.png" style="display: none; position: absolute; top: 0; left: 0; z-index: 3" />'));
					step1.appendTo(logoWrap);
					step2.appendTo(logoWrap);
					step3.appendTo(logoWrap);
					step1.fadeIn('normal',function() {
						step2.fadeIn('normal',function() {
							step3.fadeIn('normal',function() {
								logoWrap.animate({
									'left': logoPosition.left,
									'top': logoPosition.top
								},'normal', function() {
									$('#wrapper #hd .logo').css({'visibility':'hidden'});
									$('#wrapper').fadeIn('normal', function() {
										logoWrap.hide();
										$('#wrapper #hd .logo').css({'visibility':'visible'});
										$('body').height('');
									});
								});
							});
						})
					});
				}, (wt.broswerSucks ? 100 : 0));
			});
		} else {
			return;
		}
	}
wt.backgrounds=function() {
	var images=$("ul.all-attachments img");
	setInterval(function(){
	var next=images.filter(":visible").parent().next().size() ? images.filter(":visible").parent().next().find("img") : images.eq(0);
	images.filter(":visible").fadeOut("normal");
	next.fadeIn("normal");	
	},7000)
	images.hide();
	images.eq(0).show();
}

})();