
var Local = {

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

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

};


(function() {

var $ = jQuery

if (!window['wt']) {
	window['wt'] = {};
	var wt = window['wt']
}

wt.browserSucks = (jQuery.browser.msie && jQuery.browser.version < 7) ? true : false;

$(document).ready(
	function() {
		services()
		wt.hero()
		$('script[src*=scriptaculous],script[src*=effects],script[src*=lightbox],script[src=*image-gallery],link[href*=lightbox]').remove()
		wt.lb()
		if($('html#gallery').size()) {
			$('li.thumbnail:first-child a').click()
		}
		wt.gallery()
	}
)

function services() {
	var divs = $('#services_images div')
	divs.each(function(i) {
		var div = $(this)
		div.origWidth = div.width()
		div.targetWidth = div.width() * 2
		div.origMarginLeft = parseInt(div.css('margin-left'),10)
		div.targetMarginLeft = div.marginLeft * 2
		div.origLeft = (div.outerWidth({'margin': true}) * i) - (i == 0 ? 0 : 3)
		div.targetLeft = div.origLeft - (div.width() * 0.5)
		div.css({
			'position': 'absolute',
			'left': div.origLeft,
			'opacity': 0.5,
			'z-index': 1
		})
		div.hover(
			function(){
				div.css({
					'z-index': 100
				}).animate({
					'width': div.targetWidth,
					'margin-left': div.targetMarginLeft,
					'left': div.targetLeft,
					'opacity': 1
				}, 'fast')
				.addClass('hover')
			},
			function() {
				div.css({
					'z-index': 1
				}).animate({
					'width': div.origWidth,
					'margin-left': div.origMarginLeft,
					'left': div.origLeft,
					'opacity': 0.5
				}, 'fast')
				.removeClass('hover')
			}
		)
	})
}

if(typeof wt == 'undefined') {
	var wt = {}
}

wt.hero = function() {
	if(!$('#imageFadeContainer').size()) {
		return
	}
	
	if($('html#home').size() || $('html#people').size()) {
    wt.hero.useControls = false;
    }
	
	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'})
									)
							)
							.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'})
											.css({'display': 'none'})
									)
							)
							.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 = true
wt.hero.useCaptions = false


wt.gallery = function() {
	$('li.nav-gallery a').click(function(c) {
		c.preventDefault()
		if(!$('#gallery-loader').size()) {
			$('body').append(
				$(jQuery('<div id="gallery-loader" style="display: none; position: absolute; left: -100000px; top: -100000px;" />'))
			)
		}
		$('#gallery-loader').load(
			$(this).attr('href') + ' a[rel*=lightbox]',
			{},
			function() {
				if(!$('#overlay').size()) {
					$('body').append(
						$(jQuery('<div />'))
							.attr({ 'id': 'overlay'})
					)
				}
				$('#overlay').append(
					$(jQuery('<div />'))
						.addClass('loading')
						.html('The Gallery is being loaded')
				)
				if(jQuery.browser.msie && jQuery.browser.version < 7) {
					$('#overlay').show()
				} else {
					$('#overlay').fadeIn('normal')
				}
				wt.lb(true, $('#gallery-loader'), function() { setTimeout(function() { wt.lb.play.click() }, wt.lb.slideshowDisplay) })
			}
		)
	})
}

wt.lb = function(ajax, html, callback) {
	if(typeof ajax == 'undefined') { ajax = false; }
	if(typeof html == 'undefined') { html = ''; }
	if(typeof initLightbox == 'function') { Event.stopObserving(window, 'load', initLightbox, false); }
	$(window).load(function(){ Lightbox = function() { return false; }; ImageGallery = function() { return false; }	})
	if(!ajax) { wt.lb.links = $('a[rel*=lightbox]') } else if(typeof html == 'object') { wt.lb.links = html.find('a[rel*=lightbox]'); } else { return }
	wt.lb.slideshowDisplay = $("input[name=galleryvalue]").size() ? $("input[name=galleryvalue]").val() : wt.lb.slideshowDisplay
	if(!wt.lb.links.size()) { return }
	if(!$('#overlay').size()) { $('body').append($(jQuery('<div id="overlay" style="display: none"></div>'))) }
	wt.lb.overlay = $('#overlay')
	wt.lb.overlay.click(function() { wt.lb.closeLightbox() });
	if(!$('#popup').size()) { $('body').append($(jQuery('<div id="popup" class="lightbox" style="display: none"></div>'))) }
	wt.lb.popup = $('#popup')
	wt.lb.closeLightbox = function() {
		wt.lb.popup.fadeOut('normal', function(){ wt.browserSucks ? wt.lb.overlay.hide() : wt.lb.overlay.fadeOut('normal') })
		wt.lb.popup.fadeOut('normal')
		wt.lb.target.find('img').remove()
		if(wt.lb.useKeyboardNav) { $(document).unbind('keypress',wt.lb.hotkeys) }
		if(typeof wt.lb.loop != 'undefined') {
        clearInterval(wt.lb.loop)
        }
	}
	
	if(!wt.lb.popup.find('a.close').size()) {
		wt.lb.close = $(jQuery('<a class="close" href="#close">Close</a>'))
		wt.lb.close.click(function(c){ c.preventDefault(); wt.lb.closeLightbox(); })
		wt.lb.close.appendTo(wt.lb.popup)
	}
	
	wt.lb.hotkeys = function(e) {
		if(!wt.lb.useKeyboardNav) { return; }
		e.preventDefault()
		var key = e.which
		if(key == 0 || key == 120) { // Esc or X
			wt.lb.closeLightbox()
		} else if(key == 110) { // P
			if(typeof wt.lb.next != 'undefined') {
			    wt.lb.isLoading = true
				wt.lb.next.click()
			}
		} else if(key == 112) { // N
			if(typeof wt.lb.prev != 'undefined') {
			    wt.lb.isLoading = true
				wt.lb.prev.click()
			}
		}
	}

	if(typeof wt.lb.target == 'undefined') {
		wt.lb.target = $(jQuery('<div class="content-wrapper"></div>'))
		wt.lb.popup.append(wt.lb.target)
	}
	
	wt.lb.clear = function() {
		wt.lb.target.empty()
	}

	if(wt.lb.thumbsOnPopup) {
		wt.lb.thumbs = $(jQuery('<div class="thumbs"></div>'))
		wt.lb.thumbsTarget = $(jQuery('<div class="thumbs-wrapper">'))
		wt.lb.thumbsTarget.appendTo(wt.lb.thumbs)
		wt.lb.thumbsTarget.width(wt.lb.thumbWidth * wt.lb.links.size())
		wt.lb.popup.append(wt.lb.thumbs)
		wt.lb.thumbs.prev = $(jQuery('<a class="prev" href="#previous" title="Previous Photos">Previous</a>'))
		wt.lb.thumbs.prev.click(function(c) {
			c.preventDefault()
			wt.lb.isLoading = true
		    
			wt.lb.thumbs.prev.blur()
			var totalWidth = wt.lb.thumbsTarget.width()
			var viewWidth = wt.lb.thumbs.width()
			var currentLeft = parseInt(wt.lb.thumbsTarget.css('left'),10)
			if(currentLeft + viewWidth >= 0) {
				if(currentLeft != 0 ) {
					wt.lb.thumbsTarget.animate({
						'left': 0
					})
				}
			} else {
				wt.lb.thumbsTarget.animate({
					'left': currentLeft + viewWidth
				})
			}
		})
		wt.lb.thumbs.prev.prependTo(wt.lb.thumbs)
		wt.lb.thumbs.next = $(jQuery('<a class="next" href="#next" title="Next Photos">Next</a>'))
		wt.lb.thumbs.next.click(function(c) {
			c.preventDefault()
			wt.lb.isLoading = true
			wt.lb.thumbs.next.blur()
			var totalWidth = wt.lb.thumbsTarget.width()
			var viewWidth = wt.lb.thumbs.width()
			var currentLeft = parseInt(wt.lb.thumbsTarget.css('left'),10)
			if(currentLeft - viewWidth < 0 - totalWidth) {
				if(totalWidth + (currentLeft - viewWidth) < totalWidth % viewWidth) {
					wt.lb.thumbsTarget.animate({
						'left': 0 - totalWidth + (totalWidth % viewWidth)
					})
				}
			} else {
				wt.lb.thumbsTarget.animate({
					'left': currentLeft - viewWidth
				})
			}
		})
		wt.lb.thumbs.next.appendTo(wt.lb.thumbs)
	}
	if(!wt.lb.thumbsOnPopup || wt.lb.prevNextAlways) {
		if(typeof wt.lb.prev == 'undefined') {
			wt.lb.prev = $(jQuery('<a class="prev" href="#previous" title="Previous Photo">Previous</a>'))
			wt.lb.slideshow ? null : wt.lb.prev.css({'visibility':'hidden'})
			wt.lb.prev.click(function(c) {
				c.preventDefault()
				wt.lb.isLoading = true
				wt.lb.prev.blur()
				var current = wt.lb.links.index(wt.lb.links.filter('[current]'))
				if(current > -1) {
					if(current - 1 > -1) {
						$(wt.lb.links[current - 1]).click()
					} else if(wt.lb.loopPhotos) {
						$(wt.lb.links[wt.lb.links.size() - 1]).click()
					}
				}
			}).prependTo(wt.lb.popup)
			if(!wt.lb.slideshow) {
				wt.lb.prev.hover(
					function() { wt.lb.prev.css({'visibility': 'visible'}); wt.lb.next.css({'visibility': 'visible'}) },
					function() { wt.lb.prev.css({'visibility': 'hidden'}); wt.lb.next.css({'visibility': 'hidden'}) }
				)
			}
		}
		
		if(typeof wt.lb.next == 'undefined') {
			wt.lb.next = $(jQuery('<a class="next" href="#next" title="Next Photo">Next</a>'))
			wt.lb.slideshow ? null : wt.lb.next.css({'visibility':'hidden'})
			wt.lb.next.click(function(c) {
				c.preventDefault()
				wt.lb.isLoading = true
				wt.lb.next.blur()
				var current = wt.lb.links.index(wt.lb.links.filter('[current]'))
				if(current > -1) {
					if(current + 1 < wt.lb.links.size()) {
						$(wt.lb.links[current + 1]).click()
					} else if(wt.lb.loopPhotos) {
						$(wt.lb.links[0]).click()
					}
				}
			}).appendTo(wt.lb.popup)
			if(!wt.lb.slideshow) {
				wt.lb.next.hover(
					function() { wt.lb.prev.css({'visibility': 'visible'}); wt.lb.next.css({'visibility': 'visible'}) },
					function() { wt.lb.prev.css({'visibility': 'hidden'}); wt.lb.next.css({'visibility': 'hidden'}) }
				)
			}
		}

		if(!wt.lb.slideshow) {
			wt.lb.target.hover(
				function() { wt.lb.prev.css({'visibility': 'visible'}); wt.lb.next.css({'visibility': 'visible'}) },
				function() { wt.lb.prev.css({'visibility': 'hidden'}); wt.lb.next.css({'visibility': 'hidden'}) }
			)
		}
	}
	
	if(wt.lb.slideshow) {
		if(typeof wt.lb.pause == 'undefined') {
			wt.lb.pause = $(jQuery('<a class="pause" style="display:none" href="#pause" title="Pause Photos">Pause</a>'))
			wt.lb.pause.click(function(c) {
				c.preventDefault()
				wt.lb.pause.css({'display': 'none'})
				wt.lb.play.css({'display': 'block'})
				clearInterval(wt.lb.loop)
			}).appendTo(wt.lb.popup)
		}

		if(typeof wt.lb.play == 'undefined') {
			wt.lb.play = $(jQuery('<a class="play" style="display: block" href="#play" title="Play Photos">Play</a>'))
			wt.lb.play.click(function(c) {
				c.preventDefault()
				wt.lb.play.css({'display': 'none'})
				wt.lb.pause.css({'display': 'block'})
				if(typeof wt.lb.loop) {
					clearInterval(wt.lb.loop)
				}
				wt.lb.loop = setInterval(function(){
				    if(!wt.lb.isLoading) { 
						if(typeof wt.lb.next != 'undefined') {
						     wt.lb.next.click()
						} else {
							var current = wt.lb.links.index(wt.lb.links.filter('[current]'))
							if(current > -1) {
								if(current + 1 < wt.lb.links.size()) {
									$(wt.lb.links[current + 1]).click()
								} else if(wt.lb.loopPhotos) {
									$(wt.lb.links[0]).click()
								}
							}
						}
					} else { return; }
        			
				}, wt.lb.slideshowDisplay * 1000)
			})
			wt.lb.play.appendTo(wt.lb.popup)
		}
	}

	if(wt.lb.useCaptions) { wt.lb.caption = $(jQuery('<div class="caption"></div>')); wt.lb.caption.appendTo(wt.lb.target) }

	wt.lb.links.each(function(i){
		var link = $(this)
		link.find('img').css({
			'width': '',
			'height': ''
		})
		if(wt.lb.thumbsOnPopup) {
			link.clone(true).click(function(c) {
				c.preventDefault()
				link.click()
			}).appendTo(wt.lb.thumbsTarget)
		}
		link.click(function(c){
			c.preventDefault()
			link.blur()
			wt.lb.links.removeAttr('current')
			link.attr({'current': 'current'})
			var img = new Image()
			if(wt.lb.popup.css('display') != 'block') {
				wt.lb.useKeyboardNav ? $(document).keypress(wt.lb.hotkeys) : null
				img.onload = function() {
					$(img).appendTo(wt.lb.target)
					wt.browserSucks ? wt.lb.overlay.show() : wt.lb.overlay.fadeIn('normal')
					var new_css = wt.lb.resizePopup ? {'width': img.width,'height': img.height,'margin-left': (0 - Math.round(img.width / 2)) + 'px','margin-top': (0 - Math.round(img.height / 2)) + 'px'} : {}
					wt.lb.popup.css(new_css)
					wt.lb.popup.fadeIn('normal')
					if(ajax) { wt.lb.overlay.find('.loading').remove() }
					wt.lb.isLoading = false
    				
				}
				img.src = link.attr('href')
				
				if(wt.lb.useCaptions) { wt.lb.caption.html(link.attr('title')) }
				if(wt.lb.slideshow && wt.lb.slideshowAutostart) { wt.lb.play.click() }
			} else {
				wt.lb.target.addClass('loading')
				if(wt.lb.useCaptions) { wt.lb.caption.fadeOut('normal', function() { wt.lb.caption.html(link.attr('title')) }) }
				wt.lb.target.find('img').fadeOut('normal', function() {
					img.style.display = 'none'
					img.onload = function() {
						wt.lb.target.find('img').remove()
						$(img).appendTo(wt.lb.target)
						if(wt.lb.useCaptions) {	wt.lb.caption.fadeIn('normal') }
						if(wt.lb.resizePopup) {
							wt.lb.popup.animate({'width': img.width,'height': img.height,'margin-left': (0 - Math.round(img.width / 2)) + 'px','margin-top': (0 - Math.round(img.height / 2)) + 'px'}, 'normal', function() {
								$(img).fadeIn('normal', function() { /* wt.lb.target.removeClass('loading') */ })
								if(ajax) { wt.lb.overlay.find('.loading').remove() }
							})
						} else {
							$(img).fadeIn('normal', function() { /* wt.lb.target.removeClass('loading') */ })
							if(ajax) { wt.lb.overlay.find('.loading').remove() }
						}
						wt.lb.isLoading = false
    					
					}
					img.src = link.attr('href')
    				
				})
			}
		})
	})
	wt.lb.links.filter('[current]').size() ? null : $(wt.lb.links[0]).attr({'current':'current'});
	(typeof callback == 'function') ? callback() : null;
}

// Lightbox options
wt.lb.thumbsOnPopup = false
wt.lb.useCaptions = false
wt.lb.thumbWidth = 196
wt.lb.prevNextAlways = true
wt.lb.slideshow = true
wt.lb.slideshowDisplay = 5 // In Seconds
wt.lb.slideshowAutostart = true
wt.lb.loopPhotos = true
wt.lb.useKeyboardNav = false
wt.lb.resizePopup = false

})();
