/* Author: Jonathan Guitton, heavily inspired by the isotope exemple sources :-)

*/
$(document).ready(function () {
    "use strict";
    //fancybox for images
    $('.fancy').fancybox({
        padding: 3,
        margin: 50,
        topRatio: 0,
        prevEffect: 'fade',
        nextEffect: 'fade'
    });
    //fancybox for inline html content
    $('.fancy-inline').fancybox({
        padding: 3,
        margin: 50,
        topRatio: 0,
        prevEffect: 'fade',
        nextEffect: 'fade',
        type: 'inline'
    });

    //Isotope with hash history (BBQ)
    $('#subcontainer img').imagesLoaded(function () {
        $('#patience').hide();
        $('div.item.hidden').removeClass('hidden');
        var $container, isotopeOptions, defaultOptions, setUpOptions, $optionSets, isOptionLinkClicked, hashChanged;
        $container = $('#subcontainer');
        isotopeOptions = {};
        defaultOptions = {
            filter: '.accueil',
            layoutMode: 'masonry'
        };
        setUpOptions = $.extend({}, defaultOptions, {
            itemSelector: '.item',
            masonry: {
                columnWidth: 105
            }
        });
        $container.isotope(setUpOptions);
        $optionSets = $('#options').find('.option-set');
        isOptionLinkClicked = false;

        function changeSelectedLink($elem) {
            $elem.parents('.option-set').find('.selected').removeClass('selected');
            $elem.addClass('selected');

            var id = $elem.attr('id');
            if (id === 'optionContact') {
                $('#lienContact').removeClass('hidden');
            } else {
                $('#lienContact').addClass('hidden');
            }
            if (id === 'optionNoirEtBlanc') {
                $('#lienNoirEtBlanc').removeClass('hidden');
            } else {
                $('#lienNoirEtBlanc').addClass('hidden');
            }
        }
        $optionSets.find('a').click(function (e) {
            e.preventDefault();
            var $this, href, option;
            $this = $(this);
            if ($this.hasClass('selected')) {
                return;
            }
            changeSelectedLink($this);
            href = $this.attr('href').replace(/^#/, '');
            option = $.deparam(href, true);
            $.extend(isotopeOptions, option);
            $.bbq.pushState(isotopeOptions);
            isOptionLinkClicked = true;
            return false;
        });
        hashChanged = false;
        $(window).bind('hashchange', function (event) {
            var hashOptions, aniEngine, options, hrefObj, hrefValue, $selectedLink, key;
            hashOptions = window.location.hash ? $.deparam.fragment(window.location.hash, true) : {};
            aniEngine = hashChanged ? 'best-available' : 'none';
            options = $.extend({}, defaultOptions, hashOptions, {
                animationEngine: aniEngine
            });
            $container.isotope(options);
            isotopeOptions = hashOptions;
            if (!isOptionLinkClicked) {
                for (key in options) {
                    if (key === 'filter') {
                        hrefObj = {};
                        hrefObj[key] = options[key];
                        hrefValue = $.param(hrefObj);
                        $selectedLink = $optionSets.find('a[href="#' + hrefValue + '"]');
                        changeSelectedLink($selectedLink);
                    }
                }
            }
            isOptionLinkClicked = false;
            hashChanged = true;
        }).trigger('hashchange');
    });
});
