/* taywa gmbh ft@taywa.ch */

$(document).ready(function () {

    $('.ms-rteElement-mail').each(function () {
        if ($(this).attr('href') != null) {
            var str = $(this).attr('href').replace("mailto:", "");
            $(this).attr('href', 'mailto:' + ($.rotate13(str)));
        }
    });

    $('.contact-box-link').each(function () {
        if ($(this).attr('href') == "#data") {
            var text = $(this).html();
            if (text.indexOf("@") > 0) {
                $(this).html(($.rotate13(text)));
            }
        }
    });

    /* emergency */
    $('#emergency').hover(function () {
        $('#emergencyfly').animate({ 'opacity': 1 }, 300);
    }, function () {
        $('#emergencyfly').animate({ 'opacity': 0 }, 300);
    });

    /* events */
    $('.clickme').click(function (e) {
        $(e.target).parent().hide();
        $(e.target).parent().next().show();
    });

    $('.closeme').click(function (e) {
        $(e.target).parent().hide();
        $(e.target).parent().prev().show();
    });

    if ($('#subnav').html() != null) {
        if ($('#subnav').html().trim().length > 10) {
            $('#mainnavsub').css('opacity', 0);
            $('#subnav').css('opacity', 0);
            $('#maincol').css('opacity', 0);
            /* second level navigation only visible for patienten so event has to fire on main col otherwise */
            if ($('#mainnavsub').html() != null)
                $('#mainnavsub').animate({ 'opacity': 1 }, 500, function () { subnavin(); });
            else
                $('#maincol').animate({ 'opacity': 1 }, 500, function () { subnavin(); });
        }
    }

    /* hide h2 and p if empty */
    if ($('#cont h2').first().html() != null) {
        if ($('#cont h2').first().html().trim().length < 3) {
            $('#cont h2').first().css('display', 'none');
        }
    }
    if ($('#cont p').first().html() != null) {
        if ($('#cont p').first().html().trim().length < 3) {
            $('#cont p').first().css('display', 'none');
        }
    }

    var input_search = $(".searchField");
    if (input_search) {
        var val = input_search.val();
        $(".searchField")
	            .focus(function () {
	                if ($(this).val() == val)
	                    $(this).val("");
	            })
	            .blur(function () {
	                if ($(this).val() == "")
	                    $(this).val(val);
	                else
	                    val = input_search.val();
	            });
    }

});

var subnavin = function () {
    //alert("yo");

    var ul = $('#subnav ul .level1');
    var height = ul.css('height');

    //ul.css('opacity', 0);

    $('#subnav').css('opacity', 1);


    ul.css('height', 0).animate({ height: height }, 500);

    $('#maincol').animate({ 'opacity': 1 }, 500);
    //$('#subnav').animate({'opacity': 1}, 500, function() {  maincolin(); }); 
}

var maincolin = function () {
    //alert("ya");
    $('#maincol').animate({ 'opacity': 1 }, 500);
}


/* adjust heigth of #bottom div */
var bottomAdJust = function () {

    wh = $(window).height();
    ah = $('#all').height();

    if (wh > ah) {
        diff = wh - ah;
        bh = $('#bottom').height();
        bh = bh + diff;
        $('#bottom').css('height', bh + 'px');
        //     console.log(bh);
    }
}


$(window).load(function () {

    var navClick = function (e) {

        /* comment next 3 lines out for live version 
        if (document.title == 'page') {
            e.preventDefault();
        }*/

        $(this).blur();
        $(this).css('color', '#E85C0F');

        var tli = $(this).parent().children('ul').first();
        //console.log(tli);


        tli.css('display', 'block');

        var height = tli.css('height');


        tli.css('height', 0).animate({ height: height }, 500);

        $('#subnav li.act ul.level1').animate({ height: 0 }, 500)

        $('#subnav li.act a').css('color', '#9D9699');
    }

    $('#subnav a.level0').click(navClick);

    bottomAdJust();

});



$(window).resize(
    function () {
        bottomAdJust();
    });



