var Local = {

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

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

};

(function() {

var $ = jQuery

$(document).ready(function(){
	$('a').each(
		function() {
			$(this).click(
				function() {
					$(this).blur()
				}
			)
		}
	)
	ie_hover();
	parts();
	videos();
	photos();
	$('input[type=text], textarea').each(
		function() {
			var default_text = $(this).val()
			$(this).focus(
				function() {
					if($(this).val() == default_text) {
						$(this).val('')
					}
				}
			)
			$(this).blur(
				function() {
					if($(this).val() == '') {
						$(this).val(default_text)
					}
				}
			)
		}
	)
	$('div#nav a').each(
		function() {
			if($(this).parents('html#jobs').size()) {
				return
			}
			if(String(window.location).indexOf($(this).attr('href')) == -1) {
				$(this).parent('li').removeClass('ancestor').removeClass('selected')
			} else {
				$(this).parent('li').addClass('selected')
			}
		}
	)
	wt.lb()
})

$(window).load(function(){
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		var maxWidth = $('#main').width()
		$('#main img').each(function(){
			var img = $(this)
			var ratio = img.width() / img.height()
			if(img.outerWidth({margin: true}) > maxWidth) {
				var new_width = maxWidth - (img.outerWidth({margin: true}) - img.width())
				img.width(new_width)
//				img.height(new_width / ratio)
			}
		})
	}
})

function ie_hover() {
	if(!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version >= 7)) {
		return
	}
	var csshoverReg = /(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):hover/i;
	var currentSheet = null;

	parseStylesheets();	

	function parseStylesheets() {
		var sheets = window.document.styleSheets;
		for(var i = 0; i < sheets.length; i++) {
			parseStylesheet(sheets[i])
		}
	}

	function parseStylesheet(sheet) {
		if(sheet.imports) {
			try{
				var imports = sheet.imports;
				for(var i=0; i < imports.length; i++) {
					parseStylesheet(imports[i])
				}
			} catch(securityException) {
			}
		}
		try{
			var rules = sheet.rules;
			currentSheet = sheet;
			for(var x = 0; x <= rules.length; x++) {
				parseCSSRules(rules[x])
			}
		} catch(securityException) {
		}
	}
	
	function parseCSSRules(rule) {
		var select = rule.selectorText;
		var styles = rule.style.cssText;
		if(!csshoverReg.test(select) || !styles) {
			return;
		}

//		$('#debug').append($(jQuery('<p />')).html(select + ' { ' + styles + ' }'))

		var elem = select.replace(/:hover/,'');

		currentSheet.addRule(select.replace(/:hover/,'.psuedohover'), styles)

		$(elem).each(
			function() {
				$(this).hover(
					function() {
						$(this).addClass('psuedohover')
					},
					function() {
						$(this).removeClass('psuedohover')
					}
				)
			}
		)
	}
}

function parts() {
	$('div.parts-catalog ul.all-attachments').each(
		function() {
			if($('ul.all-attachments').find('img.attachment-image').size()) {
				$(this).before(
					$(jQuery('<div />'))
						.addClass('slideshow')
				)
			}
			$(this).children('li').each(
				function() {
					if($(this).find('img').hasClass('attachment-image')) {
						$(this).parent('ul.all-attachments').prev('div.slideshow').append($(this).children())
					} else {
						$(this).remove();
					}
				}
			)
			$(this).remove();
		}
	)
}

function videos() {
	$('div.videos ul.all-attachments li').children().each(
		function() {
			if(!($(this).hasClass('ext-flv') || $(this).hasClass('ext-mov'))) {
				$(this).parent().remove()
			} else {
				$(this).click(function(c){
					c.preventDefault()
					launchVideo($(this))
				})
			}
		}
	)
	
	$('div.video-attachments a.ext-flv, a[rel=video]').each(function() {
		$(this).click(function(c){
			c.preventDefault()
			launchVideo($(this))
		})
	})
	
	if($('div.videos ul.all-attachments li').size()) {
		$('div.videos').prepend('<h2>Videos</h2>')
	}
}

function photos() {
	var images = $('div.slideshow img.attachment-image')
	var thumbs = $(jQuery('<div />')).addClass('thumbnails')
	$('div.slideshow').prepend(thumbs)
	var loop = 1;
	images.each(function(i){
		var img = $(this)
		var li = img.parent('li')
		img.attr({number: i})
		var link = $(jQuery('<a />'))
		link.addClass('thumb')
		if(loop == 1) {
			link.addClass('thumb-left')
		} else if(loop == 3) {
			link.addClass('thumb-right')
			loop = 0;
		}
		loop++;
		link.attr({href: '#view-fullsize'})
		link.append(img)
		thumbs.append(link)
		link.click(function(c){
			c.preventDefault()
			launchPhoto(img.attr('src'), link)
		})
		li.remove()
	})
	if($('div.slideshow ul.all-attachments a.attachment-file').size()) {
		$('div.slideshow ul.all-attachments li a').each(function(){
			if($(this).hasClass('ext-flv') || $(this).hasClass('ext-mov')) {
				$(this).parent('li').remove()
			}
		})
		$('div.slideshow ul.all-attachments').before(
				$(jQuery('<h2 />')).html('HiRes Downloads')
			)
	}
}

function launchPhoto(source, link) {
	if(!$('#popup').size()) {
		$('body').append(
				$(jQuery('<div id="popup" style="display: none"><div id="overlay"></div></div>'))
			)
	}
	var popup = $('#popup')
	var overlay = popup.find('#overlay')
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		popup.width($('body').width())
		popup.height($('html').height())
		popup.css({
			top: document.documentElement.scrollTop
		})
		overlay.width($('body').width())
		overlay.height($('html').height())
	}
	if(source.length < 1) {
		return
	}
	var dimensions = false
	if(overlay.find('.photo-viewer').size()) {
		var container = overlay.find('.photo-viewer')
		dimensions = { x: container.find('img').width(), y: container.find('img').height() }
		container.find('img').fadeOut()
	} else {
		var container = $(jQuery('<div />'))
		container.addClass('photo-viewer')
		overlay.append(container)
	}
	container.addClass('loading')
	var img = new Image();
	img.onload = function(){
		if(!dimensions) {
			dimensions = { x: img.width, y: img.height }
		}
		$(img).hide()
		if(container.find('img').size()) {
			container.find('img').replaceWith($(img))
		} else {
			container.append($(img))
		}
		if(!container.find('.close').size()) {
			var close = $(jQuery('<div />'))
			close
				.addClass('close')
				.click(function() {
					popup.fadeOut()
				})
			container.append(close)
		}
		var prev = $(jQuery('<div />'))
		prev.addClass('prev')
		prev
			.click(
				function() {
					if(link.prev('a.thumb').size()) {
						link.prev('a.thumb').click()
					}
				}
			)
		if(!link.prev('a.thumb').size()) {
			prev.css({visibility: 'hidden'})
		}
		if(container.find('.prev').size()) {
			container.find('.prev').replaceWith(prev)
		} else {
			container.prepend(prev)
		}

		var next = $(jQuery('<div />'))
		next.addClass('next').unbind()
		next
			.click(
				function() {
					if(link.next('a.thumb').size()) {
						link.next('a.thumb').click()
					}
				}
			)
		if(!link.next('a.thumb').size()) {
			next.css({visibility: 'hidden'})
		}
		if(container.find('.next').size()) {
			container.find('.next').replaceWith(next)
		} else {
			container.append(next)
		}
		if(popup.is(':visible')) {
			$(img).fadeIn()
			if(dimensions && (dimensions.x != img.width || dimensions.y != img.height)) {
				container.animate({
					width: img.width,
					height: img.height,
					marginTop: (0 - (img.height / 2)) + 'px',
					marginLeft: (0 - (img.width / 2)) + 'px'
				}, 'normal')
				prev.animate({
					top: ((img.height / 2) - 7) + 'px'
				})
				next.animate({
					top: ((img.height / 2) - 7) + 'px'
				})
			} else {
				prev.css({
					top: ((img.height / 2) - 7) + 'px'
				})
				next.css({
					top: ((img.height / 2) - 7) + 'px'
				})
			}
		} else {
			$(img).show()
			container.css({
				width: img.width,
				height: img.height,
				marginTop: (0 - (img.height / 2)) + 'px',
				marginLeft: (0 - (img.width / 2)) + 'px'
			})
			prev.css({
				top: ((img.height / 2) - 7) + 'px'
			})
			next.css({
				top: ((img.height / 2) - 7) + 'px'
			})
			popup.fadeIn('slow')
		}
	}
	img.src = source
	container.removeClass('loading')
}

function launchVideo(vid) {
	var video = vid.clone(true)
	if(!$('#popup').size()) {
		$('body').append('<div id="overlay"></div>').append('<div id="popup"></div>')
	}
	var popup = $('#popup')
	var overlay = $('#overlay')
	var videoViewer = $(jQuery('<div class="video-viewer"></div>'))
	popup.append(videoViewer)
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		popup.width($('body').width())
		popup.height($('html').height())
		popup.css({
			top: document.documentElement.scrollTop
		})
		overlay.width($('body').width())
		overlay.height($('html').height())
	}
	videoViewer.empty()
	videoViewer.append(video)
	var close = $(jQuery('<div />'))
	close
		.addClass('close')
		.click(function() {
			if(!wt.browerSucks) {
				overlay.fadeOut()
			} else {
				overlay.hide()
			}
			popup.fadeOut()
		})
    
	overlay.click(function() {
		popup.find('div.close').click()
	});
    

	$.fn.media.defaults.flvPlayer = '/mediaplayer.swf'
	$.fn.media.defaults.width = 320
	$.fn.media.defaults.height = 260
	$.fn.media.defaults.autostart = true
	
	video.media()

	videoViewer.append(close)

	if(!wt.browerSucks) {
		overlay.fadeIn()
	} else {
		overlay.show()
	}
	popup.fadeIn()
}

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

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

wt.lb = function(ajax, html) {
	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.thumbnail[rel*=image]') } else if(typeof html == 'object') { wt.lb.links = html.find('a.thumbnail[rel*=image]') } else { return }
	if(!wt.lb.links.size()) { return }
	if(!$('#wt-overlay').size()) { $('body').append($(jQuery('<div id="wt-overlay" style="display: none"></div>'))) }
	wt.lb.overlay = $('#wt-overlay')
    wt.lb.overlay.click(function() { wt.lb.closeLightbox() });
	if(!$('#wt-popup').size()) { $('body').append($(jQuery('<div id="wt-popup" class="lightbox" style="display: none"></div>'))) }
	wt.lb.popup = $('#wt-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; }
		var key = e.which
		if(key == 0 || key == 120) { // Esc or X
			e.preventDefault()
			wt.lb.closeLightbox()
		} else if(key == 110) { // P
			e.preventDefault()
			if(typeof wt.lb.next != 'undefined') {
				wt.lb.next.click()
			}
		} else if(key == 112) { // N
			e.preventDefault()
			if(typeof wt.lb.prev != 'undefined') {
				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)
	}

	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.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.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.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.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(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()
							}
						}
					}
				}, 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() {
					if(wt.browserSucks && img.height > 440) {
						img.style.height = '440px'
					}
					$(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() }
				}
				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()
						if(wt.browserSucks && img.height > 440) {
							img.style.height = '440px'
						}
						$(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() }
						}
					}
					img.src = link.attr('href')
				})
			}
		})
	})
	wt.lb.links.filter('[current]').size() ? null : $(wt.lb.links[0]).attr({'current':'current'})
}

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

})();
