var Annotation = new Object();


Annotation.theOverlay = null;
Annotation.doReloadView = true;

Annotation.getRules = function()
{
	var self = this;
	
	var my_rules = {
		'.cof-annotate-pre-stub' : function(el)
		{
			el.onfocus = function()
			{	
				document.getElementById( 'cof_annotate_cancel' ).focus();
			}
		},

		'.cof-annotate-post-stub' : function(el)
		{
			el.onfocus = function()
			{	
				document.getElementById( 'cof_annotate_save' ).focus();
			}
		},		
				
		'.cof-annotate-view-image-selector' : function(el)
		{
			el.onmouseover = function(e)
			{
				self.showNote(el);
			},
			
			el.onmouseout = function(e)
			{
				self.hideNote(el);
			}
		},
		
		'.cof-annotate-button' : function(el)
		{
			el.onclick = function()
			{
				if( !ContentObjectFactory.ajax )
				{
					var cof_id = ContentObjectFactory.getContentFrameworkId( el );
					var the_framework = ContentObjectFactory.contentobjects[cof_id];
									
					var resource = el.id.slice( el.id.lastIndexOf('_') + 1 );
					var url = the_framework.getUrl() + '/annotate/' + resource;
				
					location.href = url;
				}
				else
				{
					var cof_id = ContentObjectFactory.getContentFrameworkId( el );
					var the_framework = ContentObjectFactory.contentobjects[cof_id];
					var cof_index = the_framework.getContentIndex();
					var the_index = the_framework.getModuleIndexFromElement( el );
							
					self.getOverlay( cof_index, the_index );
				}
			},
			
			el.onkeypress = function(e)
			{
				return ContentObjectFactory.doKeyPress( el, e );
			};	
		}
	};

	return my_rules;
}


Annotation.cancelNotes = function()
{
	if( Annotation.theOverlay )
	{
		var theParent = Annotation.theOverlay.parentNode;
		theParent.removeChild( Annotation.theOverlay );
		Annotation.theOverlay = null;
	}			
}

Annotation.saveNotes = function( cof_index, the_index, notes )
{
	var the_framework = ContentObjectFactory.getFrameworkFromIndex(cof_index);
	var the_module = the_framework.modules[the_index]['module'];
	
	var self = this;
	var cof_i = cof_index;
	var the_i = the_index;
	var params = '&module_id=' + the_module + '&notes=' + notes;
	
	diva.invokeServerMethod(
		the_framework.rest, 'saveannotations', {},
		{
			callback: function(t) { Annotation.reloadView(cof_i, the_i); },
			postBody: params,
			method: 'post'
		}
	);
}

Annotation.reloadView = function( cof_index, the_index )
{
	if( Annotation.theOverlay )
	{
		var theParent = Annotation.theOverlay.parentNode;
		theParent.removeChild( Annotation.theOverlay );
		Annotation.theOverlay = null;
	}			
		
	if( Annotation.doReloadView )
	{
		var the_framework = ContentObjectFactory.getFrameworkFromIndex(cof_index);
		the_framework.hurryUpAndWait( the_index );
		the_framework.loadView( the_index );
	}
}

Annotation.getOverlay = function( cof_index, the_index )
{
	if( Annotation.theOverlay == null )
	{
		var the_framework = ContentObjectFactory.getFrameworkFromIndex(cof_index);
		var the_module = the_framework.modules[the_index]['module'];
		
		var self = this;
		var params = '&urlbase=' + the_framework.url + '&cof_index=' + cof_index + '&module_id=' + the_module + '&the_index=' + the_index;
		
		diva.invokeServerMethod(
			the_framework.rest, 'annotatemodule', {},
			{
				callback: function(t) { self.showOverlay(t, cof_index, the_index); },
				postBody: params,
				method: 'post'
			}
		);
	}
}

Annotation.showOverlay = function( t, cof_index, the_index )
{
 	var the_frame = Builder.node( 'div', { className:'cof-annotation-overlay-frame', style:'z-index: 50;' } );
 	var the_overlay = Builder.node( 'div', { className:'cof-annotation-overlay' } );
 	
	var yScroll = ContentObjectFactory.getScroll();
	if( yScroll < 16 ) yScroll = 16;	
	
 	var the_content = Builder.node( 'div', { style:'position: absolute; width: 100%; height: auto; z-index: 101; top: ' + yScroll + 'px;' } );
	the_content.innerHTML = t.responseText;	
 		
 	the_overlay.appendChild( the_frame );	
 	the_overlay.appendChild( the_content );	

 	var the_page = document.getElementById( 'pagebody' );
 	the_page.appendChild( the_overlay );

 	Element.setStyle( the_content, { 'z-index': '100', position: 'absolute' });
		
 	var cof_i = cof_index;
 	var the_i = the_index;
 	
 	Annotation.theOverlay = the_overlay;
 	
	setTimeout( function() 
	{
		ContentObjectFactory.resizeFrameElement( the_frame );
		var notation = new NotationClass();
		notation.init( 'cof_annotate_images' );	
		notation.storeIndices( cof_i, the_i );
	}, 250 );
}

Annotation.getBorderWidth = function( el )
{
	return 1;
		
//  !!! it would be nice if this worked in safari too
//	if( !ContentObjectFactory.isIE )
//	{
//		return el.currentStyle['borderWidth'];
//	}
//	else
//	{
//		var c = document.defaultView.getComputedStyle(el, '');
//		return c.getPropertyValue('border-top-width');
//	}
}
	
Annotation.updatePos = function( el, t, l, w, h )
{	
	el.style.top = t + 'px';
	el.style.left = l + 'px';
	el.style.width = w + 'px';
	el.style.height = h + 'px';	
}  

Annotation.showNote = function( el )
{
	if( Element.hasClassName )
	{
		while( !Element.hasClassName( el, 'cof-annotate-view-image-selector-base' ) )
			el = el.parentNode;
			
		var container = document.getElementsByClassName( 'cof-view-image-annotation-container', el.parentNode );
		
		if( container.length == 0 )
		{
			var note = document.getElementsByClassName( 'cof-view-image-annotation', el );
			var text = note[0].value;
			
			var container = Builder.node( 'textarea', { className:'cof-view-image-annotation-container', 'readonly': 'readonly', style:'z-index: 100;' } );
			container.value = text;
			
			var etop = parseInt( Element.getStyle( el, 'top' ) );
			var eleft = parseInt( Element.getStyle( el, 'left' ) );
			var dimensions = Element.getDimensions( el );
					
			el.parentNode.appendChild( container );
			
			var textDims = Element.getDimensions( container );
			var imageDims = Element.getDimensions( el.parentNode );
			
			if( eleft + textDims.width > imageDims.width )
				eleft = eleft - (eleft + textDims.width - imageDims.width);
				
			etop = etop + dimensions.height;
			if( etop + textDims.height > imageDims.height )
				etop = etop - (etop + textDims.height - imageDims.height);			
			
			Element.setStyle( container, { position: 'absolute', left: eleft + 'px', top: etop + 'px' } );
		}
	}
}

Annotation.hideNote = function( el )
{
	if( Element.hasClassName )
	{
		while( !Element.hasClassName( el, 'cof-annotate-view-image-selector-base' ) )
			el = el.parentNode;
			
		var container = document.getElementsByClassName( 'cof-view-image-annotation-container', el.parentNode );
		
		if( container.length > 0 )
			el.parentNode.removeChild( container[0] );
	}
}

Annotation.addNote = function( id, text, top, left, height, width )
{
	var resource = id.slice( id.lastIndexOf('_') + 1 );

	var theImage = document.getElementById( id );
	var dimensions = Element.getDimensions( theImage );

  var t = parseInt(dimensions.height * top);
  var l = parseInt(dimensions.width * left);
  var w = parseInt(dimensions.width * width);
  var h = parseInt(dimensions.height * height);	

	var selectorLayer = Builder.node
	('div',
		{
			className:'cof-annotate-view-image-selector cof-annotate-view-image-selector-base',
			style:'z-index: 100;'
		}
	);
	
	var sel_pane  = Builder.node('div',
	{
		className:'cof-annotate-view-image-selector cof-annotate-view-image-pane',
		style:'z-index: 100;'
	});
	
	if( diva.isIE )
		Element.setStyle( sel_pane, { 'background-color': 'white', 'filter': 'alpha(opacity=00)' } );
	
	var sel_frame  = Builder.node
	('div',
		{
			className:'cof-annotate-view-image-selector cof-annotate-view-image-selector-frame',
			style:'z-index: 100;'
		}
	);
	
	var theText = Builder.node
	('input',
		{
			type: 'hidden',
			className: 'cof-view-image-annotation',
			style:'z-index: 150;'
		}
	);
	
	theText.value = text;					

	selectorLayer.appendChild( sel_pane );
	selectorLayer.appendChild( sel_frame );
	selectorLayer.appendChild( theText );
	
	// update main selector div
	this.updatePos( selectorLayer, t, l, w, h );

	// update selector widgets
	var bw = parseInt(this.getBorderWidth(selectorLayer.childNodes[0])) * 2;
	var nw = 0;
	var nh = 0;
	
	if( w - bw < 0 )
		nw = w;
	else
		nw = w - bw;
		
	if( h - bw < 0 )
		nh = h;
	else
		nh = h - bw;

	this.updatePos( selectorLayer.childNodes[0], 0, 0, w, h );
	this.updatePos( selectorLayer.childNodes[1], 0, 0, nw, nh );
	
	theImage.appendChild( selectorLayer );	
}
