var ajaxLoading = false;
var windowHash = location.pathname.substring(1)

if (document.location.hash) {
	document.location = '/' + document.location.hash.substr(1);
}

$(document).ready(function() {

	$('.template_navigation_dynamic a:not(.template_nav_category, .notavailable, .no-ajax)').live('click', function(e) {
		e.preventDefault();

        if (ajaxLoading == true) {
            return false;
        }

		if ($(this).hasClass('ticked')) {
			$(this).removeClass('ticked');
		} else {
			$(this).addClass('ticked');
		}

        var href = $(this).attr('href');
        if (href.indexOf(window.location.hostname) != -1) {
            var hostname = window.location.hostname;
            href = href.substring(href.indexOf(hostname)+hostname.length, href.length);
        }

		windowHash = href.substr(1);
		window.location.hash = windowHash;

        updateSidebarFilter(windowHash, { ajax: true, page_number: '1', current_nav_item: $.trim($(this).attr('rel').replace(' nofollow', '')) });

        return false;
	});
});

function updateSidebarFilter(newHash, data) {
    ajaxLoading = true;
    toggleProductOverlay();

    $.getJSON('/products/'+newHash+'?ajax', data, function (response) {

            if ($('.productlist_grid:visible').length) {
                $('.productlist_grid').html(response[0]);
            } else {
                $('.productlist_list').html(response[0]);
            }

	    $('.productlist_paging').html(response[1]);
	    $('.productlist_filter_showing').html(response[2]);

		html_list = '';

		if (response[4]) {

		$.each(response[4], function(name, values) {

			html_list += '<li><a href="' + values.url + '" class="no-ajax">' + values.category_name + ' <span>(' + values.total_products + ')</span></a></li>';

		});


		if ($('.template_display_categories')) {
			$('.template_display_categories').html(html_list);
		}

		}

            $.each(response[3], function(filter, values) {
                /**
                 * for each filter, find the container
                 */
                var container = $('.'+filter+'_list');
                if (container.length)
                {
                    $.each(values, function(value, data)
                    {
                        /**
                         * find the related link
                         */
                        var link = container.find('a[rel="'+value+'"], a[rel="'+value+' nofollow"]');
                        if (link.length)
                        {
                            link.removeClass('ticked').removeClass('notavailable').removeClass('clickable');
                            link.addClass(data.link_status);

                            if (data.link_status == 'notavailable')
                            {
                                link.removeAttr('href');
                            }
                            else
                            {
                                link.attr('href', data.url);
                            }

                            if (link.find('span').length)
                            {
                                link.find('span').html('('+data.total_products+')');
                            }
                        }
                        return;
                    });

                    /**
                     * check if this container has any 
                     * ticked values, if so, make sure the clear
                     * button is shown, otherwise hide it
                     */
                    var clear = container.prev('a.reset');
                    if (clear.length)
                    {
                        clear.attr('href', values.clear_url);
                        if (container.find('a.ticked').length)
                        {
                            clear.show();
                        }
                        else
                        {
                            clear.hide();
                        }
                    }
                }
        });
        ajaxLoading = false;
        toggleProductOverlay();
	});
}

