var FADE_IN_MILI = 500,
    SUN_ANIMATE_SPEED = 500;

function getTransformProperty(element) {
    var properties = ['transform', 'WebkitTransform', 'MozTransform'];
    var p;
    while (p = properties.shift()) {
        if (typeof element.style[p] != 'undefined') {
            return p;
        }
    }
    return false;
}

function anitmate_sun(){
  var div = document.getElementById('sun_rays');
  if (div !== null) {
    var property = getTransformProperty(div);
    if (property) {
        var d = 0;
        setInterval(
            function () {
                div.style[property] = 'rotate(' + (d++ % 360) + 'deg)';
            },
            100
        );
    }
  }
}

$(document).ready(function(){
    // presentation sequence. Front-page only. 
    
    anitmate_sun();
        
    var body = $('body');
    if ((body.hasClass('home') && !body.hasClass('paged')) &&
        !navigator.userAgent.match(/like Mac OS X/i)) { // don't animate when using iphone/ipod
    
        $('#page, #pretty_header, #sun, #px_page, #colophon, #access, #sun_rays').hide();

        $('#sun').css('top','55px');

        setTimeout(function() {
            $('#page,#pretty_header').fadeIn(FADE_IN_MILI,function() {
                $('#sun').show().animate({top:10}, SUN_ANIMATE_SPEED, function() {
                  $('#px_page').fadeIn(FADE_IN_MILI);
                  $('#access').fadeIn(FADE_IN_MILI);
                  $('#colophon').fadeIn(FADE_IN_MILI);
                  $('#sun_rays').fadeIn(FADE_IN_MILI);
                });
            });
        }, 1000);
    }
    
    
    $('.hoverable').hover(function() {
       $(this).find('.hover').fadeIn(500);
    },function() {
        $(this).find('.hover').fadeOut(500);
    });
    
    $('.share_links li').hide();
    
    $('.download').hover(function() {
        $(this).find('.share_links li').fadeIn(500);
    },function() {
        $(this).find('.share_links li').fadeOut(500);
    });
    
    var show_overlay = function($e) {
        $e.height($e.parent().find('img').height());
        $e.fadeIn(100);
    };
    
    var hide_overlay = function($e) {
        $e.fadeOut(100);
    };
    
    $('a.image').hover(function() {
        show_overlay($(this).find('.overlay'));
    },function() {
        hide_overlay($(this).find('.overlay'));
    });
    
});
