﻿var StormStorage = {
    setObject: function () { },
    getObject: function () { }
};
if (typeof (localStorage) !== 'undefined') {
    StormStorage.setObject = function (key, value) {
        localStorage.setItem(key, $.toJSON(value));
    }

    StormStorage.getObject = function (key) {
        var value = localStorage.getItem(key);
        if (value === null) {
            return null;
        }
        return $.evalJSON(value);
    }
}

function tenMinutesAgo() {
    return new Date().getTime() - (10 * 60 * 1000);
}

// Define Storm namespace
var Storm = {
    getLatestTweet: function () {
        if (typeof(localStorage) !== 'undefined') {
            var tweet = StormStorage.getObject("stormid:twitter:latest");
            if (!tweet || tweet.updated < tenMinutesAgo()) {
                $.getJSON('http://twitter.com/statuses/user_timeline/stormid.json?count=1&callback=?', function (data) {
                    var tweet = data[0];
                    $('#twitter').append('<p>' + posttext(tweet.text) + '</p>');
                    var toStore = { text: tweet.text, updated: new Date().getTime() };
                    StormStorage.setObject("stormid:twitter:latest", toStore);
                });
            } else {
                $('#twitter').append('<p>' + posttext(tweet.text) + '</p>');
            }
        } else {
            $.getJSON('http://twitter.com/statuses/user_timeline/stormid.json?count=1&callback=?', function (data) {
                var tweet = data[0];
                $('#twitter').append('<p>' + posttext(tweet.text) + '</p>');
            });
        }
    },
    twitterAnywhere: function () {
        twttr.anywhere(function (twitter) {
            twitter("a.twitter-follow").hovercards({
                infer: true
            });
        });
    },
    fancyThumbnails: function () {
        $('.thumb').each(function () {
            var text = $('span', this);
            var height = text.outerHeight();
            text.css({ bottom: '-' + height + 'px' });
            $('a', this).hover(function () {
                text.stop().animate({ bottom: '0px' }, { queue: false, duration: 200 });
            }, function () {
                text.stop().animate({ bottom: '-' + height + 'px' }, { queue: false, duration: 600 });
            });
        });
    },
    searchInit: function () {
        $('.srch input[type="text"]').focus(function () {
            if ($(this).val() === "Search") {
                $(this).val("");
            }
        });
    },
    pageInit: function () {
        this.getLatestTweet();
        this.twitterAnywhere();
        this.fancyThumbnails();
        this.searchInit();
    }
};

$(document).ready(function () {
    Storm.pageInit();
});

/**
Copyright (c) 2008, Michele Costantino Soccio. All rights reserved.
Copyright (c) 2008, Alan Hogan. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
Modified by Rory Fitzpatrick, May 2009
*/

function posttext(text) {
    var wwwregular = /(^|\s)(www(?:\.\w+){2,})/ig;
    var regular = /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g;
    var atregular = /\B@([_a-z0-9]+)/ig;
    text = text.replace(wwwregular, 'http://$&');
    text = text.replace(regular, '<a href="$1">$1</a>');
    text = text.replace(atregular, '@<a href="http://twitter.com/$1">$1</a>');
    return text;
}

function parse_tweet_date(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    return Date.parse(time_value);
}

function relative_time(date) {
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}