
/* jQuery enhancements
----------------------------------------------------------------------------*/

jQuery.fn.replace = function() {
    var stack = [];
    return this.domManip(arguments, true, 1, function(a){
        this.parentNode.replaceChild(a, this);
        stack.push(a);
    }).pushStack(stack);
};

jQuery.fn.scrollTo = function (speed, easing, callback) {
	var opt = jQuery.speed(speed, easing, callback);
	
    var first = this.slice(0, 1);
    var target = first.offset();
    
    if (target) {
        var e = document.documentElement || document.body;
        var original = {
            top: e.scrollTop,
            left: e.scrollLeft
        };
	
    	var t0 = new Date().getTime();
    	var q = this;
    	var i = 0;
        var step = function () {
            var n = new Date().getTime() - t0;
        	var state = n / opt.duration;
        	var s = jQuery.easing[opt.easing || (jQuery.easing.swing ? "swing" : "linear")](state, n, 0, 1, opt.duration);
        	
            var left = original.left + s * (target.left - original.left);
            var top = original.top + s * (target.top - original.top);
            window.scrollTo(left, top);
            if (s > 0.999) clearInterval(q.timer);
        };
    
        var id = first.attr("id");
        if (id) location.hash = id;
        
        q.timer = setInterval(step, 13);
    }
            
    return this;
};


/* Search
----------------------------------------------------------------------------*/

$(function () {
    var placeholder = "Suchen..."
    if ($.browser.safari) {
        $(".search").html(
            '<form action="/find"><input type="search" name="keyphrase"' +
            ' id="keyphrase" placeholder="' +
            placeholder +'" autosave="ematia.de" results="10"/></form>'
        ).removeClass("search").addClass("search-safari");
    } else {
        $("#keyphrase").val(placeholder).addClass("inactive")
        .focus(function () {
            if ($(this).val() == placeholder)
                $(this).val("").removeClass("inactive");
        })
        .blur(function () {
            if ($(this).val() == "")
                $(this).addClass("inactive").val(placeholder);            
        });
    }
});


/* External links
----------------------------------------------------------------------------*/

$(function () {
    $(".content div.news a[href^='http://']").addClass("extern");
    
    $("a[href^='http']")
    .not(".intern")
    .not("a[href^='http://ematia.de']")
    .addClass("extern");
    
    $(".extern, .extern-hidden").click(function () {
        window.open(this.href);
        return false;
    });
});


/* Browser fixes
----------------------------------------------------------------------------*/

$(function () {
    if ($.browser.msie) {
        var alphaFilter = function(src) {
            return "DXImageTransform.Microsoft.AlphaImageLoader(src='" +
                   src +"', sizingMethod='scale')";
        };
        
        $(".alpha")
        .not("img")
        .each(function () {
            var src = $(this).attr("src");
            $(this).css({filter: alphaFilter(src), background: "none"});
        })
        .end()
        .filter("img")
        .each(function () {
            var src = $(this).css("background-image");
            $(this).css({filter: alphaFilter(src), background: "none"});
        })
        .end();
    }
});

/* Suppress outlines
----------------------------------------------------------------------------*/

$(function () {
    $("a").css("outline", "none");
});

