﻿function getId(href) {
    return href.substring(href.lastIndexOf('#'));
}

var nav = $('nav').clone();
var header = $('header').clone();
var articles = $('article');

var opacity = 0.85;

// even, odd, first, bottom
$('ul.matrix li:even').addClass('even');
$('ul.matrix li:odd').addClass('odd');
$('#promise ul.matrix li:last-child').prev().andSelf().addClass('bottom');
$('section:first-child, details:first-child').addClass('first-child');

// add nav to each article
// change active link image
articles.each(function () {
    var article = $(this);
    var id = article.attr('id');

    article.find('nav, header').remove();

    article.prepend($(innerShiv('<nav>' + nav.html() + '</nav>', false)));
    article.prepend($(innerShiv('<header>' + header.html() + '</header>', false)));

    var active = article.find("a[href$='#" + id + "'] img");
    var src = active.attr('src');

    active
                .attr('src', src.replace('\.jpg', 'Active.jpg'))
                .attr('width', '164');
});

// change no active links on hover
$('nav img').each(function () {
    var img = $(this);
    var src = img.attr('src');

    if (src.indexOf('Active') == -1) {
        img.hover(function () {
            img.attr('src', src.replace('\.jpg', 'Hover.jpg'));
        }, function () {
            img.attr('src', src);
        });
    }
});

// scroll into view
var scroll = function (href) {
    var id = getId(href);
    var article = $(id);
    var left = article.offset().left;

    $('html, body').animate({ 'scrollLeft': left });

    window.location.hash = id;
};

$('nav a').each(function () {
    var anchor = $(this);

    anchor.bind('click', function (e) {
        e.preventDefault();

        var href = anchor.attr('href');

        scroll(href);
    });
});

// blockquotes
$('#wholovesus .matrix a').each(function () {
    var anchor = $(this);
    var href = anchor.attr('href');
    var blockquote = $(getId(href));

    anchor.hover(function () {
        blockquote.show();
    }, function () {
        blockquote.hide();
    });

    anchor.click(function (e) {
        e.preventDefault();
    });
});

// promise
$('#promise .matrix li').each(function () {
    var item = $(this);
    var anchor = item.find('a');
    var href = anchor.attr('href');
    var section = $(getId(href));

    section.css('opacity', opacity);

    item.hover(function () {
        section.show();
    }, function () {
        section.hide();
    });

    anchor.click(function (e) {
        e.preventDefault();
    });
});

// people
var figure = $('#person');

$('#meettheteam .matrix li').each(function () {
    var person = $(this);
    var img = person.find('img');
    var large = img.attr('src').replace('Thumbnail', '');
    var name = person.find('span');

    person.hover(function () {
        figure.attr('src', large);
        figure.show();
        name.show();
    }, function () {
        figure.hide();
        name.hide();
    });
});

// international
$('details').each(function () {
    var details = $(this);
    var summary = details.children('summary');
    var content = summary.siblings();

    content.css('opacity', opacity);

    details.hover(function () {
        content.show();
    }, function () {
        content.hide();
    });
});

var hash = window.location.hash;

if (hash.length > 1) {
    scroll(hash);
}
