$(document).ready(function(){

    function removeThickBoxEvents() {
        $('.thickbox').each(function(i) {
            $(this).unbind('click');
        });
    }

    function bindThickBoxEvents() {
        removeThickBoxEvents();
        tb_init('a.thickbox, area.thickbox, input.thickbox');
    }


    function ajaxBindings() {
        bindShowNewsDetail();
        bindShowMagnifiert();
        removeThickBoxEvents();
        bindThickBoxEvents();
        $('div.browse a').click(function(){
            var link = $(this).attr('href');
            $("div#news-container").empty();
            $("div#news-loader-wrap").show();
            $("#news-container").load(link,function(){
                $("div#news-loader-wrap").hide();
                ajaxBindings();
            });
            return false;
        });
        $("div.news-img-bar").hide();
    };

    function bindShowNewsDetail() {
        $("a.newsdetail").click(function(){
            var url = $(this).attr("href");
            $("div#news-loader-wrap").show();
            $.get(url, function(data){
                $("div#news-loader-wrap").hide();
                $("#news-container").empty().append(data);
            });
            return false;
        });
    };

    function bindShowMagnifiert() {
        $(".news-border").hover(function(){
            $(this).find(".news-img-bar").slideDown();
        }, function(){
            $(this).find(".news-img-bar").slideUp();
        });
    };

    var link = $("ul.tabs li:first a").attr('href');
        $("#news-container").load(link, function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
    });

    $("li#news-tab-glied").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        var link = $("li#news-tab-glied a").attr('href');
//        $("#news-container").load("../custom/skripte/getNews.php?art=allgemein",function(){
        $("#news-container").load(link,function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });

    $("li#news-tab-allg").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
				var link = $("li#news-tab-allg a").attr('href');
//        $("#news-container").load("../custom/skripte/getNews.php?art=allgemein",function(){
        $("#news-container").load(link,function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });

    $("li#news-tab-jm").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
				var link = $("li#news-tab-jm a").attr('href');
        $("#news-container").load(link,function(){
//        $("#news-container").load("../custom/skripte/getNews.php?art=jungemenschen",function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });

    $("li#news-tab-familie").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        
				var link = $("li#news-tab-familie a").attr('href');
//				$("#news-container").load("../custom/skripte/getNews.php?art=familie",function(){
				$("#news-container").load(link,function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });

    $("li#news-tab-arbeit").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
				var link = $("li#news-tab-arbeit a").attr('href');
//        $("#news-container").load("../custom/skripte/getNews.php?art=arbeitswelt",function(){
        $("#news-container").load(link,function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });

    $("li#news-tab-einewelt").click(function(){
        $("div#news-container").empty();
        $("div#news-loader-wrap").show();
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
				var link = $("li#news-tab-einewelt a").attr('href');
//        $("#news-container").load("../custom/skripte/getNews.php?art=einewelt",function(){
        $("#news-container").load(link,function(){
            $("div#news-loader-wrap").hide();
            ajaxBindings();
        });
        return false;
    });
});