//add nth-last-child support jquery
(function ($) { var c = /:(nth)-last-child(?:\((even|odd|[\dn+-]*)\))?/, a = $.expr, b = a.filter.CHILD; a[":"]["nth-last-child"] = function (h, g, e, k) { var j = e[0].match(c), f = $(h.parentNode).children(), d; j = a.preFilter.CHILD(j); b(h, j); d = f.eq(f.length - h.nodeIndex)[0]; return b(d, j) } })(jQuery);

$(function () {
    function changeSlide(slide) {
        try {
            clearTimeout(nextSlide);
            slide = slide || $('#carousel .nav .current').next()[0] || $('#carousel .nav a:first-child')[0];

            var $slide = $(slide),
                $img = $($('#carousel img')[$('#carousel a').index(slide)]);

            $slide.siblings('.current').removeClass('current');
            $slide.addClass('current');

            if (Modernizr.csstransitions) {
                $img.siblings('.current').removeClass('current');
                $img.addClass('current');
            } else {
                $img.siblings('.current').fadeOut(1000, function () {
                    $(this).removeClass('current');
                });
                $img.fadeIn(1000, function () {
                    $img.addClass('current');
                });
            }
        } catch (err) { }
        nextSlide = setTimeout(changeSlide, 5000);
    }
    nextSlide = setTimeout(changeSlide, 5000);

    var dtcount = 1;
    $('dt').each(function () {
        $(this).prepend(dtcount + '. ');
        dtcount++;
    });
    $('#carousel a').click(function (e) {
        e.preventDefault();
        changeSlide(this);
    });

    $.getJSON('http://www.search.twitter.com/search.json?callback=?&q=from:PimvandenBerg', function (data) {
        var l_objdata = new Array();
        l_objTweets = $.toJSON(data);
        $.ajax({
            type: 'POST',
            url: '/Templates/Ajax/Twitter.aspx',
            cache: false,
            async: false,
            data: l_objTweets,
            dataType: "json",
            contentType: "application/json; charset=UTF-8",
            success: function (result) {
            }
        });
    });

    var $videos = $('#video-mover');
    $.getJSON('http://vimeo.com/api/v2/user5585500/videos.json?callback=?', function (data) {
        var $article = $('<article />'),
            $img = $('<img alt="" />'),
            $header = $('<header />'),
            $h1 = $('<h1 />'),
            $p = $('<p />'),
            $iframe = $('<iframe />'),
            count = 0;

        $.each(data, function (key, val) {
            $article = $('<article />');
            $img = $('<img alt="" />');
            $header = $('<header />');
            $h1 = $('<h1 />');
            $p = $('<p />');
            $iframe = $('<iframe />');

            $.each(val, function (key, val) {
                switch (key) {
                    case 'thumbnail_large':
                        // $img.attr('src', val);
                        $img = '<img alt="" src="' + val + '" />';
                        break;
                    case 'title':
                        $h1.html(val);
                        break;
                    case 'description':
                        $p.html(val);
                        break;
                    case 'id':
                        //                        $iframe.attr('src', 'http://player.vimeo.com/video/' + val);
                        //                        $iframe.attr('width', '460');
                        //                        $iframe.attr('height', '258');
                        //                        $iframe.css('display', 'none');
                        //                        $iframe.css('frameborder', '0');

                        $iframe = '<iframe src="http://player.vimeo.com/video/' + val + '" width="460" height="258" style="display:none;" />'
                        break;
                }
            });
            $videos.append('<div class="article">' +
                    $img +
                    $iframe +
                    '<header><h1>' +
                    $h1.clone().html()
                    + '</h1></header>' +
                    $p.clone().html() + "</div>"
            );
            count++;
        });
        $videos.width(count * 480 + 960);
    });
    $('#videos').delegate('div img', 'click', function () {
        $(this).hide();
        $(this).siblings('iframe').show();
    });
    $('div.video-nav a:last-child').click(function (e) {
        e.preventDefault();
        $videos.stop(true, true);
        $videos.append($videos.children(':first-child, :nth-child(2)').clone());
        $videos.animate({ left: '-960px' }, function () {
            $videos.css('left', '0px').children(':first-child, :nth-child(2)').remove();
        });

    });
    $('div.video-nav a:first-child').click(function (e) {
        e.preventDefault();
        $videos.stop(true, true);
        $videos.css('left', '-960px').prepend($videos.children(':last-child, :nth-last-child(2)').clone());
        $videos.animate({ left: '0px' }, function () {
            $videos.children(':last-child, :nth-last-child(2)').remove();
        });
    });

    $('#ReadMore').click(function (e) {
        e.preventDefault();
        $('#MoreNews').slideToggle('slow');
        $('#ReadMore').hide();
        $('#CollapseNews').show();
    });

    $('#CollapseNews').click(function (e) {
        e.preventDefault();
        $('#MoreNews').slideToggle('slow');
        $('#ReadMore').slideToggle('slow');
        $('#CollapseNews').hide();
    });

    $('section.manifesto>a, #main>section.manifesto>div>a').click(function (e) {
        e.preventDefault();
        $('section.manifesto>div').slideToggle('slow');
    });
    $('section.about p:not(:first-of-type):not(:nth-of-type(2))').hide();
    if ($('section.about p').length <= 2) {
        $('section.about a.black').hide();
    }
    $('section.about a.black').click(function (e) {
        e.preventDefault();
        $('section.about p').slideDown('fast');
        $('section.about a.collapse').show();
        $(this).hide();
    });
    $('section.about a.collapse').click(function (e) {
        e.preventDefault();
        $('section.about p:not(:first-of-type):not(:nth-of-type(2))').slideUp('fast');
        $('section.about a.collapse').hide();
        $('section.about a.black').show();
    });

    $('#main>div>section:last-of-type img').hover(function () {
        $(this).attr('src', $(this).attr('src').replace('.png', '-hover.png'));
    }, function () {
        $(this).attr('src', $(this).attr('src').replace('-hover', ''));
    });
    $('#main>section:nth-child(2) div.content a:last-child').click(function (e) {
        e.preventDefault();
        $('#main>section:nth-child(2) div.content div').animate({
            maxHeight: 'none'
        }, 1000);
    });

    $('#main>section:nth-child(2) p:not(:first-of-type):not(:nth-of-type(2))').hide();
    if ($('#main>section:nth-child(2) p').length <= 2) {
        $('#main>section:nth-child(2) a.black').hide();
    }
    $('#main>section:nth-child(2) a.black').click(function (e) {
        e.preventDefault();
        $('#main>section:nth-child(2) p').slideDown('fast');
        $('#main>section:nth-child(2) a.collapse').show();
        $(this).hide();
    });
    $('#main>section:nth-child(2) a.collapse').click(function (e) {
        e.preventDefault();
        $('#main>section:nth-child(2) p:not(:first-of-type):not(:nth-of-type(2))').slideUp('fast');
        $('#main>section:nth-child(2) a.collapse').hide();
        $('#main>section:nth-child(2) a.black').show();
    });

    $(window).load(function () {
        var parts = document.location.href.split("#");
        var trgt = parts[1];
        if (trgt != null) {
            var target_offset = $("#" + trgt + 'form').offset();
            var target_top = target_offset.top;
            $('html, body').animate({ scrollTop: target_top }, 500);

        }
    });
});
