var wt = {};

(function() {
	var $ = jQuery

	$(document).ready(function() {
		wt.videoLinks()
		wt.lightbox()
		$('a').click(function() {
			$(this).blur()
		});
	});

	wt.videoLinks = function() {
		var links = $('a.video')
		jQuery.fn.media.defaults.flvPlayer = '/mediaplayer.swf'
		jQuery.fn.media.defaults.mp3Player = '/mediaplayer.swf'

		links.each(function() {
			var link = $(this)
			var video = link.attr('href')
			var linkClone = link.clone(true)
			link.click(function(c) {
				c.preventDefault()
				if(typeof wt.lightbox.photoTarget == 'undefined') {
					wt.lightbox.photoTarget = $(jQuery('<div />'))
					wt.lightbox.photoTarget.addClass('photo-target')
					wt.lightbox.popup.append(wt.lightbox.photoTarget)
				}
				wt.lightbox.popup.removeClass('lightbox').addClass('video')
				wt.lightbox.photoTarget.append(linkClone)
				linkClone.media({'autoplay': true, 'bgColor': 'transparent', 'width': 400, 'height': 320, 'caption': false, flashvars: {'autostart': true }})
				wt.lightbox.overlay.width($(window).width())
				if(jQuery.browser.msie && jQuery.browser.version < 7) {
					wt.lightbox.overlay.height($(document).height() > $(window).height() ? $(document).height() : $(window).height())
					wt.lightbox.overlay.css({
						'top': $(document).scrollTop(),
						'left': $(document).scrollLeft(),
						'margin': 0
					})
				} else {
					wt.lightbox.overlay.height($(window).height())
					wt.lightbox.overlay.css({
						'top': 0,
						'left': 0,
						'margin': 0
					})
				}
				if(jQuery.browser.msie) {
					wt.lightbox.overlay.show()
				} else {
					wt.lightbox.overlay.fadeIn('normal')
				}
				wt.lightbox.popup.fadeIn('normal')
			});
		});
	}
	
	wt.lightbox = 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(!$('#wt-overlay').size()) {
			$('body').append(
					$(jQuery('<div />'))
						.attr({ 'id': 'wt-overlay' })
						.css({'display': 'none'})
				)
		}
		wt.lightbox.overlay = $('#wt-overlay')
		wt.lightbox.overlay.click(function() {
			wt.lightbox.closeLightbox()
		})

		if(!$('#wt-popup').size()) {
			$('body').append(
					$(jQuery('<div />'))
						.attr({ 'id': 'wt-popup' })
						.addClass('lightbox')
						.css({'display': 'none'})
				)
		}
		wt.lightbox.popup = $('#wt-popup')

		wt.lightbox.closeLightbox = function() {
			wt.lightbox.popup.fadeOut('normal', function(){
				if(jQuery.browser.msie) {
					wt.lightbox.overlay.hide()
				} else {
					wt.lightbox.overlay.fadeOut('normal')
				}
			})
			wt.lightbox.popup.fadeOut('normal')
			wt.lightbox.photoTarget.children().remove()
			$(document).unbind('keypress',wt.lightbox.hotkeys)
		}

		if(!wt.lightbox.popup.find('a.close').size()) {
			wt.lightbox.close = $(jQuery('<a />'))
			wt.lightbox.close.addClass('close').html('Close').attr({'href': '#close'}).click(function(c){
				c.preventDefault() 
				wt.lightbox.closeLightbox()
			})
			wt.lightbox.close.appendTo(wt.lightbox.popup)
		}

		wt.lightbox.hotkeys = function(e) {
			e.preventDefault()
			var key = e.which
			if(key == 0 || key == 120) { // Esc or X
				wt.lightbox.closeLightbox()
			} else if(key == 110) { // P
				if(typeof wt.lightbox.next != 'undefined') {
					wt.lightbox.next.click()
				}
			} else if(key == 112) { // N
				if(typeof wt.lightbox.prev != 'undefined') {
					wt.lightbox.prev.click()
				}
			}
		}

		if(!ajax) {
			wt.lightbox.links = $('a[rel*=lightbox]')
		} else if(typeof html == 'object') {
			wt.lightbox.links = html.find('a[rel*=lightbox]')
		} else {
			return
		}

		if(!wt.lightbox.links.size()) {
			return
		}

		if(wt.lightbox.thumbsOnPopup && !wt.lightbox.popup.find('photo-wrapper').size()) {
			wt.lightbox.thumbs = $(jQuery('<div />'))
			wt.lightbox.thumbs.addClass('thumbs')
			wt.lightbox.thumbsTarget = $(jQuery('<div />'))
			wt.lightbox.thumbsTarget.addClass('thumbs-wrapper').appendTo(wt.lightbox.thumbs)
			wt.lightbox.photoTarget = $(jQuery('<div />'))
			wt.lightbox.photoTarget.addClass('photo-wrapper')
			wt.lightbox.thumbsTarget.width(wt.lightbox.thumbWidth * wt.lightbox.links.size())
		} else {
			wt.lightbox.photoTarget = wt.lightbox.popup
		}
		if(wt.lightbox.thumbsOnPopup) {
			if(!wt.lightbox.popup.find('photo-wrapper').size()) {
				wt.lightbox.popup.append(wt.lightbox.photoTarget)
				wt.lightbox.popup.append(wt.lightbox.thumbs)
			}
			wt.lightbox.thumbs.prev = $(jQuery('<a />'))
			wt.lightbox.thumbs.prev.addClass('prev').html('Previous').attr({'href': '#previous','title': 'Previous Photos'}).click(function(c) {
				c.preventDefault()
				wt.lightbox.thumbs.prev.blur()
				var totalWidth = wt.lightbox.thumbsTarget.width()
				var viewWidth = wt.lightbox.thumbs.width()
				var currentLeft = parseInt(wt.lightbox.thumbsTarget.css('left'),10)
				if(currentLeft + viewWidth >= 0) {
					if(currentLeft != 0 ) {
						wt.lightbox.thumbsTarget.animate({
							'left': 0
						})
					}
				} else {
					wt.lightbox.thumbsTarget.animate({
						'left': currentLeft + viewWidth
					})
				}
			})
			wt.lightbox.thumbs.prev.prependTo(wt.lightbox.thumbs)
			wt.lightbox.thumbs.next = $(jQuery('<a />'))
			wt.lightbox.thumbs.next.addClass('next').html('Next').attr({'href': '#next','title': 'Next Photos'}).click(function(c) {
				c.preventDefault()
				wt.lightbox.thumbs.next.blur()
				var totalWidth = wt.lightbox.thumbsTarget.width()
				var viewWidth = wt.lightbox.thumbs.width()
				var currentLeft = parseInt(wt.lightbox.thumbsTarget.css('left'),10)
				if(currentLeft - viewWidth < 0 - totalWidth) {
					if(totalWidth + (currentLeft - viewWidth) < totalWidth % viewWidth) {
						wt.lightbox.thumbsTarget.animate({
							'left': 0 - totalWidth + (totalWidth % viewWidth)
						})
					}
				} else {
					wt.lightbox.thumbsTarget.animate({
						'left': currentLeft - viewWidth
					})
				}
			})
			wt.lightbox.thumbs.next.appendTo(wt.lightbox.thumbs)
		}
		if(!wt.lightbox.thumbsOnPopup || wt.lightbox.prevNextAlways) {
			if(typeof wt.lightbox.prev == 'undefined') {
				wt.lightbox.prev = $(jQuery('<a />'))
				wt.lightbox.prev.addClass('prev').html('Previous').attr({'href': '#previous','title': 'Previous Photo'}).css({'visibility': 'hidden'}).click(function(c) {
					c.preventDefault()
					wt.lightbox.prev.blur()
					if(wt.lightbox.photoTarget.find('img').size()) {
						var count = parseInt(wt.lightbox.photoTarget.find('img').attr('count'),10)
						$(wt.lightbox.links[count - 1]).click()	
					}
				})
				wt.lightbox.prev.hover(
					function() {
						wt.lightbox.prev.css({'visibility': 'visible'})
						wt.lightbox.next.css({'visibility': 'visible'})
					},
					function() {
						wt.lightbox.prev.css({'visibility': 'hidden'})
						wt.lightbox.next.css({'visibility': 'hidden'})
					}
				)
				wt.lightbox.prev.prependTo(wt.lightbox.photoTarget)
			}

			if(typeof wt.lightbox.next == 'undefined') {
				wt.lightbox.next = $(jQuery('<a />'))
				wt.lightbox.next.addClass('next').html('Next').attr({'href': '#next','title': 'Next Photo'}).css({'visibility': 'hidden'}).click(function(c) {
					c.preventDefault()
					wt.lightbox.next.blur()
					if(wt.lightbox.photoTarget.find('img').size()) {
						var count = parseInt(wt.lightbox.photoTarget.find('img').attr('count'),10)
						$(wt.lightbox.links[count + 1]).click()	
					}
				})
				wt.lightbox.next.hover(
					function() {
						wt.lightbox.prev.css({'visibility': 'visible'})
						wt.lightbox.next.css({'visibility': 'visible'})
					},
					function() {
						wt.lightbox.prev.css({'visibility': 'hidden'})
						wt.lightbox.next.css({'visibility': 'hidden'})
					}
				)
				wt.lightbox.next.appendTo(wt.lightbox.photoTarget)
			}

			wt.lightbox.photoTarget.hover(
				function() {
					wt.lightbox.prev.css({'visibility': 'visible'})
					wt.lightbox.next.css({'visibility': 'visible'})
				},
				function() {
					wt.lightbox.prev.css({'visibility': 'hidden'})
					wt.lightbox.next.css({'visibility': 'hidden'})
				}
			)
		}
		if(wt.lightbox.useCaptions) {
			wt.lightbox.caption = $(jQuery('<div />'))
			wt.lightbox.caption.addClass('caption').appendTo(wt.lightbox.photoTarget)
		}

		wt.lightbox.links.each(function(i){
			var link = $(this)
			link.find('img').css({
				'width': '',
				'height': ''
			})
			link.attr({'count': i})
			if(wt.lightbox.thumbsOnPopup) {
				link.clone(true).click(function(c) {
					c.preventDefault()
					link.click()
				}).appendTo(wt.lightbox.thumbsTarget)
			}
			link.click(function(c){
				c.preventDefault()
				link.blur()
				var img = new Image()
				if(wt.lightbox.popup.css('display') != 'block') {
					$(document).keypress(wt.lightbox.hotkeys)
					img.onload = function() {
						$(img).appendTo(wt.lightbox.photoTarget)
						if(jQuery.browser.msie && jQuery.browser.version < 7) {
							wt.lightbox.overlay.show()
						} else {
							wt.lightbox.overlay.fadeIn('normal')
						}
						wt.lightbox.popup.fadeIn('normal')
						if(ajax) {
							wt.lightbox.overlay.find('.loading').remove()
						}
					}
					img.src = link.attr('href')
					$(img).attr({ 'count': link.attr('count') })
					if(wt.lightbox.useCaptions) {
						wt.lightbox.caption.html(link.attr('title'))
					}
				} else {
					wt.lightbox.photoTarget.addClass('loading')
					if(wt.lightbox.useCaptions) {
						wt.lightbox.caption.fadeOut('normal', function() {
							wt.lightbox.caption.html(link.attr('title'))
						})
					}
					wt.lightbox.photoTarget.find('img').fadeOut('normal', function() {
						img.style.display = 'none'
						img.onload = function() {
							wt.lightbox.photoTarget.find('img').remove()
							$(img).appendTo(wt.lightbox.photoTarget)
							if(wt.lightbox.useCaptions) {
								wt.lightbox.caption.fadeIn('normal')
							}
							$(img).fadeIn('normal', function() {
								wt.lightbox.photoTarget.removeClass('loading')
							})
							if(ajax) {
								wt.lightbox.overlay.find('.loading').remove()
							}
						}
						img.src = link.attr('href')
						$(img).attr({ 'count': link.attr('count') })
					})
				}
			})
		})
	}

	// Lightbox options
	wt.lightbox.thumbsOnPopup = false
	wt.lightbox.useCaptions = false
	wt.lightbox.thumbWidth = 196
	wt.lightbox.prevNextAlways = false

})();