/**
 * Behaviours
 */
Behaviours  = {
	'BODY': function( obj ) {
		var dimensions = document.viewport.getDimensions();
		if( dimensions.width >= 1024 )
			$(obj).addClassName( 'widescreen' );
		else
			$(obj).addClassName( 'smallscreen' );
	},
	'A.confirm': function( obj ) {
		// Remove default handler
		obj.onclick = null;
		
		// Add new confirmation handler
		Event.observe( obj, 'click', function( e ) {
			if( !confirm(this.attributes['cw:confirm'].value) ) {
				e.stop();
			}
		});
	},
	'#main-menu LI A': function( obj ) {
		Event.observe( obj, 'mouseover', MainMenu.mouseOver );
		Event.observe( obj, 'mouseout', MainMenu.mouseOut );
	},
	'DIV.hover_target': function( obj ) {
		Event.observe( obj, 'mouseover', Hover.over );
		Event.observe( obj, 'mouseout', Hover.out );
	},
	'TABLE.index': function( obj ) {
		var form = obj.up( 'FORM', 0 );
		
		if( !form )
			return;

		// Setup togglers
		form.select( 'SPAN.togglers' ).each( function( obj ) {
			var links = obj.select( 'A' );
			Event.observe( links[0], 'click', function() { Index.selectAll( form ); } );
			Event.observe( links[1], 'click', function() { Index.selectNone( form ); } );
		});
		
		// Setup delete action
		form.select( 'DIV.selector-actions A.delete' ).each( function(obj) {
			Event.observe( obj, 'click', function( e ) {
				if( e.stopped ) return;
				form.submit();
				return false;
			});
		});
		
		// Setup selectors
		form.select( 'TD.selector INPUT' ).each( function(obj) {
			Event.observe( obj, 'click', function() { Index.click(obj); } );
		});
		
		// Update view
		Index.updateView( form );
	},
	'INPUT.search': function( obj ) {
		Event.observe( obj, 'keydown', function( event ) {
			if( event.keyCode == Event.KEY_RETURN ) {
				obj.form.submit();
			}
		});
	},
	'#modal-container': function( obj ) {
		centerElement( obj );
		Event.observe( window, 'resize', function() {
			centerElement( obj );
		});
	}
};

function __init() {
	applyBehaviours();
	//Wicket.Ajax.registerPostCallHandler( applyBehaviours );
	
	if( Wicket != undefined && Wicket.Ajax != undefined )	{
		Wicket.Ajax.registerPreCallHandler(function() {
			$(document.body).addClassName( 'ajax-running' );
		});
		Wicket.Ajax.registerPostCallHandler(function() {
			$(document.body).removeClassName( 'ajax-running' );
		});
	}
}

function wicketGlobalPostCallHandler() {
	applyBehaviours();
}

function applyBehaviours() {
	$H(Behaviours).each( function(pair) {
		$$(pair.key).each( function(obj) {
			// Do no apply a behaviour more than once
			if( obj.__behaviours && obj.__behaviours[pair.key] ) {
				// alert( 'skipping behaviour ' + pair.key );
				return;
			}
			pair.value( obj );
			
			if( !obj.__behaviours )
				obj.__behaviours = {};
			
			obj.__behaviours[pair.key] = true;
		} );
	});
}

Event.observe( document, 'dom:loaded', __init );

/**
 * Main menu functions
 */
MainMenu = {
	mouseOver: function( event ) {
		$(this.parentNode).addClassName('hover');
	},
	mouseOut: function( event ) {
		$(this.parentNode).removeClassName('hover');
	}
};

/** 
 * Index functions
 */
Index = {
	click: function( selector ) {
		Index.updateView( selector.up('FORM', 0) );
	},
	selectAll: function( obj ) {
		obj	= $(obj);
		obj.select( 'TD.selector INPUT' ).each( function(selector) {
			selector.checked = true;
			Index.click( selector );
		});
	},
	selectNone: function( obj ) {
		obj	= $(obj);
		obj.select( 'TD.selector INPUT' ).each( function(selector) {
			selector.checked = false;
			Index.click( selector );
		});
	},
	updateView: function( obj ) {
		obj = $(obj);
		var selected = 0;
		obj.select( 'TD.selector INPUT' ).each(function(selector) {
			if( selector.checked ) {
				$(selector).up('TR',0).addClassName('selected');
				selected++;
			} else {
				$(selector).up('TR',0).removeClassName('selected');
			}
		});
		
		obj.select('DIV.selector-actions').each(function(element) {
			if( selected == 0 )
				element.hide();
			else
				element.show();
		});
	}
};

/**
 * Hover functions
 */
Hover = {
	active: null,
	over: function( event ) {
		if( Hover.active != null && Hover.active.src != this ) {
			Hover.out.call( Hover.active.src, null, true );
		}
		if( this.attributes['rb:hover'] != undefined ) {
			var target	= $(this.getAttribute('rb:hover'));
			if( target ) {
				if( this.hideTimer ) {
					clearTimeout( this.hideTimer );
					this.hideTimer = null;
				}
				//Element.show( target );
				target.style.display = 'inline';
				Hover.active = { target: target, src: this };
			}
		}
	},
	out: function( event, nodelay ) {
		if( this.attributes['rb:hover'] != undefined ) {
			var target	= $(this.getAttribute('rb:hover'));
			if( target ) {
				if( nodelay ) {
					Element.hide( target );
					Hover.active = null;
				}
				else
					this.hideTimer = function() {
						Element.hide( target );
						if( Hover.active && Hover.active.target == target )
							Hover.active = null;
					}.delay( 1.5 );
			}
		}
	}
};

function centerElement( obj ) {
	var windowHeight = document.viewport.getHeight();
	var containerHeight = obj.getHeight();
	var marginTop = Math.round( (windowHeight / 2) - (containerHeight / 2) );
	if( marginTop > 0 )
		obj.style.marginTop = marginTop + 'px';
}
