var Local = {

    onCommentReply: function(id) {
		Comments.openReply(id);
	},

    onCancelReply: function(id) {
		Comments.closeReply();
	}

};

if(!window['terminal']) {
	window['terminal'] = {};
	var terminal = window['terminal'];
}

(function(){
	var $ = jQuery;

	$(document).ready(function() {
		terminal.ampersands();
		terminal.padFooter();
		terminal.gallery();
		terminal.heroshots();
		terminal.nav();
		$('a').filter(function() {
            return this.hostname && this.hostname !== location.hostname;
        }).attr('target', '_blank');
        
	});
	
	$(window).resize(function() {
		terminal.padFooter();
	});
	
	terminal.padFooter = function() {
		if($('body').height() < $(window).height()) {
			$('#ft').height($('#ft').height() + ($(window).height() - $('body').height()));
		}
	}
	
	terminal.gallery = function() {
		if(!$('#gallery').size()) { return; }
		var thumbs = $('.thumbs a');
		thumbs.each(function() {
			var thumb = $(this);
			
			thumb.click(function(c) {
				c.preventDefault();
				var currentImage = $('.gallery-container img');
				if(currentImage.attr('src') == thumb.attr('href')) { return; }
				currentImage.fadeOut('normal',function(){
					currentImage.remove();
					var img = new Image();
					img.onload = function() {
						$(img).css({'display':'none'}).appendTo($('.gallery-container')).fadeIn('normal');
					}
					img.src = thumb.attr('href');
				});
			});
		});
		$('.gallery-container').append(thumbs.eq(0).find('img').clone());
	}
	
	terminal.ampersands = function() {
		$("body *:contains('&')").each(function() {
			$(this).html($(this).html().replace(/&amp;/g, '<span class="ampersand">&amp;</span>'));
		});
		$('span.ampersand span.ampersand').each(function() {
			$(this).parent().replaceWith($(this))
		});
	}
	
	terminal.heroshots = function() {
		var heroshots = $('.heroshots');
		if(!heroshots.size()) { return; }
		
		heroshots.each(function() {
			var heroshot = $(this);
			var frame = $(jQuery('<div class="frame"></div>'));
			heroshot.prepend(frame);
			
			if(!heroshot.find('input').size()) { return; }
			
			var fade = heroshot.find('input[name=fadevalue]').val() * 1000;
			var dur = heroshot.find('input[name=showvalue]').val() * 1000;
			heroshot.find('input').remove();

			var images = heroshot.find('img');
			if(images.size() < 2) { return; }
			images.css({'display':'none'});
			images.eq(0).show();
			if(images.eq(0).parent('a').size()) {
				frame.bind('click', function() {
					window.location = images.eq(0).parent('a').attr('href');
				}).css({'cursor':'pointer'});
			}
			
			setInterval(function(){
				frame.unbind('click').css({'cursor':'default'});
				var current = images.filter(':visible');
				var next = current.parent('a').size() ? (current.parent('a').next('a,img').size() ? current.parent('a').next('a,img') : images.eq(0)) : (current.next('a,img').size() ? current.next('a,img') : images.eq(0));
				if(next.parent('a').size()) {
					frame.bind('click', function() {
						window.location = next.parent('a').attr('href');
					}).css({'cursor':'pointer'});
				}
				if(typeof next[0] != 'undefined' && next[0].nodeName.toLowerCase() == 'a') {
					frame.bind('click', function() {
						window.location = next.attr('href');
					}).css({'cursor':'pointer'});
					next = next.find('img');
				}
				current.fadeOut(fade);
				next.fadeIn(fade);
			}, fade + dur);
		});
	}
	
	terminal.nav = function() {
		$('#hd ul.navigation li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
	
})();
