/*
 * Zuständig dafür die Variations Drop-Down Felder aktuell zu halten
 */

var itemId = 0;
var showOverallAvailibilityStatement = true;
function VariationListener() {
	
  if(typeof window['console'] != 'undefined' && typeof console.debug == 'function')
    var debug = false;

  this.notify = function(item) {
    itemId = item.id;
    nextSelectBox = $('select#colorSelectBox');
    var itemColor = viewerModel.getItemById(itemId).color;
    nextSelectBox.children().each(function() {
      var t = $(this);
      if( t.attr('text') == itemColor) {
        t.context.selected = true;
      }
     });


    var selections = new Array();
    for(aItem in viewerModel.items ) {
      var bItem = viewerModel.getItemById(aItem);

      if (bItem.color == item.color) {
        selections.push(bItem);

      }
    }
    var all = new Array();
    var count = 0;
    selectBoxes = $('.OrderForm select');
    selectBoxes.each(function() {
      var t = $(this);
      t.attr("disabled", false);
      if( t.attr('id') != 'colorSelectBox') {

        var dimensionName = t.attr('name');


        // selectbox leeren
        t.children().each(function() {
          $(this).remove();
        });

        var buff = new Array();
        if (debug) {
          console.debug(" ----- > all: " + all);
          console.debug(" ----- > box: " + dimensionName);
        }
        
        // Suche in Hauptliste nach passenden items je nach dimension
        for (var i = 0; i <= selections.length; i++) {

            var xItem = selections[i];

            if (xItem != null) {
              if (debug) {
                console.debug("Item: " + xItem.id);
              }
              var result = new Array();
              var variMatch = new Array();
              var notFound = true;
              for(var j = 0; j < xItem.vari.length; j++) {

                var variMix = (xItem.vari[j] + "").split("|");

                var kicker = false;


                if (all.length > count) {
                  for(var k = 0; k < all.length; k++) {

                    var variFilterMix = (all[k] + "").split("|");
                    if (debug)
                      console.debug("filter : " + variFilterMix + " ?= " + variMix);
                    if (variMix[0] == dimensionName) {
                      variMatch[0] = variMix[0];
                      variMatch[1] = variMix[1];
                      if (debug)
                        console.debug("find : " + variMatch);
                    }
                    if (variMix[0] == variFilterMix[0] && variMix[1] == variFilterMix[1]) {
                      if (debug)
                        console.debug("filter match: " + variFilterMix + " is: " + variMix);
                      notFound = false;
                    }
                  }
                } else {
                  variMatch[0] = variMix[0];
                  variMatch[1] = variMix[1];
                  notFound = false;
                }
                if (all.length > 0 && debug)
                  console.debug("dimensionName = " + dimensionName + "; variMatch = " + variMatch );
                if (dimensionName == variMatch[0]) {
                  result[0] = variMatch[0];
                  result[1] = variMatch[1];
                }
                if (all.length > 0 && debug)
                  console.debug("dimensionName = " + dimensionName + "; variMix = " + variMix + "; result = " + result);
                if (notFound) continue;
                if (all.length > 0 && debug)
                  console.debug("try to add: " + result[0]);
                if (dimensionName == result[0] && !buff.contains(result[1])) {

                  if (debug)
                    console.debug("add to box: " + dimensionName + " : " + result[1] + " - " + xItem.id + " V{" + xItem.vari +"}");
                  buff.push(result[1]);
                  if (dimensionName == 'warranty_MOD') {
                	
                    var warrys = result[1].split("\xa7");
                    for(var m = 0; m < warrys.length; m++) {
                      var warExp = warrys[m].split(":");                      
                      t.addOption(warExp[0] + ":" + warExp[2], warExp[1] + " "+monate+" "+garantie, (item.promEndz == warExp[0]));

                    }

                  } else if (dimensionName == 'Var_Size' && shopIsNL=='true') {
                	  var selected = (item.id == xItem.id || item.vari.contains(result[0] + "|" + result[1]));
              		  t.addOption(xItem.id , result[1]  + " - " +  xItem.availabilityRaw, selected);
              		  showOverallAvailibilityStatement = false;
                  } else {
                    var selected = (item.id == xItem.id || item.vari.contains(result[0] + "|" + result[1]));
                    t.addOption(xItem.id , result[1], selected);
                    showOverallAvailibilityStatement = true;
                  }
                break;
                } else {
                  if (all.length > 0 && debug)
                    console.debug("fail: " + dimensionName + " !=" + result[0] + " && " + result[1] + " schon in liste");
                }
              }
            }
          }

        if (debug)
          console.debug("add dimensionfilter: " + dimensionName + "|" + t.selectedTexts());
        if(t.children().length > 0) {
          all.push( dimensionName + "|" + t.selectedTexts() );
        }
      }
      if(t.attr('name') == 'Var_Size'){
        t.sortSizeOptions();
      }
      else t.sortOptions();
      if (t.children().size() == 0){
        t.hide();
        $("#br"+t.attr('name')).remove();
      } else {
        t.show();
        if($("#br"+t.attr('name')).length == 0)
          t.after("<br id='br"+t.attr('name')+"'/>");
      }


     });
    selectBoxes = $('.OrderForm select');
    selectBoxes.each(function() {
      var t = $(this);
      if (debug)
        console.debug(":: " + t.selectedTexts());
    });
    if (debug)
      console.debug(":: " + item.vari + " " + item.color);
  };
}


