var $wall = null;
var $slider = null;
var $people = null;

jQuery(function($) {
        
    // Fill the caption at first
    /*
    var caption = $("#slidecaption");
    if (caption.length > 0 && page_id == 1 && mainslides.length > 0) {
        $('.title', caption).html(mainslides[0].title);
        $('.content', caption).html(mainslides[0].description);
    }
    */
    
    // Touch class
    if (is_touch_device()) {
        $('body').addClass('mobile');
    }
    
    // Cufon init
    Cufon.replace(jQuery('.Cufon, #Menu li a, #Information li, #Information li a, #slidecaption .title, #slidecaption .content, #Sorting, #Sorting a, #People .person .content p, h1, h2, h4, h5, h6'), { hover: true });
    
    // Start Galleria
    Galleria.loadTheme('/upload_dir/themes/web/Unik/js/themes/fullscreen/galleria.fullscreen.min.js');
    
    var backgroundSlider = $('#BackgroundSlider');
    if (backgroundSlider.length > 0) {
        $slider = backgroundSlider.galleria({
            dataSource: mainslides,
            transition: is_touch_device() ? 'slide' : 'fade',
            transitionSpeed: 1000,
            preload: 3,
            autoplay: 4500,
            thumbnails: false,
            showImageNav: false,
            showCounter: false,
            showInfo: false,
            extend: function(options) {
                var gallery = this; // "this" is the gallery instance
                gallery.bind(Galleria.LOADSTART, function(e) {
                    if (gallery.hasInfo() && page_id == 1) {
                        var data = gallery.getData(e.index);
                        var cap = jQuery("#slidecaption");
                        jQuery(".title", cap).html(data.title);
                        jQuery(".content", cap).html(data.description);
                        Cufon.refresh();
                    }
                });
            },
            debug: false,
            imageCrop: true,
            _hideDock: true
        });
    }

    // Remove the first element of the main menu
    //$("nav li:first-child").remove();

    // Fade in
    //$("#Wrapper").delay(200).fadeIn(400);
    
    // Cases
    $wall = $("#Cases");
    if ($wall.length > 0) {
        
        log('Found the Cases wall');

        // Hover
        $('.item, .arrowLeft, .arrowRight').hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
        
        // Thumb
        $('#Cases .case').hover(function() {
            $('.thumb img', $(this)).animate({
                'opacity': 0.2
            }, 250);
        }, function() {
            $('.thumb img', $(this)).animate({
                'opacity': 1
            }, 250);
        });
        
        $('#CaseActive .top img').hover(function() {
            $(this).animate({
                opacity: 0.2
            }, 250);
        }, function() {
            $(this).animate({
                opacity: 1
            }, 250);
        });
        
        // Init slider
        /*
        var slide = $("#CaseActive #slideshow .item");
        if (slide.length > 1) {
            setTimeout(function() {
                $slider = $("#CaseActive #slideshow").jcarousel({
                    scroll: 1,
                    auto: 5,
                    wrap: 'circular',
                    animation: 'slow'
                });
            }, 500);
        }
        */

        // Clicker
        /* No more sweet clicker :(
        $('.case').click(function() {
            
            if (!$(this).hasClass('active')) {
                
                // Make sure none is hover
                $('.case.hover').removeClass('hover');

                // Remove previous if any
                postCloseFilter();

                // Add this
                $(this).addClass('active');

                // Init slideshow
                $slider = $(this).slider();

                // Redraw Masonry
                setTimeout("$wall.masonry('reload')", 25);
                
                // Scroll
                setTimeout('scrollToActive()', 200);
                
                // Add the video
                setTimeout('postOpenFilter()', 800);
            }

        });
        */

        // Masonry
        /*
        $wall.masonry({
            
            itemSelector: '.case:not(.invis)',
            isAnimated: true,
            gutterWidth: 5,
            isResizable: true,
            saveOptions: true,
            singleMode: false
            
        });
        */
        
        // Sorting
        /*
        $("#Sorting a").click(function(e) {
            e.preventDefault();
            masonryFilter($(this).attr('data-filter'));
            $("#Sorting .active").removeClass('active');
            $(this).addClass('active');
        });
        */
        
        // Make sure to reload Masonry to ensure compatibility with Webkit browsers
        //setTimeout('$wall.masonry("reload")', 1000);
    }
    
    var caseactive = $("#CaseActive");
    if (caseactive.length > 0) {
        
        log('Active case detected');
        
        $('html').addClass('fullscreen');
        
        $slider = $('#CaseSlides', caseactive).galleria({
            transition: is_touch_device() ? 'slide' : 'fade',
            transitionSpeed: 1000,
            preload: 3,
            autoplay: 5000,
            showImagenav: true,
            thumbnails: false,
            showCounter: false,
            showInfo: false,
            debug: false,
            imageCrop: true,
            _hideDock: true
        });
        
    }
    
    // People
    $people = $("#People");
    if ($people.length > 0) {
        
        window.log('Found the People wall');

        // Thumb
        $('.person', $people).hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });

        // Masonry
        /*
        $people.masonry({
            
            itemSelector: '.person:not(.invis)',
            isAnimated: true,
            gutterWidth: 5,
            isResizable: true,
            saveOptions: true,
            singleMode: false
            
        });
        */
        
        // Sorting
        /*
        $("#Sorting a").click(function(e) {
            e.preventDefault();
            masonryFilter($(this).attr('data-filter'), $people);
            $("#Sorting .active").removeClass('active');
            $(this).addClass('active');
        });
        */
    }

});

function is_touch_device() {
    var ua = navigator.userAgent;
    
    function test_touch_event() {
        try {  
            document.createEvent("TouchEvent");  
            return true;  
        } catch (e) {  
            return false;  
        }  
    }

    if ((test_touch_event()) || ua.match(/(iPhone|iPod|iPad)/) 
    || ua.match(/BlackBerry/) || ua.match(/Android/)) {
        return true;
    } else {
        return false;
    }
}

/*
function scrollToActive() {
    jQuery.scrollTo('div.active', 350, {offset: { top: -30 } , axis: "y"} );
}

function postOpenFilter() {
    var el = jQuery('.case.active');
    var embed = el.attr('data-embed');
    if (embed != 'Oprettet i Admin' && embed != '') {
        jQuery('.embed', el).html('<iframe width="260" height="195" src="' + embed + '?autohide=2&showsearch=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
    }
}

function postCloseFilter() {
    jQuery('.case.active').removeClass('active').find('.embed').html('');
    if ($slider != null) $slider.slider().destroy();
}

function masonryFilter(selector, instance) {
    setTimeout(function() { Cufon.refresh(); }, 100);
    if (! instance) instance = $wall;
    window.log('Filter: ' + selector);
    if(selector == 'all' || selector == '' || !selector) {
        // show all hidden boxes
        instance.children('.invis')
            .toggleClass('invis').fadeIn(500);
    } else {
        newselector = '[data-group="' + selector + '"]';
        // hide visible boxes 
        instance.children().not(newselector).not('.invis')
          .toggleClass('invis').fadeOut(500);
        // show hidden boxes
        instance.children('.invis'+newselector)
        .toggleClass('invis').fadeIn(500);
    }
    instance.masonry('reload');
}

function openFullscreen(images, show, id) {
    if (! show) show = 0;
    jQuery("#GalleriaContainer").galleria({
        dataSource: images,
        show: show,
        transition: 'fade',
        transitionSpeed: 1000,
        preload: 3,
        autoplay: 3000,
        thumbnails: true,
        showImageNav: false,
        showCounter: false,
        showInfo: false,
        debug: false,
        imageCrop: true,
        _hideDock: false
    });
    
    // Hide the wrapper
    jQuery("#Pattern").hide();
    jQuery("#Wrapper").hide();
    jQuery("#slidecaption").hide();
    jQuery("body").addClass('fullscreen');
    jQuery("#fullscreenbutton").click(function() {
        closeFullscreen();
    }).show();
}
function closeFullscreen() {
    var gallery = Galleria.get(Galleria.get().length-1);
    if (gallery) {
        gallery.exitFullscreen();
    }
    jQuery("#GalleriaContainer").html("");
    
    // Show the wrapper
    jQuery("#Pattern").show();
    jQuery("#Wrapper").show();
    jQuery("#slidecaption").show();
    jQuery("body").removeClass('fullscreen');
    jQuery("#fullscreenbutton").unbind('click').hide();
    
    // Refresh masonry just in case
    $wall.masonry('reload');
}
*/
