(function($) {
    $.fn.slideDownAndFadeIn = function(settings) {
        var config = { 'speed': 300 };
        if (settings) $.extend(config, settings);
        this.each(function() {
            $(this).css({ opacity: 0.0 }).slideDown(config.speed, function() {
                $(this).animate({ opacity: 1.0 }, config.speed);
            });
        });
        return this;
    };
})(jQuery);

var ie6 = /msie 6/i.test(navigator.userAgent);
var ie7 = /msie 7/i.test(navigator.userAgent);

$(document).ready(function() {

    var speed = 300;

    // Give classes to input elements based on their type attribute
    $('input').each(function() {
       var type = $(this).attr('type');
       $(this).addClass(type);
    });

    if ($('h1 cufontext:first').text().charAt(0) == 'A') {
        $('h1').css('marginLeft', '0px');
    }

    // Replace <hr> with <div class="hr"> since IE6-7 adds an indestructible border around <hr> tags with a background-image.
    if (ie6 || ie7) {
        $('hr').each(function() {
            var c = $(this).attr('class');
            c = (c.length) ? c + ' hr' : 'hr';
            $(this).replaceWith('<div class="' + c + '"></div>');
        });
    }

    $('a.read_more').click(function() {

        $(this).fadeOut(speed, function() {
            $(this).hide();
            $('div.read_more:first').fadeIn(speed).removeClass('read_more');
        });

        return false;
    });

    initUnderlinedLinks('a.underline');

    var navHeight = $('#nav').height() + 245;
    // Handle edge browser height situations
    var absolutelyFixedPositioning = function() {
        if (ie6) return true;
        if ($(window).height() < navHeight) {
            if ($('.fixed').css('position') != 'absolute') {
                $('.fixed').css({ position: 'absolute' });
                $('#footer').appendTo('#nav').css({ position: 'static', marginTop: '30px' });
            }
        } else {
            if ($('.fixed').css('position') != 'fixed') {
                $('.fixed').css({ position: 'fixed' });
                $('#footer').appendTo('body').css({ position: 'fixed', marginTop: '0px' });
            }
        }
    };
    absolutelyFixedPositioning();
    $(window).resize(absolutelyFixedPositioning);

    // Simple Slideshow
    $('.slideshow').simpleSlideshow({ cycle: 5, fade: 0.5 });

});

Cufon.replace('h1, h2, h3, h4, .cufon, div.asset');

Cufon.replace('#nav a, a.underline', {
    hover: true
});
  
function initUnderlinedLinks(selector, context) {
    context = context || 'body';
    $(selector, context).each(function() {
        $(this).css({
            backgroundPosition: Math.round(Math.random() * 9999) + 'px -21px'
        });
    }).hover(
        function() {
            try {
                var newPos = $(this).css('backgroundPosition').split(' ')[0] + ' -71px';
                $(this).css('backgroundPosition', newPos);
            } catch (e) { }
        },
        function() {
            try {
                var newPos = $(this).css('backgroundPosition').split(' ')[0] + ' -21px';
                $(this).css('backgroundPosition', newPos);
            } catch (e) { }
        }
    );
}


