	
/* extending prototype to have a Element.getInnerText function */
Element.addMethods({  
  getInnerText: function(element) {
    element = $(element);
    return element.innerText && !window.opera ? element.innerText
      : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
  }
});

initMovePortlets = function() {
	var createSideBoxDiv = function( innerElement ){

		/*
			creating this DOM for a box with rounded corners via css styles:
				<div class="box2">
				  <div class="bi2">
	          	    <div class="bt2"><div></div></div>
	                    <p>
							<!-- content here -->
	                    </p> 
	                    <div class="bb2"><div></div></div>
	               </div>
	            </div>
	    */


		var my_div = new Element('div', { 'class': 'box2' });
		
			var div_bi2 = new Element('div', { 'class': 'bi2' });
				
				var div_bt2 = new Element('div', { 'class': 'bt2' });
				div_bt2.appendChild(new Element('div'));
				
			div_bi2.appendChild(div_bt2);
				
				var div_p = new Element('p');
				div_p.appendChild(innerElement);
				
			div_bi2.appendChild(div_p);
			
				var div_bb2 = new Element('div', { 'class': 'bb2' });
				div_bb2.appendChild(new Element('div'));
				
			div_bi2.appendChild(div_bb2);
		
		my_div.appendChild(div_bi2);
		return my_div;
		
	}
	
	//console.debug("moving columns:");
   	var currentColumnCounter = 2;
   	
   	$$(".layoutColumn").each(   // the portal contrains <TABLE class=layoutcolumn>; we'll think that there are only 2 of them.
   		function (columnTable) {
   			//console.debug();
   			Element.extend(columnTable);
   			
   			//console.debug(columnTable);
   			var thing = $(columnTable).remove(); // remove from DOM
   			if (currentColumnCounter == 2)
   			{
   				
   				// Place this in the Central Column, which is column2Portlets:
   				$('column' + currentColumnCounter + 'Portlets').appendChild(thing);
   			}else if (currentColumnCounter == 3)
   			{
   				//console.debug('created dom');
				// insert it in the document
				
				$('column' + currentColumnCounter + 'Portlets').appendChild(thing);
				currentColumnCounter = 2;
   			}
   			currentColumnCounter++;
   		} // function
   	); // .each               



	//var porlets2 = $$("#column3Portlets>table>tbody>tr:nth-child(even)>td:first");
	//down(".wpsPortletBody").getInnerText();

	var porlets2 = $$("#column2Portlets>table>tbody>tr:nth-child(even)>td:first");
	console.debug("porlets2 size = " + porlets2.size());
	porlets2.each(function(item){ console.debug(item.down('.wpsPortletBody').getInnerText()); });

	// and now create boxes around the td's of the right column   	
	// css3 magic: #column3Portlets>table>tbody>tr:nth-child(even)>td:first
   	var rightColumnPortletCount = $$('#column3Portlets>table>tbody>tr:nth-child(even)>td:first').each(
		function (columnTable_td) {
			var wrapper_for_td_contents = new Element('span');
			   						
			columnTable_td.childElements().each(function(td_child){
				var removed_td_child = td_child.remove();
				wrapper_for_td_contents.appendChild(removed_td_child);
				}
			);
			var a_newDiv = createSideBoxDiv(wrapper_for_td_contents); // create a box around the element
		
			$('column3SideDivs').appendChild(a_newDiv); // insert into side bar
		}
	).size();
   	console.debug("moved columns;");
   	console.debug("rightColumnPortletCount = " + rightColumnPortletCount);
   	
   	if (rightColumnPortletCount == 0) {
   		$$('.column3width').invoke('removeClassName','column3width')
   	}
}

