/// <reference path="jquery-1.4.2.min.js" />

var $j = jQuery.noConflict();

var color;

Number.prototype.toHex = function () { var o = this.toString(16); return o[1] ? o : "0" + o; }
function CreateHexString(source) {
    return source.replace(/rgb\((\d+)\,\s(\d+)\,\s(\d+)\)/g, function (a, b, c, d) { return "#" + parseInt(b).toHex() + parseInt(c).toHex() + parseInt(d).toHex(); })
}


function SwitchToHighlightColor(linkItem) {
    linkItem.css("color", "#5D5D5D");
}

function SwitchToDefaultColor(linkItem) {
    if (linkItem.attr("initialColor"))
        linkItem.css("color", linkItem.attr("initialColor"));
}

var hideNewsContainer = function (news) {
    $j(news.find(".teaser")).css("font-weight", "normal");

    if ($j('.toggleTeaser', news).length) {
        $j('.teaser', news).hide();
        $j('.toggleTeaser', news).show();
    }

    $j(news.find(".text")).hide();
    $j(news.find(".showNewsContent")).show();
}

var setBookmark = function () {

    var title = document.title;
    var url = location.href;

    // FF
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    }
    // OPERA
    else if (window.opera && window.print) {
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    // IE
    else if (document.all) {
        window.external.AddFavorite(url, title);
    }

}

var showModal = function () {
    $j('#notFoundModal').show();
    var top = (document.documentElement.clientHeight / 2) - $j('#modalContentWrapper').height();
    $j('#modalContentWrapper').css('top', top);
}

$j(document).ready(function () {


    $j(".showNewsContent, img.newsImg, .pressNewsTitle").click(function () {

        var news = $j(this).parents(".newsContent");
        if (news.length == 0) {
            news = $j(this).parent().find('.newsContent');
        }

        var content = $j('.showNewsContent', news);
        content.hide();


        if ($j('.toggleTeaser', news).length) {
            $j('.teaser', news).show();
            $j('.toggleTeaser', news).hide();
        }

        $j(news.find(".teaser")).css("font-weight", "bold");
        $j(news.find(".text")).show();

        $j(".newsContent").not(news).each(function () {
            hideNewsContainer(content);
        });

    });

    $j(".hideNewsContent").click(function () {

        var news = $j(this).parents(".newsContent");
        hideNewsContainer(news);
    });

    $j("#siteContent img.LightBox").each(function () {
        var lightboxContainer = $j('<a href="#" class="lightbox" />');

        var src = $j(this).attr("src");
        lightboxContainer.attr("href", src.substring(0, src.indexOf('?')) + "?mw=1000&mh=800")

        $j(this).wrap(lightboxContainer);
    });

    $j("#siteContent a.lightbox, a.projectListImage").lightBox();

    if ($j("ul.navigationSubMenu").length) {
        $j("ul.navigationSubMenu").next("li").css("border-top", "1px solid #FFFFFF");
    }

    if ($j("div#additionalText").length) {
        var navigationUL = $j("#navigation ul.navigation");
        var addTextDIV = $j("#additionalText");
        var cellSurroundingDIV = $j(".NavigationCarrier");

        var navigationULHeight = navigationUL.height()
        var addTextDIVHeight = addTextDIV.height();
        var cellSurroundingDIVMinHeight = parseInt(cellSurroundingDIV.css("min-height"));
        var heightSum = navigationULHeight + addTextDIVHeight + 40;

        if (heightSum > cellSurroundingDIVMinHeight) {
            cellSurroundingDIV.css("min-height", heightSum);
            cellSurroundingDIVMinHeight = heightSum;
        }

        $j("#additionalText").css({
            "top": cellSurroundingDIVMinHeight - addTextDIVHeight - 10,
            "display": "block"
        });

    }

    $j("ul.serviceNavigation").each(function () {

        $j("li:last", $j(this)).css("border-right", "0");

    });

    $j("div.tile div.tileInformation ul.navigation").each(function () {

        $j("li:last", $j(this)).css("border-bottom", "0");

    });

    $j("#navigation ul.navigation li:last > a").css("border-bottom", "0");

    $j("div.tile:not(.inactive)").each(function () {

        color = $j(this).attr("color");
        $j("h3", $j(this)).css("color", color);
        $j(".tileColor", $j(this)).css("background-color", color);

        $j("#navigation > ul.navigation").css("background-color", color);

    });

    $j("div.tile.inactive").each(function () {

        $j("h3", $j(this)).css("color", "#B6B8B8");
        $j(".tileColor", $j(this)).css("background-color", "#B6B8B8");

    });

    $j("div.tile").hover(function () {

        $j("div.alphaBackground", $j(this)).css("background-color", $j(this).attr("color")).css("opacity", "0.5").fadeIn("fast");
        $j("div.tileInformation", $j(this)).fadeIn("fast");

    }, function () {

        $j("div.alphaBackground", $j(this)).fadeOut("fast");
        $j("div.tileInformation", $j(this)).fadeOut("fast");

    });

    $j("div.tile.inactive").hover(function () {

        var tileColor = $j(this).attr("color");
        $j("h3", $j(this)).css("color", tileColor);
        $j(".tileColor", $j(this)).css("background-color", tileColor);

    }, function () {

        $j("h3", $j(this)).css("color", "#B6B8B8");
        $j(".tileColor", $j(this)).css("background-color", "#B6B8B8");

    });

    $j("a.sliderControl").click(function () {

        $j("a.sliderControl").css("color", "#000000");

        $j("div.sliderItem").hide();
        $j($j("div.sliderItem").get($j(this).text() - 1)).show();

        $j(this).css("color", color);

    });

    $j("a:not(.downloadButton, .button, .navigation a, #subsidiaryBox a, .keepColor)").css("color", color);
    $j("div.breadcrumb span").css("color", color);

    $j(".RealtySearch select, .RealtySearch input[type=submit]").css("background-color", color);

    $j("#formContainer select, #formContainer input[type=submit]").css("background-color", color);


    $j("#siteContent h2, .RealtySearch h2").css("color", color);
    $j('#modalContentHead').css("background-color", color);

    $j("a.downloadButton, a.button").hover(function () {
        $j(this).css("background-color", "#5D5D5D");
    }, function () {

        $j("div.tile:not(.inactive)").each(function () {
            color = $j(this).attr("color");
        });

        if (color === undefined) {
            color = "#B3B3B3";
        }

        $j(this).css("background-color", color);
    });

    $j("a.downloadButton, a.button").css("background-color", color);

    $j("a.sliderControl:last").css("border-right", "none");

    $j($j('.jobDescription:last').find('hr')).hide();

    $j('.jobDescription a.unitDownloadButton').mouseenter(function () {

        $j(this).css('background-color', '#5D5D5D');

    }).mouseleave(function () {

        $j(this).css('background-color', '#000000');

    });

    $j('table#searchResults tr:last').css('border-bottom', 'none');

    $j('input[type="submit"]').mouseenter(function () {

        var currentColor = $j(this).css('background-color');
        $j(this).attr('color', currentColor);
        $j(this).css('background-color', '#5D5D5D');

    }).mouseleave(function () {

        var originColor = $j(this).attr('color');
        $j(this).css('background-color', originColor);

    });


    var highlightSelectors = ["ul.serviceNavigation li a", "div#additionalText a", "#siteContent a", "div#footer a", "#siteContentWide a", ".showNewsContent", "#SitemapLink a"]

    for (var i = 0; i < highlightSelectors.length; i++) {
        var currentSelector = highlightSelectors[i];
        var currentItem = $j(currentSelector);

        if (currentItem.hasClass('keepColor'))
            continue;

        var color = currentItem.css("color");
        currentItem.attr("initialColor", color);

        currentItem.mouseenter(function () {
            SwitchToHighlightColor($j(this));
        }).mouseleave(function () {
            SwitchToDefaultColor($j(this));
        });
    }
});

function SelectOption(controlId, valueToSelect) {
    var control = $j("#" + controlId);

    if (control.length > 0) {
        control.val(valueToSelect);
    }
}

function AddMapMarkers(imageContainerId) {
    if (imageContainerId) {
        var imageContainer = $j("#" + imageContainerId);

        if (imageContainer && (imageContainer.length > 0)) {
            $j(".MapMarkerLink").each(function () {
                var markerTop = $j(this).attr("Top");
                var markerLeft = $j(this).attr("Left");
                var markerCity = $j(this).attr("RelatedCity");
                var nameOverride = $j(this).attr("NameOverride");
                var displayedLabelText = markerCity;

                if (nameOverride)
                    displayedLabelText = nameOverride;

                if (markerTop && markerLeft) {
                    var mapMarker = $j('<div class="MapLocationMarker" RelatedCity=' + markerCity.toLowerCase() + '><div class="Indicator"></div><div class="Label">' + displayedLabelText + '</div><div stlye="clear:both;"></div></div>');
                    mapMarker.css({ top: parseInt(markerTop), left: parseInt(markerLeft) });
                    imageContainer.append(mapMarker);
                }
            });
        }
    }
}

function SelectOnMap(cityName) {
    if (cityName) {
        cityName = cityName.toLowerCase();

        $j(".MapLocationMarker").removeClass("MapLocationMarkerInverted");
        $j(".MapLocationMarker[RelatedCity=" + cityName + "]").addClass("MapLocationMarkerInverted");
    }
};


$j(document).ready(function () {
    // mdash for all <li>-items in #siteContent
    var listItems = $j("li", $j("#siteContent"));

    listItems.each(function () {
        var current = $j(this);
        var text = current.html();

        current.html("<div><div class='mdash'>&#0150;</div><div class='liText'>" + text + "</div><div style='clear:both;'></div></div>");
    });
});

