var Local = {

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

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

};

(function() {
	var $ = jQuery
	
	$(document).ready(
		function() {
			heroes()
			$('#content > :first-child').addClass('first-child')
			$('#content > :last-child, #sidebar > :last-child, #content div.page-content > :last-child').addClass('last-child')
			if(!$('div.corporate-membership').size()){
				$('.page-content *[style]').removeAttr('style')
			}
			$('font').each(
				function() {
					$(this).replaceWith($(this).html())
				}
			)
			nav()
			forms()
			tables()
		}
	)
	
	$(window).load(
		function() {
			maxWidth()
			if($('#sidebar').outerHeight() < $('#content').outerHeight()) {
				$('#sidebar').height(($('#content').outerHeight() - $('#sidebar').outerHeight()) + $('#sidebar').height())
			}
		}
	)

	function heroes() {
		if($('#imageFadeContainer').size() <= 0 || $('#imageFadeContainer').find('img').size() == 1) {
			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()
		container.parent().append(
			$(jQuery('<div />')).attr({id: 'hero-fg'})
		)
		images.hide()
		$(images[0]).show().addClass('current')
		if($(images[0]).attr('href') != undefined && overlay) {
			container.parent().find('#hero-fg').bind('click', function(){ window.location = $(images[0]).attr('href') }).css({cursor: 'pointer'})
		}
		setInterval(
			function() {
				if(container.find('#hero-fg').size()) {
					container.parent().find('#hero-fg').bind('click', function(){}).css({cursor: 'default'})
				}
				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)
				if(to.attr('href') != undefined && container.find('#hero-fg').size()) {
					container.parent().find('#hero-fg').bind('click', function(){ window.location = to.attr('href') }).css({cursor: 'pointer'})
				}
			},
			fade + show
		)
	}
	
	function nav() {
		$('#navigation ul.navigation li.subnav').each(
			function() {
				$(this).find('ul').addClass('subnav').appendTo($(this).prev('li'))
				$(this).remove()
			}
		)
		$('#navigation ul.subnav li:first-child').addClass('first')
		$('#navigation ul.subnav li:last-child').addClass('last')
		$('#navigation ul.navigation > li').hover(
			function() {
				if(($(this).offset().left - $(this).parent().offset().left) + $(this).find('ul.subnav').width() > $(this).parent().width()) {
					$(this).find('ul.subnav').css({
						left: 'auto',
						right: '0px'
					})
				}
				$(this).addClass('hover').find('ul.subnav').show()
			},
			function() {
				$(this).removeClass('hover').find('ul.subnav').hide()
			}
		)
	}

	function scrollbarWidth() { 
		var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>'); 
		// Append our div, do our calculation and then remove it 
		$('body').append(div); 
		var w1 = $('div', div).innerWidth(); 
		div.css('overflow-y', 'scroll'); 
		var w2 = $('div', div).innerWidth(); 
		$(div).remove(); 
		return (w1 - w2); 
	}
	
	function forms() {
		var form = $('#questionnaire')
		form.find('.header,.question-element').each(
			function() {
				var highest = 0
				$(this).children().each(
					function() {
						if($(this).height() > highest) {
							highest = $(this).height()
						}
					}
				)
				$(this).height(highest)
			}
		)
		var inputs = $('input[type=text],input[type=password],textarea,select')
		inputs.focus(function(){ $(this).addClass('focus') })
		inputs.blur(function(){ $(this).removeClass('focus') })
	}
	
	function tables() {
		$('table tr').each(function(i){
			var row = $(this)
			if(row.find('th').size() > 1) {
				return
			}
			row.hover(function(){
				if(!jQuery.browser.msie) {
					row.find('td,th').css({
						backgroundColor: 'rgba(255,255,136,0.5)'
					})
				} else {
					row.find('td,th').css({
						backgroundColor: 'rgb(255,255,136)'
					})
				}
			},function(){
				row.find('td,th').css({backgroundColor: ''})
			})
			if(!row.hasClass('even') || !row.hasClass('odd')) {
				if(i % 2) {
					row.addClass('even')
				} else {
					row.addClass('odd')
				}
			}
			$('tr:not(:first-child) th').addClass('left')
		})
	}
	
	function maxWidth() {
		if(!(jQuery.browser.msie && jQuery.browser.version < 7)) {
			return
		}
		var parentWidth = 480
		$('div.page-content img').each(function(){
			var img = $(this)
			if(img.width() > parentWidth) {
				img.css({
					width: parentWidth + 'px',
					height: 'auto'
				})
			}
		})
	}
	
})();