var BlockUpdater = Class.create();
BlockUpdater.prototype = {
    initialize: function() {
	//window.alert("Say hello" );
    },
    setBlockId: function(block_id) {
	this.currentBlockId = block_id;
    },
    ajaxUpdate: function(responseXML) {
	//var block_id=responseXML.childNodes[0].getAttribute("blockId");
	//window.alert("block id : "+responseXML);
	var htmlElement=$( this.currentBlockId );
	htmlElement.innerHTML = responseXML;
    }
};
//var ajax_object=Array();
bUpdater = new BlockUpdater();
  var CustomAjaxEngine = Class.create();
  var ajax_lock;
  CustomAjaxEngine.prototype = Object.extend( new Rico.AjaxEngine(), {
    //responseText:,
    registerCallback : function( callback_obj ) {
      this.responser = callback_obj;
    },
    _onRequestComplete : function(request) {
      if(!request)
          return;
      // User can set an onFailure option - which will be called by prototype
      if (request.status != 200)
        return;

      if( request.responseXML )
	var response = request.responseXML.getElementsByTagName("ajax-response");
      else if( request.responseText ) {
        this.responseText = request.responseText;
        this.responser.ajaxUpdate(request.responseText);
      }

      if (response == null || response.length != 1)
         return;
      this._processAjaxResponse( response[0].childNodes );
      
      // Check if user has set a onComplete function
      var onRicoComplete = this.options.onRicoComplete;
      if (onRicoComplete != null)
          onRicoComplete();
   }
  } );
//var c_ajaxEngine = new CustomAjaxEngine();
//var c_ajaxEngine = new Rico.AjaxEngine();
//  c_ajaxEngine.registerRequest( "block_act","/program/family/family_block_ajax.php" );
  var CustomDND = Class.create();
CustomDND.prototype = Object.extend( new Rico.DragAndDrop(), {
    initialize: function() {
	this.dropZones                = new Array();
      this.draggables               = new Array();
      this.currentDragObjects       = new Array();
      this.dragElement              = null;
      this.lastSelectedDraggable    = null;
      this.currentDragObjectVisible = false;
      this.interestedInMotionEvents = false;
      this._mouseDown = this._mouseDownHandler.bindAsEventListener(this);
      this._mouseMove = this._mouseMoveHandler.bindAsEventListener(this);
      this._mouseUp = this._mouseUpHandler.bindAsEventListener(this);
    },
   _mouseMoveHandler: function(e) {
      var nsEvent = e.which != undefined;
      if ( !this.interestedInMotionEvents ) {
         //this._terminateEvent(e);
         return;
      }

      if ( ! this.hasSelection() )
         return;

      if ( ! this.currentDragObjectVisible )
         this._startDrag(e);

      if ( !this.activatedDropZones )
         this._activateRegisteredDropZones();

      //if ( !this.adjustedForDraggableSize )
      //   this._adjustForDraggableSize(e);

      this._updateDraggableLocation(e);
      //this._updateDraggableHover(e);
      //this._updateDropZonesHover(e);

      this._terminateEvent(e);
   },
   _updateDropZonesHover: function(e) {
      var n = this.dropZones.length;
      for ( var i = 0 ; i < n ; i++ ) {
	  /*if ( this.dropZones[i].id == "dropZone4" )
	      if ( ! this._mousePointInDropZoneAll( e, this.dropZones[i] ) ) {
		  this.dropZones[i].hideHover();
		  this.currentDropZone = null;
	      }
	      else*/
	      if ( ! this._mousePointInDropZone( e, this.dropZones[i] ) ) {
		  this.dropZones[i].hideHover();
		  this.currentDropZone = null;
	      }
      }
      
      for ( var i = 0 ; i < n ; i++ ) {
	  /*if ( this.dropZones[i].id =="dropZone4" ) {
	      if ( ! this._mousePointInDropZoneAll( e, this.dropZones[i] ) ) {
		  if ( this.dropZones[i].canAccept(this.currentDragObjects) ) {
		      this.dropZones[i].showHover();
		      this.currentDropZone = this.dropZones[i];
		      //var nullBlockTest = $( "temp_div" );
		      //if( !nullBlockTest )
		      this.appendNullBlock();
		  }
	      }
	      } else {*/
	      if ( this._mousePointInDropZone( e, this.dropZones[i] ) ) {
		  if ( this.dropZones[i].canAccept(this.currentDragObjects) ) {
		      this.dropZones[i].showHover();
		      this.currentDropZone = this.dropZones[i];
		      //var nullBlockTest = $( "temp_div" );
		      //if( !nullBlockTest )
		      this.appendNullBlock();
		  }
	      }
	      //}
      }
   },

    locateDropzone: function( dropzone_id ) {
	var this_dropzone;
	for( var i=0 ; i < this.dropZones.length; i++ ) {
	    if( this.dropZones[i].id == dropzone_id )
		this_dropzone = this.dropZones[i];
	}
	return this_dropzone;
    },

    _onMouseDropZone: function( dropzone_id, e ) {
	//var this_dropzone=this.locateDropzone(dropzone_id);
        if ( this.dragging ) {
	    if( e.currentTarget )
		var this_dropzone=this.locateDropzone(e.currentTarget.id);
	    else {
		if( e.toElement )
		    var event_obj = e.toElement;
		else
		    var event_obj = e.srcElement;
		while( event_obj ) {
		    //window.alert( event_obj.id.substring(0,8) );
		    if( event_obj.id.substring(0,8) == "dropZone" )
			break;
		    event_obj = event_obj.parentNode;
		}
		var this_dropzone = this.locateDropzone( event_obj.id );
	    }
	    if( !this_dropzone ) {
		return false;
	    }
	    if ( this_dropzone.canAccept(this.currentDragObjects) ) {
		this_dropzone.showHover();
		this.currentDropZone = this_dropzone;
		this.appendNullBlock();
	    }
	}
    },

    locateDraggable: function( draggable_id ) {
	var this_draggable;
	for( var i=0 ; i < this.draggables.length; i++ ) {
	    if( this.draggables[i].id == draggable_id )
		this_draggable = this.draggables[i];
	}
	return this_draggable;
    },

    MouseOnDraggable: function( draggable_id,e ) {
	      var t_port = $( "t_port" );
	var not_tail = false;
	      if( e.currentTarget ) {
		  if( e.currentTarget.id.substring(0,11) != "block_whole" )
		      return false;
		  var t_target = e.currentTarget;
	      } else {
		  var event_obj = e.toElement;
		  // find draggble object in IE
		  while( event_obj ) {
		      if( event_obj.id.substring(0,11) == "block_whole" )
			  break;
		      event_obj = event_obj.parentNode;
		  }
		  var t_target = event_obj;
	      }
	if( this.dragging ) {
	    if( this.currentDragId == draggable_id )
		return false;
	    var currentDropzone = this.locateDropzone(t_target.parentNode.id);
	    //t_port.innerHTML = currentDropzone.id;
	    if( !currentDropzone.canAccept(this.currentDragObjects) )
	      return false;
	    var sibling_search = t_target.nextSibling;
	    while( sibling_search ) {
		if( sibling_search.nodeName == "DIV" ) {
		    not_tail=true;
		    break;
		}
		sibling_search = sibling_search.nextSibling;
	    }
	    var this_draggable = this.locateDraggable( draggable_id );
	    var this_draggable_order = Number(this_draggable.block_order);
	    if( e.target )
		var target_obj = e.target;
	    else
		var target_obj = e.srcElement;
	    var put_result;
	    //document.getElementById( "t_port" ).value=t_target.id;
	    if( target_obj.id.substring(0,11) == "block_title" ) {
		//document.getElementById( "t_port" ).value=t_target.parentNode.id+this.currentDragDropzoneId;
		put_result = this_draggable.putNullBlock( this.currentDragWidth, this.currentDragHeight, t_target, this);
		/*if( t_target.parentNode.id == this.currentDragDropzoneId )
		    if( this_draggable_order - 1 < 1 )
			this_draggable_order = 1;
		    else
		    this_draggable_order--;*/
	    } else
		if( not_tail ) {
		    put_result = this_draggable.putNullBlock( this.currentDragWidth, this.currentDragHeight, sibling_search, this );
		    //this_draggable_order++;
		} else {
		    put_result = this.appendNullBlock( t_target );
		    //this_draggable_order = 0;
		}
	    if( put_result ) {
		this.putDragId = this_draggable.id;
	    } else if ( this.currentDropZone ) {
		this.appendNullBlock();
	    }
	    /*var temp_block = document.getElementById("temp_div");
	    if( temp_block.nextSibling ) {
		var next_sibling = temp_block.nextSibling;
		this_draggable_order = 0;
		not_tail = false;
		while( next_sibling != null ) {
		    // Check if put at the end
		    if( next_sibling.nodeName == "DIV" )
			if( next_sibling.className != "blank_row" ) {
			    not_tail = true;
			    break;
			}
		    next_sibling = next_sibling.nextSibling;
		}
	    } else {
		this_draggable_order = 0;
		not_tail = false;
	    }
	    if( not_tail ) {
		// count from "temp_div" up to first DIV
		temp_block = temp_block.previousSibling;
		this_draggable_order = 1;
		while( temp_block != null ) {
		    if( temp_block.nodeName == "DIV" )
			if( temp_block.id.substring(0,11) == "block_whole" )
			    this_draggable_order++;
		    temp_block = temp_block.previousSibling;
		}
	    }
	    this.currentOrder = this_draggable_order;*/
	}
	//document.getElementById( "t_port" ).value=this.currentOrder;
    },

    _updateDraggableHover: function(e) {
      var n = this.draggables.length;
      var put_result;
      for ( var i = 0 ; i < n ; i++ ) {
	  if ( ! this._mousePointInDraggable( e, this.draggables[i], this.currentDragId ) ) {
            this.draggables[i].hideHover();
	    if( this.draggables[i].removeNullBlock( this.putDragId ) )
		this.currentOrder = 0;
	  }
      }

      for ( var i = 0 ; i < n ; i++ ) {
	  if ( this._mousePointInDraggable( e, this.draggables[i], this.currentDragId ) ) {
               this.draggables[i].showHover();
	       put_result = this.draggables[i].putNullBlock( this.currentDragWidth, this.currentDragHeight, this.currentDragId, this );
	       if( put_result ) {
		   this.putDragId = this.draggables[i].id;
		   this.currentOrder = this.draggables[i].block_order;
	       } else if ( this.currentDropZone ) {
		   this.appendNullBlock();
	       }
         }
      }
    },

    appendNullBlock: function() {
	if( !this.currentDropZone )
	    if( arguments.length )
		var htmlElement = arguments[0].parentNode;
	    else
		return false;
	else
	    var htmlElement = this.currentDropZone.getHTMLElement();
	if( !htmlElement )
	    return false;
	var nullBlockTest = $( "temp_div" );
	if( nullBlockTest )
	    if( arguments.length )
		nullBlockTest.parentNode.removeChild(nullBlockTest);
	    else
		if( nullBlockTest.parentNode.id != this.currentDropZone.id )
		    nullBlockTest.parentNode.removeChild(nullBlockTest);
		else
		    return true;

	var NullBlock = document.createElement( "DIV" );
	//NullBlock.style.width = this.currentDragWidth + "px";
	NullBlock.style.width = htmlElement.style.width;
	NullBlock.style.height = this.currentDragHeight + "px";
	NullBlock.style.borderColor = "#FF0000";
	NullBlock.style.borderWidth = "2px";
	NullBlock.style.borderStyle = "dashed";
	NullBlock.id = "temp_div";
	htmlElement.appendChild( NullBlock );
	this.currentOrder = 0;
	return true;
    },

    _placeDraggableInDropZone: function(e) {
        var foundDropZone = false;
	var n = this.dropZones.length;
	for ( var i = 0 ; i < n ; i++ ) {
	    if ( this._mousePointInDropZone( e, this.dropZones[i] ) ) {
		if ( this.dropZones[i].canAccept(this.currentDragObjects) ) {
		    this.dropZones[i].hideHover();
		    var nullBlockTest = $( "temp_div" );
		    if( nullBlockTest == null )
			this.currentOrder = 0;
		    this.dropZones[i].accept(this.currentDragObjects);
		    this.currentDropzone = this.dropZones[i];
		    foundDropZone = true;
		    break;
		}
	    }
	}
	
	return foundDropZone;
    },
    _updateDraggableLocation: function(e) {
        var dragObjectStyle = this.dragElement.style;
	/*dragObjectStyle.left = (e.screenX + this._leftOffset(e)) + "px"
	  dragObjectStyle.top  = (e.screenY + this._topOffset(e)) + "px";*/
	//dragObjectStyle.left = (e.screenX - this.startx + Number( dragObjectStyle.width.substring(0,dragObjectStyle.width.length-2) )*2 ) + "px";
	if( e.pageX ) {
	    dragObjectStyle.left = (e.pageX + 10 ) + "px";
	    dragObjectStyle.top  = (e.pageY + 10 ) + "px";
	} else {
	    dragObjectStyle.left = (e.x + 10 ) + "px";
            dragObjectStyle.top  = (e.y + this._topOffset(e) + 10 ) + "px";
	}
    },
    _startDrag: function(e) {
      var htmlElement;
      for ( var i = 0 ; i < this.currentDragObjects.length ; i++ ) {
         this.currentDragObjects[i].startDrag();
	 this.currentDragId = this.currentDragObjects[i].id;
	 this.currentDragDropzoneId = this.currentDragObjects[i].currentDragDropzoneId;
	 this.currentDragWidth = this.currentDragObjects[i].getWidth();
	 this.currentDragHeight = this.currentDragObjects[i].getHeight();
	 htmlElement = this.currentDragObjects[i].getHTMLElement();
	 //htmlElement.style.left = (e.screenX + 100) + "px";
	 this.dragging = true;
      }

      this._makeDraggableObjectVisible(e);
   },
   _endDrag: function() {
	for ( var i = 0 ; i < this.currentDragObjects.length ; i++ ) {
	    this.currentDragObjects[i].endDrag(this.currentDropzone, this.currentOrder);
	}
	//var currentDragObj = $(this.currentDragId);
	//currentDragObj.UpdateContent();
	//this.UpdateDraggableOrder();
	var n = this.dropZones.length;
	for ( var i = 0 ; i < n ; i++ )
	    this.dropZones[i].hideHover();
	this.dragging = false;
   },

   UpdateDraggableOrder: function() {
       var n = this.draggables.length;
       for ( var i = 0 ; i < n ; i++ ) {
	   this.draggables[i].UpdateOrder();
	   //this.draggables[i].UpdateContent();
       }
   },
    _leftOffset: function(e) {
	      return e.offsetX ? document.body.scrollLeft : 0;
	  },

    _topOffset: function(e) {
	      return e.offsetY ? document.body.scrollTop : 0;
	  },

    _mousePointInDropZone: function( e, dropZone ) {

      var absoluteRect = dropZone.getAbsoluteRect();
      return e.clientX  > absoluteRect.left + this._leftOffset(e) &&
             e.clientX  < absoluteRect.right + this._leftOffset(e) &&
      e.clientY  > absoluteRect.top + this._topOffset(e);//   &&
      //e.clientY  < absoluteRect.bottom + this._topOffset(e);
   },

    _mousePointInDropZoneAll: function( e, dropZone ) {

      var absoluteRect = dropZone.getAbsoluteRect();
      return e.clientX  > absoluteRect.left + this._leftOffset(e) &&
             e.clientX  < absoluteRect.right + this._leftOffset(e) &&
	     e.clientY  > absoluteRect.top + this._topOffset(e) &&
             e.clientY  < absoluteRect.bottom + this._topOffset(e);
   },

    _mousePointInDraggable: function( e, draggable, c_drag_id ) {

      var absoluteRect = draggable.getAbsoluteRect();
      var currentDraggable = $( c_drag_id );
      var currentDraggableY = Number(currentDraggable.style.top.substring(0,currentDraggable.style.top.length-2)) - document.documentElement.scrollTop;
      var currentDraggableX = Number(currentDraggable.style.left.substring(0,currentDraggable.style.left.length-2)) - document.documentElement.scrollLeft;
      //var textBlock = document.getElementsByName( "qfront" );
      //textBlock[0].value = ( currentDraggableY - document.documentElement.scrollTop ) + "," + e.clientY + "," + document.documentElement.scrollTop;
      //textBlock[0].value = currentDraggable.;

      /*return currentDraggableX > absoluteRect.left + this._leftOffset(e) &&
             currentDraggableX < absoluteRect.right + this._leftOffset(e) &&
             currentDraggableY > absoluteRect.top + this._topOffset(e)   &&
             currentDraggableY < absoluteRect.bottom + this._topOffset(e);*/
      return e.clientX  > absoluteRect.left + this._leftOffset(e) &&
             e.clientX  < absoluteRect.right + this._leftOffset(e) &&
             e.clientY  > absoluteRect.top + this._topOffset(e)   &&
             e.clientY  < absoluteRect.bottom + this._topOffset(e);
   },
    stopDraggable: function( draggable_id ) {
	var target_draggable = null;
	for( var i=0;i < this.draggables.length;i++ ) {
	  if ( this.draggables[i] )
	    if ( this.draggables[i].id == draggable_id ) {
	      target_draggable = this.draggables[i];
	      //delete this.draggables[i];
	      break;
            }
        }
	if ( target_draggable ) {
	  var htmlElement = target_draggable.getMouseDownHTMLElement();
	  Event.stopObserving( htmlElement, "mousedown",this._onmousedown );
	  Event.stopObserving( htmlElement, "mousedown",this._mouseDown );
	}
    },
    resumeDraggable: function( draggable_id ) {
	var target_draggable = null;
	for( var i=0;i < this.draggables.length;i++ ) {
	  if ( this.draggables[i].id == draggable_id ) {
	    target_draggable = this.draggables[i];
	    break;
          }
        }
	if ( target_draggable ) {
	  this.registerDraggable( target_draggable );
	  /*var htmlElement = target_draggable.getMouseDownHTMLElement();
	  Event.observe( htmlElement, "mousedown",this._onmousedown );
	  Event.observe( htmlElement, "mousedown",this._mouseDown );*/
	}
    }
  } );
  var c_dndMgr = new CustomDND();
  c_dndMgr.initializeEventHandlers();

  var CustomDraggable = Class.create();
CustomDraggable.prototype = Object.extend( new Rico.Draggable(), {
	  initialize: function( type, htmlElement, draggable_id, block_order, block_owner, owner_class, allow_dropzone ) {
      this.type          = type;
      this.htmlElement   = $(htmlElement);
      this.id = draggable_id;
      this.currentDragDropzoneId = this.htmlElement.parentNode.id;
      this.block_order = block_order;
      this.block_owner = block_owner;
      this.owner_class = owner_class;
      this.allow_dropzone = allow_dropzone;
      this.selected      = false;
    },
   getHTMLElement: function() {
      return this.htmlElement;
   },
    
    getAbsoluteRect: function() {
      if ( this.absoluteRect == null ) {
         var htmlElement = this.getHTMLElement();
         var pos = RicoUtil.toViewportPosition(htmlElement);

         this.absoluteRect = {
            top:    pos.y,
            left:   pos.x,
            bottom: pos.y + htmlElement.offsetHeight,
            right:  pos.x + htmlElement.offsetWidth
         };
      }
      return this.absoluteRect;
    },
    showHover: function() {
      var htmlElement = this.getHTMLElement();
      if ( htmlElement == null || this.showingHover )
         return;

      this.saveBorderWidth = htmlElement.style.borderWidth;
      this.saveBorderStyle = htmlElement.style.borderStyle;
      this.saveBorderColor = htmlElement.style.borderColor;

      this.showingHover = true;
      //htmlElement.style.borderWidth = "3px";
      //htmlElement.style.borderStyle = "solid";
      //htmlElement.style.height = 100%;
      //htmlElement.style.borderColor = "#ff9900";
      //htmlElement.style.borderColor = "#ffffff";
    },
    hideHover: function() {
      var htmlElement = this.getHTMLElement();
      if ( htmlElement == null || !this.showingHover )
         return;

      htmlElement.style.borderWidth = this.saveBorderWidth;
      htmlElement.style.borderStyle = this.saveBorderStyle;
      htmlElement.style.borderColor = this.saveBorderColor;
      this.showingHover = false;
    },
    startDrag: function() {
      var htmlElement = this.getHTMLElement();
      this.block_width = htmlElement.clientWidth;
      this.block_height = htmlElement.clientHeight;
    },
    getWidth: function() {
	      return this.block_width;
    },
    getHeight: function() {
	      return this.block_height;
    },
	  putNullBlock: function(width, height, dragging_obj, dnd_obj) {
	      // put null before dragging_obj
      var htmlElement = $( this.id );
      var nullBlockTest = $( "temp_div" );
      
      if( nullBlockTest )
	  nullBlockTest.parentNode.removeChild(nullBlockTest);
      //if ( dragging_obj.id != this.id ) {
      var NullBlock = document.createElement( "DIV" );
      
      NullBlock.style.width = htmlElement.style.width;
      NullBlock.style.height = height+"px";
      NullBlock.style.borderColor = "#FF0000";
      NullBlock.style.borderWidth = "2px";
      NullBlock.style.borderStyle = "dashed";
      NullBlock.id = "temp_div";
      if( dragging_obj.parentNode ) {
	  var parent_obj = dragging_obj.parentNode;
	  var while_stop = false;
	  // to make sure that we are at the right DIV
	  while( !while_stop && ( parent_obj != null ) ) {
	      if( parent_obj.nodeName == "DIV" )
		  if( parent_obj.id.substring(0,8) != "dropZone" )
		      parent_obj = parent_obj.parentNode;
		  else
		      while_stop = true;
	      else
		  if( parent_obj == null )
		      return false;
		  else
		      parent_obj = parent_obj.parentNode;
	  }
	  if( parent_obj == null )
	      return false;
	  //dragging_obj.parentNode.insertBefore( NullBlock, dragging_obj );
	  parent_obj.insertBefore( NullBlock, dragging_obj );
	  var temp_block = NullBlock.previousSibling;
	  // the following commented out lines are for debug using
	  //var temp_show = document.getElementById( "tempshow" );
	  //var firstPSibling = temp_block;
	  //var count_hist = "";
	  var this_draggable_order = 1;
	  while( temp_block != null ) {
	      if( temp_block.nodeName == "DIV" )
		  if( temp_block.id.substring(0,11) == "block_whole" ) {
		      if( temp_block.id != dnd_obj.currentDragId )
			  this_draggable_order++;
		      //count_hist += ","+temp_block.id;
		  }
	      temp_block = temp_block.previousSibling;
	  }
	  dnd_obj.currentOrder = this_draggable_order;
	  //if( temp_show )
	  //    temp_show.value = this_draggable_order+","+this.id+","+dragging_obj.id+count_hist;
	  //window.alert( this_draggable_order );
      } else
	  return false;
	//htmlElement.appendChild( NullBlock );
	return true;
      //}
      //return false;
    },
    removeNullBlock: function( put_id ) {
      var htmlElement = $( "temp_div" );
      //var htmlElement=document.getElementById("temp_div");
      window.alert(put_id);
      if( htmlElement )
	  if( this.id != put_id ) {
	      //htmlElement.parentNode.style.visibility="hidden";
	      htmlElement.parentNode.removeChild(htmlElement) ;
	      return true;
	  }
      return false;
    },
      removeNullBlock2: function( null_block ) {
	      null_block.parentNode.removeChild(null_block);
	  },
    endDrag: function(dropzone_obj, c_order) {
	var ajax_object=ajax_init_object();
	var target_uri="/program/family/family_block_ajax.php";
	var uri_get_parameter="ed_type=mv_block&"+this.owner_class+"="+this.block_owner+"&block_id=" + this.id.substring(11)+"&new_pos=" + dropzone_obj.drop_pos+"&new_order=" + c_order;
	//var uri_get_parameter="ed_type=mv_block&"+this.owner_class+"="+this.block_owner+"&block_id=" + this.id.substring(11)+"&new_pos=" + dropzone_obj.drop_pos+"&new_order=" + new_order;
	ajax_object.open("GET",target_uri+"?"+uri_get_parameter,true);
	ajax_object.setRequestHeader('User-Agent','XMLHTTP/1.0');
	ajax_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax_object.onreadystatechange = function() {
	    if (ajax_object.readyState != 4)
		return;
	    if( ajax_object.responseXML ) {
		var responseObject = ajax_object.responseXML.getElementsByTagName("blockcontent");
		if( responseObject.length ) {
		    var block_id = ajax_object.responseXML.getElementsByTagName("blockid")[0].getAttribute("id");
		    //window.alert(responseObject[0].text);
		    if( responseObject[0].textContent )
			var contentHTML = responseObject[0].textContent;
		    else
			var contentHTML = responseObject[0].text;
		    //var target_block = document.getElementById( block_id );
		    var target_block = $( block_id );
		    if( contentHTML )
			target_block.innerHTML = contentHTML;
		} else {
		    var fail_id = ajax_object.responseXML.getElementsByTagName("failblockid")[0].getAttribute("id");
		    var body_obj = document.getElementById(fail_id);
		    var error_msg = ajax_object.responseXML.getElementsByTagName("blockbody" );
		    body_obj.parentNode.removeChild( body_obj );
		    if( error_msg[0].textContent )
			window.alert(error_msg[0].textContent);
		    else
			window.alert(error_msg[0].text);
                    return false;
		}
		var block_order_array = ajax_object.responseXML.getElementsByTagName("blockorder");
		var new_order;
		for( var i=0; i < block_order_array.length;i++ ) {
		    target_block = $( block_order_array[i].getAttribute("id") );
		    target_block.block_order = block_order_array[i].getAttribute("value");
		}
	    } else
		window.alert("No object!!");
	}
	ajax_object.send(null);
    },
    UpdateOrder: function() {
	var ajax_object=ajax_init_object();
	var target_uri="/program/family/family_block_ajax.php";
	var uri_get_parameter="ed_type=get_order&"+this.owner_class+"="+this.block_owner+"&block_id=" + this.id.substring(11);
	ajax_object.open("GET",target_uri+"?"+uri_get_parameter,true);
        ajax_object.setRequestHeader('User-Agent','XMLHTTP/1.0');
        ajax_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax_object.onreadystatechange = function() {
            if (ajax_object.readyState != 4)
                return;
            //nedit_obj.innerHTML=ajax_object.responseText;                                             
            if( ajax_object.responseXML ) {
		//window.alert(ajax_object.responseText);
                var responseObject = ajax_object.responseXML.getElementsByTagName("blockorder");
                var block_id = ajax_object.responseXML.getElementsByTagName("blockid")[0].getAttribute("id");
		var new_order = responseObject[0].getAttribute("value");
		window.alert( "new order : "+new_order );
                var target_block = $( block_id );
                target_block.block_order=new_order;
            } else
                window.alert("No object!!");
        }
        ajax_object.send(null);

    },
    ajaxUpdate: function(ajaxResponse) {
	      //this.id = c_ajaxEngine.responseText ;
	if( c_ajaxEngine.responseText.length ) {
	    if( c_ajaxEngine.responseText.length < 4 ) {
		//window.alert(c_ajaxEngine.responseText);
		this.block_order=c_ajaxEngine.responseText;
	    }/* else {
		var htmlElement=this.getHTMLElement();
		htmlElement.innerHTML=c_ajaxEngine.responseText;
		}*/
	}
      //window.alert( "New ID : "+ this.id );
      //this.renewId(ajaxResponse.childNodes[0]);
    },
    renewId: function(newid_obj) {
      if( newid_obj.getAttribute("newid") ) {
	window.alert( "New ID : "+getAttribute("newid") )
        this.id = newid_obj.getAttribute("newid");
      }
    }
  } );
  var CustomDropzone = Class.create();
CustomDropzone.prototype = Object.extend( new Rico.Dropzone(), {
	  initialize: function( htmlElement,dropzone_id,dropzone_pos ) {
      this.htmlElement  = $(htmlElement);
      this.id = dropzone_id;
      this.drop_pos = dropzone_pos;
      this.absoluteRect = null;
    },
    activate: function() {
      var htmlElement = this.getHTMLElement();
      if (htmlElement == null  || this.showingActive)
         return;

      this.showingActive = true;
      this.saveBackgroundColor = htmlElement.style.backgroundColor;

    },
    canAccept: function(draggableObjects) {
      var zone_name_match=eval("/"+this.id+"/");
      if( draggableObjects[0].allow_dropzone.match(zone_name_match) )
	  return true;
      else
	  return false;
    },
    showHover: function() {
      var htmlElement = this.getHTMLElement();
      if ( htmlElement == null || this.showingHover )
         return;

      this.saveBorderWidth = htmlElement.style.borderWidth;
      this.saveBorderStyle = htmlElement.style.borderStyle;
      this.saveBorderColor = htmlElement.style.borderColor;

      this.showingHover = true;
      htmlElement.style.borderWidth = "2px";
      htmlElement.style.borderStyle = "solid";
      //htmlElement.style.height = 100%;
      //htmlElement.style.borderColor = "#ff9900";
      htmlElement.style.borderColor = "#ffff00";
    },
    accept: function(draggableObjects) {
      var htmlElement = this.getHTMLElement();
      //var tempElement = $( "temp_div" );
      var tempElement = document.getElementById( "temp_div" );
      if ( htmlElement == null )
         return;

      n = draggableObjects.length;
      for ( var i = 0 ; i < n ; i++ )
      {
         var theGUI = draggableObjects[i].getDroppedGUI();
         if ( RicoUtil.getElementsComputedStyle( theGUI, "position" ) == "absolute" )
         {
            theGUI.style.position = "static";
            theGUI.style.top = "";
            theGUI.style.top = "";
	    dropzone_str = this.id ;
         }
	 theGUI.style.width = htmlElement.style.width;
	 theGUI.currentDragDropzoneId = this.id;
	 if( document.all ) {
	     // for IE compatibility
	     if( tempElement ) {
		 var sibling_search = tempElement.nextSibling;
		 var is_tail = true;
		 while( sibling_search ) {
		     //window.alert( sibling_search.nodeName );
		     if( sibling_search.nodeName != "#text" && sibling_search.id != "temp_div" ) {
			 is_tail = false;
			 break;
		     }
		     sibling_search = sibling_search.nextSibling;
		 }
		 //window.alert( theGUI.id+" "+tempElement.id+" "+sibling_search.id );
		 tempElement.parentNode.removeChild( tempElement );
		 if( is_tail )
		     htmlElement.appendChild(theGUI);
		 else {
		     sibling_search.parentNode.insertBefore( theGUI, sibling_search );
		 }
	     } else
		 htmlElement.appendChild(theGUI);
	     //window.alert( theGUI.nodeName );
	 } else
	     if( tempElement )
		 htmlElement.replaceChild( theGUI,tempElement );
	     else
		 htmlElement.appendChild(theGUI);
      }
    }
  } );
