var iFoto  = 0;
var imgArr = new Array();

jQuery(document).ready(function() {
    $("#film p").hide();
    $("#film").html($("#film").html() + '<div id="fotoleiste_old" class="img_unten"></div><div id="fotoleiste_neu" class="img_oben"></div>');

    
    var countFotos = 0;
    var maxHeight  = 192;

    $("#film p").each(function() {
       imgArr[imgArr.length] = $(this).html();
       if ($(this).height() > maxHeight) {
           maxHeight = $(this).height();
       }
       countFotos++;
       if (countFotos == 0) {
          $("#fotoleiste_old").html($(this).html());
       } else if (countFotos == 1) {
          $("#fotoleiste_neu").html($(this).html());
       }
    });

    if (countFotos > 0) {
        $(".img_unten").css('height', maxHeight + 'px');
        $(".img_oben").css('height', maxHeight + 'px');
        $(".img_oben").css('margin-top', '-' + maxHeight + 'px');
        changeFoto();
    }

    // Table
   
    var sClass = '';
    var i = 0;
    /*
    $(".table table tr").each(function() {
        
        if ($(this).attr('class') != 'headline') {
            i++;

            if (i % 2 == 0) {
                sClass = 'even';
            } else {
                sClass = 'odd';
            }
            $(this).addClass(sClass);
        } else {
            //i = 0;
        }
    });
*/
    $(".table table").each(function() {
        var i = 0;
        $(this).find("tr").each(function() {

            if ($(this).attr('class') != 'headline') {
                i++;

                if (i % 2 == 0) {
                    sClass = 'even';
                } else {
                    sClass = 'odd';
                }
                $(this).addClass(sClass);
            } else {
                //i = 0;
            }
        });
    });
    var laschen = '';
    var firstEntry = '';
    var iLasche = 0;
    $(".table h1").each(function() {
        html = $(this).html();
        laschen += '<li class="lasche" id="lasche_' + html.replace(' ', '_') + '"><a href="javascript:changeLasche(\'' + html + '\');">' + html + '</a></li>';
        $(this).hide();
        iLasche++;
        if (iLasche == 1) {
            firstEntry = html;
        }
    });

    if (laschen.length > 0) {
        laschen += '<div class="clear"></div>';
        $("#right").html('<ul class="laschen">' + laschen + '</ul>' + $("#right").html());

        var maxHeight = 0;
        $("#right .laschen .lasche").each(function() {
            if ($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
        });
        $("#right .laschen .lasche").css('height', maxHeight + 'px');

        var maxHeight = 0;

        $("#right .table tr.headline td").each(function() {
            $(this).addClass('headerTd');
        });

        $("#right .table tr td").each(function() {
            if (!$(this).hasClass('headerTd')) {
                
                if ($(this).height() > maxHeight) {
                    maxHeight = $(this).height();
                }

                if ($(this).html().indexOf('barnet_tabelle_punkt.png') > 0) {
                    $(this).css('vertical-align', 'middle');
                }
            }
        });

        $("#right .table tr td").css('height', maxHeight + 'px');
        $("#right .table tr.headline td").css('height','16px');
        changeLasche(firstEntry);
    }
    changeLeft();
    window.setTimeout("changeMainHeight()", 200);

    $(window).bind('resize', changeAfterResize);

    
    
    jQuery('#nav_main .level_2 span, #nav_sub .level_1 span').each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Nega-Stat/g, '<span style="font-family:Times New Roman;">Nega-Stat</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ P190/g, '<span style="font-family:Times New Roman;"> P190</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ SR/g, '<span style="font-family:Times New Roman;"> SR</span>'));
    });
    
    jQuery('#nav_main .level_1 a, #nav_sub .level_1 a').each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Nega-Stat/g, '<span style="font-family:Times New Roman;color:#AA2630;font-weight:normal;">Nega-Stat</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ P190/g, '<span style="font-family:Times New Roman;color:#AA2630;font-weight:normal;"> P190</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ SR/g, '<span style="font-family:Times New Roman;color:#AA2630;font-weight:normal;"> SR</span>'));
    });
    
    jQuery('h1').each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Nega-Stat/g, '<span style="font-family:Times New Roman;">Nega-Stat</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ P190/g, '<span style="font-family:Times New Roman;"> P190</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ SR/g, '<span style="font-family:Times New Roman;"> SR</span>'));
    })
    
    jQuery('.ce_text p, label').each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Nega-Stat/g, '<span style="font-family:Times New Roman;">Nega-Stat</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ P190/g, '<span style="font-family:Times New Roman;"> P190</span>'));
        jQuery(this).html(jQuery(this).html().replace(/ SR/g, '<span style="font-family:Times New Roman;"> SR</span>'));
    })
    
    jQuery('#nav_main .level_2 a.trail').each(function() {
        jQuery(this).html(jQuery(this).html().replace(/Nega-Stat/g, '<span style="font-family:Times New Roman;color:#706F6F;">Nega-Stat</span>'));
    })
    
   

});


function changeAfterResize() {
    var winHeight = $(window).height();
    var winWidth  = $(window).width();
    var px = winHeight - 58;
    var l  = Math.round((winWidth - 976) / 2);


    $('#footer').css('top', px + 'px');
    $('#footer').css('left', l + 'px');
}

function changeLeft() {
    var winWidth  = $(window).width();
    var l  = Math.round((winWidth - 976) / 2);
    $('#footer').css('left', l + 'px');

    var winHeight = $(window).height();
    var px = winHeight - 54;
    $('#footer').css('top', px + 'px');
}

function changeMainHeight() {
    var winHeight = $(window).height();
    var winWidth  = $(window).width();
    var px = winHeight - 54;
    var l  = Math.round((winWidth - 976) / 2);


    $('#footer').css('top', px + 'px');
    $('#footer').css('left', l + 'px');


    var max = 0;
    if ($('#left').height() > max)  max = $('#left').height();
    if ($('#main').height() > max)  max = $('#main').height();
    if ($('#right').height() > max) max = $('#right').height();
    max = max + 38;
    $('#main').css('height', max + 'px');

    /*
    var winHeight  = $(window).height();
    var bodyHeight = $('body').height();



    if (winHeight > bodyHeight) {
        var px = winHeight - bodyHeight - 8;
        $('#footer').css('margin-top', px + 'px');
    }

    */
   
    var isiPad = navigator.userAgent.match(/iPad/i) != null;
    if (isiPad) {
        $('#footer').css('position', 'relative');
        $('#footer').css('left', '0');
        $('#footer').css('top', '0');
    }
}

function navComMouseOver() {
    $("#nav_com a").addClass('hover');
}

function navComMouseOut() {
    $("#nav_com a").removeClass('hover');
}

function changeLasche(id) {
    $("#right .laschen .lasche").removeClass('active');
    $("#right .laschen #lasche_" + id.replace(' ', '_')).addClass('active');

    $("#right .table").hide();
    $("#right ." + id.replace(' ', '_')).show();
}


function changeFoto() {
   var html    = $("#fotoleiste_neu").html();
   if (html.length > 0) {
       var idx     = html.indexOf('>');
       var newHtml = html.substr(0, idx + 1);

       $("#fotoleiste_neu").html(newHtml);
   }
   
   $("#fotoleiste_neu").animate({opacity: 1.0}, 1400, function() {
       $("#fotoleiste_old").html(html);
       $("#fotoleiste_neu").css('opacity', '0.0');
         if (iFoto == imgArr.length - 1) {
          $("#fotoleiste_neu").html(imgArr[0]);
          iFoto = 0;
       } else {
          iFoto++;
          $("#fotoleiste_neu").html(imgArr[iFoto]);
       }

       window.setTimeout("changeFoto()", 1400);
   });
}

function searchJumpIn() {
  if (jQuery("#ctrl_keywords").val() == 'Search') {
    jQuery("#ctrl_keywords").val('');
    jQuery("#ctrl_keywords").css('color', '#706f6f');
  }
}

function searchJumpOut() {
  if (jQuery("#ctrl_keywords").val() == 'Search' || jQuery("#ctrl_keywords").val() == '') {
    jQuery("#ctrl_keywords").val('Search');
    jQuery("#ctrl_keywords").css('color', '#c6c6c6');
  }

}

