﻿var OBJSearch=Class.create()
OBJSearch.prototype = {
    initialize: function(JSONParams) {
        this.permanentTag = (JSONParams.permanentTag) ? JSONParams.permanentTag : null
        this.permanentTagFamille = (JSONParams.permanentTagFamille) ? JSONParams.permanentTagFamille : null
        this.permanentTagResult = (JSONParams.permanentTagResult) ? JSONParams.permanentTagResult : null

        this.defautTag = (JSONParams.defautTag) ? JSONParams.defautTag : null
        this.isActive = true
        this.items = new OBJSearchItems(JSONParams.items, this);
        this.slidePrix;
        this.slidePointure;
        this.onSelectItem = searchEvent
        if (this.permanentTag) {
            this.slidePrix = new OBJSearchBiSlide(this.permanentTagResult.slidePrix, this);
            this.slidePointure = new OBJSearchSlide(this.permanentTagResult.slidePointure, this);
            this.filter(this.permanentTagResult.listFamille, this.permanentTag, this.permanentTagFamille, 'permanent')
            if (JSONParams.slidePrix) {
                //spe shoeshoe
                var d = document.location.href.split('?')
                d = d[0].split('/')
                d = d[d.length - 1]
                if (d == 'detail-produit.aspx') {
                    this.slidePrix.isRebuildSlidePrices = true
                }

                if (JSONParams.slidePrix.minSelected) {
                    this.slidePrix.minSelected = JSONParams.slidePrix.minSelected;

                    this.slidePrix.slide.setValue(this.slidePrix.minSelected, 0)

                }
                if (JSONParams.slidePrix.maxSelected) {
                    this.slidePrix.maxSelected = JSONParams.slidePrix.maxSelected;
                    this.slidePrix.slide.setValue(this.slidePrix.maxSelected, 1)
                }
                if (d == 'detail-produit.aspx') {
                    this.slidePrix.isRebuildSlidePrices = false
                }
            }
        }
        //spe shoeshoe/////////

        this.onload = searchEvent
        this.isPriceChangedByUser = false

        if (this.defautTag) {
            var ar = new Array
            for (var y = 0; y < this.defautTag.length; y++) {

                var it = this.items.getItem(this.defautTag[y].famille)
                if (it) {
                    if (this.defautTag[y].famille != this.slidePointure.tagFamilleId) {
                        var fe = it.element.firstDescendant();
                        while (fe) {
                            var dtk = fe.getAttribute('datakey')
                            if (dtk == this.defautTag[y].tag) {
                                it.selectItem(fe, true)
                                fe = null
                            } else {
                                fe = fe.next();
                            }
                        }
                    } else {
                        ar.push(this.defautTag[y].nom)
                    }
                }
            }
            if (ar.length > 0) this.slidePointure.setAllowebValue(ar)
            if (typeof this.onSelectItem == 'function') {
                var tag = this.getTag();
                if (tag != this.permanentTag) {
                    //spe shoeshoe
                    var d = document.location.href.split('?')
                    d = d[0].split('/')
                    d = d[d.length - 1]
                    if (d != 'detail-produit.aspx') {

                        setTimeout(function() { this.onSelectItem(tag, this.permanentTag, this.slidePrix.minSelected, this.slidePrix.maxSelected); } .bind(this), 1000)
                    }
                } else {
                    //spe shoeshoe
                    var d = document.location.href.split('?')
                    d = d[0].split('/')
                    d = d[d.length - 1]
                    if (d != 'detail-produit.aspx') {
                        if ($("loading")) $("loading").remove();
                        initializeSearch()
                    }
                }
            }
            //  this.onSelectItem(this.getTag(),this.permanentTag, this.slidePrix.minDefaut, this.slidePrix.maxDefaut, 0);

            // this.filter(this.defautTagResult,this.defautTag,this.defautTagFamille,'defaut')
            //   this.slidePrix=new OBJSearchBiSlide(this.defautTagResult.slidePrix,this);
            ///////////////////////
            //   this.filterByDefautTag();
            //    setTimeout('searchInit(objsearch.getTag(),objsearch.permanentTag, objsearch.slidePrix.minDefaut, objsearch.slidePrix.maxDefaut, 0)',1000);
            ///////////////////////
        }

    },

    inin: function() {
    for (var y = 0; y < this.items.length; y++) {
        //   this.items[i].initSelectedItem()
        if (this.items[y].initSelectedItem) {
            this.items[y].listTag = new Array
            var bool = true;
            for (var i = 0; i < this.items[y].items.length; i++) {
                this.items[y].items[i].el.show()
                if (this.items[y].items[i].el.className == 'true') {
                    this.items[y].unselectItem(this.items[y].items[i].el);
                    this.items[y].items[i].el.className = ''
                }
            }
            if (this.items[y].selection) {
                this.items[y].selection.update('')
                if (this.items[y].selectionTitre) {
                    this.items[y].selectionTitre.setStyle('display:none;')
                };
            }
            var papy = this
            this.items[y].filter()
        }
    }
     var ar = new Array
     for (var y = 0; y < this.defautTag.length; y++) {

         var it = this.items.getItem(this.defautTag[y].famille)
         if (it) {
             if (this.defautTag[y].famille != this.slidePointure.tagFamilleId) {
                 var fe = it.element.firstDescendant();
                 while (fe) {
                     var dtk = fe.getAttribute('datakey')
                     if (dtk == this.defautTag[y].tag) {
                         it.selectItem(fe, true)
                         fe = null
                     } else {
                         fe = fe.next();
                     }
                 }
             } else {
                 ar.push(this.defautTag[y].nom)
             }
         }
     }
     if (ar.length > 0) this.slidePointure.setAllowebValue(ar)
     if (typeof this.onSelectItem == 'function') {
         var tag = this.getTag();
         if (tag != this.permanentTag) {
             //spe shoeshoe
             var d = document.location.href.split('?')
             d = d[0].split('/')
             d = d[d.length - 1]
             if (d != 'detail-produit.aspx') {

               //  setTimeout(function() { this.onSelectItem(tag, this.permanentTag, this.slidePrix.minSelected, this.slidePrix.maxSelected); } .bind(this), 1000)
             }
         } else {
             //spe shoeshoe
             var d = document.location.href.split('?')
             d = d[0].split('/')
             d = d[d.length - 1]
             if (d != 'detail-produit.aspx') {
                 if ($("loading")) $("loading").remove();
                 initializeSearch()
             }
         }
     }
    },
    //spe shoeshoe
    AjaxActivate: function() {
        this.isActive = true
        for (var i = 0; i < this.items.length; i++) {
            this.items[i].element.parentNode.className = this.items[i].elementClassName
            this.items[i].disable = false
        }
        this.slidePointure.valueElement.parentNode.setStyle('cursor:e-resize;')
        this.slidePrix.minValueElement.parentNode.setStyle('cursor:e-resize;')
        this.slidePrix.maxValueElement.parentNode.setStyle('cursor:e-resize;')
        this.slidePrix.slide.disabled = false
        this.slidePointure.slide.disabled = false
    },

    AjaxDesactivate: function() {
        this.isActive = false
        for (var i = 0; i < this.items.length; i++) {
            this.items[i].element.parentNode.className = this.items[i].elementClassName + 'Inactif'
            this.items[i].disable = true
        }
        this.slidePointure.valueElement.parentNode.setStyle('cursor:wait;')
        this.slidePrix.minValueElement.parentNode.setStyle('cursor:wait;')
        this.slidePrix.maxValueElement.parentNode.setStyle('cursor:wait;')
        this.slidePrix.slide.disabled = true
        this.slidePointure.slide.disabled = true
    },

    filter: function(r, tag, tagfamille, isDefault) {

        for (var i = 0; i < r.length; i++) {
            if (r[i].famille != this.slidePointure.tagFamilleId) {
                var it = this.items.getItem(r[i].famille)

                for (var j = 0; j < it.items.length; j++) {
                    var its = it.items[j]
                    var hide = true;
                    for (var y = 0; y < r[i].tags.length; y++) {
                        //if(fe.getAttribute("datakey")==r[i].tags[y]){ hide=false; break; } 
                        if (its.id == r[i].tags[y]) { hide = false; break; }
                    }
                    if (hide == true) {//si a masque
                        var hbt = its.hideByTag
                        if (hbt == null) { hbt = ''; }
                        if (its.display != false) {
                            var ite = this.items.getItem(tagfamille)
                            if (ite) {
                                if (ite.listTag.length < 2) {
                                    it.hideElement(its);
                                }
                            } else {
                                it.hideElement(its);
                                //fe.hide();
                            }
                        } else {//si masque
                            it.hideElement(its);
                            //fe.hide();
                        }

                        its.hideByTag = hbt + '(' + tagfamille + '-' + tag + ')'
                    } else {//si a afficher
                        var hbt = its.hideByTag
                        if (hbt == null) { hbt = ''; }
                        if (its.display != false) {//si afficher
                            it.showElement(its);
                            //fe.show();
                        } else {//si masquer
                            // var tab=this.splithideByTag(hbt)
                            var byMember = false
                            // for(var k=0;k<tab.length;k++){
                            if (hbt.indexOf('(' + tagfamille + '-') != 0) {
                                //si masquer par non membre
                                byMember = true
                            }
                            //}

                            if (byMember == true) {//si masquer par non membre

                            } else {//si masquer par membre
                                it.showElement(its);
                                //fe.show()
                            }
                        }
                    }

                }

            }
        }

    },

    // transforme la chaine 'val' en un tab
    splithideByTag: function(val) {
        var reg = new RegExp("[()]+", "g");
        var retour = new Array;
        var tab = val.split(reg);
        for (var j = 0; j < tab.length; j++) {
            if (tab[j] != '') {
                retour.push(tab[j])
            }
        }
        return retour;
    },
/*
    databind: function(tag, redirect) {
        alert('databind')
        var reg = new RegExp("[;]+", "g");
        var tableau = tag.split(reg);

        for (var i = 0; i < this.items.length; i++) {
            var bool = true;
            var fe = this.items[i].element.firstDescendant();
            while (bool) {
                if (fe) {
                    fe.setStyle('display:block');
                    fe.className = ''
                    if (tableau.length > 0) {
                        for (var c = 0; c < tableau.length; c++) {
                            if (fe.getAttribute('datakey') == tableau[c]) {
                                if (c == (tableau.length - 1)) {
                                    if (redirect != false) {
                                        this.items[i].selectItem(fe, true)
                                    } else {
                                        this.items[i].selectItem(fe, false)
                                    }
                                } else {
                                    this.items[i].selectItem(fe, false)
                                }
                            }
                        }
                    }
                    fe = fe.next();
                } else {
                    bool = false;
                }
            }
            if (this.items[i].selection) {
                this.items[i].selection.update('')
                if (this.items[y].selection.descendants.length == 0) {
                    if (this.items[y].selectionTitre) { this.items[y].selectionTitre.setStyle('display:none;') };
                } else {
                    if (this.items[y].selectionTitre) { this.items[y].selectionTitre.setStyle('display:block;') };
                }
            }
        }

       // alert('databind')
        if (tag == '') {
            alert('yes')
            searchEvent('', $('ctl00_ContentPlaceHolder1_recherche1_defautTag').value, prixMin, prixMax);
        }

    },*/

    filterByDefautTag: function() {
        alert('filterByDefautTag')
        if (this.defautTag) {
            this.filter(this.defautTagResult, this.defautTag, this.defautTagFamille, 'defaut')
            this.slidePrix
        } else {
            if (this.permanentTag) {
                this.filter(this.permanentTagResult.listFamille, this.permanentTag, this.permanentTagFamille, 'permanent')
            }
        }
    },

    filterItem: function(tag, tagFamily) {
        alert('filterItem')
        tag = "" + tag
        var t = tag.split('-');
        t.sort()
        for (var y = 0; y < this.items.length; y++) {
            if (tagFamily != this.items[y].tagFamilleId) {
                var fe = this.items[y].element.firstDescendant();
                while (fe) {
                    var hide = true;
                    for (var i = 0; i < t.length; i++) {
                        if (fe.getAttribute("datakey") == t[i]) { hide = false; break; }
                    }
                    if (hide == true) {
                        if (fe.className != 'true') { fe.setStyle('display:none;'); }
                    }
                    else {
                        fe.setStyle('display:block;');
                    }
                    fe = fe.next();
                }
            }
        }
    },

    getTags: function() {

        var tag = '';
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].listTag.length > 0) {
                tag = tag + this.items[i].tagFamilleId + "_"
                for (var y = 0; y < this.items[i].listTag.length; y++) {
                    tag = tag + this.items[i].listTag[y] + '-';
                }
                tag = tag.substring(0, tag.length - 1);
                tag = tag + ";"
            }
        }

        if (tag.length > 0) {
            tag = tag.substring(0, tag.length - 1) + ';' + this.permanentTagFamille + '_' + this.permanentTag + ";";
        } else {
            tag = this.permanentTagFamille + '_' + this.permanentTag + ";";
        }

        var j = 1;
        var params = 'tag=' + tag
        var tagPointure = this.slidePointure.getTags()
        if (tag.length > 0) {
            if (tagPointure.length > 0) {
                params = params + this.slidePointure.tagFamilleId + '_' + tagPointure + ";";
            }
        } else {
            if (tagPointure.length > 0) {
                params = tagPointure + ";";
            }

        }




        params = params.substring(0, params.length - 1) + this.slidePrix.getTags()
        for (var i = 0; i < this.items.length; i++) {
            params = params + '&famille' + j + '=' + this.items[i].tagFamilleId;
            j = j + 1
        }
        //tagfamille pointure
        params = params + '&famille' + j + '=' + this.slidePointure.tagFamilleId



        return params
    },

    getTag: function() {

        var tag = '';
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].listTag.length > 0) {
                for (var y = 0; y < this.items[i].listTag.length; y++) {
                    tag = tag + this.items[i].listTag[y] + '-';
                }
                tag = tag.substring(0, tag.length - 1);
                tag = tag + "_"
            }
        }

        if (tag.length > 0) {
            tag = tag.substring(0, tag.length - 1) + '_' + this.permanentTag;
        } else {
            tag = this.permanentTag
        }
        var tagPointure = this.slidePointure.getTags()
        if (tagPointure.length > 0 && tag.length > 0) { tag = tag + '_' + tagPointure; } else { tag = tag + tagPointure; }

        return tag
    },

    initSearchControl: function() {
        
        for (var y = 0; y < this.items.length; y++) {
            //   this.items[i].initSelectedItem()
            if (this.items[y].initSelectedItem) {
                this.items[y].listTag = new Array
                var bool = true;
                for (var i = 0; i < this.items[y].items.length; i++) {
                    this.items[y].items[i].el.show()
                    if (this.items[y].items[i].el.className == 'true') {
                        this.items[y].unselectItem(this.items[y].items[i].el);
                        this.items[y].items[i].el.className = ''
                    }
                }
                if (this.items[y].selection) {
                    this.items[y].selection.update('')
                    if (this.items[y].selectionTitre) {
                        this.items[y].selectionTitre.setStyle('display:none;')
                    };
                }
                var papy = this
                this.items[y].filter()
            }
        }
        papy.onSelectItem(papy.getTag(), papy.permanentTag, papy.slidePrix.minSelected, papy.slidePrix.maxSelected);
        this.filter(this.permanentTagResult.listFamille, this.permanentTag, this.permanentTagFamille, 'permanent')

        this.slidePrix.initSlide()
        this.slidePointure.initSlide()

        if ($("loading")) $("loading").remove();
    }

}


var OBJSearchItem=Class.create()
OBJSearchItem.prototype = {
    initialize:function(JSONParams,parent){
        this.parent=parent
        this.tagFamilleId=JSONParams.tagFamilleId;
        this.disable=false
        this.listTag=new Array
        this.items=(JSONParams.items) ? JSONParams.items : null;
        this.element=(JSONParams.element) ? $(JSONParams.element) : null;
        this.elementClassName=(JSONParams.CssClass) ? JSONParams.CssClass : '';
        this.selection=(JSONParams.selection) ? $(JSONParams.selection) : null;
        this.searchLibelle=(JSONParams.searchLibelle) ? $(JSONParams.searchLibelle) : null;
        this.initSelectedButton=(JSONParams.initSelectedButton) ? $(JSONParams.initSelectedButton) : null;
        if(JSONParams.selectionTitre){
            if($(JSONParams.selectionTitre)){
                this.selectionTitre=$(JSONParams.selectionTitre);
                this.selectionTitre.hide();
            }
        }
        if(JSONParams.initSelectedButton){
            this.initSelectedButton.observe('click',function(e){
                this.initSelectedItem();
            }.bind(this));
        }
        this.initItem()
        this.init()
    },
    
    initItem:function(){
        var fe=this.element.firstDescendant();
        for(var i=0;i<this.items.length;i++){
            this.items[i].el=fe
            fe=fe.next()
        }
    },
    
    filter:function(){
        var i
        var tagfamille=this.tagFamilleId
        var r=this.parent.parent.permanentTagResult.listFamille
        for(var k=0;k<r.length;k++){
            if(r[k].famille==this.tagFamilleId){
                i=k
            }
        }
   
            var it=this
            for(var j=0;j<it.items.length;j++){
                var its=it.items[j]
                var hide=true;
                for(var y=0;y<r[i].tags.length;y++){
                    //if(fe.getAttribute("datakey")==r[i].tags[y]){ hide=false; break; } 
                    if(its.id==r[i].tags[y]){ hide=false; break; } 
                }
                if(hide==true){//si a masque
                    var hbt=its.hideByTag
                    if(hbt==null){ hbt=''; }
                    if(its.display!=false){
                        var ite=this
                        if(ite){
                            if(ite.listTag.length<2){
                                it.hideElement(its);
                            }
                        }else{
                            it.hideElement(its);
                            //fe.hide();
                        }
                    }else{//si masque
                        it.hideElement(its);
                        //fe.hide();
                    }
                    
                    its.hideByTag=hbt+'('+tagfamille+'-'+tag+')'
                }else{//si a afficher
                    var hbt=its.hideByTag
                    if(hbt==null){ hbt=''; }
                    if(its.display!=false){//si afficher
                        it.showElement(its);
                        //fe.show();
                    }else{//si masquer
                       // var tab=this.splithideByTag(hbt)
                        var byMember=false
                       // for(var k=0;k<tab.length;k++){
                            if(hbt.indexOf('('+tagfamille+'-')!=0){
                                //si masquer par non membre
                                byMember=true
                            }
                        //}
                        
                        if(byMember==true){//si masquer par non membre
                            
                        }else{//si masquer par membre
                            it.showElement(its);
                            //fe.show()
                        }
                    }
                }
            
            }
      
    
    },
    
    
    hideElement:function(it){
            it.el.hide()
            it.display=true
    },
    
    showElement:function(it){
            it.el.show()
            it.display=false
    },
    
    // li : l'element qui a ete selectionné
    // boole : todo
    selectItem:function(li,boole){
        var tag=li.getAttribute('datakey')
        var papy=this.parent.parent
        this.addTag(tag);
        this.initSearchItem();
         
      
        var params=this.parent.parent.getTags()
        this.parent.parent.AjaxDesactivate()
        var a = new Ajax.Request('OBJSearchs.ajax', {
                        method:'POST',
                        postBody:params,
                        onComplete:function(transport){
                            var r = transport.responseJSON;
                            this.parent.parent.filter(r.listFamille,tag,this.tagFamilleId);
                            this.parent.parent.slidePrix.bind(r.slidePrix)
                            this.parent.parent.slidePointure.bind(r.slidePointure)
                            this.parent.parent.AjaxActivate()
                            
                            //mis a jour du prix
                            
        }.bind(this)});
        
        
       
        li.className='true';
        if(this.selection){
            this.selection.insert('<li datakey="'+tag+'">'+li.innerHTML+'</li>')
            if(this.selectionTitre){ this.selectionTitre.show()};
        }   
        ///////////////////////////////////////////////////////////////
        if(boole){ }
        ///////////////////////////////////////////////////////////////
            
    
    },
 
    
    unselectItem:function(li){
        if(this.parent.parent.isActive==true){
        var tag=li.getAttribute('datakey')
        var papy=this.parent.parent
        this.removeTag(tag);
        this.initSearchItem();
         var params=this.parent.parent.getTags()
       this.parent.parent.AjaxDesactivate()
        var a = new Ajax.Request('OBJSearchs.ajax', {
                        method:'POST',
                        postBody:params,
                        onComplete:function(transport){
                            var r = transport.responseJSON;
                            this.parent.parent.filter(r.listFamille,tag,this.tagFamilleId);
                            this.parent.parent.slidePrix.bind(r.slidePrix)
                            this.parent.parent.slidePointure.bind(r.slidePointure)
                            this.parent.parent.AjaxActivate()
                            //mis a jour du prix
                            
        }.bind(this)});
        
        li.className=''
        
        
        if(this.selection){
            var bool=true;
            fe=this.selection.firstDescendant();
            while(bool){
                if(fe){
                    if(fe.getAttribute('datakey')==tag){
                        fe.remove()
                        bool=false;
                    }else{
                        fe=fe.next();
                    }
                }else{
                    bool=false;
                }
            }
                
            if(!this.selection.firstDescendant()){
                if(this.selectionTitre){ this.selectionTitre.hide()};
            }else{
                if(this.selectionTitre){ this.selectionTitre.show()};
            }
            }
        }
       
        
        ///////////////////////////////////////////////////////////////    
        //this.parent.parent.majSearchControl(this.tagFamilleId,true);
        ///////////////////////////////////////////////////////////////
    },
    
    
    
    // a finir mais marche
    init:function(){
        var papy=this.parent.parent
        this.element.observe('click',function(e){
            if(this.disable==false){
            
            if(e.target.nodeName=='LI'){
                if(e.target.className=='true'){
                    this.unselectItem(e.target)
                }else{
                    this.selectItem(e.target,true)
                }
               
                //////////////////////// A REFAIRE /////////////////////////////// 
                if (typeof papy.onSelectItem == 'function'){
                    var tag=papy.getTag();
                    if(tag!=papy.permanentTag){
                        papy.onSelectItem(tag,papy.permanentTag, papy.slidePrix.minSelected, papy.slidePrix.maxSelected);
                    }else{
                        //spe shoeshoe
                        if($("loading")) $("loading").remove();
                       initializeSearch()
                    }
                }
                //////////////////////// A REFAIRE ///////////////////////////////
            }
            }
        }.bind(this));
        
        if(this.selection){
            this.selection.observe('click',function(e){
                if(e.target.nodeName=='LI'){
                    this.unselectItem(e.target)
                    //////////////////////// A REFAIRE ///////////////////////////////
                    if (typeof papy.onSelectItem == 'function'){
                        var tag=papy.getTag();
                        
                        if(tag!=papy.permanentTag){
                            papy.onSelectItem(tag,papy.permanentTag, papy.slidePrix.minSelected, papy.slidePrix.maxSelected);
                        }else{
                            //spe shoeshoe
                            if($("loading")) $("loading").remove();
                           initializeSearch()
                        }
                    }
                    //////////////////////// A REFAIRE ///////////////////////////////
                }
            }.bind(this));
        }
        
        if(this.searchLibelle){
            this.searchLibelle.observe('keyup',function(e){
                this.searchItem()
            }.bind(this));
        }
    },
   
    initSelectedItem:function(){
        if(this.initSelectedItem){
            this.listTag=new Array
            var bool=true;
            for(var i=0;i<this.items.length;i++){
                this.items[i].el.show()
                if(this.items[i].el.className=='true'){
                    this.unselectItem(this.items[i].el);
                    this.items[i].el.className=''
                }
            }
  
            if(this.selection){
                    this.selection.update('')
                    if(this.selectionTitre){ this.selectionTitre.setStyle('display:none;')};
            } 
            var papy=this.parent.parent
      
           this.filter()
           papy.onSelectItem(papy.getTag(),papy.permanentTag, papy.slidePrix.minSelected, papy.slidePrix.maxSelected);
        }
    },

    
    //good
    addTag:function(tag){
        this.listTag.push(tag);
    },

    //good
    removeTag:function(tag){
        var c=new Array
        for(var i=0;i<this.listTag.length;i++){
            if(this.listTag[i]!=tag){  c.push(this.listTag[i]); }
        }
        this.listTag=c;
    },    
    
    // initialize le champ de recherche
    //a garder
    initSearchItem:function(){
        if(this.searchLibelle){
             this.searchLibelle.value='';
             var bool=true;
             var fe=this.element.firstDescendant();
             while(bool){
                if(fe){
                    if(fe.hasAttribute('hideBySearch')){
                        fe.removeAttribute('hideBySearch');
                        fe.show()
                        fe=fe.next();
                    }else{
                        bool=false;
                    }
                }
            }
        }
    },
    
    //filtre les element depuis une recherche par l'input
    //a garder
    searchItem:function(){
        var bool=true;
        var fe=this.element.firstDescendant();
        while(bool){
            if(fe){
                if(fe.innerHTML.toLowerCase().indexOf($('search'+this.tagFamilleId).value.toLowerCase(),0)!=0){
                    if(fe.getStyle('display')!='none'){
                        fe.hide()
                        fe.setAttribute('hideBySearch','true'); 
                    }
                }else{
                    if(fe.hasAttribute('hideBySearch')){
                        fe.removeAttribute('hideBySearch');
                        fe.show()
                    }
                }
                fe=fe.next();
            }else{
                bool=false;
            }
        }
    },
    
    getElementByTag:function(tag){
        var bool=true;
        var fe=this.element.firstDescendant();
        while(bool){
            if(fe){
                if(fe.getAttribute('datakey')==tag) return fe;
                fe=fe.next();
            }else{
                bool=false;
            }
        }
        return null
    }
    
}

var OBJSearchSlide=Class.create()
OBJSearchSlide.prototype = {
    initialize:function(JSONParams,parent){
		
        this.parent=parent
        this.tagFamilleId=(JSONParams.tagFamilleId)? JSONParams.tagFamilleId : null;
        this.slide
        this.valueElement=$('pValue')
        this.defautValue=(JSONParams.value) ? JSONParams.value : null;
        this.value=this.defautValue;
        this.selectedValue
        this.allowedValue=(JSONParams.allowedValue) ? JSONParams.allowedValue : null;
        this.isRebuildSlide=false
        this.initSlide();
    },
    
    bind:function(JSONparam){
        this.isRebuildSlide=true
        this.allowedValue=JSONparam.allowedValue
        this.value=JSONparam.value
        this.slide.allowedValues=JSONparam.allowedValue
        this.isRebuildSlide=false
    },
    
    setAllowebValue:function(val){
        //this.allowedValue=val
        //this.slide.allowedValues=val
        this.value=Math.floor(parseInt(val[0]))
        this.slide.setValue(Math.floor(parseInt(val[0])))
        this.updateElement(Math.floor(parseInt(val[0])))
    },
    
    getTags:function(){
        if(this.selectedValue==null){
            return ''
        }else{
            var params=''
            for(var i=0;i<this.value.length;i++){
                if(parseInt(this.value[i].value)==parseInt(this.selectedValue)){
                    params=params+this.value[i].id+'-';
                }
            }
            if(params.length>0){ params=params.substr(0,params.length-1)}
            return params
        }
        
    },
    
    initSlide:function(){
        
        this.updateElement('?')
        this.slide=new Control.Slider($('sliderPointure').select('.handle'), 'sliderPointure', {
              range: $R(this.allowedValue[0], this.allowedValue[this.allowedValue.length-1]),
              values :this.allowedValue,
              restricted: true,
              sliderValue:  this.allowedValue,
              onSlide: function(value) {
               // to redo
              
                //
                $('pValue').update(Math.floor(value))
                
              },
              onChange: function(value){
                   var val
                
                                // to redo
                var p=this
                
                $('pValue').update(Math.floor(value))
                
                if(p.isRebuildSlide==false){
                    p.selectedValue=Math.floor(value)
                    var params=this.parent.getTags()
                    this.parent.AjaxDesactivate()
                    var a = new Ajax.Request('OBJSearchs.ajax', {
                                    method:'POST',
                                    postBody:params,
                                    onComplete:function(transport){
                                        var r = transport.responseJSON;
                                        this.parent.filter(r.listFamille,0,0);
                                        this.parent.slidePrix.bind(r.slidePrix)
                                        this.parent.AjaxActivate()
                    }.bind(this)});
                    
                    p.parent.onSelectItem(p.parent.getTag(),p.parent.permanentTag, p.parent.slidePrix.minSelected, p.parent.slidePrix.maxSelected);
                    }
              }.bind(this)
            });
            
            $('initPointure').observe('click',function(e){
                var p=this
                p.selectedValue=null
                var params=this.parent.getTags()
                var a = new Ajax.Request('OBJSearchs.ajax', {
                                    method:'POST',
                                    postBody:params,
                                    onComplete:function(transport){
                                        var r = transport.responseJSON;
                                        this.parent.filter(r.listFamille,0,0);
                                        this.parent.slidePrix.bind(r.slidePrix)
                                        this.parent.AjaxActivate()
                }.bind(this)});
                p.parent.onSelectItem(p.parent.getTag(),p.parent.permanentTag, p.parent.slidePrix.minSelected, p.parent.slidePrix.maxSelected);
                this.initSlide()    
           
            }.bind(this))
           
    },
    
    //mis a jour des element graphiques
    updateElement:function(val){
        this.valueElement.update(val)       
    }
    
}

var OBJSearchBiSlide=Class.create()
OBJSearchBiSlide.prototype = {
    initialize:function(JSONParams,parent){
        this.parent=parent
        this.minAllowed=null;
        this.maxAllowed=null;
        
        this.minDefaut=(JSONParams.prixMin) ? JSONParams.prixMin : null;
        this.maxDefaut=(JSONParams.prixMax) ? JSONParams.prixMax : null;
        
        
        this.slide
        this.minSelected=(JSONParams.minSelected) ? JSONParams.prixMin : null;;
        this.maxSelected=(JSONParams.maxSelected) ? JSONParams.prixMin : null;;
        
        this.minValueElement=$('minValue')       
        this.maxValueElement=$('maxValue')
        
        this.isRebuildSlidePrices=false
        this.initSlide();
    },
    
    getTags:function(){
        var params=''
        if(this.minSelected!=null){
            params=params+'&pmin='+this.minSelected
        }
        if(this.maxSelected!=null){
            params=params+'&pmax='+this.maxSelected
        }
        
        return params
    },
    
    /////////////////// en cours ///////////////////
    getMinValue:function(){
        if(this.minSelected!=null){
            if(this.minAllowed>this.minSelected){
                this.minSelected
            }else{
                this.minAllowed
            }
        }else{
            return this.minAllowed
        }
    },
    
    getMaxValue:function(){
        if(this.maxSelected!=null){
            if(this.maxAllowed<this.maxSelected){
                this.maxSelected
            }else{
                this.maxAllowed
            }
        }else{
            return this.maxAllowed
        }
    },
    /////////////////// en cours ///////////////////
    
    bind:function(JSONparam){
        this.isRebuildSlidePrices=true
        this.minAllowed=JSONparam.prixMin
        this.maxAllowed=JSONparam.prixMax
        
        if(this.minSelected==null){
             this.slide.setValue(this.minAllowed,0)
        }
        if(this.maxSelected==null){
            this.slide.setValue(this.maxAllowed+1,1)
        }
        this.isRebuildSlidePrices=false
    },
    
    setValue:function(values){
        this.isRebuildSlidePrices=true
        
        if(values==false){
            this.slide.setValue(prixMin,0)
            this.slide.setValue(prixMax,1)
        }else{
       
            this.minAllowed=values[values.length-1]
            this.maxAllowed=values[0]
            this.slide.setValue(values[0],0)
            this.slide.setValue(values[values.length-1],1)
        }
        this.isRebuildSlidePrices=false

    },
    
    initSlide:function(){
        this.minAllowed=this.minDefaut
        this.maxAllowed=this.maxDefaut
        this.updateElement(this.minAllowed,this.maxAllowed)
     
        this.slide=new Control.Slider($('sliderPrix').select('.handle'), 'sliderPrix', {
            range: $R(this.minAllowed, this.maxAllowed),
            restricted: true,
            sliderValue:  [this.minAllowed, this.maxAllowed],
            onSlide: function(value) {
                // to redo
                
                //
                
                var min
                var max
                if(value[0]>this.maxAllowed){
                    min=this.maxAllowed-1
                    this.slide.setValue(min,0)
                }else{
                    min=Math.floor(value[0])
                }
                
                if(value[1]<this.minAllowed){
                    max=this.minAllowed+1
                    this.slide.setValue(max,1)
                }else{
                    max=Math.floor(value[1])+1
                }
                $('minValue').update(min+'€')    
                $('maxValue').update(max+'€')    
        
               // this.updateElement(min,max)
               // this.minValueElement.update(min+'€')
             //   this.minValueElement.update(max+'€')
              //
             //   this.parent.prixMin=min+''
             //   this.parent.prixMax=max+''
              //  
            }.bind(this),
            onChange: function(value) {
                var min
                var max
                                // to redo
                
                //
                if(value[0]>this.maxAllowed){
                    min=this.maxAllowed-1
                    this.slide.setValue(min,0)
                }else{
                    min=Math.floor(value[0])
                }
                
                if(value[1]<this.minAllowed){
                    max=this.minAllowed+1
                    this.slide.setValue(max,1)
                }else{
                    max=Math.floor(value[1])+1
                }
               
                $('minValue').update(min+'€')    
                $('maxValue').update(max+'€')    
        
                
                if(this.isRebuildSlidePrices==false){
                    this.minSelected=min
                    this.maxSelected=max
                    var params=this.parent.getTags()
                    this.parent.AjaxDesactivate()
                    var a = new Ajax.Request('OBJSearchs.ajax', {
                                    method:'POST',
                                    postBody:params,
                                    onComplete:function(transport){
                                        var r = transport.responseJSON;
                                        this.parent.filter(r.listFamille,0,0);
                                        this.parent.slidePointure.bind(r.slidePointure)
                                        this.parent.AjaxActivate()
                    }.bind(this)});
                    
                   this.parent.onSelectItem(this.parent.getTag(),this.parent.defautTag, min, max);
                }

            }.bind(this)
    });
    
    
    },
    
    //mis a jour des element graphiques
    updateElement:function(min,max){
        this.minValueElement.update(min+'€')       
        this.maxValueElement.update(max+'€')       
    }

}

var OBJSearchItems=Class.create()
OBJSearchItems.prototype = {
     initialize:function(JSONParams,parent){
        var a = new Array()
        Object.extend(this,$A(a))
        this['push'] = a['push']
        this['length'] = a['length']
        this['indexOf'] = a['indexOf']
        this['splice'] = a['splice']
        if(parent) this.parent=parent
        for(var i=0;i<JSONParams.length;i++){
            this.addItem(JSONParams[i]);
        }
    },
    
    addItem:function(JSONitem){
        var r = new OBJSearchItem(JSONitem,this);
        this.push(r);
        return r;
    },
    
    removeItem:function(it){
        var i=this.getIndex(it);
        this.splice(i,1);
        it.remove();
    },
    
    getItem:function(familleId){
        for(var i=0;i<this.length;i++){
            if(this[i].tagFamilleId==familleId){
                return this[i]
            }
        }
        return null
    
    }
}



