﻿/*
 ********************************************************************
 ** Product Filter JS ***********************************************
 ** Intended for use with Default.aspx under category directories. **
 ********************************************************************
 *
 * Copyright (C)2009 SHTEC.  All Rights Reserved.
 * This file is only allowed to be distributed via http://panasonic.hk/av/
 *
 * Init version: Simon WONG 20090707
 *
 */

// Apply disabled effect when the ShowHitOnly is currently active

var IsNoFilterAtShowAll = true;

$(document).ready(function() {
    // *INIT* //
    // Hide all productitems
    // applyProductFilters() will take care of this: 
    $('div.pdtouter').css('visibility', 'hidden');
    $('input.tagcb:checkbox').addClass('jscb');
    $('input#fShowAllCb').attr('checked', '').removeAttr('checked');
    $('div.pdtinner a.nojsfb').removeClass('nojsfb');




    $('a#fShowHitOnly').live('click', function(ee) {
        //ee.preventDefault();
        $('input#fShowAllCb').removeAttr('checked');
        $('input.jscb:checked').removeAttr('checked'); // Clear all filters as well
        //            {
        //                $('a#fShowHitOnly').addClass('fInUse');
        //                $('a#fShowAll').removeClass('fInUse');
        //            }
        $(this).blur();
        $(document).trigger({
            type: "CB_STATE_CHANGED",
            state: "af"
        });
        return false;
    });
    $('a#fShowAll').live('click', function(ee) {
        //ee.preventDefault();

        if ($('input#fShowAllCb').attr('checked') == '' || $('input#fShowAllCb').attr('checked') == false) {
            $('input#fShowAllCb').attr('checked', 'checked').data('NOFILTERRESET', '1');
        } else {
            $('input.jscb:checked').removeAttr('checked'); // Clear all filters as well
        }
        //            {
        //                $('a#fShowHitOnly').removeClass('fInUse');
        //                $('a#fShowAll').addClass('fInUse');
        //            }
        $(this).blur();
        $(document).trigger({
            type: "CB_STATE_CHANGED",
            state: "af"
        });
        return false;
    });


    $('div.pdtinner a').hover(
		    function() {
		        $(this).parent().parent().find('div.pdtfmSel')
		        .stop(true, true).fadeIn('slow', function(ee) { });
		    },
            function() {
                $(this).parent().parent().find('div.pdtfmSel')
		        .stop(true, true).fadeOut('slow', function(ee) { });
            }
    	);
    /*
    $('div.pdtinner').hover(
    function() {
    $(this).parent().find('div.pdtfmSel')
    .stop(true, true).fadeIn('slow', function(ee) { });
    },
    function() {
    $(this).parent().find('div.pdtfmSel')
    .stop(true, true).fadeOut('slow', function(ee) { });
    }
    );*/

    $('input.jscb:checkbox').live('click', function(ee) {
        $(this).blur();
        ee.stopPropagation();

        if ($(this).attr('disabled') != '' && $('input#fShowAllCb').attr('checked') == true) {
            return;
        }

        if ($(this).attr('checked') == true) {


            // BEGIN - LOGIC:NO_FILTER_ON_HIT_PRODUCT
            //$('input#fShowAllCb').attr('checked', 'checked');
            //$('a#fShowHitOnly').removeClass('fInUse');
            //$('a#fShowAll').addClass('fInUse');
            // END - LOGIC:NO_FILTER_ON_HIT_PRODUCT

            $(this).parent().parent().find('span.cboz').removeClass('cbozunchecked').addClass('cbozchecked');
            $(this).parent().parent().find('span.ctxtz').removeClass('ctxtzunchecked').addClass('ctxtzchecked');

            if ($('input#fShowAllCb').attr('checked') == false) {
                $('a#fShowAll').click();
                return;
            }
        } else {
            $(this).parent().parent().find('span.cboz').removeClass('cbozchecked').addClass('cbozunchecked');
            $(this).parent().parent().find('span.ctxtz').removeClass('ctxtzchecked').addClass('ctxtzunchecked');
        }
        //$('#debug').html("!" + $(this).attr('checked'));

        $(document).trigger('CB_STATE_CHANGED');
    });


    $('input.jscb:checkbox').livequery('change', function(ee) {
        //ee.preventDefault();
        $(this).blur();
        ee.stopPropagation();

        if ($(this).attr('disabled') != '') {
            return;
        }

        if ($(this).attr('checked') == true) {
            $(this).parent().parent().find('span.cboz').removeClass('cbozunchecked').addClass('cbozchecked');
            $(this).parent().parent().find('span.ctxtz').removeClass('ctxtzunchecked').addClass('ctxtzchecked');
        } else {
            $(this).parent().parent().find('span.cboz').removeClass('cbozchecked').addClass('cbozunchecked');
            $(this).parent().parent().find('span.ctxtz').removeClass('ctxtzchecked').addClass('ctxtzunchecked');
        }
        //$('#debug').html("!" + $(this).attr('checked'));

        $(document).trigger('CB_STATE_CHANGED');
    });

    $(document).bind('CB_STATE_CHANGED', function(ee) {
        $('input.jscb').attr('disabled', 'disabled');
        if (typeof (ee) != 'undefined' && ee['state'] == 'af') {
            $.historyLoad(($('input#fShowAllCb').attr('checked') == true) ? "all" : "hit");
        } else {
            setTimeout(function() { applyProductFilters(); }, 10);
        }
    });
    $(document).bind('REFRESH_LAYOUT', function(ee) {
        setTimeout(function() { refreshLayout(); }, 10);
    });

    // *DONE INIT* //
    if (typeof (document.location.hash) != 'undefined' && document.location.hash != '') {
        $.historyInit(pageload, "" + document.location.hash);
        //$.historyInit(pageload, "");
        /*
        if ((document.location.hash + "").indexOf('hit') > -1) {
        $('a#fShowHitOnly').click();
        } else {
        $('a#fShowAll').click();
        }
        */
    }
    else {
        $.historyInit(pageload, "");
        $('a#fShowHitOnly').click();
    }


    /* HELPERS */
    function pageload(hash) {
        //alert("pageload: " + hash);
        // hash doesn't contain the first # character.

        hash = hash.replace(/^.*#/, '');

        if (hash == "*") return;

        //if (hash) {



        // restore ajax loaded state
        if ($.browser.msie) {
            // jquery's $.load() function does't work when hash include special characters like åäö.
            hash = encodeURIComponent(hash);
        }
        //$("#load").load(hash + ".html");

        if (hash == 'all') {

            $('input#fShowAllCb').attr('checked', 'checked');
            
            if ($('input#fShowAllCb').data('NOFILTERRESET') != '1') {
                $('input.jscb:checked').removeAttr('checked'); // Clear all filters as well if not invoked by filter click
            }
            {
                $('a#fShowHitOnly').removeClass('fInUse');
                $('a#fShowAll').addClass('fInUse');
            }
            setTimeout(function() { applyProductFilters(); }, 10);
        } else {

            $('input#fShowAllCb').removeAttr('checked');
            $('input.jscb:checked').removeAttr('checked'); // Clear all filters as well
            {
                $('a#fShowHitOnly').addClass('fInUse');
                $('a#fShowAll').removeClass('fInUse');
            }
            setTimeout(function() { applyProductFilters(); }, 10);
        }
        //} else {
        // start page
        //$("#load").empty();
        //$('a#fShowHitOnly').click();
        //alert("!3");
        //}
        return false;
    }
    function refreshLayout() {
        // ALWAYS ON THE LAST LINE
        $('div#filterContainer').css('visibility', 'visible');
        $('div#axinner').css('visibility', 'visible');
        $('a#fShowAll').css('visibility', 'visible');
        $('a#fShowHitOnly').css('visibility', 'visible');
        // Assign the background-image class to the product if the client supports JS
        $('div.pdtfm').removeClass('pdtfmSel').addClass('pdtfmSel').each(function(idx) {
            $(this).height($(this).parent().find('div.pdtinner a').height() + 'px')
                .stop(true, true).fadeOut(0);
        })

        document.title = $("a#catnloc").html();
    }
    function applyProductFilters() {
        var d = new Date();

        //$('input.tagcb').removeClass('jscb');   // temporarily revoke

        if ($('input.jscb:checkbox:checked').size() < 1) {
            $('div.pdtouter').css('visibility', 'visible').css('position', 'relative').css('left', '0px');
            //$('span.cboz').removeClass().addClass('cboz').addClass('cbozunchecked');
        } else {
            var checkedF = new Array();

            $('input.jscb:checked').each(function(ee) {
                checkedF.push($(this).attr('rel'));
            });

            $('div.pdtouter').each(function(eee) {
                var show = false;
                var matchCountAND = 0;

                for (var i = 0; i < checkedF.length; i++) {
                    if ($(this).attr('rel').indexOf("," + checkedF[i] + ",") > -1) {
                        //show = true;
                        //break;
                        matchCountAND++;
                    }
                }

                show = (matchCountAND == checkedF.length);

                if (show) {
                    $(this).css('visibility', 'visible').css('position', 'relative').css('left', '0px');
                } else {
                    $(this).css('visibility', 'hidden').css('position', 'absolute').css('left', '-9999px');
                }
            });
        }


        if ($('input#fShowAllCb').attr('checked') != true) {
            $('div.pdtouter').each(function(eee) {
                if (!$(this).hasClass('pdthit')) {
                    $(this).css('visibility', 'hidden').css('position', 'absolute').css('left', '-9999px');
                }
            });
        }

        var filteredCount = 0;
        $('div.scPdtList').each(function(ee) {
            var po = $(this).find('div.pdtouter');
            var found = false;

            po.each(function(eee) {
                if ($(this).css('visibility') != 'hidden') {
                    found = true;
                    filteredCount++;    // NO return false TO ALLOW COUNTING;
                }
            });

            if (found) {
                //$(this).css('visibility', 'visible').css('position', 'relative');
                $(this).css('display', 'block');
            } else {
                $(this).css('display', 'none');
                //$(this).css('visibility', 'hidden').css('position', 'absolute');
            }
        });

        $('span#filteredCount').html(filteredCount);

        $('input.jscb:checkbox').each(function(ee) {
            var refcb = $(this);
            var cboz = refcb.parent().parent().find('span.cboz');
            var ctxtz = refcb.parent().parent().find('span.ctxtz');
            var tx = ',' + refcb.attr('rel') + ',';

            var found = false;

            // LOGIC:NO_FILTER_ON_HIT_PRODUCT
            // If current is Hit
            if (IsNoFilterAtShowAll && $('input#fShowAllCb').attr('checked') != true) {
                found = true;
                // so that all checkboxes are enabled, skip the loop right away...
            } else {
                $('div.pdtouter').each(function(eee) {
                    if ($(this).css('visibility') == 'hidden') return true;

                    var tmprel = $(this).attr('rel');
                    if (tmprel.indexOf(tx) > -1) {
                        found = true;
                        return false;
                    }
                });
            }



            if (found) {
                if ($(this).attr('checked') != '') {
                    cboz.removeClass().addClass('cboz').addClass('cbozchecked');
                    ctxtz.removeClass().addClass('ctxtz').addClass('ctxtzchecked');
                } else {
                    cboz.removeClass().addClass('cboz').addClass('cbozunchecked');
                    ctxtz.removeClass().addClass('ctxtz').addClass('ctxtzunchecked');
                }
                $(this).removeAttr('disabled');
            } else {
                cboz.removeClass().addClass('cboz').addClass('cbozdisabled');
                ctxtz.removeClass().addClass('ctxtz').addClass('ctxtzdisabled');
                $(this).attr('disabled', 'disabled');
            }
        });



        // ALWAYS ON THE LAST LINE
        $(document).trigger('REFRESH_LAYOUT');
    }
});
