jQuery(document).ready(function () {
    $('#navigation ul.menu').supersubs({
        minWidth: 10,
        maxWidth: 30,
        extraWidth: 1
    }).superfish({ hoverClass: "sfHover", speed: 'slow', dropShadows: false, delay: 0, animation: { height: 'show'} });
    $('#navigation ul.menu>li>ul>li, #navigation ul.menu>li>ul>li>ul>li').hover(function () { //mouse in
        $(this).stop().animate({ paddingLeft: '10px' }, 200);
    }, function () { //mouse out
        $(this).stop().animate({ paddingLeft: 0 }, 200);
    });
    $('#frontpage-portfolio').jScrollPane({ horizontalDragMaxWidth: 24 });
    $('#frontpage-aboutus').jScrollPane({verticalDragMaxHeight: 24});
    $('.view, .postimage a').css({ opacity: 0 });
    $('#frontpage-portfolio .view, .postimage a').hover(function () {

        $(this).addClass('currentitem');
        $('.currentitem').stop().animate({ opacity: 1 }, 300);

    }, function () {

        $('.currentitem').stop().animate({ opacity: 0 }, 300);
        $('.currentitem').removeClass('currentitem');

    });
    $('#portfolioright img').hover(function () {
        $(this).addClass('currentitem');
        $('.currentitem').stop().animate({ opacity: 0.7 }, 300);

    }, function () {
        $('.currentitem').stop().animate({ opacity: 1 }, 300);
        $('.currentitem').removeClass('currentitem');

    });
    $('.twitterfeed').vTicker({
        speed: 500,
        pause: 5000,
        showItems: 1,
        animation: 'fade',
        mousePause: false,
        direction: 'up'
    });
    $('#slider').append('<div class="slider-caption"></div>');
    jQuery(".tabs_container").each(function () {
        jQuery("ul.tabs", this).tabs("div.panes > div", { tabs: 'li', effect: 'fade', fadeOutSpeed: -400 });
    });
    jQuery("h4.toggle").toggle(function () {
        jQuery(this).addClass("toggle_active");
    }, function () {
        jQuery(this).removeClass("toggle_active");
    });
    jQuery("h4.toggle").click(function () {
        jQuery(this).next(".toggle_body .block").slideToggle();

    });
    $("a[rel^='prettyPhoto']").prettyPhoto({ "theme": 'premium_photo', "slideshow": 5000, "overlay_gallery": false });
    (function ($) {
        $.fn.sorted = function (customOptions) {
            var options = {
                reversed: false,
                by: function (a) {
                    return a.text();
                }
            };
            $.extend(options, customOptions);

            $data = $(this);
            arr = $data.get();
            arr.sort(function (a, b) {

                var valA = options.by($(a));
                var valB = options.by($(b));
                if (options.reversed) {
                    return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
                } else {
                    return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
                }
            });
            return $(arr);
        };

    })(jQuery);

    $(function () {
        var read_button = function (class_names) {
            var r = {
                selected: false,
                type: 0
            };
            for (var i = 0; i < class_names.length; i++) {
                if (class_names[i].indexOf('selected-') == 0) {
                    r.selected = true;
                }
                if (class_names[i].indexOf('segment-') == 0) {
                    r.segment = class_names[i].split('-')[1];
                }
            };
            return r;
        };

        var determine_sort = function ($buttons) {
            var $selected = $buttons.parent().filter('[class*="selected-"]');
            return $selected.find('a').attr('data-value');
        };

        var determine_kind = function ($buttons) {
            var $selected = $buttons.parent().filter('[class*="selected-"]');
            return $selected.find('a').attr('data-value');
        };

        var $preferences = {
            duration: 600,
            easing: 'easeInOutQuad',
            adjustHeight: 'dynamic'
        };

        var $list = $('#columns');
        var $data = $list.clone();

        var $controls = $('#portfolio-control');

        $controls.each(function (i) {

            var $control = $(this);
            var $buttons = $control.find('a');

            $buttons.bind('click', function (e) {

                var $button = $(this);
                var $button_container = $button.parent();
                var button_properties = read_button($button_container.attr('class').split(' '));
                var selected = button_properties.selected;
                var button_segment = button_properties.segment;

                if (!selected) {

                    $buttons.parent().removeClass();
                    $button_container.addClass('selected-' + button_segment);

                    var sorting_type = determine_sort($controls.eq(1).find('a'));
                    var sorting_kind = determine_kind($controls.eq(0).find('a'));

                    if (sorting_kind == 'all') {
                        var $filtered_data = $data.find('li');
                    } else {
                        var $filtered_data = $data.find('li.' + sorting_kind);
                    }

                    var $sorted_data = $filtered_data.sorted({
                        by: function (v) {
                            return $(v).find('strong').text().toLowerCase();
                        }
                    });

                    $list.quicksand($sorted_data, $preferences, function () {
                        $('#portfolioright img').hover(function () {
                            $(this).addClass('currentitem');
                            $('.currentitem').stop().animate({ opacity: 0.7 }, 300);

                        }, function () {
                            $('.currentitem').stop().animate({ opacity: 1 }, 300);
                            $('.currentitem').removeClass('currentitem');

                        });
                        $("a[rel^='prettyPhoto']").prettyPhoto({ "theme": 'premium_photo', "slideshow": 5000, "overlay_gallery": false });
                    });

                }

                e.preventDefault();
            });

        });

    });
});
