try{Typekit.load();}catch(e){}

jQuery.extend(jQuery.easing, {
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

$(document).ready(function() {

    var stareIE = ($.browser.msie && parseInt($.browser.version, 10) < 9);

    // emulate placeholder if browser doesn't support it
    var placeholderSupported = 'placeholder' in document.createElement('input') && 'placeholder' in document.createElement('textarea');
    if (!placeholderSupported) {
        $('[placeholder]').each(function() {
            var $this = $(this);
            var value = $this.attr('placeholder');
            $this.val(value).focus(function() {
                if ($this.val() == value) {
                    $this.val('');
                }
            }).blur(function() {
                if ($this.val() == '') {
                    $this.val(value);
                }
            }).blur();
            $this.parents('form').addClass('hasplaceholders');
        });
        // do not submit placeholder values
        $('form.hasplaceholders').submit(function() {
            $(this).find('[placeholder]').each(function() {
                var $this = $(this);
                if ($this.val() == $this.attr('placeholder')) {
                    $this.val('');
                }
            })
        });
    }

    // homepage slideshow
    if ($('#slideshow > img').length > 1) {
        var slides = [], current = 0, slide, slideFnc;
        var delay = 4000, speed = 1000;
        $('#slideshow > img').each(function() {
            slides.push($(this).attr('src'));
            if (slides.length > 1) {
                $(this).remove();
            }
        });
        setTimeout((slideFnc = function() {
            if (++current >= slides.length) {
                current = 0;
            }
            slide = slides[current];
            $('<img />').load(function() {
                $(this).hide().insertAfter('#slideshow > img:first').fadeIn(speed, function() {
                    $('#slideshow > img:first').remove();
                    setTimeout(slideFnc, delay);
                });
            }).attr('src', slide);
        }), delay);
    }

    // industries slideshow
    if ($('#slides > div').length > 1) {
        // setup:
        var consts = {
            slideWidth: 584,
            navWidth: 510,
            navItemWidth: 24,
            speed: 500,
            delay: 6000
        };
        var $slides = $('#slides > div');
        var $slider = $('#slides');
        var html = '<div id="nav"><ul><li><a href="">Prev</a><li><a href="">Next</a></ul><ul>';
        for (var i = 0; i < $slides.length; ++i) {
            var $slide = $slider.find('div:nth-child(' + (i + 1) + ')');
            html += '<li><a id="slider-slide-' + i + '" href="" data-left="-' + (i * consts.slideWidth) + '" data-title="' + $slide.find('strong').text() + '" data-thumb="' + $slide.data('thumb') + '">' + (i + 1) + '</a>';
        }
        html += '</ul></div>';
        $slider.width(consts.slideWidth * $slides.length).append(html);
        $('#slider ul + ul li:first a').css('margin-left', (consts.navWidth - $slides.length * consts.navItemWidth) / 2);

        // engine:
        var current = 0, currentLp = 0, max = -consts.slideWidth * ($slides.length - 1);
        $('#slider ul + ul li a').click(function(e) {
            e.preventDefault();
            $slider.find('a.current').removeClass('current');
            currentLp = parseInt($(this).attr('id').replace(/slider-slide-/, ''), 10);
            current = $(this).addClass('current').data('left');
            $slider.stop().animate({'margin-left': current}, consts.speed, 'easeOutExpo');
        });
        $('#slider ul:first-child a:first').click(function(e) {
            e.preventDefault();
            if (current == 0) {
                return;
            }
            $('a#slider-slide-' + (currentLp - 1)).trigger('click');
        });
        $('#slider ul:first-child a:last').click(function(e) {
            e.preventDefault();
            if (current == max) {
                return;
            }
            $('a#slider-slide-' + (currentLp + 1)).trigger('click');
        });

        // tooltip:
        $('body').append('<div id="tooltip"><div></div><div><div>&nbsp;</div></div></div>');
        var $tooltip = $('#tooltip');
        var offset = $('#nav ul + ul a:first').offset();
        var firstRun = true;
        $tooltip.css({
            top: offset.top - $tooltip.outerHeight(),
            left: (offset.left > 0) ? offset.left - 49 : $('#nav ul + ul a:first').offset().left - 49,
            opacity: 0,
            zIndex: 9999
        }).hide();
        if (stareIE) {
            $tooltip.css({'opacity': 1, 'filter': ''});
        }
        $('#nav ul + ul a').hover(function() {
            var $this = $(this);
            $tooltip.find('div:first').html($this.data('title'));
            $tooltip.find('div + div > div').css('background-image', 'url(' + $this.data('thumb') + ')');
            var newLeft = $this.offset().left - 49;
            $tooltip.css('top', $('#nav ul + ul a:first').offset().top - $tooltip.outerHeight()).show();
            if (firstRun) {
                $tooltip.stop().css({left: newLeft});
                firstRun = false;
            }
            if (stareIE) {
                $tooltip.stop().animate({left: newLeft}, 200);
            }
            else {
                $tooltip.stop().animate({opacity: 1, left: newLeft}, 200);
            }
        }, function() {
            if (stareIE) {
                $tooltip.hide();
                firstRun = true;
            }
            else {
                $tooltip.stop().animate({opacity: 0}, 200, function() {
                    $(this).hide();
                    firstRun = true;
                });
            }
        });

        $('#slider ul + ul li:first a').addClass('current');
        var industriesSlider = function() {
            $((current == max) ? '#slider ul + ul li:first a' : '#slider ul:first-child a:last').click();
        };
        var slideInterval = setInterval(industriesSlider, consts.delay);

        $('#slider').hover(function() {
            clearInterval(slideInterval);
        }, function() {
            slideInterval = setInterval(industriesSlider, consts.delay);
        });
    }

    $('.text ul li:last-child, .contact ul li:last-child, .postfooter ul li:last-child').addClass('last');
    $('.form-submit input#submit').val('Submit').addClass('button buttonsingle');
    $('#commentform p label + span').each(function() {
        $(this).siblings('label').prepend($(this).clone());
        $(this).remove();
    });

    if ($(window).width() < 955) {
        $('body').addClass('narrow');
    }

    // mozilla button fix
    if ($.browser.mozilla) {
        $('button.button p').css('margin', 0);
    }

    // IE7-8 fixes
    if (stareIE) {
        $('hr').each(function() {
            $(this).replaceWith('<div class="hr ' + $(this).attr('class') + '" />');
        });

        if (parseInt($.browser.version, 10) < 8) {
            $('#feature .text li:first').append('|');
            $('#feature p + ul li, .contact.blog ul li').not(':first-child').not(':last-child').append('|');
            $('#copy ul li').not(':first-child').prepend('| ');
            $('#text blockquote.float p:first-child').prepend('”').append('”');
        }
    }
});

$(window).resize(function() {
    if ($(window).width() < 955) {
        $('body').addClass('narrow');
    }
    else {
        $('body').removeClass('narrow');
    }
});
