﻿$(document).ready(function () {

    //$('<span>More Information</span>').appendTo('h2.title a:not(.box h2.title a,#mainFooter h2.title a)');

    // Twitter interferes with Tiny MCE
    // if not in full admin mode enable Twitter
    if ($('#mainAdminMenu').exists() == false) {
        //call Twitter script on DOM ready with jQuery
        $.getScript('http://twitter.com/statuses/user_timeline/' + username + '.json?callback=twitterCallback2&count=1');
    }

    // setup ad/news gallery
    if ($('.ad-gallery').exists()) {

        var galleries = $('.ad-gallery').adGallery();
        $('#switch-effect').change(
        function () {
            galleries[0].settings.effect = $(this).val();
            return false;
        });

        $('#toggle-slideshow').click(function () {
            galleries[0].slideshow.toggle();
            return false;
        });
    }

    // incidental animations
    $('#mainMenu a, .morelink a').hover(
        function () { $(this).animate({ left: '6px' }, 150) },
        function () { $(this).animate({ left: '0px' }, 150) }
    );

    $('.newslink').hover(
        function () { $(this).animate({ right: '0' }, 150) },
        function () { $(this).animate({ right: '10px' }, 150) }
    );
    $('.toplink').hover(
        function () { $(this).animate({ top: '-3px' }, 150) },
        function () { $(this).animate({ top: '0' }, 150) }
    );

    $('#rightCol .googleadvert').prepend('<span class="advert">Advertisement</span>');

    $('#friendForm').addClass('formClose');

    $('.sendfriendlink').click(function (e) {
        if ($('#friendForm').is(':visible')) {

            $('#friendForm').slideUp();
        } else {
            $('#friendForm').slideDown();
        }
    });
});

$

var username = 'Aftermarket01';

function twitterCallback2(twitters) {
    var statusHTML = [];
    for (var i = 0; i < twitters.length; i++) {
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function (url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function (reply) {
            return reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<p>\"' + status + '\" &ndash; <small>' + relative_time(twitters[i].created_at) + '</small></p>');
    }

    $('.loading').fadeOut(500, function () {
        $('#latest_tweet').html($(statusHTML.join('')).hide().fadeIn(750));

        /* WRITE TWEET TO RETWEET BUTTON 
        var tweetText = $("#latest_tweet").text();
        var lastQuote = tweetText.lastIndexOf("\"");
        var reTweet = tweetText.substring(0, lastQuote)
        var reTweetURL = "http://www.twitter.com/home?status=RT @" + username + " " + reTweet;
        $(".retweet").attr('href', reTweetURL);
        document.getElementById('myRetweet').href = reTweetURL;
        */
    });

    //document.getElementById('latest_tweet').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_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';
    }
}
