Ajax.InPlaceEditor.prototype.__enterEditMode = Ajax.InPlaceEditor.prototype.enterEditMode;

Object.extend(Ajax.InPlaceEditor.prototype, {
  enterEditMode:function(e) {
  	if (typeof(e) != 'string' || e != 'click') return false;
    this.__enterEditMode();
  }
});


				
var defaultModalWindow = function(elm, options) {
		var w = new Control.Modal(elm, Object.extend({
			className:'modal', 
			fade:false,
			overlayOpacity: 0.3,
			iframe:false
		},options || {})); 
		return w;
}


var window_factory = function(container,options) {  
		
	var window_header = new Element('div',{  
	 className: 'window-header'  
	});  
	
	var window_close = new Element('div',{  
	 className: 'window-close'  
	});  
	var window_title = new Element('div',{  
	 className: 'window-title'  
	});  
	var window_content = new Element('div',{  
	 className: 'window-content'  
	});  
	var window_footer = new Element('div',{  
	 className: 'window-footer'  
	});  
	
	var w = new Control.Window(container,Object.extend({  
	 className: 'window',  
	 closeOnClick: window_close,  
	 draggable: window_header,  
	 //resizable: true,
	 height: 350,
	 width:250,
	 insertRemoteContentAt: window_content,  
	 afterOpen: function(){
	      
	 }
	},options || {}));  
	
	window_title.update('Loading...');
	w.container.insert(window_header);  
	window_header.insert(window_close);  
	window_header.insert(window_title);  
	w.container.insert(window_content); 
	w.container.insert(window_footer); 
	w.container.setStyle({position:'fixed'});
	return w;  
}
		

var RbmExtendedElement = {
		
	samePreviousSiblings: function(element) {
		siblings = new Array;
    	element.previousSiblings().each(function(elm) {
    		if (elm.tagName == element.tagName) siblings.push(elm);
    	});
    	return siblings;
	},
	
	sameNextSiblings: function(element) {
		siblings = new Array;
    	element.nextSiblings().each(function(elm) {
    		if (elm.tagName == element.tagName) siblings.push(elm);
    	});
    	return siblings;
	},
	
	movetop: function(element){
        element = $(element);
    	var parent = element.parentNode;
		var siblings = element.previousSiblings();
		if (siblings) {
			element.remove();
			parent.insertBefore(element, siblings[siblings.length-1]);
        }
        return element;
    },
	
	movebottom: function(element){
        element = $(element);
    	var parent = element.parentNode;
		var siblings = element.nextSiblings();
		if (siblings) {
			element.remove();
			parent.insertBefore(element, siblings[siblings.length]);
        }
        return element;
    }, 
	
	moveup: function(element, count){
        element = $(element);
        if (!count) count = 1;
    	var parent = element.parentNode;
		var siblings = element.samePreviousSiblings();
		if (siblings && siblings.length > count-1) {
			element.remove();
			parent.insertBefore(element, siblings[count-1]);
        }
        return element;
    },
    
	movedown: function(element, count){
        element = $(element);
        if (!count) count = 1;
    	var parent = element.parentNode;
		var siblings = element.sameNextSiblings();
		if (siblings && siblings.length >= count) {
			element.remove();
			parent.insertBefore(element, siblings[count]);
        }
        return element;
    },
    
    canmoveup: function(element, count) {
    	if (!count) count = 1;
    	return ($(element).samePreviousSiblings().length >= count);
    },
    
    canmovedown: function(element, count) {
    	if (!count) count = 1;
    	return ($(element).sameNextSiblings().length >= count);
    },
    
    hasId: function(element) {
		if (element.id) return element.id.match(/^[a-zA-Z]+Item_[0-9]+|_?.*/g);
		return false;
    },
    
    getId: function(element) {
    	if (element.id) {
    		var match = element.id.match(/^[a-zA-Z]+Item_[0-9]+|_?.*/g);
    		if (match) {
    			var all = match[0].split('_');
    			return all[1];
    		}
    	}
    	return false;
    },
    
    copyStyleToTextarea: function(element, source) {
		source = $(source);
		if (source) {
			element.setStyle({
				'fontFamily': source.getStyle('fontFamily'),	
				'color': source.getStyle('color'),		
				'lineHeight': source.getStyle('lineHeight'),		
				'letterSpacing': source.getStyle('letterSpacing'),		
				'margin': source.getStyle('margin'),
				'marginTop': source.getStyle('marginTop'),
				'marginBottom': source.getStyle('marginBottom'),
				'marginLeft': source.getStyle('marginLeft'),
				'marginRight': source.getStyle('marginRight'),
				'padding': source.getStyle('padding'),
				'paddingTop': source.getStyle('paddingTop'),
				'paddingBottom': source.getStyle('paddingBottom'),
				'paddingLeft': source.getStyle('paddingLeft'),
				'paddingRight': source.getStyle('paddingRight'),
				'fontWeight': source.getStyle('fontWeight'),
				'fontStyle': source.getStyle('fontStyle'),
				'background': source.getStyle('background'),
				'textTransform': source.getStyle('textTransform'),
				'textAlign': source.getStyle('textAlign'),
				'top' : '0px',
				'left' : '-1px',
				'position' : 'relative',
				'width' : (element.getWidth()+5)+'px'
			});
		}
		
    }
    
}


Element.addMethods(RbmExtendedElement);
