
var l, pw, ph, prevh, prevw, thumb, hash, tid, t, relink, content, oldhash = "",
    settings = {};
var click = true;

function pageload(hash, page) {
    l = t;

    if (hash) {
        t = '#hit-' + hash;
    } else {
        $(t).empty();
    }

    relink = content;
    h = $(t).height();
    w = $(t).width();
    if (!page) {
        the_url = $(t).find('a').attr('id');
    } else {
        the_url = page;
    }
    if ($(t).length == 0) {
        the_url = $('a[href$="#' + hash + '"]').attr('id');
        page = the_url;
        $('#page').addClass('post').empty();
    }

    if (hash) {
        $.ajax({
            url: "/" + the_url,
            cache: false,
            success: function(html) {
                $(l).fadeOut(settings.speed, function() {
                    $(this).empty();
                    $(this).append(relink);
                    $(this).removeClass('open opencols');
                    imageCycle();
                });
                $(l).fadeIn();
                prevh = $(t).height();
                prevw = $(t).width();
                ph = $(t).find('img').height();
                pw = $(t).find('img').width();
                $("#loader").fadeOut(settings.duration + 100, function() {
                    if (!page) {
                        $('#page').removeClass('post').empty();
                        content = $(t).html();
                        tid = $(t).find('a').attr('id');
                        thumb = $(t).find('img').attr('src');
                        oldhash = $(t).find('a').attr('href');
                        $(t).fadeOut('fast', function() {
                            $(t).empty();
                            $(t).append(html);
                            imageCycle();
                            $('.post:not(.allcols)').animate({
                                opacity: 0.2
                            });
                            _height = $("#hit-" + hash).height();
                            $("#hit-" + hash + ' .single').fadeTo(0, 0);
                            $(t).addClass('open opencols');
                            $("#content").grid(settings, t, false, true);
                            var cycleHeight = $('.imageCycle').height();
                            $('#cycleControls').css({
                                height: cycleHeight + 'px'
                            });
                            if ($.createCycleControls != true) {
                                $('#cycleControls').css({
                                    'visibility': 'hidden'
                                });
                            }
                            click = true;
                            $('.opencols').animate({
                                opacity: 1
                            });
                            return true;
                        });
                    } else {
                        $('#page').addClass('post').empty();
                        $('#page').append(html).fadeTo(0, 0);
                        $('#page .single').fadeTo(0, 0);
                        $("#content").grid(settings, '#page', false, true);
                    }
                });
            }
        });
    } else {
        // $(t).empty();
        // t is terug naar normaal, dus null maken. Als je dit niet doet gaat bij het openen van een volgende item t uit en in faden.
        t = null;

        // l (eigenlijk t) resetten en daarna grid resetten
        $(l).fadeOut(settings.speed, function() {
            $(this).empty();
            $(this).append(relink);
            $(this).removeClass('open opencols');
            imageCycle();

            $('.post').animate({
                opacity: 1
            });
            $(l).fadeIn();
            $("#content").grid(settings, '', false, false);
        });
    }
}


$(function() {

    $('.case .imageWrapper').hover(

    function() {
        $(this).children('.iconPlus').show();
    }, function() {
        $(this).children('.iconPlus').hide();
    });

    var pageName = $('#wrapper').attr('class');
    if (pageName == 'social-initiaves') {

        $('.imageCycleBig').cycle({
            fx: 'scrollHorz',
            speed: 800,
            timeout: 4000,
            prev: '#prevCycle',
            next: '#nextCycle'
        });

        $('.imageCycleBig, #cycleControlsBig').hover(

        function() {
            $('#cycleControlsBig').show();
        }, function() {
            $('#cycleControlsBig').hide();
        });
    }

    if (pageName == 'ourwork') {
        $(window).load(

        function() {
            // disable animation at start
            settings.animateUpdate = false;
            $("#content").grid(settings, '', false, false);
            // enable animation
            settings.animateUpdate = true;

            $(".page > a[rel='history']").live('click', function(e) {
                var hash = this.href;
                var page = this.id;
                hash = hash.replace(/^.*#/, '');
                $.historyLoad(hash, page);
                $("#loader").css('top', e.pageY - 10);
                $("#loader").css('left', e.pageX - 10);
                $("#loader").fadeIn(100);
            });
            $("#content div > a[rel='history']").live('click', function(e) {
                if (click === true) {
                    click = false;
                    var hash = this.href;
                    hash = hash.replace(/^.*#/, '');
                    $.historyLoad(hash);
                    $("#loader").css('top', e.pageY - 10);
                    $("#loader").css('left', e.pageX - 10);
                    $("#loader").fadeIn(100);
                    $(this).children('.iconPlus').hide();
                    return true;
                } else {
                    return false;

                }
            });

            if ((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

            } else {
                $("#content div > a[rel='history']").live('mouseover mouseout', function(event) {
                    if (event.type == 'mouseover') {
                        $(this).children('.iconPlus').show();
                    } else {
                        $(this).children('.iconPlus').hide();
                    }
                });
            }

            $.historyInit(pageload);


            $('#cycleControls').live('click', function() {
                $('.imageCycle').cycle('pause');
            });
        });

    }

    $('.post').animate({
        opacity: 1
    });

    $('#logo').flash({
        swf: '/flash/elastiklogo.swf',
        height: 245,
        width: 240,
        wmode: 'transparent'
    });


});

function imageCycle() {
    var size = $('.imageCycle').children('img').attr('height');
    $('.imageCycle').css({
        height: size
    });
    $('.imageCycle').after('<div id="cycleControls"><div id="prevCycle"></div><div id="nextCycle"></div></div>');
    $('.imageCycle').cycle({
        fx: 'scrollHorz',
        speed: 800,
        timeout: 4000,
        prev: '#prevCycle',
        next: '#nextCycle'
    });
}

function alphaReset() {
    $('.post').animate({
        opacity: 1
    });
} 
