var p = {
    loaderSrc:null,
    init:function(){
        p.backToTop();
        //p.home.init();
        //p.subNav();
        p.tweet();
        p.inputs();
        
        p.loaderSrc = $('#ajax_loader').attr('src');
    },
    preload:function(){
        var images = [
            'images/global/ajax-loader.gif'
        ], i=0;
        
        for(;i<images.length;i++){(new Image()).src = Phantom.STATIC_URL+images[i];}
    },
    inputs:function(){
        $('.inits')
            .each(function(i, el){
                if($(this).val() && $(this).data('initval')){
                    // Pass
                } else if(!$(this).data('initval') && $(this).val()){
                    $(this).data('initval', $(this).val());
                }else{
                    $(this).val($(this).data('initval'));
                }
            })
            .live('focus blur', function(e){
                switch(e.type){
                    case 'focusin':
                        if(!$(this).data('initval') || $(this).val() === $(this).data('initval')){
                            $(this).val('');
                        }else if($(this).data('initval') !== $(this).val()){
                            //this selects the text if its not the init value instead of clearing the input
                            $(this).select();
                        }
                    break;
                    case 'focusout':
                        if($(this).val()===''){
                            $(this).val($(this).data('initval'));
                        }
                    break;
                }
        });
    },
//footer
    backToTop:function(){
        $('#back_to_top').click(function(e){
            e.preventDefault()
            $('html, body').animate({'scrollTop':'0px'}, 500);
        });
    },
    /*
    subNav:function(){
        if(!$('#phantom-content_nav')){return;}
        var duration = 250;
        $('#phantom-sub_nav .sub_nav_a:not(.exempt)').live('click', function(e){
            if(!$(this).parent('.sub_nav_li').hasClass('active')){
                $('#phantom-sub_nav .sub_nav_li').removeClass('active').children('.first_level').slideUp(duration);
                
                $(this).siblings('.first_level').slideDown(duration);
                $(this).parent('.sub_nav_li').addClass('active');
            }else{
                $('#phantom-sub_nav .sub_nav_li').removeClass('active').children('.first_level').slideUp(duration);
            }
        });
    },
    */
    tweet:function(){
        if($('#phantom-tweet p.tweet_quote').length == 0)
        {
            $.getJSON('/latest-tweet/', function(tweet){
                $('#phantom-tweet').append(
                    $('<p class="tweet_quote"></p>').html(tweet.text),
                    $('<p class="tweet_info"><span id="tweet_timestamp"></span> <a href="http://twitter.com/fireworks" target="_blank" rel="nofollow">@Fireworks</a></p>')
                );
                
                $('#tweet_timestamp').text(p.howLongAgo(tweet.date));
            });
        }
        else
        {
            $('#tweet_timestamp').text(p.howLongAgo($('#tweet_timestamp').data('timestamp')));
        }
        
    },
    howLongAgo: function(created_time){
        var created = new Date(created_time).getTime(),
            posted = (new Date().getTime()-created)/1000/60;

        if(posted > 1 && posted < 2)
            return 'about a minute ago';
        else if(posted > 2 && posted < 60)
            return Math.floor(posted)+' minutes ago';
        else if(posted < 1)
            return Math.floor(posted*60)+' seconds ago';
        else if(posted >= 60 && posted < 120)
            return 'about an hour ago';
        else if(posted >= 120 && posted < 1440)
            return Math.floor(posted/60)+' hours ago';
        else if(posted >= 1440 && posted < 2880)
            return 'a day ago';
        else if(posted >= 2880)
            return Math.floor(posted/60/24)+' days ago';
    }
};
$(document).ready(p.init);$(window).load(p.preload);
