$j = jQuery.noConflict();


(function($) {
    /**
     * Get the current coordinates of the first element in the set of matched
     * elements, relative to the closest positioned ancestor element that
     * matches the selector.
     * @param {Object} selector
     */
    jQuery.fn.positionAncestor = function(selector) {
        var left = 0;
        var top = 0;
        this.each(function(index, element) {
            // check if current element has an ancestor matching a selector
            // and that ancestor is positioned
            var $ancestor = $(this).closest(selector);
            if ($ancestor.length && $ancestor.css("position") !== "static") {
                var $child = $(this);
                var childMarginEdgeLeft = $child.offset().left - parseInt($child.css("marginLeft"), 10);
                var childMarginEdgeTop = $child.offset().top - parseInt($child.css("marginTop"), 10);
                var ancestorPaddingEdgeLeft = $ancestor.offset().left + parseInt($ancestor.css("borderLeftWidth"), 10);
                var ancestorPaddingEdgeTop = $ancestor.offset().top + parseInt($ancestor.css("borderTopWidth"), 10);
                left = childMarginEdgeLeft - ancestorPaddingEdgeLeft;
                top = childMarginEdgeTop - ancestorPaddingEdgeTop;
                // we have found the ancestor and computed the position
                // stop iterating
                return false;
            }
        });
        return {
            left:    left,
            top:    top
        }
    };
}(jQuery));


$j(function() {
    /*$j('#content').bind('wheel', function(event, delta) {
        scrollContent(delta * 50, false);
        return false;
    });

    $j('.scrollUp').click(function() {
        var scrollStep = $j('#content').innerHeight() - 50;
        scrollContent(scrollStep, true);
    });

    $j('.scrollDown').click(function() {
        var scrollStep = $j('#content').innerHeight() - 50;
        scrollContent(-(scrollStep), true);
    })*/


    function scrollContent(amt, smooth) {
        var sc = $j('.scrollableContent');
        var newTop = parseInt(sc.css('top').replace('px', '')) + amt;
        newTop = newTop < 0 ? newTop : 0;
        newTop = newTop > (sc.innerHeight() * -1 + 100) ? newTop : sc.innerHeight() * -1 + 100;
        if (smooth) {
            sc.animate({top:newTop + 'px'}, 700);
        } else {
            sc.css('top', newTop + 'px');
        }
    }


    /*$j(window).bind('resize', function() {
        setContentHeight();
    });
    setContentHeight();*/


    // create mail link
    var e = ['ma', 'ilto',':','mox','iebooks','@','gmai','l.c','om'];
    $j('#em').attr('href', e.join(''));

    // set external links to open in new window or tab
    $j('a[href*=http://]').attr("target", "_blank");


    /*$j('#basicsPage #content #navLinks a').live('click', function() {
        var label = $j(this).attr('href');
        var el = $j(label);
        var sc = $j('.scrollableContent');
        var newTop = el.positionAncestor('.scrollableContent').top * -1;
        sc.animate({top: newTop}, 500);
        return false;
    });
    $j('#basicsPage #content .topLink').live('click', function() {
        var sc = $j('.scrollableContent');
        sc.animate({top: 0}, 500);
        return false;
    });*/
});

function setContentHeight() {
    var height = $j(window).height() - 370;
    if (height < 435) {
        height = 435;
    }
    $j('.scrollable').height(height);
}


