function myGallery(){
    $("a[rel=example-group]").fancybox({
        'transitionIn'		: 'fade',
        'transitionOut'		: 'fade',
        'titlePosition' 	: 'over',
        'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
        }
    });
                
    $(function() {
        $("ul li img").css("opacity","1.0");  
        $("ul li img").hover(function () {

            $(this).stop().animate({
                opacity: 0.5
            }, "slow");
        },
        function () {
            $(this).stop().animate({
                opacity: 1.0
            }, "slow");
        });
    });
    $("img").lazyload({
        effect      : "fadeIn"
    });
}

var $current = '';

function description($div){  
    if($current != ''){
        $("#"+$current).hide();
        $("#"+$div).fadeIn('fast');
    }
    $("#"+$div).fadeIn('fast');
   
    $current = $div;
      
}

jQuery(document).ready(function(){
    myGallery();
});

(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 $filterType = $('#filter input[name="type"]');
    var $filterSort = $('#filter input[name="sort"]');
    var $applications = $('#applications');
    var $data = $applications.clone();
    $filterType.add($filterSort).change(function(e) {
        if ($($filterType+':checked').val() == 'all') {
            var $filteredData = $data.find('li');
        } else {
            var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
        }
        if ($('#filter input[name="sort"]:checked').val() == "size") {
            var $sortedData = $filteredData.sorted({
                by: function(v) {
                    return parseFloat($(v).find('span[data-type=size]').text());
                }
            });
        } else {
            var $sortedData = $filteredData.sorted({
                by: function(v) {
                    return $(v).find('strong').text().toLowerCase();
                }
            });
        } 
        
        var $current = $($filterType+":checked").val(); 
        description($current);
        $applications.quicksand($sortedData, {
            duration: 800,
            easing: 'easeInOutQuad'
        }, function(){
            myGallery();
        });
    });

});
