var Local = {

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

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

};

(function() {
	var $ = jQuery

	$(document).ready(
		function() {
			 menu()
//			 ampersands()
			 news()
			 input_defaults()
			 heroes()
			 hero_captions()
			 search_button()
			 paged_navigation()
			 portfolio()
			 if(!$('html#home, html#ftp').size()) {
			 	var cols = $('div.glossary, div.contact').size() ? 3 : 2
				$('#content div.page-content').equalizeText(cols)
			 }
			 if ($('html').height() > $('#wrapper').outerHeight()) {
				  $('#footer').height(
				  $('#footer').height() + ($('html').height() - $('#wrapper').outerHeight())
				  )
			 }
			 $('a').click(function() { $(this).blur() })
		}
	)
	
	function ampersands() {
		 $('body').html(String($('body').html()).replace(/&amp;/ig, '<span class="ampersand">&amp;</span>'))
	}

	function heroes() {
		if($('#imageFadeContainer').size() <= 0) {
			return
		}
		var container = $('#imageFadeContainer')
		var fade = container.find('input[name=fadevalue]').val() * 1000
		var show = container.find('input[name=showvalue]').val() * 1000
		container.children('input').remove()
		var images = container.children()
		images.hide()
		$(images[0]).show().addClass('current')
		setInterval(
			function() {
				var from = container.find('.current')
				from.removeClass('current')
				var to = (from.next().size() > 0 ? from.next() : $(images[0]))
				to.addClass('current')
				from.fadeOut(fade)
				to.fadeIn(fade)
			},
			fade + show
		)
	}
	
	function news() {
		 var ticker = $('div#news-ticker')
		 if(!ticker.size()) {
		 	return
		 }
		 var list = ticker.find('ul')
		 var items = list.find('li')
		 var highest = 0
	
		 items.each(
		 function() {
			  if ($(this).height() > highest) {
					highest = $(this).height()
	
			  }
	
		 }
		 )
		 items.each(
		 function() {
			  var diff = highest - $(this).height()
			  $(this).css({
					paddingTop: diff / 2,
					paddingBottom: diff / 2,
					display: 'block'
			  })
	
		 }
		 )
		 items.hide()
		 $(items[0])
		 	.attr({
				current: 'current'
			 })
			 .css({
				  display: 'block'
			 })
	
		 // Now we correct the position of the ftp link to match the new position of the navigation after all this manipulating
		 var logo = $('#logo img')
		 var diff = $('#navigation').offset().top + parseInt($('#navigation').css('padding-top'), 10) - logo.outerHeight({
			  margin: true
		 })
		 logo.css({
			  marginBottom: (diff + parseInt(logo.css('margin-bottom'), 10) - 1) + 'px'
	
		 })
	
		 // And now, the animation
		 var show = 5
		 var fade = 1
	
		 setInterval(
		 function() {
			  var current = list.find('li[current]')
			  if (current.next('li').size()) {
					// This is not the last item
					current.fadeOut(
						fade * 1000, 
						'easeOutSine', 
						function() {
							 current.removeAttr('current')
							 current.next('li').attr({
								  current: 'current'
							 })
						}
					)
					current.next('li').fadeIn(fade*1000, 'easeOutSine')
	
			  } else {
					// This is the last item
					current.fadeOut(
						fade * 1000, 
						'easeOutSine', 
						function() {
							 current.removeAttr('current')
							 $(items[0]).attr({
								  current: 'current'
							 })
						}
					)
					$(items[0]).fadeOut(fade*1000, 'easeOutSine')
			  }
	
		 },
		 (show + fade) * 1000
		 )
	
	}
	
	function menu() {
		$('div#navigation li.subnav').each(
			function() {
				if($(this).prev('li.nav-about').size()) {
					$(this).children('ul').addClass('subnav').appendTo($(this).prev('li'))
				}
				$(this).remove()
			}
		)
		 $('ul.subnav').each(
		 function() {
			  $(this)
		  		.attr({
						orig_height: $(this).height()
				})
	
		 }
		 )
		 setTimeout(
		 	function() {
				 $('ul.subnav').height(0)
		 	}, 100
		 )
		 var highest = {
			  inner: 0,
			  outer: 0
		 }
		 $('ul.navigation li').each(
		 function() {
			  var toplevel = $(this)
			  highest.inner = toplevel.height() > highest.inner ? toplevel.height() : highest.inner
			  highest.outer = toplevel.height() > highest.outer ? toplevel.outerHeight(true) : highest.outer
			  var subnav = toplevel.find('ul.subnav')
			  toplevel.hover(
			  function() {
					subnav.stop()
					toplevel.siblings().find('ul.subnav').animate({
						 height: 0
	
					},
					300, 'easeInSine')
					subnav.animate({
						 height: subnav.attr('orig_height')
	
					},
					800, 'easeOutSine')
	
			  },
			  function() {
					setTimeout(
					function() {
						 subnav.animate({
							  height: 0
	
						 },
						 300, 'easeInSine')
	
					},
					500
					)
	
			  }
			  )
	
		 }
		 )
		 if(!(jQuery.browser.msie && jQuery.browser.version < 7)) {
//			 $('ul.navigation').height(highest.outer)
//			 $('ul.navigation > li').height(highest.inner)
		 }
	
	}
	
	function input_defaults() {
		 $('input[type=text], textarea.input-textarea').each(
		 function() {
			  var starting_text = $(this).val()
			  $(this).focus(
			  function() {
					$(this).val(starting_text == $(this).val() ? '': $(this).val())
	
			  }
			  )
			  $(this).blur(
			  function() {
					$(this).val('' == $(this).val() ? starting_text: $(this).val())
	
			  }
			  )
	
		 }
		 )
	
	}
	
	function hero_captions() {
		 if (!$('#imageFadeContainer').size()) {
			  var caption = $('div#heroshot img').attr('alt')
			  if (caption && caption.length > 0) {
					$('div#heroshot')
					.after(
					$(jQuery('<div />'))
					.attr({
						 id: 'heroshot-caption'
					})
					.text(caption)
					.css({
						 marginBottom: $('div#heroshot').css('margin-bottom')
					})
					)
					.css({
						 marginBottom: 0
					})
	
			  }
	
		 }
	
	}
	
	function search_button() {
		var button = $('#search-button')
		var input = $('#google-search input[type=text]')
		if(jQuery.browser.safari) {
			button.css({top: '1px'})
		}
		button.attr({orig_width: button.width()})
		setTimeout(
			function() {
				button.width(0)
			}, 100
		)
		input
			.focus(
				function() {
					button.animate({
						width: button.attr('orig_width')
					}, 250)
				}
			)
			.blur(
				function() {
					button.animate({
						width: 0
					}, 250)
				}
			)
	}

	function paged_navigation() {
		var nav = $('.paged_navigation')
		if(!nav.size()) { return }
		
		var pages = nav.find('a:not(.prev_link,.next_link)')
		var current = nav.find('span.selected a').text()
		var prev = nav.find('.prev_link').size() ? nav.find('.prev_link') : ''
		var next = nav.find('.next_link').size() ? nav.find('.next_link') : ''
			
		nav
			.empty()
			.append(prev)
			.append(
				$(jQuery('<span />'))
					.addClass('pages')
					.append(
						$(jQuery('<span />'))
							.addClass('current')
							.text('' + current + '')
					)
					.append(' of ')
					.append('' + pages.size() + '')
			)
			.append(next)
	}
	
	var portfolio_is_running = false
	
	function portfolio() {
		if(!$('div.portfolio').size()) { return }
		var container = $('#heroshot')
		container.removeClass('loading')
		$('#heroshot ul.all-attachments img[class*=thumb]').parent('li').remove()
		$('#heroshot[title$= ]').attr({title: String($('#heroshot').attr('title')).replace(/:\s/gi, '')})
		if($('div#heroshot-caption').size()) {
			$('div#heroshot-caption').text($('#heroshot').attr('title'))
		} else {
			$('div#heroshot')
				.after(
					$(jQuery('<div />'))
						.attr({
							 id: 'heroshot-caption'
						})
						.text($('#heroshot').attr('title'))
						.css({
							 marginBottom: $('div#heroshot').css('margin-bottom')
						})
				)
				.css({
					 marginBottom: 0
				})
		}
		if($('#ajax_holder').size()) {
			var holder = $('#ajax_holder')
		} else {
			var holder = $(jQuery('<div />'))
			$('body').append(
				holder.attr({id: 'ajax_holder'}).css({positioff: 'absolute', top: '-10000px', left: '-10000px'})
			)
		}
		
		var thumbs = $('div.portfolio-thumbs a')

		if(!$('div.portfolio-thumbs a.selected').size()) {
			$(thumbs[0]).addClass('selected')
		}

		if(container.find('img').size() < 2) {
			container.find('.previous,.next').remove()
		}
		
		container.find('ul.all-attachments').find('img').each(
			function(i) {
				if(i < 1) {
					$(this).css({left: 0}).attr({current: 'current'})
				} else {
					$(this).css({
						left: (i * container.width()) + 'px',
						width: '746px',
						marginLeft: '93px',
						height: '498px',
						marginTop: '-249px'
					})
				}
			}
		)
		
		container.find('ul.all-attachments').width(container.width() * container.find('ul.all-attachments').find('img').size())
		
		container.find('.previous,.next').each(
			function() {
				$(this)
					.click(
						function(click) {
							click.preventDefault()
							portfolio_slide(this.className)
							$(this).blur()
						}
					)
			}
		)
		if(!container.find('img[current]').parent('li').prev('li').find('img').size()) {
			$('#heroshot .previous').css({display: 'none'})
		} else {
			$('#heroshot .previous').css({display: 'block'})
		}
		if(!container.find('img[current]').parent('li').next('li').find('img').size()) {
			$('#heroshot .next').css({display: 'none'})
		} else {
			$('#heroshot .next').css({display: 'block'})
		}
		
		thumbs.each(
			function() {
				$(this).click(
					function(click) {
						click.preventDefault()
						if(portfolio_is_running) {
							return
						}
						portfolio_is_running = true
						$('div.portfolio-thumbs a.selected').removeClass('selected')
						$(this).addClass('selected')
						container.addClass('loading')
						container.children('ul.all-attachments').fadeOut('500')
						holder.load(
							this.href + ' #heroshot',
							{},
							function() {
								holder.children().fadeOut('500',
									function() {
										setTimeout(
											function() {
												holder.find('ul.all-attachments img.thumbnail').parent('li').remove()
												container.attr({title: holder.find('#heroshot').attr('title')}).html(holder.find('#heroshot').html())
												holder.empty()
												portfolio_is_running = false
											}, 510
										)
										setTimeout(
											function() {
												portfolio()											
												container.children('ul.all-attachments').fadeIn('500')
											}, 520
										)
									}
								)
							}
						)
					}
				)
			}
		)
	}
	
	function portfolio_slide(direction) {
		var current = $('#heroshot img[current]')
		var to = null
		switch(direction) {
			case 'previous':
				to = current.parent('li').prev('li').find('img')
				break;
			case 'next':
				to = current.parent('li').next('li').find('img')
				break;
		}
		if(to.size()) {
			current.animate(
				{
					width: '746px',
					marginLeft: '93px',
					height: '498px',
					marginTop: '-249px'						
				},
				800,
				'easeInSine',
				function() {
					current.parents('ul.all-attachments').animate(
						{
							left: (0 - parseInt(to.css('left'), 10)) + 'px'
						},
						1500,
						'swing',
						function() {
							current.removeAttr('current')
							to.animate(
								{
									width: '933px',
									marginLeft: '0px',
									height: '622px',
									marginTop: '-311px'						
								},
								800,
								'easeOutSine',
								function() {
									if(!to.parent('li').prev('li').find('img').size()) {
										$('#heroshot .previous').css({display: 'none'})
									} else {
										$('#heroshot .previous').css({display: 'block'})
									}
									if(!to.parent('li').next('li').find('img').size()) {
										$('#heroshot .next').css({display: 'none'})
									} else {
										$('#heroshot .next').css({display: 'block'})
									}
								}
							)
							.attr({current: 'current'})
						}
					)
				}
			)
		}
	}

})();
