// onload
function changeForms(){
    if($(".Rollbok .actionSelect").val() == "date"){
        $(".Rollbok .searchAll").hide();
        $(".Rollbok .dateSearch").show();
    } else {
        $(".Rollbok .searchAll").show();
        $(".Rollbok .dateSearch").hide();
    }
}
$(function() {
    $(".front-list .flashPoster").siblings("a").addClass("poster");

    if(!($.browser.msie) || ($.browser.msie && $.browser.version > 5.5)){
        $(".front-list").addNthChild();
        $(".front-list a").fetchPerformance();
    }
    if($.browser.msie && $.browser.version > 5.5){
        var imgSrc = "/Web/Core/UI/img/trans_bg.gif";
        $(".front-list a.poster").css('backgroundImage','url(' + imgSrc +')');
    }
    if($(".Rollbok .actionSelect").val() == "date"){
        $(".Rollbok .searchAll").hide();
        $(".Rollbok .dateSearch").show();
    } else {
        $(".Rollbok .searchAll").show();
        $(".Rollbok .dateSearch").hide();
    }
});

jQuery.fn.log = function (msg) {
    if(console){
        console.log("%s: %o", msg, this);
    }
    return this;
};
jQuery.fn.addNthChild = function () {
    this.find("li:nth-child(3n-2)").addClass("one");
    this.find("li:nth-child(3n-1)").addClass("two");
    this.find("li:nth-child(3n)").addClass("three");
    return this;
};

jQuery.fn.fetchPerformance = function(){
    this.click(function(){
        var time = 10;
        var link = $(this);

        $.ajax({
            type: "GET",
            url: link.attr("href")+"?sp=Affisch",
            success: function(html){
                var performance = push.generate(html)
                $("body").addClass("thinking");
                performance.css({
                    height: (link.parent("li").height()-55) + "px",
                    width:(659 - 60) +"px",
                    top: 0
                });
                if(link.parent("li").hasClass("one")){
                    performance.css({
                        left: "-" + link.parent("li").parent("ul").width()+"px"
                    });
                    link.parent("li")
                    .css("z-index", "999")
                    .append(performance);
                    var timeout = setTimeout(function(){
                        performance.animate({left: "0"}, 600);
                        $("body").removeClass("thinking");
                    }, time);
                }
                if(link.parent("li").hasClass("two")){
                    performance.css({
                        left: "-" + (link.parent("li").parent("ul").width()*2)+"px"
                    });
                    link.parent("li")
                    .css("z-index", "999")
                    .append(performance);
                    var timeout = setTimeout(function(){
                        performance.animate({left: "-220px"}, 1000);
                        $("body").removeClass("thinking");
                    }, time);
                }
                if(link.parent("li").hasClass("three")){
                    performance.css({
                        left: link.parent("li").parent("ul").width()+"px"
                    });
                    link.parent("li").append(performance);
                    var timeout = setTimeout(function(){
                        performance.animate({left: "-440px"}, 1000);
                        $("body").removeClass("thinking");
                    }, time);
                }
                performance.mouseout(function(e){
                    var tg = (window.event) ? e.srcElement : e.target;
                    if ($(tg).get(0) != performance.get(0)){
                        return;
                    }
                    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
                    while (reltg != tg && reltg.nodeName != 'BODY')
                    reltg = reltg.parentNode
                    if (reltg == tg){
                        return;
                    }
                    if($(this).parent("li").hasClass("one")){
                        $(this).animate({opacity: "0", show: "hide"}, 600, function(){
                            $(this).remove();
                        });
                    }
                    if($(this).parent("li").hasClass("two")){
                        $(this).animate({opacity: "0", show: "hide"}, 600, function(){
                            $(this).remove();
                        });
                    }
                    if($(this).parent("li").hasClass("three")){
                        $(this).animate({opacity: "0", show: "hide"}, 600, function(){
                            $(this).remove();
                        });
                    }
                    $(this).parent("li").css("z-index", "0");
                });
            },
            error: function(){
                window.location = link.attr("href");
            }
        });
        return false;
    });
};
var push = {
    generate: function(html){
        var source = $("<div/>").append(html.replace(/<script(.|\s)*?\/script>/g, ""));
        var holder = source.find("#ctrlShortPageDiv");
        holder.prepend(source.find(".headerlist h3"));
        holder.prepend(source.find("#primary h1"));
        holder.find("img").each(function(i){
            if($(this).hasClass("background")){
                holder.css("background-image", "url("+$(this).attr("src")+")");
                $(this).remove();
            }
        }).trigger("load");

        return holder;
    },
    move: function(elem){
        elem.animate({left: "0"}, 600);
    }
}


