
var Local = {

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

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

};

(function() {
	var j = jQuery
	
	var wt = {}
	
	wt.debug = (String(window.location).match('iago') ? true : false)
	
	wt.log = function(item) {
		if(wt.debug) {
			if(typeof console != 'undefined') {
				console.log(item)
			} else {
				alert(item)
			}
		}
	}
	
	wt.navHovers = function() {
		j('#home #wrapper ul.navigation li.nav-a-man-named-pearl, #home #wrapper ul.navigation li.nav-balancing-the-scales, #home #wrapper ul.navigation li.nav-norman-blake-documentary, #home #wrapper ul.navigation li.nav-new-orleans, #home #wrapper ul.navigation li.nav-chief-braveheart').each(function() {
			var item = j(this)
			var hoverImg = j(jQuery('<div />'))
			hoverImg
				.css({
					'right': '-600px'
				})
				.addClass('hover-image')
			item.append(hoverImg)
			item.hover(
				function() {
					hoverImg.animate({
						'right': (0 - 600) + item.width()
					}, 'normal')
				},
				function() {
					hoverImg.animate({
						'right': (0 - 600)
					}, 'normal')
				}
			)
		})
	}
	
	wt.navAjax = function() {
		j('#home #wrapper ul.navigation li a').each(function() {
			var item = j(this)
			if(item.parent('li').hasClass('nav-home')) {
				return
			} else if(item.parent('li').hasClass('nav-a-man-named-pearl') || item.parent('li').hasClass('nav-balancing-the-scales') || item.parent('li').hasClass('nav-norman-blake-documentary') || item.parent('li').hasClass('nav-new-orleans') || item.parent('li').hasClass('nav-chief-braveheart')) {
				var wrapper = 'div.page-content'
			} else {
				var wrapper = 'div.ajax-wrapper'
			}
			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()
					}
				)
			})
		})
	}
	
	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))
			})
		}
		
	}
	
	j(document).ready(function() {
		wt.navHovers()
		wt.navAjax()
		
		if( j('html#home') ){
			j(window).bind('resize',function(){
				if( j('ul.navigation').height() + j('div#footer').height() > j(window).height()/2 + 107 ){
					j('ul.navigation').css({position:'relative', margin:'20px 0 0 0', top:0, left:0})
					j('div#footer').css({position:'static'})
				} else if( j('div#footer').css('position') == 'static' ) {
					j('ul.navigation').css({position:'absolute', margin:' -167px 0 0 -426px', top:'50%', left:'50%'})
					j('div#footer').css({position:'absolute'})
				}
			})
			j(window).trigger('resize')
		}		
	})
})();
