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

function max_length_check(element, length) {
	element = $(element);
	if (element.value.length > length) element.value = element.value.substring(0, length);
	$(element.readAttribute('id') + '_count').update(length - element.value.length);
}

Event.observe(document, 'mousemove', function(event) {
	if (($('click_to_edit') && $('click_to_edit').visible()) || ($('enlarge') && $('enlarge').visible()) || ($('help_pop') && $('help_pop').visible())) {
		var mousex, mousey;
		if (event.pageX || event.pageY) {
			mousex = event.pageX;
			mousey = event.pageY;
		} else if (event.clientX || event.clientY) {
			mousex = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mousey = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		if ($('click_to_edit') && $('click_to_edit').visible()) {
			// set the position of the 'click to edit'
			$('click_to_edit').setStyle({
				'top': mousey - 50 + 'px',
				'left': mousex - 65 + 'px'
			});
		}
		
		// set the position of the 'enlarge' icon
		if ($('enlarge') && $('enlarge').visible()) {
			$('enlarge').setStyle({
				'top': mousey - 5 + 'px',
				'left': mousex + 20 + 'px'
			});
		}
		
		// set the position of the help window
		if ($('help_pop') && $('help_pop').visible()) {
			$('help_pop').setStyle({
				'top': mousey - 10 - $('help_pop').getHeight() + 'px',
				'left': mousex - ($('help_pop').getWidth() / 2) + 'px'
			});
		}
	}
});

Event.observe(document, "dom:loaded", function() {
	// sub navigation functionality
	$$('#sub_nav ul li a').each(function(x) {
		if (x.href == 'javascript:;') {
			x.onclick = function() {
				$$('#sub_nav ul li a').each(function(tab) { 
					if (tab.hasClassName('selected')) tab.removeClassName('selected');
					$(tab.up('li').readAttribute('id') + '_content').hide();
				});
				x.addClassName('selected');
				$(x.up('li').readAttribute('id') + '_content').show();
			}
		}
	});
	
	if ($$('.textfield').length) {
		$$('.textfield').each(function(field) {
			field.onfocus = function() {
				if (this.up('.inline_add_edit')) this.up('div').className = 'form_group_selected';
				else this.className = 'textfield_selected';
			};
			field.onblur = function() {
				if (this.up('.inline_add_edit')) this.up('div').className = 'form_group';
				else this.className = 'textfield';
			};
		});	
		if ($$('.textfield')[0].readAttribute('getFocus') == 1) $$('.textfield')[0].focus();
	}
});

function switch_tab(new_tab) {
	$$('#sub_nav ul li a').each(function(tab) { 
		if (tab.hasClassName('selected')) tab.removeClassName('selected');
		$(tab.up('li').readAttribute('id') + '_content').hide();
	});
	new_tab.addClassName('selected');
	$(new_tab.up('li').readAttribute('id') + '_content').show();
}

var PopContent = Class.create({
	speed: .3,
	overlay: undefined,
	content: undefined,
	onOpen: undefined,
	initialize: function(overlay, content, options) {
		this.key_events = this.key_events.bindAsEventListener(this);
		
		if (options) {
			if (options.onOpen) this.onOpen = options.onOpen;
		}
	
		this.content = $(content);
		this.overlay = $(overlay);
		
		this.overlay.observe('click', (function(event) { event.stop(); this.close(); }).bind(this));
	},
	open: function() {
		// set the height of the popup_overlay
		this.overlay.setStyle({ height: this.get_window_height() + "px" });
		// set the left position of the popimg viewer
		this.content.setStyle({ left: (document.viewport.getWidth() / 2) - (this.content.getWidth() / 2) + 'px' });
		
		// fade in the popup_overlay and the popimg container
		new Effect.Appear(this.overlay, { 
			from: 0, 
			to: .84, 
			duration: this.speed,
			afterFinish: (function() {
				new Effect.Appear(this.content, { 
					from: 0, 
					to: 1, 
					duration: this.speed,
					afterFinish: (function() {
						this.enable_keyboard();
						if (this.onOpen) this.onOpen();
					}).bind(this)
				});
				new Effect.ScrollTo(this.content);
			}).bind(this)
		});
	},
	close: function() {
		this.content.hide();
		this.disable_keyboard();
		new Effect.Fade(this.overlay, { duration: this.speed });	
	},
	enable_keyboard: function() { Event.observe(document, "keydown", this.key_events); },
	disable_keyboard: function() { Event.stopObserving(document, "keydown", this.key_events); },
	key_events: function(event) {
		switch (event.keyCode) {
			// esc, 'x', or 'c'
			case 27:
			case 67:
			case 88:
				this.close();
				break;
			default:
				break;
		}
	},
	get_window_height: function() {
		// retrieve the height of the document including scrollable area
		var document_height, window_height;
		
		if (window.innerHeight && window.scrollMaxY) document_height = window.innerHeight + window.scrollMaxY;
		else if (document.body.scrollHeight > document.body.offsetHeight) document_height = document.body.scrollHeight;
		else document_height = document.body.offsetHeight;
		
		if (self.innerHeight) window_height = self.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) window_height = document.documentElement.clientHeight;
		else if (document.body) window_height = document.body.clientHeight;
		
		return (document_height < window_height) ? window_height : document_height;
	}
});