var Local = {

    onCommentReply: function(id) {
		Comments.openReply(id);
	},

    onCancelReply: function(id) {
		Comments.closeReply();
	}

};

(function() {
	var $ = jQuery
	$(document).ready(
		function() {
			$('.yui-u:first-child').addClass('first')
			items()
			leaves()
			heroes()
			footer_fix() // This has to run last
		}
	)

	function heroes(overlay) {
		if(overlay == undefined) {
			var overlay = false
		}
		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()
		if(images.size() < 2) {
			$(images[0]).show()
			return
		}
		if(overlay) {
			container.prepend(
				$(jQuery('<div />'))
					.attr({id: 'hero-fg'})
			)
		}
		images.hide()
		$(images[0]).show().addClass('current')
		if($(images[0]).attr('href') != undefined && overlay) {
			container.find('#hero-fg').bind('click', function(){ window.location = $(images[0]).attr('href') }).css({cursor: 'pointer'})
		}
		setInterval(
			function() {
				if(overlay) {
					container.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 && overlay) {
					container.find('#hero-fg').bind('click', function(){ window.location = to.attr('href') }).css({cursor: 'pointer'})
				}
			},
			fade + show
		)
	}

	function footer_fix() {
		if($('body').outerHeight() < $('html').height()) {
			$('#custom-doc').css({
				paddingBottom: $('html').height() - $('body').outerHeight() + parseInt($('#custom-doc').css('padding-bottom'),10)
			})
		}
	}

	function items() {
		var highest = 0
		var items = $('div.block-resource-center .item')
		var container = $(items[0]).parent()
		container.addClass('yui-g')
		var limit = Math.round(items.size() / 2) // The number of items to have in each column
		var column = $(jQuery('<div />'))
		column.addClass('yui-u').addClass('first')
		container.append(column)
		items.each(
			function(count) {
				$(this)
					.hover(
						function() {
							$(this).addClass('item-hover')
						},
						function() {
							$(this).removeClass('item-hover')
						}
					)
					.click(
						function() {
							window.location = $(this).find('a').attr('href')
						}
					)
				if(count == limit) {
					limit = limit + limit
					column = null
					column = $(jQuery('<div />'))
					column.addClass('yui-u')
					container.append(column)
				}
				column.append($(this))
			}
		)
		items.each(
			function() {
				highest = $(this).height() > highest ? $(this).height() : highest			
			}
		)
		items.height(highest)
	}

	function leaves() {
		var leaf = $('ul#leaves li')
		if(!leaf.size()) { return }
		leaf.find('img').hide()
		leaf.each(
			function(i) {
				$(this).hover(
					function() {
						var random = Math.floor(Math.random() * (leaf.size() - 2))
						$(this).find('img').css({backgroundImage: ($(leaf[random]).css('background-image'))}).fadeIn('300')
					},
					function() {
						$(this).find('img').fadeOut('300')
					}
				)
			}
		)
	}
})();
