﻿var ie7 = IE7Recognizer();
var ie8 = IE8Recognizer();
var chr = ChrRecognizer();
var counter_loaging = 0;
var counter_img = 0;
var chLoad = null;
$(document).ready(function () {
    counter_img = $('#slideshow img').length;
    $('#slideshow img:first').addClass('active');
    $('#slideshow img').load(function () {
        counter_loaging++;
        $(this).css("display", "block");
    });
    chLoad = setInterval("checkCounter()", 500);

    $(".pink").hover(function () {
        $(this).addClass("pover");
    }, function () {
        $(this).removeClass("pover");
    });
    var cufontext = ".aktuelt .akh .akhb h3, .interisting .inth .inthb h3, .search h3, .startintro .image h1, .startintro .image h2, .startintro .image h3, .subcontact h3, ";
    cufontext += ".startintro .image h4, .startintro .image h5, .startintro .image h6, .blue h1, .blue h2, ";
    cufontext += ".blue h3, .blue h4, .blue h5, .blue h6, .blue1 h1, .blue1 h2, .blue1 h3, .blue1 h4, ";
    cufontext += ".dorange h1, .dorange h2, .dorange h3, .dorange h4, .dorange h5, .dorange h6, ";
    cufontext += ".blue1 h5, .blue1 h6, .search-btn a, .button em.bs, .orange .pretext h3, ";
    cufontext += ".menu ul li a.first em.mmc, .tbox .count-text, .lang a, .tbox .digital, .banner .info h4, ";
    cufontext += ".left h1, .orangeH .text, .np-h h3, .for-a-text .texto strong, .for-a-text .texto span";
    Cufon.replace(cufontext, { fontFamily: 'bellgoth', hover: true, hoverables: { a: true, em: true} });
    if (ie7) {
        $(".shadow .button").each(function () {
            var w = $(this).width();
            $(this).parent().parent().parent().parent().css("width", w + 12 + "px");
        });
    }
    $(".button.brown").hover(function () {
        $(this).addClass("dark");
    }, function () {
        $(this).removeClass("dark");
    });
    $(".button").hover(function () {
        if ($(this).attr("class") == "button")
            $(this).addClass("dblue");
    }, function () {
        $(this).removeClass("dblue");
    });
    $(".search-btn").hover(function () {
        $(this).addClass("onn");
    }, function () {
        $(this).removeClass("onn");
    });
    var htable = $(".sb table:first").height();
    $(".advansed-btn.down").click(function () {
        $(this).hide();
        $(".advansed-btn.up").css("display", "inline-block");
        $("#sub-advansed").slideUp();
        $("#advansed").slideUp();
        if (ie7) {
            $(".col2").css("height", h + "px");
            mt = Math.floor((htable - 50) / 2) - 55;
            $(".search-btn").css("margin-top", mt + "px");
        }
        if (chr) {
            $(".col2").css("height", h + "px");
            mt = Math.floor((htable - 50) / 2);
            $(".search-btn").css("margin-top", mt + "px");
        }
    });
    $(".advansed-btn.up").click(function () {
        $(this).hide();
        $(".advansed-btn.down").css("display", "inline-block");
        $("#sub-advansed").slideDown();
        $("#advansed").slideDown();
        if (ie7) {
            h = $(".sb table:first").height();
            $(".col2").css("height", h + "px");
            mt = Math.floor((h - 50) / 2) - 33;
            $(".search-btn").css("margin-top", mt + "px");
        }
        if (chr) {
            h = $(".sb table:first").height();
            $(".col2").css("height", h + "px");
            mt = Math.floor((h - 50) / 2) + 50;
            $(".search-btn").css("margin-top", mt + "px");
        }
    });

    $('.menu li').hover(function () {
        $(this).addClass("over");
        Cufon.refresh();
        if (this.timer)
            clearTimeout(this.timer);
        chl = $(this).find('.submenu').children().length;
        if (chl == 0) return false;
        $(this).find('.submenu').show();
        var _this_li = this;
        /* fixes for IE7 */
        if (ie7) {
            $(this).find("iframe").each(function () {
                h = $(_this_li).find(".submenu:first").height();
                $(this).css("height", h + "px");
                $(this).show();
            });
        }
    }, function () {
        $(this).removeClass("over");
        Cufon.refresh();
        var _this = this;
        if (ie7) {
            $(this).find("iframe").each(function () {
                $(this).hide();
            });
        }
        if ($(this).parent().is('li')) {
            this.timer = setTimeout(function () {
                $(_this).find('.submenu').hide();
            }, 300);
        } else {
            $(_this).find('.submenu').hide();
        }
    });
    $("#tab-1").click(function () {
        $("#tab-content-2").hide();
        $("#tab-content-1").show();
        $("#tab-2").addClass("off");
        $("#tab-1").removeClass("off");
    });
    $("#tab-2").click(function () {
        $("#tab-content-1").hide();
        $("#tab-content-2").show();
        $("#tab-2").removeClass("off");
        $("#tab-1").addClass("off");
    });
    $(".typetext").focus(function () { $(this).addClass("focused"); }).blur(function () { $(this).removeClass("focused"); });
});
function IE8Recognizer() {
   if (navigator.userAgent.toString().indexOf("MSIE 8.0") != -1) return true;
   return false;    
}
function IE7Recognizer() {
   if (navigator.userAgent.toString().indexOf("MSIE 7.0") != -1) return true;
   return false;    
}
function ChrRecognizer() {
   if (navigator.userAgent.toString().indexOf("Chrome") != -1) return true;
   return false;    
}
function slideSwitch() {
    var $active = $('#slideshow img.active');
    var $all= $('#slideshow img');
    var currIndex = $active.index();
    var indexEQt = $('#slideshow img').length - 1;
    var indexRounded = Math.random(indexEQt);
    var indexEQ = Math.round(indexRounded*(indexEQt));
    if (currIndex == indexEQ) {
        if (currIndex == 0) indexEQ = 1;
        else if (currIndex == indexEQt) indexEQ = indexEQt - 1;
        else indexEQ = indexEQ + 1;
    }
    if ($active.length == 0) 
        $active = $('#slideshow img:last');
    var $next = $all.eq(indexEQ);
    $active.addClass('last-active');
    $next.css({opacity: 0.0});
    $next.addClass('active');
    $next.animate({opacity: 1.0}, 1500, function() {
        $active.removeClass('active last-active');
    });
}
function checkCounter() {
    if (counter_img == counter_loaging) {
        clearTimeout(chLoad);
        setInterval("slideSwitch()", 5000);
    }
}
