var searchText;
var currentSlide = 1;
var intervalID = -1;
var currentHash = '';

$(function() {
    // Zoekveld:
    searchText = $("#top input[type=text]").val();
    $("#top input[type=text]").focus(
            function() {
                if ($(this).val() == searchText) {
                    $(this).val('');
                }
            }).blur(function() {
                if ($(this).val() == '') {
                    $(this).val(searchText);
                }
            });

    // Externe links:
    $("a[rel=external]").click(function() {
        window.open($(this).attr("href"));
        return false;
    });

    // Subs:
    $("ul.sub a, #sub.indicator a").click(function() {
        showSub($(this).attr("rel"));
        return false;
    });

    // Zorg dat de footer altijd tot onderaan de pagina komt:
    $(window).resize(
            function() {
                $("#footer").height(50);
            }).resize();

    $("div.columnize").columnize({lastNeverTallest: true, width: 300});

    // Check voor een hash:
    if (window.location.hash != '' && window.location.hash != '#') {
        showSub(window.location.hash.replace('#tab-' , '') , false);
    }

    setInterval("checkHashChange()" , 100);
    initpage();
    //showSub(1,1);
});

function initpage() {
    if (window.location.hash != '' && window.location.hash != '#') {
        showSub(window.location.hash.replace('#tab-' , ''));
    } else {
        showSub(1);
    }
}

function checkHashChange() {
    if (window.location.hash != currentHash) {
        if (window.location.hash != '' && window.location.hash != '#') {
            showSub(window.location.hash.replace('#tab-' , ''));
        } else {
            showSub(1);
        }
    }
}

function showSub(nr , animate) {
    if (animate == null) {
        animate = true;
    }
    if(jQuery.browser.msie){
        animate = false;
    }
    if (nr != 1) {
        window.location.hash = 'tab-' + nr;
    } else {
        window.location.href.substr(0 , window.location.href.indexOf('#'));
    }
    
    currentHash = window.location.hash;

    if (animate) {
        // Animeer de slides:
        $("#slides div.slide").stop().animate({opacity: 0} , function() { $(this).hide(); });
        $("#sub-" + nr).stop().show().css({opacity: 0}).animate({opacity: 1});
    } else {
        $("#slides div.slide").hide();
        $("#sub-" + nr).show();
    }
    // Toon het juiste bolleke:
    $("ul.sub a, #sub.indicator a").removeClass("active");
    $("ul.sub a[rel=" + nr + "], #sub.indicator a[rel=" + nr + "]").addClass("active");

    var h = parseInt($("#sub-" + nr).height());
    if (h > 315) {
        $("#content").stop().animate({height: h + 40},150);
    } else {
        $("#content").stop().animate({height: 315},150);
    }
}
