KiehlsFilter_Class= function() {
 this.CURRENT_PRODUCTS_ON_FILTERS=""; //later????
 this.CLASS_NAME_OFF="LGMEDGrey_10R";
 this.CLASS_NAME_ON="LGBlack_10B";
 this.MAIN_DIV_EXTERNAL_CONTAINER="MAIN#$#CONTAINER#$#FILTER"

this.ResolveLeftRightWhiteSpace=function() {
          var leftmaxcontent=0;
          var rightmaxcontent=0;
          var leftOBJ=$get('MATCHING_ACTUAL_LEFTCONTENT_HEIGHT');
          var rightOBJ=$get('MATCHING_ACTUAL_CONTENTPRDLIST_HEIGHT');
          var parentLeft=$get('PARENTMATCHING_LEFT');
          var parentRight=$get('PARENTMATCHING_RIGHT');
          if (leftOBJ!=null) {
                  if(leftOBJ.offsetHeight){ 
                          leftmaxcontent=leftOBJ.offsetHeight; 					
                  } 
                  else if(leftOBJ.style.pixelHeight){ 
                    leftmaxcontent=leftOBJ.style.pixelHeight;					 
                  } 
          }
          if (rightOBJ!=null) {
              if(rightOBJ.offsetHeight){ 
                      rightmaxcontent=rightOBJ.offsetHeight; 					
              } 
              else if(leftOBJ.style.pixelHeight){ 
                   rightmaxcontent=rightOBJ.style.pixelHeight;					 
              } 
          }
          
          var maxHeight=Math.max(rightmaxcontent,leftmaxcontent); 
          parentLeft.style.height=maxHeight + "px"; 
          parentRight.style.height=maxHeight + "px"; 
         


}
//It hides the labels of filtering for those that does not have a match. //later on see if I can do a webbox on server side to keep the logic clean
this.ResolveHas=function( _TopicCode,_has) {
     var obj_Label=$get('LABEL_' + _TopicCode);
     var obj_MainContainer=$get('MAINCONTAINER_' + _TopicCode);
    
      if (_has==false) {
         obj_Label.style.display="none";
         obj_MainContainer.style.display="none";
      }
}

this.OnFilterItemClick=function(TopicCode, _checked) {
   //if all button is sleected on skin types, call first this function below
   if (_checked==true && TopicCode=="Skin_AllProducts") {
      this.ToggleSkinTypesAll();
   }
   ToggleProductBubble('hidden');
   this.ToggleFilterItem(TopicCode, _checked);
   this.ManageState();
   this.ResolveLeftRightWhiteSpace();
}
//Changes the style sheet of the selected item
this.ToggleFilterItem=function(TopicCode, _checked) {
    var obj_id="FILTER:LABEL:" + TopicCode;
    var obj=$get(obj_id);

     if (_checked) {
      obj.className= this.CLASS_NAME_ON;
    }
    else {
      obj.className=this.CLASS_NAME_OFF;
    }
    //alert(obj.className);
}

this.ManageState=function () {
   var _ProductKeys=this.GetProductKeysToBeDisplayed();
   if (_ProductKeys=="") {
        this.ClearAllFilters();
   }
   else {
       this.ManageDisableFilters(_ProductKeys);
       this.FilterProductByList(_ProductKeys);
   }

}

this.ManageDisableFilters=function  (_prdKeys) {
  var _MatchedKeys=":" + _prdKeys + ":";
 
 var _obj_MainFilterContainer=$get(this.MAIN_DIV_EXTERNAL_CONTAINER);
 var  _allInputControls = _obj_MainFilterContainer.getElementsByTagName("input");
     for (var i=0; i < _allInputControls.length; i++) {
       
          if(_allInputControls[i].type=="checkbox") { 
                if (_allInputControls[i].id.indexOf(":CHECKBOX:")!=-1) {
                       if (_allInputControls[i].checked==false) {
                        var _topCode=this.GetTopicCodeFromCheckBoxID(_allInputControls[i].id);
                        var div_prd_list_id="FILTER:PRODUCTLIST:" + _topCode;
                        var div_prd_list_obj=$get(div_prd_list_id);
                            if (div_prd_list_obj!=null) {
                            var _prd_list=div_prd_list_obj.innerHTML;
                            var _prd_list_array=_prd_list.split(":");
                            var hasMatch=false;                                            
                                 for (var j=0; j < _prd_list_array.length; j++) {
                                  if (_MatchedKeys.indexOf(":" + _prd_list_array[j] + ":")!=-1) {
                                        hasMatch=true;  
                                        break; //breaks the internal for and continues at the alert        
                                   }
                                              
                                }
                                
                                //here we need to check if it is skin type or else
                                //if skin type show disable state
                                //if else disable the checkbox
                                 if ( _allInputControls[i].id.indexOf("FILTERSKINTYPE:")!=-1 && _allInputControls[i].id.indexOf(":Skin_AllProducts")==-1) {
                        
                               // if ( _allInputControls[i].id.indexOf("FILTERSKINTYPE:")!=-1 && _allInputControls[i].id.indexOf(":Skin_AllProducts")==-1)) {
                                   if (hasMatch==true) {
                                      this.ToggleSkinType(_topCode, false,"none","block","none"); //has match but let the off state
                                   }
                                   else {
                                   
                                      this.ToggleSkinType(_topCode, false,"none","none","block"); //does not have match but show the disable state
                                   }
                                }
                                else {
                                    if (hasMatch==true) {
                                      _allInputControls[i].disabled =false;
                                      //also change the CSS
                                    }
                                    else {
                                      _allInputControls[i].disabled =true;
                                      //Also Change the CSS
                                    
                                    }
                                 
                                }
                                                                                   
   
                         }
                         }
                             
                 }
                         
           }
         
      }
}

this.GetProductKeysToBeDisplayed=function  () {
 var products_match_in_all="";
 var _obj_MainFilterContainer=$get(this.MAIN_DIV_EXTERNAL_CONTAINER);
 var  _allInputControls = _obj_MainFilterContainer.getElementsByTagName("input");
     for (var i=0; i < _allInputControls.length; i++) {
          if(_allInputControls[i].type=="checkbox") {
                if (_allInputControls[i].id.indexOf(":CHECKBOX:")!=-1) {
                       if (_allInputControls[i].checked==true) {
                        var _topCode=this.GetTopicCodeFromCheckBoxID(_allInputControls[i].id);
                        var div_prd_list_id="FILTER:PRODUCTLIST:" + _topCode;
                        var div_prd_list_obj=$get(div_prd_list_id);
                            if (div_prd_list_obj!=null) {
                             var _prd_list=div_prd_list_obj.innerHTML;
                                     if (products_match_in_all=="") {
                                       products_match_in_all=_prd_list;
                                     }
                                     else {
                                            var _combined_match=""
                                            var _prd_list_array=_prd_list.split(":");
                                            
                                                                            
                                            for (var j=0; j < _prd_list_array.length; j++) {
                                            
                                              if (products_match_in_all.indexOf(_prd_list_array[j])!=-1) {
                                                   if (_combined_match=="") {
                                                     _combined_match=_prd_list_array[j];
                                                   }
                                                   else {
                                                     _combined_match=_combined_match + ":" + _prd_list_array[j];
                                                   }
                                                }
                                                
                                             }
                                            if (_combined_match=="") {
                                              products_match_in_all="";
                                              break;
                                            }
                                            else {
                                              products_match_in_all=_combined_match;
                                            }
                                        
   
                                     }
                            
                            }
                        
                       }
                }
          }
     }
  return products_match_in_all;
}
//Clears the CheckBoxes
this.ClearAllFilters=function  (){
  ToggleProductBubble('hidden');
  
 var _obj_MainFilterContainer=$get(this.MAIN_DIV_EXTERNAL_CONTAINER);
 var  _allInputControls = _obj_MainFilterContainer.getElementsByTagName("input");
     for (var i=0; i < _allInputControls.length; i++) {

          if(_allInputControls[i].type=="checkbox") {
                if (_allInputControls[i].id.indexOf(":CHECKBOX:")!=-1) {
                        var _topCode=this.GetTopicCodeFromCheckBoxID(_allInputControls[i].id);
                        if ( _allInputControls[i].id.indexOf("FILTERSKINTYPE:")!=-1 && _allInputControls[i].id.indexOf(":Skin_AllProducts")==-1) {
                               //do something different for skin types
                               this.ToggleSkinType(_topCode,false,"none","block","none");
                         }
                         else {
                         
                           this.ToggleFilterItem(_topCode);
                         }
                   _allInputControls[i].checked=false; //uncheck them all
                   _allInputControls[i].disabled =false; //enable them all;
                }
          }
     }
  this.ShowAllProducts();
  this.ResolveLeftRightWhiteSpace();
}


this.ToggleSkinTypesAll=function() {

     var _obj_MainFilterContainer=$get('MAINCONTAINER_SKINTYPE');
     var  _allInputControls = _obj_MainFilterContainer.getElementsByTagName("input");
     for (var i=0; i < _allInputControls.length; i++) {
          if(_allInputControls[i].type=="checkbox") {
                if (_allInputControls[i].id.indexOf(":CHECKBOX:")!=-1) {
                        var _topCode=this.GetTopicCodeFromCheckBoxID(_allInputControls[i].id);
                        if ( _allInputControls[i].id.indexOf("FILTERSKINTYPE:")!=-1 && _allInputControls[i].id.indexOf(":Skin_AllProducts")==-1) {
                               //do something different for skin types
                               this.ToggleSkinType(_topCode,true,"block","none","none");
                               _allInputControls[i].disabled =false; 
                               _allInputControls[i].checked=true; 
                               
                         }
                 
                }
          }
     }

 }
    
//Gets the TopicCode from the CheckBoxID
this.GetTopicCodeFromCheckBoxID=function  (_id) {
    var id_array=_id.split(":");
    return id_array[2]

}

//skin type specific
this.OnFilterSkinTypeItemClick=function (TopicCode, _checked) {

  ToggleProductBubble('hidden');
  if (_checked) {
        this.ToggleSkinType(TopicCode, true,"block","none","none") 
  }
  else {
       this.ToggleSkinType(TopicCode, false,"none","block","none") 
  }
  this.ManageState();
  this.ResolveLeftRightWhiteSpace();
}

this.ToggleSkinType=function (TopicCode, _Cheked,_onState,offState,DisableState) {
  var obj_ON_ID="FILTER_ON_" + TopicCode;
  var obj_OFF_ID="FILTER_OFF_" + TopicCode;
  var obj_DISABLE_ID="FILTER_DISABLE_" + TopicCode;
  
  var obj_ON=$get(obj_ON_ID);
  var obj_OFF=$get(obj_OFF_ID);
  var obj_DISABLE=$get(obj_DISABLE_ID);
  if (obj_DISABLE!=null) {obj_DISABLE.style.display=DisableState;}
  if (obj_ON!=null) {obj_ON.style.display=_onState;}
  if (obj_OFF!=null) {obj_OFF.style.display=offState;}
  if (DisableState=="none") {
      var obj_check_box_id="FILTERSKINTYPE:CHECKBOX:" +TopicCode;
      var obj_check_box=$get(obj_check_box_id);
      if (obj_check_box!=null){ obj_check_box.checked=_Cheked;} 
  }
  
  

}
this.ResolveHasSkinTypesFilter=function () {
  if (FILTER_HAS_SKINTYPE==false) {
     var obj=$get('LABEL_SKINTYPE');
     obj.style.display="none";
     var obj=$get('MAINCONTAINER_SKINTYPE');
     obj.style.display="none";
  }
}

 this.parentProductListMainContainerID="Category$$Product$$List$$Begin";
 this.productLayerIdContains="Category$_Product$_List$_";
 this.productlayertagType="div";
 
 this.ShowAllProducts=function() {
     var parentObj=$get(this.parentProductListMainContainerID);
     var list=parentObj.getElementsByTagName(this.productlayertagType);
     for (var i = 0; i < list.length ; i++) {
                 if (list[i].id.indexOf(this.productLayerIdContains) !=-1) {
                        list[i].style.display="";
                 }
     }
  
  }
  
 //_prodListToShow is a list of products delimited by : like 323:456:675:
 this.FilterProductByList=function(_prodListToShow) {
     var _listToShow=":" + _prodListToShow + ":";
     var parentObj=$get(this.parentProductListMainContainerID);
     var list=parentObj.getElementsByTagName(this.productlayertagType);
      for (var i = 0; i < list.length ; i++) {
                var currentID=list[i].id;
                if (currentID.indexOf(this.productLayerIdContains) !=-1) {
                        var currentprdCode=currentID.replace(this.productLayerIdContains,"");
                        
                        if (_listToShow.indexOf(":" + currentprdCode + ":") !=-1) {
                           list[i].style.display="";
                        }
                        else {
                           list[i].style.display="none";
                        
                        }
                   
                   
                }
                
     }
     }
   //  
     
}
 var KiehlsFilterObject=new KiehlsFilter_Class();
