/*
 *  Initializations on page load.
 */
$(document).ready(function(){
    enhanceTeaserFormat();
    $(window).resize(enhanceTeaserFormat);

    if ($.fn.autocomplete) { // check if Autocomplete script available
        $(".ac-me").autocomplete("search.txt", {
            minChars:1,
            matchSubset:1,
            matchContains:0, // #MGH-134: contained strings shouldn't match
            matchSplit:' ',  // #MGH-134: string to be compared gets split -> each sub-sequence will be compared with the entered text 
            cacheLength:10,
            onItemSelect:selectItem,
            formatItem:formatItem,
            selectOnly:1
        });
    }
    $(".ac-me").focus( function() { // select text on focus
        this.select();
    });
	
	$('#selector').click(function() {
		$(this).attr('value','');
	});

    if($("#marginal div.mod.contact.vcard a.ic.ext.url").html() != null)
    {
        if($("#marginal div.mod.contact.vcard a.ic.ext.url").html().length > 54)
        {
            var strURL = $("#marginal div.mod.contact.vcard a.ic.ext.url").html();
            var firstPart = strURL.substring(0, 54);
            var secondPart = strURL.substring(firstPart.length, strURL.length);
            strURL = firstPart + '- <br/>'+secondPart;
            $("#marginal div.mod.contact.vcard a.ic.ext.url").html(strURL)
        }
        if($("#marginal div.mod.contact.vcard a.ic.ext.url").html().length > 27)
        {
            var strURL = $("div.mod.contact.vcard a.ic.ext.url").html();
            var firstPart = strURL.substring(0, 27);
            var secondPart = strURL.substring(firstPart.length, strURL.length);
            strURL = firstPart + '- <br/>'+secondPart;
            $("#marginal div.mod.contact.vcard a.ic.ext.url").html(strURL)
        }
    }
});


/*
 * Teaser Enhancement: The teaser links should be aligned with the last row of text. Therefore,
 * if the text does not flow around the image, the links should also stay indented as far as
 * the text.
 *
 */
var enhanceTeaserFormat_alreadyCalled = false; // #MGH-147: in IE this function is invoked in an endless loop
function enhanceTeaserFormat() {
    /* #MGH-147: begin */
    if (enhanceTeaserFormat_alreadyCalled) { enhanceTeaserFormat_alreadyCalled = false; return; }
	enhanceTeaserFormat_alreadyCalled = true;
    /* #MGH-147: end */
    var isIE = $.browser.msie;

    var pFontSize = 11; // paragraph font size in px
    var rightImageMargin = 20;

    // get all teasers
    var $teaserArray = $("#main > div.landscapeteaser").add("#main > div.portraitteaser").add("#main > div.external").add("#main > div.download");

    // iterate through teasers
    $teaserArray.each(function(i){
        var $teaser = $(this);
        var image = $("img", $teaser).get(0);

        if (typeof image != "undefined") {
            var pHeight = 0;
            var paragraph = $("p", $teaser).get(0);
            if (typeof paragraph != "undefined") {
                pHeight = $(paragraph).height(); // paragraph height without padding
                var pMargin = $(paragraph).css("marginTop");
                if(isIE) {
                    pMargin = pMargin.slice(0, pMargin.indexOf("e")); // result in em
                    pHeight += Number(pMargin * pFontSize); // multiplied by font-size to get px
                } else {
                    pMargin = pMargin.slice(0, pMargin.indexOf("p")); // result in px
                    pHeight += Number(pMargin);
                }
            }
            var hHeight = 0;
            var headline = $("h2", $teaser).get(0);
            // #mgh-house pages should not take headline into account since its positioned above image
            if (typeof headline != "undefined" && typeof $('#mgh-house')[0] == "undefined") {
                hHeight = headline.offsetHeight;
                if(isIE) {
                    hHeight -= 3; // magic fudge to make it work
                }
            }
            var pLineHeight = $(paragraph).css("lineHeight");
            if(isIE) {
                pLineHeight = 16; // !hard coded! : result is 11.7pt, which is roughly 16px
            } else {
                pLineHeight = Number(pLineHeight.slice(0, pLineHeight.indexOf("p"))); // result in px
            }
            var imgHeight = image.offsetHeight;
            var imgMargin = $(image).css("marginBottom");
            if(isIE) {
                imgMargin = imgMargin.slice(0, imgMargin.indexOf("e")); // result in em
                imgHeight += Number(imgMargin * pFontSize); // multiplied by font-size to get px
            } else {
                imgMargin = imgMargin.slice(0, imgMargin.indexOf("p")); // result in px
                imgHeight += Number(imgMargin);
            }

            var marginLeft = "auto";
            var teaserHeight = "auto";

            if (Math.round(imgHeight - hHeight + pLineHeight) > pHeight) {
                //marginLeft = (image.offsetWidth + rightImageMargin) + "px";
                marginLeft = "0px"; // #MGH-135
                teaserHeight = image.offsetHeight + "px";
            }

            var list = $("ul.linkblock", $teaser).get(0);
            if (typeof list != "undefined") {
                list.style.marginLeft = marginLeft;
            }
            // if IE < 7 set teaser height, otherwise image can get cut off
            if(isIE && (typeof document.body.style.maxHeight == "undefined")) {
                $teaser.get(0).style.height = teaserHeight;
            }
        }
    });
}