/*
SearchFilters = {
    processing:false,
    skipUrls:['/dmo/view/','#'],
    Product: {
      init: function () {
          var oSpan=$('.fiche_produit .nouvelle_recherche span.retour_resultat');
            oSpan.attr('href', '').click(function() {
                                            $.ez('ezjscfilters::search',{},function(data){
                                                                                SearchFilters.response(data);
                                                                           });
                                            return false;
                                        });
            oSpan.siblings('span').eq(0).click(function() {
                                                 $.ez('ezjscfilters::reset',{},function(data){
                                                                                    SearchFilters.response(data);
                                                                               });

                                                 return false;
                                               });
      }
    },
    Products: {
      init:function() {
        var iI=0;
        $('a[href="#visuel_zoom_catalogue"]').each(function() {
               var oParent=$(this).parent();
               var sID=$(this).attr('id');
               sID=sID.replace('node_', '');
               sID=sID.replace('_zoom_link','');
               $('a',oParent).each(function(){
                   var sHref=$(this).attr('href');
                   var bValid=true;
                   if(sHref) {
                        for(var i=0,j=SearchFilters.skipUrls.length;i<j;i++) {
                            if(sHref.indexOf(SearchFilters.skipUrls[i])!=-1) {
                                bValid=false;
                                break;
                            }
                        }
                    }
                    if(bValid) {
                        $(this).attr('search_filter',sID);
                        $(this).attr('search_filter_index',iI);
                        $(this).click(function() {
                                        SearchFilters.Products.link(this);
                                        return false;
                                      });
                    }
               });
               iI++;
        });
      },
      link:function(oRef) {
            var sID=$(oRef).attr('search_filter');
            $.ez('ezjscfilters::productpage',{
                                            node_id:sID,
                                            index:$(oRef).attr('search_filter_index')
                                        },  function(data){
                                                SearchFilters.Products.load(data);
                                            }
            );
      },
      load:function(oData) {
            var oContent=jQuery(oData.content);
            var sContent=jQuery(oContent.filter('.big_col_right')).eq(0).html();
            $('#search_results_cont').html(sContent);
            var oSpan=$('.fiche_produit .nouvelle_recherche span.retour_resultat');
            oSpan.attr('href', '').click(function() {
                                            $.ez('ezjscfilters::search',{},function(data){
                                                                                SearchFilters.response(data);
                                                                           });
                                            return false;
                                        });
            oSpan.siblings('span').eq(0).click(function() {
                                                 $.ez('ezjscfilters::reset',{},function(data){
                                                                                    SearchFilters.response(data);
                                                                               });

                                                 return false;
                                               });
            $('.bloc_suivant_precedent .suivant').click(function(){
                                                            $.ez('ezjscfilters::nextproduct',{},function(data){
                                                                                                    SearchFilters.Products.load(data);
                                                                                                });
                                                            return false;
                                                        });
            $('.bloc_suivant_precedent .precedent').click(function(){
                                                            $.ez('ezjscfilters::prevproduct',{},function(data){
                                                                                                    SearchFilters.Products.load(data);
                                                                                                });
                                                            return false;
                                                        });
        },
      no_prev_link:function() {
          $('.bloc_suivant_precedent .suivant').hide();
      }
    },
    init:function() {
      /*  $('input[type="checkbox"]').each(function() {
            if($(this).attr('checked')!="checked")
                $(this).removeAttr('checked');
        });
        $('input[type="checkbox"]').click(function(){
            $.ez('ezjscfilters::search',{attr:this.value,mode:this.checked},function(data){SearchFilters.response(data)});
        });
        $('input[type="radio"]').click(function() {
            if(this.value.indexOf('^')!=-1) {
                var aData=this.value.split('^');
                var sUrl=aData[1];
               $.ez('ezjscfilters::scope',{scope:aData[0]},function(data) {
                   if(!data.error_text)
                        window.location=sUrl;
               });
            }
            else {
                $.ez('ezjscfilters::classname',{class_name:this.value},function(data){SearchFilters.response(data);
                    })
            }
        });
       // SearchFilters.Products.init();
        SearchFilters.initNav();
    },
    response:function(data) {
        //$('#search_results_cont').html(data.content);
        $(".big_col_right").eq(0).html(data.content);
        SearchFilters.initNav();
      //  SearchFilters.Products.init();
        zoomInit();
    },
    
    initNav:function() {
        var oNav=$('#filters_navigator');
        if(oNav.length) {
            $('a.prev_page',oNav).click(
                        function() {
                            $.ez('ezjscfilters::prevpage',{},function(data){SearchFilters.response(data)})
                            return false;
                            }
                        )
            ;
            $('a.next_page',oNav).click( function() {
                                $.ez('ezjscfilters::nextpage',{},function(data){SearchFilters.response(data)})
                                return false;
                            }
                        );
            $('a.switch_page',oNav).click(function() {
                                            $.ez('ezjscfilters::page',{page:$(this).html()},function(data){SearchFilters.response(data)});
                                            return false;
                                          }
                                    );
            $('a.actif',oNav).click(function(){return false;})
        }
    }
}
*/

SearchFilters = {
    skipUrls:['/dmo/view/','#'],
    Queue:{
        attrs:new Array(),
        mods:new Array(),
        processing:false,
        append:function(oReq) {
            var iLen=SearchFilters.Queue.attrs.length;
            if(SearchFilters.Queue.attrs[iLen-2]==oReq.attr&&SearchFilters.Queue.attrs[iLen-1]==oReq.mode)
                return;
            SearchFilters.Queue.attrs.push(oReq.attr);
            SearchFilters.Queue.attrs.push(oReq.mode);
        },
		Loader: {
			inited:false,
			show:function() {
				SearchFilters.Queue.Loader.init();
				$('#filter_loader_cont').show();
				$('.big_col_right div').filter('.catalogue_lentilles, .catalogue_lunettes, .fiche_produit_lentille, .fiche_produit, .marque_catalogue').hide();
			},
			hide:function() {
				SearchFilters.Queue.Loader.init();
				$('#filter_loader_cont').hide();
			},
			init:function() {
				if(SearchFilters.Queue.Loader.inited&&$('#filter_loader_cont').length) {
					return;
				}
				var sHtml=$('#filters_loader').attr('src');
				$('.big_col_right .fil_ariane').eq(0).after('<div style="display:none;text-align:center;margin:200px 0" id="filter_loader_cont"><img src="'+sHtml+'" /></div>');
				SearchFilters.Queue.Loader.inited=true;
			}
		},
        process:function () {
            if(SearchFilters.Queue.processing!=false) {
				$('#filter_form input[type="checkbox"]').siblings('select').each(function(i,e){ if(!$(this).siblings('input').eq(0).attr('checked')) $(this).remove(); })
				$('#filter_form input[type="checkbox"]').siblings('select').filter(':gt(0)').remove();
                return;
            }
            if(!SearchFilters.Queue.attrs.length) {
				SearchFilters.Queue.Loader.hide();
                return;
			}
            SearchFilters.Queue.processing=true;
			SearchFilters.Queue.Loader.show();
            var sAttr=SearchFilters.Queue.attrs.shift();
            var sMod=SearchFilters.Queue.attrs.shift();
            
            if(!sAttr||sMod==undefined)
                return;
            if(typeof(sMod)=='string') {
                var cFirst=sMod.substr(0,1);
                var sBrand=sMod.substr(1,sMod.length-1);
                if(cFirst=='+')
                    sMod=true;
                else
                    sMod=false;
            $.ez('ezjscfilters::collectionfilter',{attr:sAttr,mode:sMod,brand:sBrand,current:window.active_filters?window.active_filters:''},function(data){
                    SearchFilters.Queue.processing=false;
                    SearchFilters.response(data);
                    SearchFilters.Queue.process();
                });

            }
            else {
            $.ez('ezjscfilters::search',{attr:sAttr,mode:sMod,current:window.active_filters?window.active_filters:''},function(data){
                    SearchFilters.Queue.processing=false;
                    SearchFilters.response(data);
                    SearchFilters.Queue.process();
                });
            }
            $('#filter_form input[type="checkbox"]').siblings('select').each(function(i,e){ if(!$(this).siblings('input').eq(0).attr('checked')) $(this).remove(); })
			$('#filter_form input[type="checkbox"]').siblings('select').filter(':gt(0)').remove();
        }
    },
    Navigator:{
    	fix:function() {

        		$('#page').addClass('catalogue lunettes');
        		adjust();
    	},
        init:function() {
        	
        	
            var oNav=$('.content_affichageproduit .droite').eq(0);
            $('a',oNav).each(function() {
               if($(this).hasClass('precedent')&&!$(this).hasClass('inactif')) {
                   $(this).click(function() {$.ez('ezjscfilters::prevpage',{current:window.active_filters?window.active_filters:''},function(data){SearchFilters.response(data)})
                            return false;});
               }
               else {

                   if($(this).hasClass('suivant')&&!$(this).hasClass('inactif')) {
                       $(this).click(function() {
												   SearchFilters.Queue.Loader.show();
								$.ez('ezjscfilters::nextpage',{current:window.active_filters?window.active_filters:''},function(data){SearchFilters.response(data)})
                                return false;});
                   }
                   else {
                       $(this).click(function() {
										SearchFilters.Queue.Loader.show();
										$.ez('ezjscfilters::page',{page:$(this).html(),current:window.active_filters?window.active_filters:''},function(data){SearchFilters.response(data)});
                                            return false;});
                   }
               }
            });

        }

    },
    init:function() {
        SearchFilters.Navigator.init();
        $('#filter_form li.toggleSubMenu>span').click(function() {
           var sAction='remove';
           if($(this).siblings('ul').eq(0).hasClass('menu_cache'))
               sAction='add';
           $.ez('ezjscfilters::tabstate',{tab:$(this).html(),action:sAction});
        });
          $('input[type="checkbox"]').click(function(){

              SearchFilters.Queue.append({attr:this.value,mode:this.checked});
              SearchFilters.Queue.process();
              var aSelects=$('select',$(this).parent())
              if(aSelects) {
                  aSelects.remove();
              }
        });
        $('input[type="radio"]').click(function() {
			SearchFilters.Queue.Loader.show();
            if(this.value.indexOf('^')!=-1) {
                var aData=this.value.split('^');
                var sUrl=aData[1];
               $.ez('ezjscfilters::scope',{scope:aData[0]},function(data) {
                   if(!data.error_text)
                   {
                        window.location=sUrl;
                        // window.location=data.content;
                    }
               });
            }
            else {
                $.ez('ezjscfilters::classname',{class_name:this.value},function(data){
					if(!data.error_text) {
							if(!data.content)
	                        	window.location.reload();
							else {
								window.location=$('#parent_node_url').html();
							}
					}
//						SearchFilters.response(data);
                    })
            }
        });
        $('#filter_form select').change(function() {
                var sAttr=$(this).val();
                var sMode;
                var sBrand=$(this).siblings('label').eq(0).html();
                sBrand=trim(sBrand);
                if(sAttr=="-1") {
                   sAttr=$(this).attr('previously_checked');
                   sMode='-'+sBrand;
                }
                else {

                   $(this).attr('previously_checked',sAttr);
                   sMode='+'+sBrand;
                }
                SearchFilters.Queue.append({attr:sAttr,mode:sMode});
                SearchFilters.Queue.process();
            });
        SearchFilters.Product.init();
        SearchFilters.Products.init();
		SearchFilters.Queue.Loader.init();
    },
    Products: {
      init:function() {
          var iI=0;
        $('a[href="#visuel_zoom_catalogue"],a.lens_read_more_link').each(function() {
               var oParent=$(this).parent();
               var sID=$(this).attr('id');
               sID=sID.replace('node_', '');
               sID=sID.replace('_zoom_link','');
               $('a',oParent).each(function(){
                   var sHref=$(this).attr('href');
                   var bValid=true;
                   if(sHref) {
                        for(var i=0,j=SearchFilters.skipUrls.length;i<j;i++) {
                            if(sHref.indexOf(SearchFilters.skipUrls[i])!=-1) {
                                bValid=false;
                                break;
                            }
                        }
                    }
                    if(bValid) {
                        $(this).attr('search_filter',sID);
                        $(this).attr('search_filter_index',iI);
                        $(this).click(function() {
                                        SearchFilters.Products.link(this);
                                        return false;
                                      });
                    }
               });
               iI++;
        });
      },
      link:function(oRef) {
            var sID=$(oRef).attr('search_filter');
            $.ez('ezjscfilters::productpage',{
                                            node_id:sID,
                                            index:$(oRef).attr('search_filter_index')
                                        },  function(data){

                                                window.location=oRef.href;
                                            }
            );
      }
    },
    response:function(data) {
        $('.big_col_right').html(data.content);
        if($('#special_content')) {
            $(' div',$('#special_content')).each(function() {
               var sID=$(this).attr('id');
               var sFor=sID.substr(4,sID.length-4);
               var oTarget=$('input[id="'+sFor+'"]').eq(0);
               if(oTarget) {
                   oTarget.parent().append($(this).html());
               }
            });
            $('#special_content').remove();
            $('#filter_form select').change(function() {
                var sAttr=$(this).val();
                var sMode;
                var sBrand=$(this).siblings('label').eq(0).html();
                sBrand=trim(sBrand);
                if(sAttr=="-1") {
                   sAttr=$(this).attr('previously_checked');
                   sMode='-'+sBrand;
                }
                else {

                   $(this).attr('previously_checked',sAttr);
                   sMode='+'+sBrand;
                }
                SearchFilters.Queue.append({attr:sAttr,mode:sMode});
                SearchFilters.Queue.process();


            });
        }
        SearchFilters.Navigator.init();
        SearchFilters.Products.init();
        SearchFilters.Navigator.fix();	
		$('#filter_form input[type="checkbox"]').siblings('select').each(function(i,e){ if(!$(this).siblings('input').eq(0).attr('checked')) $(this).remove(); })
		$('#filter_form input[type="checkbox"]').siblings('select').filter(':gt(0)').remove();
    },
    Product: {
        init:function() {
            $('.retour_resultat').click(function(){
                $.ez('ezjscfilters::back',{},function(data){
                    if(!data.error_text) {
                        var sCurr=window.location.href;
                        var iPos=sCurr.lastIndexOf('/');
                        var sNew=sCurr.substr(0, iPos);
                        window.location=sNew;
                    }

                });
                return false;
            });
            $('span.precedent').click(function() {
                $.ez('ezjscfilters::prevproduct',{},function(data){
                   if(!data.error_text)  {
                       window.location=data.content;
                   
                   }
                });
            });
            $('span.suivant').click(function() {
                $.ez('ezjscfilters::nextproduct',{},function(data){
                   if(!data.error_text)  {
                       window.location=data.content;

                   }
                });

                return false;
            });
        }
    }



}

$('document').ready(function() {
    SearchFilters.init();


})

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
