
var Product = Class.create(
{
	initialize: function(uid,title, el,mparams,fparams)
	{
   this.uid = uid;
   this.title = title;
   this.image = $('i_'+el);
   this.illu = $('il_'+el);
   this.mparam = mparams.split(' ');
   this.fparam = fparams.split(' ');
   this.visible = true;
   //
  //alert('ceated:'+this.title+' param:'+this.mparam);
	},
  showImage: function() {
    if(this.illu.visible) {
      new Effect.Fade(this.illu, { queue: {position:'front',scope:'two'+this.uid}}); 
    }
    new Effect.Appear(this.image, { queue: {position:'end',scope:'two'+this.uid}}); 
  },
  
  showIllu: function() {
    if(this.image.visible) {
      new Effect.Fade(this.image, { queue: {position:'front',scope:'two'+this.uid}}); 
    }
    new Effect.Appear(this.illu, { queue: {position:'end',scope:'two'+this.uid}});   
  }
}
);


var Control = Class.create(
{
	initialize: function(uid,title, el,params)
	{
   this.uid = uid;
   this.title = title;
   this.element = $(el);
   this.param = params.split(' ');
   this.checked = true;
   this.element.checked = true;
   //alert('ceated:'+this.element.checked);
	},
  check: function() 
  {
    this.element.checked = true;
    this.checked = true;
  },
  uncheck: function() 
  {
    this.element.checked = false;
    this.checked = false;
  }
}
);

var ProductController = Class.create(
{
 
	initialize: function()
	{
    //this.products = new Array();
    this.products = new Hash();
    this.mcontrols = new Hash();
    this.fcontrols = new Hash();
  },
 	addProduct: function(id,title,el,mparams,fparams)
	{ 
    var prod = new Product(id,title,el,mparams,fparams);
    //this.products[id] = prod;
    this.products.set(id,prod);  
  },
 	addMaterialControl: function(id,title,el,params)
	{ 
  
    var cont = new Control(id,title,el,params);
    //this.mcontrols[params] = cont;
    this.mcontrols.set(params, cont);
  },
  addFormControl: function(id,title,el,params)
	{ 
  if(id != undefined && title != undefined && el != undefined) {
    var cont = new Control(id,title,el,params);
    //this.fcontrols[params] = cont;
    this.fcontrols.set(params, cont);
  }
  },
  checkall: function() 
  { 
    this.mcontrols.each(function(item,index) {
      item.value.check();
    });
    this.fcontrols.each(function(item,index) {
      item.value.check();
    });
    this.update('',true);
  },
  update: function(id,checked)
  {
    var cchecked = new Hash();
    
    if(id != '') {
    var check_type = id.substr(0,1);
    //aktualisieren der controls check/uncheck
    if(check_type == 'm') {
      var tmp = this.mcontrols.get(id);
      tmp.checked = checked;
      this.mcontrols.set(id, tmp);
    } else if (check_type == 'f'){
      var tmp = this.fcontrols.get(id);
      tmp.checked = checked;
      this.fcontrols.set(id, tmp);
    }
    }
        

    //angehackte checkboxen herausfinden
    this.mcontrols.each(function(item,index) {
      //var item = this.mcontrols[index];
      var thing = item.value;
      if(thing.checked) {
        cchecked.set( thing.param,thing);
      } 
    });
    
    //angehackte checkboxen herausfinden
    this.fcontrols.each(function(item,index) {
      //var item = this.mcontrols[index];
      var thing = item.value;
      if(thing.checked) {
        cchecked.set( thing.param,thing);
      } 
    });
  
    //ermittlen welche Bilder angezeigt werden
    //alert(cchecked.keys());
    var mshow = new Array();
    var fshow = new Array();
    this.products.each(function(item,index) {
      var name = item.value;
      name.mparam.each(function(na,i) {
        if(in_array(na,cchecked.keys())) {
          mshow.push(name);    
        }
      });
      name.fparam.each(function(na,i) {
        //alert(na);
        if(in_array(na,cchecked.keys())) {
        
          fshow.push(name);    
        }
      });
      
    });
    //
    
    this.products.each(function(item,index) {
      var name = item.value;
      if(in_array(name,mshow) && in_array(name,fshow)) {
        name.showImage(); 
      } else {
        name.showIllu();
      }
    });
    
  }
}
);




function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}
