﻿/*global $, location*/
'use strict';
$(function () {
    $('#thinkingFilters select.selectThinking').change(function () {
        $("#smallItemContainer").load("?load=thinking&amp;a=1", $("#thinkingFilters").serialize());
    });

    $('#thinkingFilters select.selectNews').change(function () {
        $("#largeItemContainer").load("?load=news&amp;a=1", $("#thinkingFilters").serialize());
    });

    // IE doesn't support 'option' hiding so we need to clone selects and remove tags
    function replaceFilter(container, clonedItem) {
        var currentValue = container.val();
        container.html(clonedItem.clone().html());
        container.val(currentValue);
    }

    var filters = {
        '#clientFilter': $('#clientFilter').clone(),
        '#disciplineFilter': $('#disciplineFilter').clone(),
        '#sectorFilter': $('#sectorFilter').clone()
    };


    function hideEmptyOptions() {
        // currently selected classes
        var classes = [], joinedClasses;
        $('#workFilters select option:selected:not(.all)').each(function () {
            var classAttr = $(this).attr('class'), idRegex = /^([a-z]\d+)\s/, id;
            classAttr = $(this).attr('class');
            idRegex = /^([a-z]\d+)\s/;
            if (classAttr !== undefined && classAttr !== null) {
                id = classAttr.match(idRegex)[1];
                if (id !== undefined && id !== null) {
                    classes.push(id);
                }
            }
        });

        // reset all search forms
        $.each(filters, function (key, value) {
            replaceFilter($(key), value);
        });

        // remove unmatching classes
        if (classes.length > 0) {
            joinedClasses = classes.join('.');

            $.each(filters, function (key, value) {
                if (key !== '#clientFilter') {
                    $(key + ' option:not(.all):not(.' + joinedClasses + ')').remove();
                }
            });
        }
    }

    $('#workFilters select').change(function () {
        // reset discipline and sector on client change
        if ($(this).attr('id') === 'clientFilter') {
            $('#disciplineFilter').val('');
            $('#sectorFilter').val('');
        }

        // hide inactive options
        hideEmptyOptions();

        // refresh page content
        var url, formData;
        url = $('#clientFilter').val() + '?a=1';
        formData = $('#workFilters select:not(#clientFilter)').serialize();
        $.get(url, formData, function (data) {
            var container = $('#workFilters');
            container.nextAll().remove();
            container.after(data);
        });
    });

    // Footer "What we do?" and "How we play" links handling.
    function openChoosenAboutUsSection(hash) {

        if (hash === undefined) {
            hash = location.hash;
        }

        if (hash === undefined || hash === '') {
            return;
        }

        // Close all.        
        $('div.openAccordion').hide().removeClass('openAccordion');
        $('div.aboutAccordion h2 a').removeClass('minusLink');

        // Open by the hash,
        $(hash + ' a.plusLink').addClass('minusLink');
        $(hash).parent().next().children().show().addClass('openAccordion');
    }

    $('#footer a.footer_about_us_link').click(function () {
        var href, hash;
        href = $(this).attr("href");
        hash = href.substring(href.indexOf('#'));
        openChoosenAboutUsSection(hash);
    });

    openChoosenAboutUsSection();
});