var ArticleDropzone = Class.create();

ArticleDropzone.prototype = (new Rico.Dropzone()).extend( {

   initialize: function( htmlElement, header, name, type, position ) {
      //this.htmlElement  = $(htmlElement);
      this.htmlElement = htmlElement;
      this.header       = $(header);
      this.name = name;
      this.type = type;
      this.refresh = true;
      this.absoluteRect = null;
      this.acceptedObjects = [];
      this.position = position;
      this.lastBackgroundColor = "#FFF";

      this.offset = navigator.userAgent.toLowerCase().indexOf("msie") >= 0 ? 0 : 1;
   },
   getHTMLElement: function() {
      //return this.htmlElement;
      return $(this.htmlElement);
   },

   activate: function() {
      new Rico.Effect.FadeTo( this.getHTMLElement(), .5, 250, 4 );
   },

   deactivate: function() {
      new Rico.Effect.FadeTo( this.getHTMLElement(), 1, 250, 4 );
   },

   showHover: function() {
      if ( this.showingHover )
         return;
	   var tElem = this.getHTMLElement().firstChild;
	   this.lastBackgroundColor = tElem.style.backgroundColor;
      this.getHTMLElement().firstChild.style.backgroundColor = "#FF0000";
      this.header.style.color = "#000000";
      //new Rico.Effect.FadeTo( this.getHTMLElement(), .1, 250, 4 );
      this.showingHover = true;
   },

   hideHover: function() {
      if ( !this.showingHover )
         return;
	   this.getHTMLElement().firstChild.style.backgroundColor = this.lastBackgroundColor;
      this.header.style.color = "#5b5b5b";
      //new Rico.Effect.FadeTo( this.getHTMLElement(), .5, 250, 4 );
      this.showingHover = false;
   },

   accept: function(draggableObjects) {
   		//alert('insert start');
   		
		if (true) {
      		n = draggableObjects.length;
      		for ( var i = 0 ; i < n ; i++ ) {
	    			//this.log("accept: [" + draggableObjects[i].name +"] for " + this.name +"]");
         		this._insertSorted(draggableObjects[i]);
         	}
         }
         //alert('insert');
         //      this.getHTMLElement().insertBefore( aDraggable.getDroppedGUI(), this.getHTMLElement() );
         
   },

   log: function(str) {
      new Insertion.Bottom( $('logger'), "<span class='logMsg'>" + str + "</span>" );
      $('logger').scrollTop = $('logger').lastChild.offsetTop;

   },

   canAccept: function(draggableObjects) {
      //this.log("canAccept " + draggableObjects.length);
      for ( var i = 0 ; i < draggableObjects.length ; i++ ) {
         //if ( draggableObjects[i].type != "Article" ) {
         if ( draggableObjects[i].type != this.type ) {
            //this.log("Cannot Insert " + draggableObjects[i].type);
            return false;
         }
         if ( draggableObjects[i].name == this.name ) {
            //this.log("Cannot Insert on self " + draggableObjects[i].name);
            return false;
         }
         //var firstChar = draggableObjects[i].name.substring(0,1).toLowerCase();
         //if ( firstChar < this.from || firstChar > this.to )
         //   return false;
         //TODO check if we can accept the article
      }

      //this.log("CanInsert " + draggableObjects[0].name);
      return true;
   },

	_insertSorted: function( aDraggable ) {
		var tDragId = aDraggable.name.replace(aDraggable.type + '_', '');
		var tDropId = this.name.replace(this.type + '_', '');
		//alert(tDragId + ' : ' + tDropId);
		//var tDragPosn = getArticlePosition(aDraggable.name);
		//var tDropPosn = getArticlePosition(this.name);
		if (this.refresh) {
			gScrollPosn = document.getElementById("contentInstance").scrollTop;
			showAjaxWaitDialog(true);
			gNewsletter.moveArticleRender(tDragId, tDropId);
			return;
		}
		var tResult = gNewsletter.moveArticle(tDragId, tDropId);
      if (true != tResult) {
         //this.log("Result is not true -" + tResult);
         return;
      }
      //TODO is this ok with direct element or do we really need .parentNode.parentNode
      //var theGUI = aDraggable.getDroppedGUI().parentNode.parentNode;
      var theGUI = aDraggable.getDroppedGUI();
      var tDragArticleId = theGUI.getAttribute("id").substr(5);
      //alert("Drag Id: " + tDragArticleId);
      theGUI = document.getElementById(tDragArticleId);
      //this.log("Moving " + theGUI.getAttribute("id"));
      //var thisName = aDraggable.name;
      //var position = this._getInsertPosition(aDraggable);
      //if ( position == "append" )
      //   this.getHTMLElement().appendChild(theGUI);
      //else
         //this.getHTMLElement().insertBefore( theGUI, this.getHTMLElement().childNodes[position+this.offset]  );
      //alert('Insert');
      var tHtmlElement = this.getHTMLElement();
      var tChildNodes = tHtmlElement.parentNode.childNodes;
      var tDropPos = -1;
      var tDragPos = -1;
      var tDragId = theGUI.getAttribute("id");
      var tDropId = tHtmlElement.getAttribute("id");
      for ( var i = 0 ; i < tChildNodes.length ; i++ ) {
      	//var tNode = tChildNodes[i];
      	//this.log("NodeType: " + tChildNodes[i].nodeType);
      	if (1 == tChildNodes[i].nodeType) {
	        var tNodeId = tChildNodes[i].getAttribute("id");
    	  	if (tNodeId == tDragId) {
      			tDragPos = i;
	      	}
    	  	if (tNodeId == tDropId) {
      			tDropPos = i;
      		}
      	}
      }
      //this.log("Drag " + tDragPos + " Drop " + tDropPos);

      if (tDropPos < tDragPos) {
	    var tInsertNode = tHtmlElement.parentNode.removeChild(theGUI);
      	tHtmlElement.parentNode.insertBefore( tInsertNode, tHtmlElement );
      } else {
      	var tInsertNode = tHtmlElement.parentNode.removeChild(theGUI);
      	var tNextChild = tHtmlElement.nextSibling;
      	if (null == tNextChild) {
        tHtmlElement.parentNode.appendChild( tInsertNode );
      	} else {
        tHtmlElement.parentNode.insertBefore( tInsertNode, tNextChild );
      	}
      }
      
      //alert('Insert End');
      //this.acceptedObjects[ this.acceptedObjects.length ] = aDraggable.name;
      //this.acceptedObjects.sort();
   },

   _getInsertPosition: function(aDraggable) {
      for( var i = 0 ; i < this.acceptedObjects.length ; i++ )
         if ( aDraggable.name < this.acceptedObjects[i] ) {
            return i;
         }
       return "append";
   }

} );
