if (parent.location.href != location.href) {
    parent.location.href = location.href;
}

$(document).bind('contextmenu', function(e) {
    return false;
});

$(function() {
    $('header nav ul li a').twipsy({placement: 'above', offset: 2});
    $('header h1').popover({placement: 'right', offset: -10, html: true,
        title: function() { return 'About the site'; },
        content: function() { return 'Written in HTML5 and using CSS3. Includes <a href="http://nicepaul.com/2010/07/19/icons/">GEL Social Media Icons</a> by Paul Annett. Powered by <a href="http://jquery.com">jQuery</a>, the brilliant <a href="http://mrdoob.github.com/three.js/">Three.js</a>, and Twpisy & Popovers from <a href="http://twitter.github.com/bootstrap/javascript.html">Bootstrap</a>. Best viewed in a modern browser... or else.'; }
    });
    
    init();
    if (navigator.userAgent.match(/Android/i) ||
        navigator.userAgent.match(/webOS/i) ||
        navigator.userAgent.match(/iPhone/i) ||
        navigator.userAgent.match(/iPod/i) ||
        navigator.userAgent.match(/BlackBerry/i) ||
        navigator.userAgent.match(/Windows Phone OS/i))
    {
        render();
    } else {
        animate();
    }
});

/**
 * Provides requestAnimationFrame in a cross browser way.
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 */
if ( !window.requestAnimationFrame ) {
    window.requestAnimationFrame = ( function() {
        return window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
            window.setTimeout( callback, 1000 / 60 );
        };
    } )();
}

/**
 * The following is based on the Three.js canvas particles random example.
 * http://mrdoob.github.com/three.js/examples/canvas_particles_random.html
 */

var container;
var camera, scene, renderer, group, particle;
var mouseX = 0, mouseY = 0;

function init() {
    container = document.getElementById( 'bg' );

    camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
    camera.position.z = 1000;

    scene = new THREE.Scene();

    var PI2 = Math.PI * 2;
    var program = function ( context ) {
        context.beginPath();
        context.arc( 0, 0, 1, 0, PI2, true );
        context.closePath();
        context.fill();
    }

    group = new THREE.Object3D();
    scene.add( group );

    for ( var i = 0; i < 500; i++ ) {
        particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
        particle.position.x = Math.random() * 2000 - 1000;
        particle.position.y = Math.random() * 2000 - 1000;
        particle.position.z = Math.random() * 2000 - 1000;
        particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
        group.add( particle );
    }

    renderer = new THREE.CanvasRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    container.appendChild( renderer.domElement );
}

function animate() {
    requestAnimationFrame( animate );
    render();
}

function render() {
    camera.position.x += ( mouseX - camera.position.x ) * 0.05;
    camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
    camera.lookAt( scene.position );

    group.rotation.x += 0.004;
    group.rotation.y += 0.008;

    renderer.render( scene, camera );
}

$(window).resize(function() {
    renderer.setSize( window.innerWidth, window.innerHeight );
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
});

