var LayoutControl = new Object();

LayoutControl.layoutHeader = 'cof_image_layout_';

LayoutControl.setLayout = function( cof_index, the_index, new_layout )
{
	var a_layout_it = document.getElementById( this.layoutHeader + 'it_' + cof_index + '_' + the_index );
	var a_layout_ib = document.getElementById( this.layoutHeader + 'ib_' + cof_index + '_' + the_index );
	var a_layout_il = document.getElementById( this.layoutHeader + 'il_' + cof_index + '_' + the_index );
	var a_layout_ir = document.getElementById( this.layoutHeader + 'ir_' + cof_index + '_' + the_index );

	
	if( Element.hasClassName(  a_layout_it, 'cof-image-layout-selected' ) )
		Element.removeClassName( a_layout_it, 'cof-image-layout-selected' ); 
		
	if( Element.hasClassName(  a_layout_ib, 'cof-image-layout-selected' ) )
		Element.removeClassName( a_layout_ib, 'cof-image-layout-selected' ); 		
		
	if( Element.hasClassName(  a_layout_il, 'cof-image-layout-selected' ) )
		Element.removeClassName( a_layout_il, 'cof-image-layout-selected' ); 	
		
	if( Element.hasClassName(  a_layout_ir, 'cof-image-layout-selected' ) )
		Element.removeClassName( a_layout_ir, 'cof-image-layout-selected' ); 
		
	switch( new_layout )
	{
		case 'IT':
				Element.addClassName( a_layout_it, 'cof-image-layout-selected' ); 
			break;
		
		case 'IB':
				Element.addClassName( a_layout_ib, 'cof-image-layout-selected' ); 
			break;		
			
		case 'IL':
				Element.addClassName( a_layout_il, 'cof-image-layout-selected' ); 
			break;			
			
		case 'IR':
				Element.addClassName( a_layout_ir, 'cof-image-layout-selected' ); 
			break;									
	}				
	
	var the_layout = document.getElementById( this.layoutHeader + cof_index + '_' + the_index );

	the_layout.value = new_layout;
}


LayoutControl.getRules = function()
{
	var self = this;
	var my_rules = {
		
		'.cof-image-layout-it' : function(el)
		{
			el.onclick = function()
			{
				var cof_id = ContentObjectFactory.getContentFrameworkId( el );
				var the_framework = ContentObjectFactory.contentobjects[cof_id];
				var the_index = the_framework.getModuleIndexFromElement( el );
				var cof_index = the_framework.getContentIndex();
					
				self.setLayout( cof_index, the_index, 'IT' );
				ContentObjectFactory.onChange( el );
				return false;
			},
			
			el.onkeypress = function(e)
			{
				return ContentObjectFactory.doKeyPress( el, e );
			};					
		},
		
		'.cof-image-layout-ib' : function(el)
		{
			el.onclick = function()
			{
				var cof_id = ContentObjectFactory.getContentFrameworkId( el );
				var the_framework = ContentObjectFactory.contentobjects[cof_id];
				var the_index = the_framework.getModuleIndexFromElement( el );
				var cof_index = the_framework.getContentIndex();
					
				self.setLayout( cof_index, the_index, 'IB' );
				ContentObjectFactory.onChange( el );
				return false;
			},
			
			el.onkeypress = function(e)
			{
				return ContentObjectFactory.doKeyPress( el, e );
			};		
		},	
		
		'.cof-image-layout-il' : function(el)
		{
			el.onclick = function()
			{
				var cof_id = ContentObjectFactory.getContentFrameworkId( el );
				var the_framework = ContentObjectFactory.contentobjects[cof_id];
				var the_index = the_framework.getModuleIndexFromElement( el );
				var cof_index = the_framework.getContentIndex();
					
				self.setLayout( cof_index, the_index, 'IL' );
				ContentObjectFactory.onChange( el );
				return false;
			},
			
			el.onkeypress = function(e)
			{
				return ContentObjectFactory.doKeyPress( el, e );
			};		
		},				
		
		'.cof-image-layout-ir' : function(el)
		{
			el.onclick = function()
			{
				var cof_id = ContentObjectFactory.getContentFrameworkId( el );
				var the_framework = ContentObjectFactory.contentobjects[cof_id];
				var the_index = the_framework.getModuleIndexFromElement( el );
				var cof_index = the_framework.getContentIndex();
					
				self.setLayout( cof_index, the_index, 'IR' );
				ContentObjectFactory.onChange( el );
				return false;
			},
			
			el.onkeypress = function(e)
			{
				return ContentObjectFactory.doKeyPress( el, e );
			};		
		}
	};
	
	return my_rules;
}