// Hash links (for tabs etc.)
var hash = document.location.hash.replace('#', '');

// Just a couple browser tests
var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
var is_win = /windows/.test(navigator.userAgent.toLowerCase());
var is_firefox = /firefox/.test(navigator.userAgent.toLowerCase());

$jq(function($) {
    // !/===> Browsers
    if ($jq.browser.safari) $("body").addClass("sfr");
    if ($jq.browser.msie) $("body").addClass("msie");
    if (is_chrome) $("body").addClass("chr");
    if (is_win && is_firefox) $("body").addClass("ffwin");
    if (is_win && $jq.browser.safari) $("body").addClass("sfrwin");
    if ($jq.browser.msie && $jq.browser.version >= 7 && $jq.browser.version < 8) $("body").addClass("msie7");
    if ($jq.browser.msie && $jq.browser.version >= 6 && $jq.browser.version < 7) $("body").addClass("msie6");


    // Focus and blur on all input text fields
    $("input[type=text], input[type=password]").addClass("txt");
    $("input[type=text]").focus(function() { $(this).addClass("focus"); });
    $("input[type=text]").blur(function() { $(this).removeClass("focus"); });

    // Clear search field
    $("a.clear-field").click(function() {
        $("#inp-s").val("").focus();
        return false;
    });

    // Input placeholder
    $("li.label-placeholder").each(function() {
        var $label = $(this).find("label:eq(0)");
        var $input = $(this).find("input:eq(0)");

        $label.click(function() {
            $(this).hide();
            $input.focus();
        });
        $input.focus(function() {
            $label.hide();
        });
        $input.blur(function() {
            var inpval = $(this).val();
            if (!inpval) $label.show();
        });
    });

    // Some table helpers
    $("table thead").each(function() {
        $(this).children("tr").children("th:last").addClass("last");
    });

    $("table td.toggle-content a.toggle").click(function() {
        $(this).parent().children(".tcnt").toggle();
        $(this).parent().parent().toggleClass('toggle-content-on');
        return false;
    });


    /* Contact list toggles */
    $(".contact-list li h4 a").click(function() {
        var $cnt = $(this).parent().parent().parent().children(".tcnt");
        $cnt.toggle();
        $cnt.parent().toggleClass("opened");
        return false;
    });


    // !/===> Autocomplete on search
    /*$("#search p.btn").hide();
    $("#search input[type=text]").autocomplete({
        source: "autocomplete.js"
		, minLength: 3
		, select: function(event, ui) {
		    //log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
		    $("#search").submit();
		}
		, open: function(event, ui) {
		    $("#search p").not(".btn").addClass("ac");
		    $(".ui-menu-item").css({ background: "rgb(119, 106, 99)" });
		    left = $(".ui-autocomplete").css('left');


		    if ($jq.browser.msie || $jq.browser.safari) {
		        left = left.replace("px", "") * 1 - 47;
		        $(".ui-autocomplete").css({ left: left, top: 10, width: 184, background: "url(images/round_search.png) no-repeat bottom" });
		    }
		    else {
		        left = left.replace("px", "") * 1 - 46;
		        $(".ui-autocomplete").css({ left: left, top: 10, width: 184, background: "url(images/round_search.png) no-repeat bottom" });
		    }

		}
		, close: function(event, ui) {
		    $("#search p").not(".btn").removeClass("ac");
		}
    });
    var search_timeout = false;
    $("#search input[type=text]").focus(function() {
        clearTimeout(search_timeout);
        $("#search p.btn").fadeIn();
    });
    $("#search input[type=text]").blur(function() {
        search_timeout = setTimeout(function() { $("#search p.btn").fadeOut(); }, 300);
    });*/


    // !/===> Form info buttons
    $("#main form li p.inf em").hover(
		 function() { $(this).parent().children("span").show(); }
		, function() { $(this).parent().children("span").hide(); }
	);
    var info_timeout = false;
    $("input.info-trigger").focus(function() {
        var $el = $(this);
        info_timeout = setTimeout(function() {
            $el.parent().find("p.inf span").fadeIn();
        }, 1500);
    });
    $("input.info-trigger").blur(function() {
        clearTimeout(info_timeout);
        $(this).parent().find("p.inf span").fadeOut();
    });


    // !/===> Meta drop down
    $("#meta .nav > li .dd").show().hide();
    $("#meta .nav > li").hover(
		 function() {
		     var total_width = 0;
		     var left_width = 0;
		     var item_width = $(this).width() + 31;
		     var item_padding = parseInt($(this).children("a").css('padding-left'));

		     $("#meta > .nav > li").each(function() {
		         total_width += ($(this).width() + 37);
		         if (!$(this).hasClass('site') && !$(this).hasClass('lang')) {
		             left_width += ($(this).width() + 37);
		         } //end if
		     });

		     // Resize items in drop down
		     $("#meta .dd li").each(function(num) {
		         if (num % 2 == 0) {
		             $(this).css({ width: (item_width + (28 - item_padding)) + "px" });
		         } else {
		             $(this).css({ width: "220px" });
		         } //end if
		     });

		     $(this).addClass("hover");
		     $(this).children(".dd").children(".wrap").children("ul").css({ "margin-left": (left_width - (32 - item_padding)) + "px" });
		     $(this).children(".dd").css({ width: (total_width + $("#search").width()) + 'px' }).show();
		 }
		, function() { $(this).removeClass("hover"); $(this).children(".dd").hide(); }
	);


    // Input placehoders
    activatePlaceholders();


    // Add some div links
    // ORIGINAL VERSION: $(".quad-entries li, .teaser .col").divlink();
    $(".quad-entries li div.link").divlink(); // NEW VERSION
    $(".articles .article, .img-txt .box").divlink();
    $(".quad-entries li div.videoLink").divlink(); // NEW VERSION



    // !/===> Drop down navigation
    var dd_open = false; // Flag to see if drop down is already open
    var do_timeout = false; // Timeout for opening the drop down
    var dd_timeout = false; // Timeout for closing the drop down
    var dd_init = true;

    var open_timeout_length = 300;
    var close_timeout_length = 300;
    $("#nav .dd").wrapInner('<div class="wrap" />');
    $("#nav .dd").show().ddHeight(15).hide();
    //$("#nav .dd").css("height", $(this).children(".li"));
    $("#nav > ul > li").each(function() {
        var has_dd = $(this).children(".dd").length;
        if (!has_dd) $(this).addClass("no-dd");
    });
    $("#nav .dd").hide();
    $("#nav > ul > li").not(".no-dd").hover(
		 function() {
		     var $el = $(this);

		     if (!dd_open) otl = open_timeout_length;
		     else otl = 0;


		     do_timeout = setTimeout(function() {
		         clearTimeout(do_timeout);
		         clearTimeout(dd_timeout);

		         $el.find("> a").addClass("hover");

		         var $dd = $el.children(".dd");
		         var $dd1 = $(".dd1");
		         var $dd2 = $(".dd2");
		         var $dds = $("#nav .dd");
		         var $wrap = $dd.children(".wrap");
		         var nav_offset = $("#nav").offset();
		         var offset = $el.offset();
		         var offset_left = offset.left - nav_offset.left;
		         var $lis = $("#nav > ul > li");
		         var $link = $el.children("a");
		         // Fade 1st level links
		         $link.dequeue().stop().fadeTo(300, 1);

		         $("#nav > ul > li > a").not($link).dequeue().stop().fadeTo(300, .5).hide().show();

		         // Show drop down
		         /*	$wrap.css({ left: offset_left }); */

		         $el.addClass("hover");


		         var height = 0;


		         if (dd_open) {
		             $dds.hide();
		             $lis.not($el).removeClass("hover");
		             $dd.show(0);
		             height = $dd.ddMaxHeight(43);
		             $dd2.css({ top: height });

		         } else {
		             //$dd.slideDown({ duration: 500, easing: "easeOutQuad" });				
		             $dd.slideDown(300, function() {
		                 height = $dd.ddMaxHeight(43);
		                 $dd2.css({ top: height });
		                 $dd2.slideDown(0);
		             });



		             $dd1.show(0);
		         } //end if

		         // Set open flag
		         dd_open = true;

		         // Set init flag
		         dd_init = false;

		     }, otl);


		 } // end mouseover()
		, function() {
		    clearTimeout(do_timeout);

		    var $el = $(this);
		    var $link = $el.children("a");
		    var $dd = $el.children(".dd");
		    var $dds = $("#nav .dd");
		    var $lis = $("#nav > ul > li");
		    var $dd1 = $(".dd1");
		    var $dd2 = $(".dd2");

		    // Fade 1st level links
		    $("#nav > ul > li > a").fadeTo(350, 1);

		    dd_timeout = setTimeout(function() {
		        $dds.not($dd).hide();
		        $dd.slideUp({ duration: 350, easing: "easeOutQuad", complete: function() { $dds.hide(); $dd1.hide(0); } });
		        $lis.removeClass("hover");
		        dd_open = false;
		        $dd2.hide(0);
		    }, close_timeout_length);
		    $el.find("> a").removeClass("hover");


		} // end mouseout()
	); //end hover()



    // !/===> Filters on article list
    $(".filters h3.toggle a").click(function() {
        $(this).toggleClass("on");
        $(".filters .cx").toggle();
        return false;
    });

    // !/===> Filters on calendar
    $(".filters-calendar h3.toggle a").click(function() {
        $(this).toggleClass("on");
        $(".filters-calendar .cx").toggle();
        return false;
    });

    $("#main .articles-list").hide();
    $(".filters .switch-view a").click(function() {
        $(".filters .switch-view a").removeClass("on");
        $(this).addClass("on");

        $("#main .articles").hide();
        if ($(this).hasClass("matrix")) $("#main .articles-matrix").show();
        else if ($(this).hasClass("list")) $("#main .articles-list").show();

        return false;
    });


    // Run custom selectbox widgets
    try {
        custom_select();
    } catch (e) { }

    // Selectbox indexes
    var all_sel = $(".offer > form > ul > li").length;
    $(".offer > form > ul > li").each(function() {
        $(this).css({ "z-index": all_sel });
        all_sel--;
    });

    // !/===> Scrolable tabs
    $(".tabbed-scroll .tabs").jCarouselLite({
        scroll: 6
		, visible: 6
		, btnNext: ".next-tabs"
		, btnPrev: ".prev-tabs"
		, speed: 500
		, circular: false
    });
    if ($.browser.opera) {
        $(".tabbed-scroll .tabs li").css({ width: 113 });
    }

    // Shadowbox
    //$jq(".media-thumbs li a").colorbox({transition:'fade', speed:500});
    var ShadowboxZToggle = function() {
        if ($jq(this).hasClass("zoom-icon-zoomed")) {
            // zoom
            $jq("#sb-body-inner")
			.css({ overflow: "scroll" })
			.find("img")
			.removeAttr("width")
			.removeAttr("height");
        } else {
            // unzoom
            $jq("#sb-body-inner")
			.css({ overflow: "hidden" })
			.find("img")
			.attr("width", $jq("#sb-body-inner").width())
			.attr("height", $jq("#sb-body-inner").height());
        }
    };
    window.setTimeout(function() {
        var sbox_init = false;
        Shadowbox.init({
		    counterType: 'default'
		                , player: 'player'
		                , overlayColor: '#2A2420'
		                , overlayOpacity: 0.9
		                , initialHeight: 350
		                , initialWidth: 500
		                , onOpen: function(obj) {
		                    if (!sbox_init) {
		                        // Move the title
		                        $("#sb-title").appendTo("#sb-wrapper"); sbox_init = true;
		                        $("#sb-title").hide();

		                        // Create text elements
		                        var $txt_cont = $('<div id="sb-verbund-text" />');
		                        var $title = $('<h2 />');
		                        var $text = $('<p />');
		                        $txt_cont.append($title);
		                        $txt_cont.append($text);
		                        $("#sb-info").prepend($txt_cont);

		                        // Create close link
		                        var $close = $('<a href="#" id="sb-verbund-close">&nbsp;</a>');
		                        $("#sb-wrapper").prepend($close);
		                        $close.click(function() {
		                            Shadowbox.close();
		                            return false;
		                        });

		                        // Add pager
		                        var $pager = $('<div id="sb-verbund-pager" />')
		                        $pager.append('<a href="#" class="prev">Previous</a>');
		                        $pager.append('<a href="#" class="next">Next</a>');
		                        $pager.append('<span class="counter">99/99</span>');
		                        $("#sb-wrapper-inner").prepend($pager);

		                        $("#sb-verbund-pager .prev").click(function() { $("#sb-verbund-text").fadeOut(); Shadowbox.previous(); return false; });
		                        $("#sb-verbund-pager .next").click(function() { $("#sb-verbund-text").fadeOut(); Shadowbox.next(); return false; });

		                        sbox_init = true;

		                        $zoomer = $jq('<div id="sb-verbund-zoomer" />');
		                        $zoomer.append(
					                $jq("<a/>")
					                .addClass("zoom-icon")
					                .attr("href", "javascript:void(0);")
					                .click(function() {
					                    $jq(this).toggleClass("zoom-icon-zoomed");
					                    ShadowboxZToggle.apply(this);
					                }).text(" ")
				                );
		                        $jq("#sb-wrapper-inner").prepend($zoomer);
		                        $jq(window).resize(function() {
		                            window.setTimeout(function() {
		                                $jq("#sb-verbund-zoomer a").each(function() {
		                                    ShadowboxZToggle.apply(this);
		                                });
		                            }, 50);
		                        });
		                    }
		                    $jq("#sb-verbund-zoomer a").each(function() {
		                        $jq(this).removeClass("zoom-icon-zoomed");
		                        ShadowboxZToggle.apply(this);
		                    });		                    
		                    
		                    var $el = Shadowbox.getCurrent();
		                    $el = $($el.link);

		                    $jq("#sb-verbund-text h2").text($el.parent().find("h2").text());
		                    $jq("#sb-verbund-text p").text($el.parent().find("p").text());
		                    var $ul;
		                    if ($el.next().attr("class") == "fotopoint") {
		                        $ul = $el.parent().parent();
		                    } else {
		                        $ul = $el.parent().parent().parent().parent();
		                    }
		                    var current = parseInt(Shadowbox.current) + 1;
		                    var $items = $ul.find(".item");
		                    var total = $items.length;
		                    $("#sb-verbund-pager .counter").text(current + "/" + total);
		                    if (total <= 1) {
		                        $("#sb-verbund-pager").hide();
		                    }
		                }
		                , onChange: function(a, b, c, d) {
		                    var $el = Shadowbox.getCurrent();
		                    $el = $jq($el.link);

		                    $jq("#sb-verbund-text h2").text($el.parent().find("h2").text());
		                    $jq("#sb-verbund-text p").text($el.parent().find("p").text());
                            var $ul;
		                    if ($el.next().attr("class") == "fotopoint") {
		                        $ul = $el.parent().parent();
		                    } else {
		                        $ul = $el.parent().parent().parent().parent();
		                    }
		                    var current = parseInt(Shadowbox.current) + 1;
		                    var $items = $ul.find(".item");
		                    var total = $items.length;
		                    $("#sb-verbund-pager .counter").text(current + "/" + total);
		                    if (total <= 1) {
		                        $("#sb-verbund-pager").hide();
		                    }
		                    $jq("#sb-verbund-zoomer a").each(function() {
		                        $jq(this).removeClass("zoom-icon-zoomed");
		                        ShadowboxZToggle.apply(this);
		                    });
		                    if ($jq.browser.msie) {
		                        $jq("#sb-verbund-zoomer").hide();
		                    } else {
		                        $jq("#sb-verbund-zoomer").fadeOut();
		                    }
		                }
		                , onFinish: function(obj) {
		                    var $el = Shadowbox.getCurrent();
		                    $el = $jq($el.link);

		                    $jq("#sb-verbund-text h2").text($el.parent().find("h2").text());
		                    $jq("#sb-verbund-text p").text($el.parent().find("p").text());
                            var $ul;
		                    if ($el.next().attr("class") == "fotopoint") {
		                        $ul = $el.parent().parent();
		                    } else {
		                        $ul = $el.parent().parent().parent().parent();
		                    }
		                    var current = parseInt(Shadowbox.current) + 1;
		                    var $items = $ul.find(".item");
		                    var total = $items.length;
		                    $("#sb-verbund-pager .counter").text(current + "/" + total);
		                    if (total <= 1) {
		                        $("#sb-verbund-pager").hide();
		                    }

		                    var sb_text = obj.title;
		                    if (sb_text == "") {
		                        sb_text = $el.children("input[type=hidden]").val();
		                    }
		                    if (typeof(sb_text) == "undefined") {
		                        sb_text = "";
		                    }
	                        sb_text = sb_text.split('|');
	                        $("#sb-verbund-text h2").text(sb_text[0]);
	                        $("#sb-verbund-text p").text(sb_text[1]);
	                        $("#sb-verbund-text").fadeIn();
	                        if (Shadowbox.dimensions.oversized) {
	                            if ($jq.browser.msie) {
	                                $jq("#sb-verbund-zoomer").show();
	                            } else {
	                                $jq("#sb-verbund-zoomer").fadeIn();
	                            }
	                        } else {
	                            if ($jq.browser.msie) {
	                                $jq("#sb-verbund-zoomer").hide();
	                            } else {
	                                $jq("#sb-verbund-zoomer").fadeOut();
	                            }
	                        }
		                }
        });
    }, 250);
});

// !=== ===> Custom functions
function custom_select() {
    if ($jq.browser.msie && $jq.browser.version < 7) {
    } else {
        $jq("select.ssel1").selectWidget({ extra_class: 'ssel1' });
        $jq("select.ssel2").selectWidget({ extra_class: 'ssel2', hide_label: true });
        $jq("select.ssel3").selectWidget({ extra_class: 'ssel3', hide_label: true });
        //$jq("select.ssel3").selectWidget({extra_class: 'ssel1', hide_label: true});
    } // endif
}


function activatePlaceholders() {
    // First we need to add placeholder attribute via JS because of failed HTML validation
    $jq("input.placeholder").each(function() {
        $jq(this).attr("placeholder", $jq(this).val()).val("");
    });

    // Then run the placeholder script for non-webkit browsers
    var detect = navigator.userAgent.toLowerCase();
    var inputs = document.getElementsByTagName("input");
    if (detect.indexOf("safari") > 0) {
        return false;
    } else {
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].getAttribute("type") == "text") {
                if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
                    inputs[i].value = inputs[i].getAttribute("placeholder");
                    inputs[i].onfocus = function() {
                        if (this.value == this.getAttribute("placeholder")) {
                            this.value = "";
                        }
                        return false;
                    } //end onfocus
                    inputs[i].onblur = function() {
                        if (this.value.length < 1) {
                            this.value = this.getAttribute("placeholder");
                        }
                    } //end onblur()
                } //end if
            } //end if
        } //end for()
    } // end if
}

//Window-resize Event(footer)
function resizeFooter() {
    window.setTimeout(function() {
        if ($jq(window).height() > $jq('#page').height()) {
            $jq('#footer').css({ "position": "absolute" });
        } else {
            $jq('#footer').css({ "position": "relative" });
        }
        $jq('#footer').hide().show();
    }, 500);
}

//Window-resize Event(Logo)
$jq(window).resize(function() {
    if ($jq(window).width() < 1155) {
        $jq('#logo a').css({ "margin-left": 85 });
        $jq('#page').css({ "margin-left": -70 });
    } else {
        $jq('#logo a').css({ "margin-left": 0 });
        $jq('#page').css({ margin: "0 auto" });
    }
    resizeFooter();
});

$jq(document).ready(function() {
    resizeFooter();
});

// !/===> Media gallery paging
$jq(document).ready(function() {
    var mt_count = 1;
    $jq(".media-thumbs").each(function() {
        var $lis = $jq(this).children("li");
        var per_page = 20;
        var pages = Math.ceil($lis.length / 20);
        $jq(this).hide();

        // Pager elements
        var $pager = $jq('<div class="media-pager"/>');
        var $pager_ul = $jq('<ul class="media-pager-ul" />');
        var $pager_txt = $jq('<div class="pager-txt" />');

        // Add pages
        for (var i = 0; i < pages; i++) {
            $pager_ul.append('<li class="cpg cpg-' + parseInt(i + 1) + '"></li>');
        } //end for
        $pager.append($pager_ul);

        // Add pager to DOM
        var current_page = 1;
        $pager_txt.html($jq("#HiddenField_MediaCenterPagesText").val().replace("{0}", "<em>" + current_page + "</em>").replace("{1}", pages));
        $pager.append($pager_txt);
        $jq(this).before($pager);


        // Now let's move the elements
        var li_counter = 1;
        $lis.each(function() {
            var cpg = Math.ceil(li_counter / per_page);
            var content = $jq(this).html();
            var item_class = "item item-" + li_counter;
            if (li_counter % 5 == 0) {
                item_class += " item-5th";
            } //end if
            var $item = $jq('<div class="mediathek_container ' + item_class + '">' + content + '</div>');

            $item.appendTo($pager_ul.children("li.cpg-" + cpg));
            li_counter++;
        });

        $jq(".mediathek_display_image").each(function() {
            $jq(this).attr("alt", "");
        });

        $jq(".mediathek_container").hover(
            function() {
                // Handler mouse over
                if ($jq(this).find("img.mediathek_download_img").size() == 1) {
                }
                $jq(this).find("img.mediathek_download_img").css("display", "inline-block");
                $jq(this).find("img.mediathek_shadowbox").css("display", "inline-block");
            },
            function() {
                // Handler mouse out
                $jq(this).find("img.mediathek_download_img").css("display", "none");
                $jq(this).find("img.mediathek_shadowbox").css("display", "none");
            });

        // Add pager links for carousel
        /* AP 2010-11-24 CHANGED var $prev = $('<a class="prev disabled mt-prev-' + mt_count + '">Prev</a>'); */
        var $prev = $jq('<a class="prev mt-prev-' + mt_count + '">Prev</a>');
        var $next = $jq('<a class="next mt-next-' + mt_count + '">Next</a>');
        $pager.append($prev);
        $pager.append($next);

        // Run carousel
        $jq(".media-pager").jCarouselLite({
            scroll: 1
			, visible: 1
			, speed: 300
			, btnNext: ".mt-next-" + mt_count
			, btnPrev: ".mt-prev-" + mt_count
			, circular: false
			, afterEnd: function(a) {
			    $pager_txt.children("em").html(a[0].classList[1].split('-')[1]);
			    /*$pager_txt.children("em").html(_parseInt($pager_txt.children("em").html(), 0) + 1);*/
			}
        });
        
        mt_count++;

        $jq(this).remove();
    });

    // !/===> Tabs 
    $jq("#main .tabbed").not(".tabbed-scroll").tabbed();
    $jq("#main .tabbed-scroll").tabbed({ scroll: true });


    if ($jq(window).height() > $jq('#page').height()) {
        $jq('#footer').css({ "position": "absolute" });
    }
    else {
        $jq('#footer').css({ "position": "relative" });
    }


    /* CONFIG */
    xOffset = -10;
    yOffset = -20;
    xoff = xOffset;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result		
    /* END CONFIG */

    $jq("a.tooltip").hover(function(e) {
        this.t = this.title;
        this.title = "";
        if ($jq(this).parent().hasClass("item-5th")) xoff = -120;
        else xoff = xOffset;
        $jq("body").append("<p id='tooltip'>" + this.t + "</p>");
        $jq("#tooltip")
					.css("top", (e.pageY - yOffset) + "px")
					.css("left", (e.pageX + xoff) + "px")
					.fadeIn("fast");
    },
				function() {
				    this.title = this.t;
				    $jq("#tooltip").remove();
				});
    $jq(".tooltip").mousemove(function(e) {
        if ($jq(this).parent().hasClass("item-5th")) xoff = -120;
        else xoff = xOffset;
        $jq("#tooltip")
					.css("top", (e.pageY - yOffset) + "px")
					.css("left", (e.pageX + xoff) + "px");
    });

});

$jq(function () {
    if ($jq("#Link_EditCss").length == 0) {
        $jq("table").not(".noborder").each(function () {
            $jq(this).css("width", "100%").css("background", "none").css("margin-bottom", "20px").css("border", "none").css("color", "#4e3f38");
            $jq(this).find("td, th").css("border", "1px solid #ccc").css("text-align", "right");
            $jq(this).find("td p, th p").css("margin", "0");
            $jq(this).find("tr:first-child th, tr:first-child td").css("border-top", "none").css("border-bottom", "none").css("font-weight", "bold");
            $jq(this).find("tr:last-child td, tr:last-child th").css("border-bottom", "none");
            $jq(this).find("td:first-child, th:first-child").css("border-left", "none").css("text-align", "left");
            $jq(this).find("td:last-child, th:last-child").css("border-right", "none");
            $jq(this).find("td[rowspan]").css({ "border-left": "1px solid #ccc", "border-bottom": "none" });
        });
    }
    $jq("img").each(function () {
        $jq(this).attr("alt", "");
    });

    /* For each side-box block set margin bottom to zero */
    $jq(".side-box div").each(function () {
        if ($jq("#Link_EditCss").length == 0) {
            $jq(this).find(":last-child").each(function () {
                if ($jq(this).parent().next().attr("class") != "btns") {
                    $jq(this).css({ 'margin-bottom': '0' });
                }
            });
        }
        $jq(this).find(":first-child").not("li").css({ 'margin-top': '0' });
    });

    /* The same for the teaser blocks */
    $jq(".gallery-categories").each(function () {
        $jq(this).find("div:last-child").css({ 'margin-bottom': '0' });
    });

    /* The same for the tab container elements */
    $jq("#main .side-box .tabbed .tab-cont .cx").each(function () {
        $jq(this).find("div:last-child").css({ 'margin-bottom': '0' });
    });

    /* Set the margins for the column modules */
    $jq("#main .side-box div.aside").each(function () {
        $jq(this).find(".widget").css({ 'margin-bottom': '30px' });
        $jq(this).find(".widget:last-child").css({ 'margin-bottom': '0' });
    });

    /* Add margin on bottom of ul, ol, table */
    $jq("#main .side-box div").each(function () {
        /*        var $elp = $jq(this).children("p");
        $jq(this).children("p + ul").css({ 'margin-top': '-15px' });
        $elp.next("ul").each(function() {
        $elp.css({ 'margin-bottom': '5px' });
        });
        */
        $jq(this).children("ul").css({ 'margin-bottom': '20px' });
        $jq(this).children("ol").css({ 'margin-bottom': '20px' });
        if ($jq("#Link_EditCss").length == 0) {
            $jq(this).children("table").css({ 'margin-bottom': '20px' });
        }
    });

    var $subnav = $jq("#main #subnav");
    var height = $subnav.children("ul").height();
    if (height < 216) {
        var offset = 6;
        if ($jq.browser.msie) {
            offset -= 3;
        } else if ($jq.browser.safari) {
            offset -= 9;
        }
        if ($subnav.children("ul").children("li").length == 0) {
            offset -= 3;
            if ($jq.browser.msie) {
                offset -= 3;
            }
        }
        var margin = 216 - height - offset;
        $subnav.children("#roundleft_subnav").css({ 'margin-top': margin + 'px' });
        $subnav.children("#roundright_subnav").css({ 'margin-top': margin + 'px' });
    } else if ($jq.browser.safari) {
        $subnav.children("#roundleft_subnav").css({ 'margin-top': 9 + 'px' });
        $subnav.children("#roundright_subnav").css({ 'margin-top': 9 + 'px' });
    }

    $jq("#main .quad-entries li div.img").each(function () {
        $jq(this).children("img").css({ 'width': '236px' });
        $jq(this).children("img").css({ 'height': '115px' });
    });
});

