<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:components="de.dtele.ui.components.*"
                autoDrawBackground="true"
                initialize="onInitialize(event)"
                dragEnter="onDragEnter(event)"
                dragOver="onDragOver(event)"
                dragExit="onDragExit(event)"
                dragDrop="onDrop(event)">
  
  <fx:Metadata>
    /**
     * An item renderer for source items
     * 
     * @author Mathias Brodala
     */
    
    /**
     * Dispatched when a source was requested to be closed
     */
    [Event("sourceClose", type="de.dtele.ui.components.events.SourceListEvent")]
    /**
     * Dispatched when a source was requested to be selected
     */
    [Event("sourceSelect", type="de.dtele.ui.components.events.SourceListEvent")]
  </fx:Metadata>
  
  <fx:Script>
    <![CDATA[
      import de.dtele.control.MediaManager;
      import de.dtele.data.IResource;
      import de.dtele.data.ISource;
      import de.dtele.net.MediaRequest;
      import de.dtele.ui.components.events.SourceListEvent;
      
      import mx.events.DragEvent;
      import mx.events.FlexEvent;
      import mx.managers.DragManager;
      
      private var selectionTimer:Timer = new Timer(1000, 1);
      
      /**
       * Tells whether a resource does not belong to this source
       * 
       * @param resource The resource to check
       * @return TRUE if the resource belongs to this source, FALSE otherwise
       */
      protected function isFromOtherSource(resource:IResource, ...a):Boolean {
        
        return resource.source != (this.data as ISource);
      }
      
      protected function onInitialize(e:FlexEvent):void {
        
        this.selectionTimer.addEventListener(TimerEvent.TIMER, this.onSelectionTimerComplete);
      }
      
      /**
       * Notifies about a select request for this source
       */
      protected function onSelectionTimerComplete(e:TimerEvent):void {
        
        this.dispatchEvent(new SourceListEvent(SourceListEvent.SELECT, this.data as ISource, true));
      }
      
      /**
       * Notifies about a close request for this source
       */
      protected function onCloseButtonClick(e:Event):void {
        
        this.dispatchEvent(new SourceListEvent(SourceListEvent.CLOSE, this.data as ISource, true));
      }
      
      /**
       * Checks if adding of resources is allowed with this source
       * and whether all resources to add are from a different source
       */
      protected function onDragEnter(e:DragEvent):void {
        
        e.preventDefault();
        
        if (e.dragSource.hasFormat("resources")) {
          
          var resources:Vector.<IResource> = e.dragSource.dataForFormat("resources") as Vector.<IResource>;
          
          if ((MediaRequest.ADD in (this.data as ISource).allowed) && resources.every(isFromOtherSource)) {
            
            DragManager.acceptDragDrop(this);
            DragManager.showFeedback(e.ctrlKey ? DragManager.MOVE : DragManager.COPY);
            
            this.selectionTimer.stop();
            this.selectionTimer.start();
          } else {
            
            DragManager.acceptDragDrop(e.dragInitiator);
            DragManager.showFeedback(DragManager.NONE);
          }
        }
      }
      
      /**
       * Indicates move or copy of resources
       */
      protected function onDragOver(e:DragEvent):void {
        
        e.preventDefault();
        
        if (e.dragSource.hasFormat("resources")) {
          
          var resources:Vector.<IResource> = e.dragSource.dataForFormat("resources") as Vector.<IResource>;
          
          if ((MediaRequest.ADD in (this.data as ISource).allowed) && resources.every(isFromOtherSource)) {
            
            this.drawFocus(true);
            
            DragManager.acceptDragDrop(this);
            DragManager.showFeedback(e.ctrlKey ? DragManager.MOVE : DragManager.COPY);
          } else {
            
            DragManager.acceptDragDrop(e.dragInitiator);
            DragManager.showFeedback(DragManager.NONE);
          }
        }
      }
      
      /**
       * Undos move or copy indicator
       */
      protected function onDragExit(e:DragEvent):void {
        
        this.drawFocus(false);
        
        this.selectionTimer.stop();
      }

      /**
       * Forwards the add request for this source
       */
      protected function onDrop(e:DragEvent):void {
        
        this.drawFocus(false);
        
        if (e.dragSource.hasFormat("resources")) {
          
          var resources:Vector.<IResource> = e.dragSource.dataForFormat("resources") as Vector.<IResource>;
          
          switch (e.action) {
            
            case DragManager.COPY:
              
              var resourcesArray:Array = new Array(resources.length);
              
              for (var i:int = 0; i < resources.length; ++i) {
                
                resourcesArray[i] = resources[i];
              }
              
              MediaManager.instance.addResources(resourcesArray, (this.data as ISource));
              break;
            
            case DragManager.MOVE:
              
              MediaManager.instance.moveResources(resources, (this.data as ISource));
              break;
          }
        }
      }

    ]]>
  </fx:Script>
  
  <s:states>
    <s:State name="normal"/>
    <s:State name="hovered"/>
    <s:State name="down"/>
  </s:states>
  
  <s:VGroup
    paddingRight="6"
    paddingBottom="6"
    paddingTop="6"
    paddingLeft="6"
    width="100%"
    x="0"
    gap="3">
    <s:HGroup
      gap="5"
      width="100%"
      verticalAlign="middle"
      textAlign="center">
      <s:Label
        text="{data.properties.title}"
        fontWeight="bold"
        fontSize="18"
        x="10"
        y="10"
        width="100%"
        textAlign="left"/>
      <components:CloseButton
        includeIn="hovered,down"
        click="onCloseButtonClick(event)"/>
    </s:HGroup>
    <s:Label
      text="{data.url}"
      x="10"
      y="35"
      fontSize="10"
      height="10"
      width="100%"
      visible="false"
      visible.hovered="true"/>
    <s:HGroup
      verticalAlign="middle">
      <s:Label
        text="{data.resources.length}"
        fontSize="15"
        color="#FFFFFF"
        backgroundColor="#999999"
        chromeColor="#FFFFFF"
        fontWeight="bold"
        paddingLeft="3"
        paddingRight="3"
        paddingTop="1"
        paddingBottom="1"
        verticalAlign="middle"/>
      <s:Label
        text="Ressourcen"
        fontSize="10"/>
    </s:HGroup>
  </s:VGroup>
  
</s:ItemRenderer>