$(function () {

    if (window.location.hash && (window.location.hash.indexOf('_') > 0)) {

        var hash = window.location.hash.replace('_', '');
        var menuSel = hash.replace('#', '');

        $('#mainnav li').removeClass('active');
        $('.' + menuSel).parent().addClass('active');

        var topVal = $(hash).position().top;
        $.scrollTo({ top: topVal - 118, left: '0' }, 1300, { easing: 'easeOutQuad' });
    }

    $('.chooseproject').click(function () {
        if ($(this).parent().hasClass('active')) {
            dropUp();
        }
        else {
            dropDown();
        }
    });

    var config = {over: dropDown, timeout: 500, out: dropUp};

    $('.shortcuts').hoverIntent(config);

    $('a.hi').click(function () {
        $('#mainnav li').removeClass('active');
        $('li a.hi').parent().addClass('active');
        $.scrollTo({ top: '0', left: '0' }, 1300, { easing: 'easeOutQuad' });
        return false;
    });

    $('a.work').click(function () {
        $('#mainnav li').removeClass('active');
        $(this).parent().addClass('active');
        $.scrollTo({ top: '1366px', left: '0' }, 1300, { easing: 'easeOutQuad' });
        return false;
    });

    $('a.contact').click(function () {
        $('#mainnav li').removeClass('active');
        $(this).parent().addClass('active');
        $.scrollTo({ top: '2917px', left: '0' }, 1300, { easing: 'easeOutQuad' });
        return false;
    });
    
    if($('.frontpage').length){
        $('#hi article.graphics').parallax({ "coeff": -0.20 });
        $('#hi article.graphics .hearts').parallax({ "coeff": 0.25 });
        $('#hi article.graphics .triangel01').parallax({ "coeff": 0.25 });
        $('#hi article.graphics .triangel02').parallax({ "coeff": 0.30 });
        $('#work article .bird_right').parallax({ "coeff": 0.35 });
        $('#work article.graphics').parallax({ "coeff": -0.06 });
        $('#contact article.graphics').parallax({ "coeff": -0.08 });
        $('#contact article.graphics .hearts').parallax({ "coeff": 0.11 });
    };
});

function dropUp() {
    $('.shortcuts').slideUp('fast', function () { $('.projects').removeClass('active'); });
};

function dropDown() {
    $('.projects').addClass('active');
    var dropHeight = $(window).height() - 120;
    $('.shortcuts').css('max-height', dropHeight).slideDown('fast');
};

(function($){
    $.fn.parallax = function(options){
        var $$ = $(this);
        offset = $$.offset();
        var defaults = {
            "start": 0,
            "stop": offset.top + $$.height(),
            "coeff": 0.95
        };
        var opts = $.extend(defaults, options);
        return this.each(function(){
            $(window).bind('scroll', function() {
                windowTop = $(window).scrollTop();
                if((windowTop >= opts.start) && (windowTop <= opts.stop)) {
                    newCoord = windowTop * opts.coeff;
                    $$.css({
                        "background-position": "center "+ newCoord + "px"
                    });
                }
            });
        });
    };
})(jQuery);

