/*
---
description: MooTools Helpers

license: MIT-style

authors:
- Luzius Kloeti

requires:
core/1.3.1: [ Options, Events ]

provides: [ Helper ]
 */

var Helper = new Class( {
	Implements: [ Options, Events ],
	element: null,
	options: {},
	
	initialize: function( element, options ) {
		this.setOptions( options );
		options = this.options;
		this.element = document.id( element );
	}
} );



/*
 * Helper Liste
 */
Helper.List = new Class( {
	Implements: [ Options, Events ],
	options: {},
	element: null,
	elements: new Array(),
	
	initialize: function( type, attributes, options ) {
		this.setOptions( options );
		var element = null;
		switch( typeOf(type) ) {
			case 'null':
				type = 'ul';
				break;
			case 'element':
				element = type;
				break;
			case 'string':
				if( 'ul' != type || 'ol' != type ) {
					type = 'ul';
				}
				break;
		}		
		if( null == element ) {
			element = new Element( type, attributes )
		}
		this.element = element;		
	},
	
	addItems: function( contents, attributes ) {
		if( null == attributes || undefined == attributes ) {
			attributes = new Array();
		}
		contents.each( function(content, key) {			
			var attr = {};
			if( null != attributes[key] && undefined != attributes[key] ) {
				attr = attributes[key];
			}
			
			var element = new Element( 'li', attr );
			switch( typeOf(content) ) {
				default:
				case 'string':
					element.set( 'html', content );
					break;
				case 'array':
				case 'element':
					element.adopt( content );
					break;
			}
			this.elements.push( element );
		}, this );
		return this.element.adopt( this.elements );
	}
} );



/*
 * Helper Definitionlist
 */
Helper.Definitionlist = new Class( {
	Implements: [ Options, Events ],
	options: {},
	element: null,
	elements: new Array(),
	
	initialize: function( element, options ) {
		this.setOptions( options );
		options = this.options;
		switch( typeOf(element) ) {
			case 'null':
				element = new Element( 'dl', this.options );
				break;
			case 'element':
				break;
			case 'string':
				if( document.id( element ) ) {
					element = document.id( element );
				} else {
					element = new Element( 'dl', this.options );
				}
				break;
		}
		this.element = element;		
	},
	
	addItems: function( contents, attributes, listTags ) {
		if( null == listTags || undefined == listTags ) {
			listTags = new Array();
			listTags.push('dt');
			listTags.push('dd');
		}
		
		contents.each( function(content, key) {
			var tag = '';
			if( 'dt' != listTags[key] && 'dd' != listTags[key] ) {
				if( 0 == key.toInt() ) {
					tag = 'dt';
				}
			} else {
				tag = listTags[key];
			}
			if( '' == tag ) {
				tag = 'dd';
			}
			
			var attr = {};
			if( null != attributes[key] && undefined != attributes[key] ) {
				attr = attributes[key];
			}
			
			var element = new Element( tag, attr );
			switch( typeOf(content) ) {
				default:
				case 'string':
					element.set( 'html', content );
					break;
				case 'array':
				case 'element':
					element.adopt( content );
					break;
			}
			this.elements.push( element );
		}, this );
		return this.element.adopt( this.elements );
	}
} );



/*
 * Helper Liste Sortierbar
 */
Helper.ListSortables = new Class( {
    
    Implements: [ Sortables ],
    
    options: {
    	injectWhere: 'bottom', // 'top', 'bottom', 'after', or 'before'
    	defaultInjectWhere: 'top',
    	target: null,

    	itemClass: null,
    	itemHoverClass: 'over',
    	itemEditClass: 'edit',
        itemEvents: {
    		mouseover: function( e ) {
    			var itemHoverClass = this.getParent( 'ul, ol' ).retrieve('Helper.ListSortables').options.itemHoverClass,
    			itemEditClass = this.getParent( 'ul, ol' ).retrieve('Helper.ListSortables').options.itemEditClass;
    			if( !this.hasClass(itemEditClass) ) {
    				this.addClass( itemHoverClass );
    			}
    		},
    		mouseout: function( e ) {
    			var itemHoverClass = this.getParent( 'ul, ol' ).retrieve('Helper.ListSortables').options.itemHoverClass;
    	    	if( this.hasClass( itemHoverClass ) ) {
    	    		this.removeClass( itemHoverClass );
    	    	}
    	    }
    	},
    	
    	
		/* Options sortable */
    	opacity: 0.4,
	    clone: false,
    	revert: {
	    	duration: 500,
	    	transition: Fx.Transitions.Sine.easeOut
	    },
	    handle: false,
	    dragOptions: {},
	    
		/* Events */
	    onSort: function( element, clone ) {},
	    onStart: function( element ) {},
	    onComplete: function( element ) {},
	    onNewItemInjected: function( element ) {},
	    onItemDestroyed: function( destroyedItem ) {},
	    onListCleared: function( destroyedItems ) {}
    },
    
    
    initialize: function( el, options ){
        this.element = document.id(el);
        var elTag = this.element.get( 'tag' );
        if( 'ul' != elTag && 'ol' != elTag ) {
        	this.element = new Element( 'ul', {
        		id: this.element.get('id'),
        		'class': this.element.get('class')
        	} ).replaces( this.element );
        }
        
        this.setOptions( options );
                
        this.element.store( 'Helper.ListSortables', this );
        this.element.getChildren( 'li' ).addEvents( this.options.itemEvents );
    },
    
    
    newItem: function( content, options ) {
    	if( null != options && undefined != options ) {
        	this.options = Object.merge( this.options, options );
    	}
    	
        if( typeOf(this.options.target) !== 'null' ) {
            var itemPosition = document.id( this.options.target );
            this.options.target = null;
        } else {
            var itemPosition = this.element;
        }
        
        if( typeOf(this.options.injectWhere) !== 'null' ) {
        	var injectWhere = this.options.injectWhere;
        } else {
        	var injectWhere = this.options.defaultInjectWhere;
        }
        
        /* Neues ListenElement */
        var attr = {};
        if( null != this.options.itemClass && undefined != this.options.itemClass && '' != this.options.itemClass ) {
			attr['class'] = this.options.itemClass;
		}
        var newItem = new Element( 'li', attr );
        newItem.addEvents( this.options.itemEvents );
        
        switch( typeOf(content) ) {
			default:
			case 'string':
				newItem.set( 'html', content );
				break;
			case 'array':
			case 'element':
				newItem.adopt( content );
				break;
		}
        /* - */
        
        /* Neues ListenElement einfügen */
        newItem.inject( itemPosition, injectWhere );
        this.addItems( newItem );

        this.fireEvent( 'newItemInjected', [newItem] );     
        return newItem;
    },
    
    destroyItem: function( item ){
        var destroyedItem = item;
        document.id( item ).destroy();
        this.fireEvent( 'itemDestroyed', [destroyedItem] );
        return destroyedItem;
    },
    
    clearList: function(){    
        var destroyedItems = this.element.getChildren( 'li' );
        this.element.empty();
        this.fireEvent( 'listCleared', [destroyedItems] );
        return destroyedItems;
    }
} );
