var activeDragable = null;
var activeHover = null;
var lastActiveHover = null;
var activeOptions = null;

function attachDragEffects() {
	
	//Boxes
	$$('.BoxObject').each(function(box){
		doAttachDragEffects(box.id);
	});
	
	//for the options element
	//Event.observe( $('global_box_options'), 'mouseover', function(e){ log('hi'); } );Event.observe( $('global_box_options'), 'mouseout', function(e){ log('bye'); } );
	
	/*Event.observe( $('global_box_options'), 'mouseover', function(e){ 
		if( activeHover != null ) {
			log( "adding BoxHover to " + activeHover.id );
			activeHover.addClassName('BoxHover');
		} else {
			log( "activeHover: " + activeHover );
		}
	});

	Event.observe( $('global_box_options'), 'mouseout', function(e){ 
		if( activeHover != null ) {
			log( "removing BoxHover to " + activeHover.id );
			activeHover.removeClassName('BoxHover');
		} else {
			log( "activeHover: " + activeHover );
		}
	});*/
	
	/*Event.observe( $('global_box_options'), 'mouseover', function(e){ 
		//if( activeHover ) {
			activeHover.addClassName('BoxHover');
			log( "mouseover on global options" );
		//}
	});
	
	Event.observe( $('global_box_options'), 'mouseout', function(e){ 
		//if( activeHover ) {
			activeHover.removeClassName('BoxHover');
			log( "mouseout on global options" );
		//}
	});*/
	
	//boxHoverOnOptions();
	
}

function doAttachDragEffects(box_id) {
	var box = $(box_id);
	head = $(box.id + "_head");
	//log( "box is: " + box.id + ", head is: " + head.id );
	
	Event.observe( head, 'mousedown', function(e){ 
		hitarea = Event.element(e).id
		if( hitarea.indexOf("_option_li") == -1 && hitarea.indexOf("_edit_li") == -1 && hitarea.indexOf("_delete_li") == -1 ) {
			activeDragable = box;
			$('global_box_options').hide();
			$$('.DropArea').each(function(drop_box){ 
				drop_box.addClassName('Revealed'); 
			});
		}
	});
	
	Event.observe( head, 'mouseup', function(e){ 
		activeDragable = null;
		$$('.DropArea').each(function(drop_box){ 
			drop_box.removeClassName('Revealed'); 
		});
	});
	
	Event.observe( box, 'mouseover', function(e){ 
		activeHover = box;
		box.addClassName('BoxHover');
	});
	
	Event.observe( box, 'mouseout', function(e){ 
		//$$('#global_box_options ul').each(function(child) {
		//$$('#global_box_options ul', '#global_box_options li', '#global_box_options a').each(function(child) {
		//	log( child );
		//});
		
		//Selector.findChildElements($('global_box_options'), ['*']).each(function(child){
		//	log( child.id );
		//	if( Event.element(e).id == child.id ) {
		//		return false;
		//	}
		//});
		
		
		//if( Event.element(e) == $('global_box_options') ) {
		//	log( "element was global_box_options, returing false" );
		//	return false;
		//} else {
			lastActiveHover = activeHover;
			activeHover = null;
			//log( "element was " + Event.element(e).id + ", removing hover class" );
			box.removeClassName('BoxHover');
			
			//TODO uncomment this and make out when it's correct
			//Selector.findChildElements($('global_box_options'), ['*']).each(function(child){
			//	log( child.id );
			//	if( Event.element(e).id == child.id ) {
			//		return false;
			//	}
			//});
			
		//	if( Event.element(e) != $('global_box_options') ) {
				//$('global_box_options').hide();
		//	}
		//}
	});
	
}


function placeGlobalOptions( element ) {
	if( activeOptions == element ) {
		$('global_box_options').hide();
		activeOptions = null;
	} else {
		var point = findPos($(element));
		
		//if( regFind( element, "\menu_box_\d_page_\d\b" ) ) {
		if( element.indexOf("_page_") == -1 ) {
			var top = point[1] - 1;
			//log( "no match" );
		} else {
			var top = point[1] + 40;
			//log( "match" );
		}
		
		var left = point[0] + $(element).getDimensions().width - ($('global_box_options').getDimensions().width + 2);
		//log( "element: " + element + ", box.w: " + $(element).getDimensions().width + ", options.w: " + $('global_box_options').getDimensions().width );
		Element.setStyle($('global_box_options'), {top:top+"px", left:left+"px", display:"block", position:"absolute"});
		activeOptions = element;
	}
}

function boxHoverOnOptions() {
	
}