try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

function fix_sizes() {
	$('enlarge_back').style.height = document.viewport.getHeight() + 'px';
}

// checks for "maxlength" attribute value of a text input or textarea element and truncates it
function check_max_length(element){
	element.value = element.value.substring(0, parseInt(element.readAttribute("maxlength")));
}

// custom scroll bar variables and functions
	var scroll_pos = 0;
	var case_page = false;
	var scroll_visible = true;
	var scroll_bar, scroller, scroller_container, scroller_body, max_y, increment;

	function scroll_init() {
		max_y = scroller_body.getHeight() - scroller_container.getHeight();
		if (max_y < 0) {
			max_y = 0;
			if (scroll_visible) {
				if (case_page) {
					new Effect.Opacity('scroll_bar', { from: 1, to: .0001 });
				} else {
					new Effect.Opacity('scroll_handle', { from: 1, to: .0001 });
					new Effect.Opacity('scroll_up', { from: 1, to: .0001 });
					new Effect.Opacity('scroll_down', { from: 1, to: .0001 });
				}
				scroll_visible = false;
			}
		} else {
			if (!scroll_visible) {
				if (case_page) {
					new Effect.Opacity('scroll_bar', { from: 0, to: .99 });
				} else {
					new Effect.Opacity('scroll_handle', { from: 0, to: .99 });
					new Effect.Opacity('scroll_up', { from: 0, to: .99 });
					new Effect.Opacity('scroll_down', { from: 0, to: .99 });
				}
				scroll_visible = true;
			}
			increment = max_y / 100;
		}
		//scroller.setValue(0);
	}
	
	function scroll_it(value) {
		scroller_body.style.top = -(Math.round(value * increment)) + 'px';
		scroll_pos = Math.round(value);
	}
	
	// http://adomas.org/javascript-mouse-wheel/
	function wheel_scroll(delta) {
		scroller.setValueBy(-delta);
	}

	function wheel(event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) { 
			delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
		} else if (event.detail) delta = -event.detail/3;
		if (delta) wheel_scroll(delta);
		if (event.preventDefault) event.preventDefault();
		event.returnValue = false;
	}
	
Event.observe(window, "load", function() {

	// check for custom scroller
		if ($('scroll_bar')) {
			scroll_bar = $('scroll_bar');
			scroller_container = scroll_bar.previous('div');
			scroller_body = scroller_container.down('div');
		
			scroller = new Control.Slider('scroll_handle','scroll_track', {
				axis: 'vertical',
				range: $R(0, 100),
				onSlide: function(value) { scroll_it(value); },
				onChange: function(value) { scroll_it(value); }
			});
			
			
			Event.observe('page_content_body', 'DOMMouseScroll', wheel);
			Event.observe('page_content_body', 'mousewheel', wheel);
			
			$('scroll_up').onclick = function() {
				if (scroll_pos >= 5) { 
					scroller.setValue(scroll_pos - 5); 
					scroll_pos -= 5;
				} else {
					scroller.setValue(0);
					scroll_pos = 0;
				}
			}
			$('scroll_down').onclick = function() {
				if (scroll_pos <= 95) { 
					scroller.setValue(scroll_pos + 5); 
					scroll_pos += 5;
				} else {
					scroller.setValue(100);
					scroll_pos = 100;
				}
			}
			
			scroll_init();
		}
		
	// footer item hovers 
	$$('#footer div').each(function(x) {
		x.onmouseover = function() { this.addClassName('hover'); }
		x.onmouseout = function() { this.removeClassName('hover'); }
		x.onclick = function() { document.location.href = this.down('a').readAttribute('href'); }
	});
});
	
Event.observe(document, "dom:loaded", function() {
	// initialize functionality for custom drop down menus
		$$('div.select').each(function(x) {
			x.onclick = function() { 
				if (this.hasClassName('selected')) this.removeClassName('selected');
				else this.addClassName('selected');
			}
		});
		
		$$('div.select li a').each(function(link) {
			$(link).observe('click', function() {
				if (this.readAttribute('href') == "#") {
					if ($(this.parentNode.parentNode).previous('span')) $(this.parentNode.parentNode).previous('span').update(link.innerHTML);
					if ($(this.parentNode.parentNode).previous('input')) $(this.parentNode.parentNode).previous('input').value = this.readAttribute('value');
					
					if ($(this).up('li').readAttribute('state')) $('state').value = $(this).up('li').readAttribute('state');
					return false;
				}
			});
		});
	
	// show nested ul's for sub navigation
		$$('ul#nav li#baby, ul#nav li#mitzvah, ul#nav li#correspondence').each(function(li) {
			li.onmouseover = function() { this.addClassName('selected'); }
			li.onmouseout = function() { this.removeClassName('selected'); }
		});
		
	fix_sizes();
});

// Events and observers
Event.observe(document, "click", hide_dropdowns);

// hide custom select menus when clicked anywhere else
function hide_dropdowns(event) {
	var tag = Event.element(event).tagName;
	if (tag == "A") return false;
	
	if (tag != "LI" && tag != "SPAN" && tag != "UL") {
		$$('div.select').each(function(ul) { ul.removeClassName('selected'); });
	}
}
