//Copyright:
//  Copyright (c) 2008 - Webentwicklung Ufer - webentwicklung(dot)ufer(at)gmail(dot)com
//
//  Done For  www.ps-wein.de
  
  var dropSearch = null;
  
  var CDropSearch = new Class(
   {
     options:
     {
       'dropzone'   : 'search_drop' ,
       'drag_class' : 'search_param',
       'drag_class_colors' : 'search_color',
       'search_form': 'form_drop'   ,
       'max_params' : 3      
     
     },
     
     initialize: function(options)
     {
       this.setOptions(options);
       
       this.params       = new Array();
       this.params_count = 0;
       
       this.color        = null;
       this.color_count  = 0;
       
       this.drag_object  = null;
       this.drag_source  = null;
       
       this.dropForm = $(this.options.search_form);
       if(!this.dropForm) 
       {
          //console.error("dropForm nicht gefundne");
          return false;          
       }
       
       this.dropForm.addEvent("submit",this.form_submit.bind(this));
       
       this.dropZone = $(this.options.dropzone);
       if(!this.dropZone) 
       {
          //console.error("dropZone nicht gefundne");
          return false;          
       }
       
       this.drags    = $$("."+String(this.options.drag_class));
       if(this.drags.length < 1)
       {
          //console.error("Fehler beim Suchen der Drags");
          return false; 
       }
       
       this.dragcolors = $$("."+String(this.options.drag_class_colors)); 
       //console.info(this.dragcolors);
       
       this.dragcolors.each(function(item)
        {     
        	item.addEvent('mousedown', function(e) 
          {
            
        		e = new Event(e).stop();
            
            var el = e.target;
            
            
            el = $(el);
        		var clone = el.clone()
        			.setStyles(el.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
        			.setStyles({'opacity': 0.7, 'position': 'absolute'})
        			.addEvent('emptydrop', function() {
        			                
                this.dispose();
        		//		dropSearch.dropZone.removeEvents();
        			}).inject(document.body);
        	  
            this.drag_source = el;
        	  this.drag_object = clone;
        	  
        	  var drag_colors = clone.makeDraggable({
        			droppables: [this.dropZone]
        		}); // this returns the dragged element
         
        		drag_colors.start(e); // start the event manual
        	}.bind(this));
        }.bind(this)
       );
      
      
       
       
       
       this.drags.each(function(item)
        {
     
        	item.addEvent('mousedown', function(e) 
          {
        		e = new Event(e).stop();
            
            var el = e.target;
            
            
        
            el = $(el);
        		var clone = el.clone()
        			.setStyles(el.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
        			.setStyles({'opacity': 0.7, 'position': 'absolute'})
        			.addEvent('emptydrop', function() {
        			
                this.dispose();
        			//	dropSearch.dropZone.removeEvents();
        			}).inject(document.body);
      
        	 this.drag_object = clone;
        	 this.drag_source = el;
         
        		var drag = clone.makeDraggable({
        			droppables: [this.dropZone]
        		}); // this returns the dragged element
         
        		drag.start(e); // start the event manual
           }.bind(this)
          );
     
        }.bind(this)
       );
       
       this.dropFX = new Fx.Tween(this.dropZone, {'duration':'short'});
       
       	this.dropZone.addEvents(
        {
          'drop': function() 
          {
        		    
          	var clone = this.drag_object;
          	var item = this.drag_source;
          	
          	var art  = clone.getProperty("art");
          	
          	if(art == "aroma")
          	{
                    
              if(this.params_count < this.options.max_params )
            	{
                  var found = 0;
                  var new_param = item.getProperty("aroma");
                  this.params.each(function(p)
                  {
                     if(new_param == p)
                        found = 1;
                  }
                 )
                       
                if(!found)
                {
                   var new_item = item.clone();
                   new_item.inject(this.dropZone.getChildren("#search_drop_aromen")[0]);
                
                   new Element("div")
                   .addEvent("click",function(ev)
                    {
                      el = (ev.target).getParent();
                            
                      aroma = el.getProperty("aroma");
                      el.dispose();
                             
                      this.del(aroma);
                              
                    }.bind(this)
                   )
                   .setHTML("[X]").inject(new_item);
                  
                   this.params.push(item.getProperty("aroma"));
                   this.params_count++;    
                           
                 }
              }
            }
            else if( art == "color")
            {
              if(this.color_count == 0)
              {
                 var new_item = item.clone();
                   new_item.inject(this.dropZone.getChildren("#search_drop_colors")[0]);
                
                   new Element("div")
                   .addEvent("click",function(ev)
                    {
                      el = (ev.target).getParent();
                            
                      el.dispose();
                             
                      this.color = null;
                      this.color_count = 0;
                              
                    }.bind(this)
                   )
                   .setHTML("[X]").inject(new_item);
                 
                 this.color = clone.getProperty("color");
                 this.color_count = 1;
              }
            
            }
            
            this.dropFX.start('background-color','');
                  
            clone.dispose();
        				
                
        		  	
                  
                //console.info("nachFX");
          }.bind(this)
          ,
        		
         'over': function() {
    
   				this.dropFX.start('background-color','');
     			}.bind(this),
    			'leave': function() {
    				this.dropFX.start('background-color','');
     			}.bind(this)
         }
        );
         
      
    
     },
     
     del: function(aroma)
     {
       this.params.erase(aroma);
       this.params_count--;
       //console.info(this.params);
       //console.info(this.params_count);
     },
     
     form_submit: function(ev)
     {
       
       this.params.each(function(param)
        {
         
          new Element("input",
           {
            "name" : "aroma[]",
            "value": param,
            "type" : "hidden"
           }
          ).inject(this.dropForm);           
        }.bind(this)
       );
       
       new Element("input",
        {
           "name"  : "color",
           "value" : this.color,
           "type"  : "hidden"
          
        }
       ).inject(this.dropForm);
     
     }        
   
   }
  );
  
  CDropSearch.implement(new Options);

