<?php
/*
* simonfilters - 2.5.0 Build 0011
*/

if(!$isThereAnyFilterEnabled && $simonfilters_front_end_diagnostics=="true") {
    echo "<fieldset style='border:1px solid red;color:red;border-radius: 5px 5px 5px 5px;'><legend><b>SimonFilters Front End Diagnostics Says:</b></legend>
    <b>Warning:</b> You haven't enabled any filters on the admin side!<br><br>
    Go to admin and enable some filters or you won't see the module!<br><br>
    (Disable 'frontend diagnostics' under the Debug Tab to disable this message.)
</fieldset>";
}elseif(!$filterCount && $simonfilters_front_end_diagnostics=="true") {
    echo "<fieldset style='border:1px solid red;color:red;border-radius: 5px 5px 5px 5px;'><legend><b>SimonFilters Front End Diagnostics Says:</b></legend>
    <b>Warning:</b> There aren't enough selected filters on the admin side to present the filter module!<br><br>
    Go to admin and enable some more filters or you won't see the module!<br><br>
    (Disable 'frontend diagnostics' under the Debug Tab to disable this message.)
</fieldset>";
}

echo '<style type="text/css">'. $css ."</style>";

if($filterCount>0) {
    $urlAjax = preg_replace('/amp;/','',$urlAjax);

    if($XRequestedWith!='SimonFilters') {
        ?>

<script><!--

    // simonfilters - 2.5.0 Build 0011
    $(function(){
        var urlAjax = '<?php echo $urlAjax;?>';
        urlAjax = urlAjax.replace(/&page=[0-9]*/,'');
        var display_type = '<?php echo $display_type;?>';
        var display_totals = '<?php echo $display_totals;?>';
        var containerID = '<?php echo $containerID; ?>';
        var containerIDtarget = '<?php echo $containerIDtarget; ?>';

        var currency_symbol_left = '<?php echo $this->currency->getSymbolLeft();?>';
        var currency_symbol_right = '<?php echo $this->currency->getSymbolRight();?>';
        var currency_code = '<?php echo $this->currency->getCode();?>';

        var position = '<?php echo $position;?>';
        var force_siblings = <?php echo $simonfilters_force_siblings;?>;
        var simonfilters_front_end_diagnostics = <?php echo $simonfilters_front_end_diagnostics;?>;

        var showprice = <?php echo $showprice?'true':'false';?>;
        var simonfilters_dynamic = <?php echo $this->config->get("simonfilters_dynamic")=='1'?'true':'false';?>;
        var debug_mode = <?php echo $debug_mode?'true':'false';?>;

        var minprice = <?php echo isset($prices['minprice'])?$prices['minprice']:0;?>;
        var maxprice = <?php echo isset($prices['maxprice'])?$prices['maxprice']:0;?>;
        var minprice_slider = <?php echo isset($minprice_slider)?$minprice_slider:0;?>;
        var maxprice_slider = <?php echo isset($maxprice_slider)?$maxprice_slider:0;?>;

        var orientation = '<?php echo $orientation;?>';

        var ignore_text = <?php echo $this->config->get("simonfilters_script_ignore_text")!=''?'/'.trim(preg_replace('/;/','|',$this->config->get("simonfilters_script_ignore_text"))).'/gim':'null';?>;
        var ignore = <?php echo $this->config->get("simonfilters_script_ignore")=='1'?'true':'false';?> && ignore_text!=null;

        var sortlis = <?php echo $this->config->get('client_side_items_sorting')=='1'?'true':'false'?>;

        var simonfilters_disableajax = <?php echo $this->config->get('simonfilters_disableajax')=='1'?'true':'false'?>;

        function sortli(){
            $('li.filter_group').each(function(){
                if(orientation=='horizontal')$(this).find("ul").children("li[class='separator']").remove();
                var elems = $(this).find("ul").children("li").remove();
                elems.sort(function(a,b){

                    sortvals = [];
                    if(sortlis){
                        sortvals=[$(a).text().toLowerCase(),$(b).text().toLowerCase()];
                    }else{
                        sortvals=[$(a).data('sort_order'), $(b).data('sort_order')];
                    }
                    if(sortvals[0]<sortvals[1])return -1;
                    if(sortvals[0]>sortvals[1])return 1;
                    return 0;
                });
                $(this).find("ul").append(elems);
                if(orientation=='horizontal'){
                    $(this).find("ul").children("li").each(function(){
                        $(this).after("<li class='separator'>-</li>");
                    });
                }
            });
        }
        sortli();

        <?php if($simondebugjs1) {
            echo($simondebugjs1);
        }?>

                //$(".category-info").after($(".box.simonfilters"));

                $("ul.text_type_horizontal li.add_filter").live("mouseover mouseout",function (event) {if (event.type == "mouseover"){$(this).addClass("simonover");}else{$(this).removeClass("simonover")};})

                if(simonfilters_front_end_diagnostics){
                    if($(containerID).length==0){
                        alert('Warning: Simonfilters is incorrectly configured.\n\nGo to admin and enter valid HTML Object IDs.\n\nIf problem persists, disable SimonFilters and contact Simon: oc@simonoop.com');
                        return false;
                    }
                }

                $(".add_filter, .clear_filters_local, .clear_filters_global").die();
                $(".add_filter, .clear_filters_local, .clear_filters_global").unbind();
                $("li.collapsible div.filter_title").die();
                $("li.collapsible div.filter_title").unbind();

                $(".add_filter, .clear_filters_local, .clear_filters_global").live('click', function(){
                    switch($(this).hasClass('clear_filters_local')||$(this).hasClass('clear_filters_global')){
                        case true:
                            if($(this).hasClass('clear_filters_local')){
                                $(this).closest("li").find("li.add_filter").removeClass('checked').addClass('unchecked');
                                $(this).fadeOut(100);
                                $checkedfilters = $($(".add_filter[class~='checked']").map(function() {  return this.id; })).toArray() ;
                                if($checkedfilters.length==0)$(".clear_filters_global").fadeOut(100);
                            }
                            if($(this).hasClass('clear_filters_global')){
                                $(".add_filter:not([class*='simonfilters_price'])").removeClass('checked').addClass('unchecked');
                                $(".clear_filters_local").hide();
                                $(this).fadeOut(100);

                                if(showprice){
                                    minprice_slider = <?php echo isset($prices['minprice'])?$prices['minprice']:0;?>;
                                    maxprice_slider = <?php echo isset($prices['maxprice'])?$prices['maxprice']:0;?>;
                                    $( "#simonfilters_price-range" ).removeClass("checked_price");
                                }
                            }
                            break;
                        case false:
                            $(this).toggleClass('checked').toggleClass('unchecked');
                            $checkedfilters = $($(".add_filter[class~='checked']").map(function() {  return this.id; })).toArray() ;
                            $(this).hasClass('checked')?$(this).closest('li.filter_group').find('.clear_filters_local').show():$(this).closest('li.filter_group').find('.clear_filters_local').hide();
                            $checkedfilters.length==0?$(".clear_filters_global").hide():$(".clear_filters_global").show();
                            break;
                        }
                        simondoit();

                    });

                    var simondoit = function(){

                        if(force_siblings || position=='content_top'){
                            $(".simonfilters").siblings().not("style,script").fadeTo(200, 0.20);
                        }else{
                            $(containerID).fadeTo(200, 0.20);
                        }

                        $checkedfilters =  $($(".add_filter[class~='checked']").map(function() {  return this.id; })).toArray() ;

                        $pricefilters = {};
                        if($( "#simonfilters_price-range" ).length>0 && $( "#simonfilters_price-range" ).hasClass('checked_price')){
                            $priceRange = $( "#simonfilters_price-range" ).slider("values");
                            $priceRange[0]=$priceRange[0].toString().replace(/[\.,]/,'SFS');
                            $priceRange[1]=$priceRange[1].toString().replace(/[\.,]/,'SFS');
                            $checkedfilters.push('p.1.'+ $priceRange);
                        }

                        function getURL(theUrl, extraParameters) {
                            var extraParametersEncoded = $.param(extraParameters);
                            var seperator = theUrl.indexOf('?') == -1 ? "?" : "&";

                            return(theUrl + seperator + extraParametersEncoded);
                        }

                        if(simonfilters_disableajax){
                            url = urlAjax;
                            url = url.replace(/&forcefiltersupdate=.*/,'');
                            url = url.replace(/forcefiltersupdate=.*/,'');
                            url = getURL(url, {forcefiltersupdate:true, checkedfilters:$checkedfilters});
                            document.location.href=url;
                        }else{
                            $.ajax({
                                type: 'GET',
                                data: {forcefiltersupdate:true, checkedfilters:$checkedfilters},
                                url: urlAjax,
                                headers: {"X-Requested-With":"SimonFilters"},
                                success: function(data){
                                    $data =$(data);

                                    if(simonfilters_dynamic){
                                        //alert('dynamic');

                                        $(containerID).html($data.find(containerIDtarget).html());

                                        $filterHTML = $data.find("div.simonfilters").html();
                                        //alert($filterHTML);
                                        $("div.simonfilters").html( $filterHTML );
                                    }else{
                                        simonfilters_current_html = $(".simonfilters").html();
                                        $data.find(".simonfilters").html(simonfilters_current_html);

                                        $(containerID).html($data.find(containerIDtarget).html());
                                    }

                                    $data.filter("script").each(function(idx, val) {
                                        if(val.text!=''){
                                            if(!ignore || (ignore && !val.text.match(ignore_text))){
                                                evaltext =val.text;
                                                if(!evaltext.match(/document.write/)){
                                                    evaltext = evaltext.replace(/\/\/-->/,'');
                                                    evaltext = evaltext.replace(/<!--/,'');
                                                    try{
                                                        eval(evaltext);
                                                    }catch(err){}
                                                }
                                            }
                                        }
                                    });
                                    if(force_siblings || position=='content_top'){
                                        $(".simonfilters").siblings().not("style,script").fadeTo(100, 1);
                                    }else{
                                        $(containerID).fadeTo(100, 1);
                                    }

                                    sortli();

                                    if(showprice) {
                                        if(simonfilters_dynamic){
                                            minprice = parseFloat($data.find("#simonfilters_transported_minprice").val());
                                            minprice_slider = parseFloat($data.find("#simonfilters_transported_minprice_slider").val());
                                            maxprice = parseFloat($data.find("#simonfilters_transported_maxprice").val());
                                            maxprice_slider = parseFloat($data.find("#simonfilters_transported_maxprice_slider").val());
                                        }
                                        $( "#simonfilters_price-range" ).addClass('checked_price')
                                        doPriceStuff();
                                    }
        <?php echo $simondebugjs2;?>
                                }
                            });
                        }
                    }


                    doPriceStuff();
                    function doPriceStuff(){
                        $("li[filtertype='p']").show();
                        if(minprice!=minprice_slider || maxprice!=maxprice_slider){$(".clear_filters_global").show()}
                        $( "#simonfilters_price_amount" ).text( currency_symbol_left + minprice_slider + currency_symbol_right +" - " + currency_symbol_left + maxprice_slider + currency_symbol_right)
                        $( "#simonfilters_price-range" ).slider({
                            range: true,
                            min: minprice,
                            max: maxprice,
                            step: <?php echo $this->config->get("simonfilters_slider_step");?>,
                            animate: true,
                            values: [ minprice_slider,  maxprice_slider ],
                            slide: function( event, ui ) {
                                $( "#simonfilters_price_amount" ).text( currency_symbol_left + ui.values[ 0 ] + currency_symbol_right +" - " + currency_symbol_left + ui.values[ 1 ] + currency_symbol_right );
                            },
                            change: function(event, ui) {
                                $( "#simonfilters_price-range" ).addClass("checked_price");
                                $(".clear_filters_global").show();

                                slidervalues = $( "#simonfilters_price-range" ).slider("values");
                                minprice_slider = slidervalues[0];
                                maxprice_slider = slidervalues[1];
                                if (event.originalEvent) {
                                    simondoit();
                                }
                            }
                        });
                        if(minprice == maxprice){
                            $("li[filtertype='p']").hide();
                        }
                    }
                    $("li.collapsible div.filter_title").live('click', function(){
                        $(this).fadeTo(100,.2, function(){$(this).fadeTo(100,1)});
                        $(this).parent().toggleClass('collapsed').toggleClass('expanded');
                        $(this).parent().hasClass('collapsed')?
                            $(this).next("div.filter_ul").fadeOut(200):$(this).next("div.filter_ul").fadeIn(200);
                    });
                });
                //--></script>
        <?php
    }

    ?>
<div class="box simonfilters">
    <div class="box-heading"><?php echo $heading_title; ?>
        <span class="clear_filters_global_container">
            <span class='clear_filters_global' <?php echo @$clear_filters_global_display;?>><?php echo $clear_filters_title;?></span>
        </span>
    </div>
    <div class="box-content">
        <div class="box-filter">
                <?php echo $HTML;?>
                <?php if($showprice) {?>
            <input type="hidden" id="simonfilters_transported_minprice" value="<?php echo $prices['minprice'];?>">
            <input type="hidden" id="simonfilters_transported_minprice_slider" value="<?php if($minprice_slider>$prices['minprice'] && $minprice_slider<$prices['maxprice']) {
                        echo $minprice_slider;
                    }else {
                        echo $prices['minprice'];
                           }?>">
            <input type="hidden" id="simonfilters_transported_maxprice" value="<?php echo $prices['maxprice'];?>">
            <input type="hidden" id="simonfilters_transported_maxprice_slider" value="<?php if($maxprice_slider<$prices['maxprice'] && $maxprice_slider>$prices['minprice']) {
                        echo $maxprice_slider;
                    }else {
                        echo $prices['maxprice'];
                           }?>">
                           <?php }?>
        </div>
    </div>
</div>
    <?php
}else {
    if($debug_mode) {
        ?>
<script><!--
        <?php echo $simondebugjs1;?>
                //--></script>
        <?php
    }
}
?>