function liEqualHeight(element) {
    $(element).each(function(){
        var currentTallest = 0;
        $(this).children().each(function(i){
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        });
        $('ul.' + element + '>li').height(currentTallest);
    });
}

function showRandomArticles() {
    $.ajax({
        type: "post",
        url: "index.php",
        data: "randomArticles=true",
        success: function(data){
            $('.articlesHome').fadeTo(1000, 0.05, function(){
                $('.articlesHome').html(data);
                $('h2').css("display", "block");
                Cufon.set('fontFamily', 'nbf1').replace('h2');
                setTimeout('showRandomArticles()', 12000);
                $('.articlesHome').fadeTo(1000, 1);
            });
        }
    });
}

$(function(){

    if ($('.articlesHome').length > 0) {
        showRandomArticles();
    }

    $('.pricelistItem').click(function() {
        window.location = $(this).find('a').attr('href');
    });

    $('.prijslijstTab tr').click(function() {
        if ($(this).find('td').length > 0) {
            window.location = $(this).find('a').attr('href');
        }
    });

    //NAVIGATION DROPDOWN
    /*
    $('#nav li').hover(
        function () { $('ul', this).fadeIn(500);  },
        function () { $('ul', this).fadeOut(200); }
    );
    */

    $('#logo').click(function(){
        window.location = "index.php";
    });

    //CUFON
    Cufon.set('fontFamily', 'nbf1').replace('#nav>ul > li > a', { hover: true })('h2')('.availibleApartments a', { hover: true });
    Cufon.set('fontFamily', 'nbf2').replace('h1')('h3')('h4');
    setTimeout("$('#nav>ul > li > a').css('display', 'block');", 50);
    setTimeout("$('h1, h2, h3, h4, h5').css('display', 'block');", 50);

    //CYCLE ARTIKEL HOME
    if ($('#header_cycle').children().size() > 0) {
        $('#header_cycle').cycle({
            fx: "fade",
            timeout: 2500,
            startingSlide: 0,
            pager: '.teaserPager',
            activePagerClass: 'activeBeam',
            pagerAnchorBuilder: function(idx, slide) {
                return '<a href="#" class="beam">&nbsp;</a>';
            }
        });
    }

    // FINDER FANCYBOX
    $(".f_fancybox").fancybox({
        type: 'image'
    });
    /*
    $("#finder_detail_widget .f_w_img").click(function(e) {
        e.preventDefault();

        $('#finder_img').attr('src', $(this).attr('href'));
    });
    */

    // FINDER CYCLE
    $('#finder').css('display', 'none')
    $('#finder img').ipl({
        all: function() {
            $('#finder').css('display', 'block')
            $('#finder').cycle({
                fx: "scrollHorz",
                timeout: 0,
                startingSlide: 0,
                pager: '.finder_pager',
                activePagerClass: 'activeBeam',
                pagerAnchorBuilder: function(idx, slide) {
                    return '<a href="#" class="beam">&nbsp;</a>';
                },
                after: function(slide,next){
                    $("#finder").animate({
                        height: $(next).height() + 25
                    }, 1000);
                }
            });
            $('#finder table tr').click(function() {
                window.location = $(this).find('.col2 a').attr('href')
            });
        }
    });


    // FINDER FLOORS
    $('.finder_floor').click(function(e) {
        e.preventDefault();
        $('#finder_img_c img').attr('src', $(this).attr('href'));
    });


    //CYCLE PHOTOGALLERY
    $('#fotogallery').cycle({
        fx: "scrollHorz",
        timeout: 0,
        startingSlide: 0,
        pager: '.pager',
        activePagerClass: 'activeBeam',
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#" class="beam">&nbsp;</a>';
        }
    });

    //CYCLE NEWS ARCHIVE
    $('#newsArchive').cycle({
        fx: "scrollVert",
        timeout: 0,
        startingSlide: 0,
        pager: '.pager',
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#"> &bull; </a>';
        },

        before: function(slide,next){
            $("#newsArchive").animate({
                height: $(next).height()
            }, 1000);
        }
    }, 500);

    //CYCLE NEWS ARCHIVE
    $('.cycleTable').cycle({
        fx: "scrollVert",
        timeout: 0,
        startingSlide: 0,
        pager: '.pager',
        rev: 1,
        next:   '#next2',
        prev:   '#prev2',
        after: function(slide,next){
            $(".cycleTable").animate({
                'height': $(next).height()
            }, 1000);
        }
    }, 500);

    $('.oddeven:even').css("background-color", "#EEE");

    //CYCLE GUESTBOOK
    $('.cycle').cycle({
        fx: "scrollVert",
        timeout: 0,
        startingSlide: 0,
        pager: '.pager',
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#"> &bull; </a>';
        },

        before: function(slide,next){
            $(".cycle").animate({
                height: $(next).height()
            }, 1000);
        }
    }, 500);

    //CYCLE GUESTBOOK
    $('.newsArchiveContainer').cycle({
        fx: "scrollVert",
        timeout: 0,
        startingSlide: 0,
        pager: '.pager',
        activePagerClass: 'activeBeam',
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#" class="beam">&nbsp;</a>';
        },
        after: function(slide,next){
            $(".newsArchiveContainer").animate({
                height: $(next).height()
            }, 1000);
        }
    }, 500);

    //FANCYBOX
    $(".fancybox").fancybox({
        type: 'image'
    });

    //FANCYBOX PHOTOGALLERY
    $(".gallery").fancybox({
        titlePosition: 'inside'
    });

    //FANCYBOX FLOORPLANNER
    $("a.fancyboxIframe").fancybox({
        width: '90%',
        height: '90%',
        autoScale: false,
        transitionIn: 'none',
        transitionOut: 'none',
        type: 'iframe'
    });

    //FANCYBOX FLOORPLANNER
    $("a.fancyboxMovie").click(function(){
            $('#header_cycle').cycle('pause');
    });

    $("a.fancyboxMovie").fancybox({
        width: 540,
        autoScale: true,
        transitionIn: 'none',
        transitionOut: 'none',
        type: 'iframe'
    });

    //FANCYBOX PROJECTEN
    //$("a.gallery").fancybox();

    //AVAILIBLE APARTMENTS
    $('.city').click(function(e){
        e.preventDefault();
        if ($(this).attr('class') == 'city active') {
            return false;
        }
        $('.active').removeClass('active');
        $(this).addClass('active');
        Cufon.refresh('.availibleApartments a');
        $('.parkThumb').stop(true, true).fadeOut(200,function(){
            $('.cityThumb').stop(true, true).fadeIn();
        });
    });
    $('.park').click(function(e){
        e.preventDefault();
        if ($(this).attr('class') == 'park active') {
            return false;
        }
        $('.active').removeClass('active');
        $(this).addClass('active');
        Cufon.refresh('.availibleApartments a');
        $('.cityThumb').stop(true, true).fadeOut(200, function(){
            $('.parkThumb').stop(true, true).fadeIn();
        });
    });

    //DROPDOWN
    $('#nav ul li').hover( function() {
        $(this).find('ul').stop(true, true).fadeIn(200);
    }, function() {
        $(this).find('ul').stop(true, true).fadeOut(200);
    });

    setTimeout('liEqualHeight(".agenda")', 250);

    //ADDTHIS
    $('.sharebutton').html('<a href="http://www.addthis.com/bookmark.php?v=20">&nbsp;</a>');
    $('.sharebutton a').click(function(e) {
        e.preventDefault();
        addthis_sendto();
    });
    $('.sharebutton a').hover(function() {
        addthis_open(this, '', 'http://www.newbabylon.nl/', 'A New City in a City')
    },
    function() {
        addthis_close();
    });

    //Pagina BEREIKBAARHEID
    $('#m_img1').hover(function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid_1.jpg');
    }, function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid.jpg');
    });
    $('#m_img2').hover(function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid_2.jpg');
    }, function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid.jpg');
    });
    $('#m_img3').hover(function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid_3.jpg');
    }, function(){
        $('#m_bereikbaarheid').attr('src', 'img/bereikbaarheid/bereikbaarheid.jpg');
    });

});



