function fixLinks() {
    var notValid = new Array("http://www.relidea.com/tegometall/");
    $("a").each(function() {
        var href = $(this).attr("href");
        if (href != null) {
            for (var i = 0; i < notValid.length; i++) {
                if (href.substring(0, notValid[i].length) == notValid[i]) {
                    $(this).attr("href", href.substring(notValid[i].length - 1));
                    break;
                }
            }
        }
    });
    $("img").each(function() {
        var href = $(this).attr("src");
        if (href != null) {
            for (var i = 0; i < notValid.length; i++) {
                if (href.substring(0, notValid[i].length) == notValid[i]) {
                    $(this).attr("src", href.substring(notValid[i].length - 1));
                    break;
                }
            }
        }
    });
}

$(document).ready(function() {
    fixLinks();
    $('ul.sf-menu').superfish();
    $(".programs-holder").filmstrip({opacity:0.3});
    $(".more").each(function() {
        if ($(this).parents("p").size() == 0) {
            $(this).prev("p").append($(this));
        }
    });
    $(".gallery, .gallery-reference").filmstrip();
    $(".gallery a").gallery();
    $("input[name='refeType']").change(function() {
        $(".reference").hide();
        $(".reference-" + $(this).val()).show();
    });

    $(".gallery-reference a.visible").click(function() {
        $(".gallery-reference-image").html('<img width="674" src="' + $(this).attr("href") + '" alt="">');
        return false;
    });
    $(".gallery-reference-image").html('<img width="674" src="' + $(".gallery-reference a").eq(0).attr("href")
        + '" alt="">').click(function() {
            var img = $(this).find("img");
            $(".gallery-reference a.visible").each(function(i) {

                if ($(this).attr("href") == img.attr("src")) {
                    $(".gallery-reference a.hidden").eq(i).click();
                }
            });

            return false;
        });

    $(".gallery-reference .hidden").gallery();
    $(".collapse-text").hide().eq(0).show();
    $(".collapse h2").click(function() {
        if (!$(this).next().is(":visible")) {
            $(".collapse-text").slideUp();
            $(this).next().slideDown();
        }

    });
//    $(".tech-image-gallery").filmstrip();
    $(".tech-image-gallery a").gallery();
    $(".tech-image-gallery, .scroll-up, .scroll-down").hide();
    $(".tech-image-link a").click(function() {
        $(".tech-image-link").removeClass("selected");
        $(this).parent().addClass("selected");
        $(".scroll-up, .scroll-down").show();
        var index = $(this).parent().index();
        $(".tech-image-gallery").hide().eq(index).show();
        return false;
    });
    $(".scroll-up").click(function() {
        var a = $(".tech-image-gallery:visible").scrollTop();
        $(".tech-image-gallery:visible").stop().animate({scrollTop:(a - 250) + "px"});
    });
    $(".scroll-down").click(function() {
        var a = $(".tech-image-gallery:visible").scrollTop();
        $(".tech-image-gallery:visible").stop().animate({scrollTop:(a + 250) + "px"});
    });

});
