﻿$(document).ready(function () {
    // initialize scrollable together with the navigator plugin
    var onLastSlide = false;
    $(".scrollable").scrollable({
        onSeek: function (event) {
            if (this.getIndex() == this.getSize() - 1) {
                onLastSlide = true;
            }
        },
        onBeforeSeek: function (event) {
            if (onLastSlide) {
                onLastSlide = false;
                this.begin();
            }
        }
    }).navigator().autoscroll({ interval: 15000 });

    // setup ul.tabs to work as tabs for each div directly under div.panes
    $("ul.homepageTabs").tabs("div.panes > div");
    // Tab logic for the planAnalysis page
    $("ul.analysisTabs").tabs("div.panes > div");
    $("ul.howItWorksTabs").tabs("div.howItWorksPanes > div");

    // tabs logic for mypeeps
    $("#myPeepsMenu ul").tabs("#pages > div");

    var path = window.location.pathname;
    var page = path.substring(path.lastIndexOf('/') + 1);
    if (page == "about.aspx") {
        $("#accordion").accordion({
            active: false, // Do not display any items to start
            header: "strong", // Initialize the accordion with the header option specified.
            alwaysOpen: false, // allow the user to close all items
            autoHeight: false // turn off the auto height so that there is no space between the entries
        });
    } else if (page == "Default.aspx") {
        $.preloadImages('Images/Homepage/FightBack.png', 'Images/Homepage/Oculus.png', 'Images/Homepage/Advisory.png',
                        'Images/Homepage/BI.png', 'Images/Homepage/MyPeeps.png', 'Images/Homepage/WebServices.png');
    }
});


$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=Fight_Back&count=1&callback=?", function (data) {
    $.each(data, function (i, item) {
        ct = item.text;
        ct = ct.replace(/http:\/\/\S+/g, '<a href="$&" target="_blank" class="tweetLink">$&</a>');
        ct = ct.replace(/\s(@)(\w+)/g, ' @<a href="http://twitter.com/$2" target="_blank" class="tweetLink">$2</a>');
        ct = ct.replace(/\s(#)(\w+)/g, ' #<a href="http://search.twitter.com/search?q=%23$2" target="_blank" class="tweetLink">$2</a>');
        ct = ct;
        $("#tweets").append('<div class="tweetContent">' + ct + "</div>");
        $("#tweets").append('<div class="time">' + H(this.created_at) /*relativeTime(time)*/ + '</div>');
    });
});

var K = function () {
    var a = navigator.userAgent;
    return { ie: a.match(/MSIE\s([^;]*)/) }
} ();

var H = function (a) {
    var b = new Date();
    var c = new Date(a);
    if (K.ie) { c = Date.parse(a.replace(/( \+)/, ' UTC$1')) }
    var d = b - c;
    var e = 1000,
        minute = e * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    if (isNaN(d) || d < 0) { return ""; }
    if (d < e * 7) { return "right now"; }
    if (d < minute) { return Math.floor(d / e) + " seconds ago"; }
    if (d < minute * 2) { return "about 1 minute ago"; }
    if (d < hour) { return Math.floor(d / minute) + " minutes ago"; }
    if (d < hour * 2) { return "about 1 hour ago"; }
    if (d < day) { return Math.floor(d / hour) + " hours ago"; }
    if (d > day && d < day * 2) { return "yesterday"; }
    if (d < day * 365) {
        return Math.floor(d / day) + " days ago";
    } else {
        return "over a year ago";
    }
};

var setNewType;
if (!setNewType) {
    setNewType = window.onload = function () {
        var a = document.getElementsByTagName('input');
        for (var i = 0; i < a.length; i++) {
            if (a[i].getAttribute('xtype')) {
                a[i].setAttribute('type', a[i].getAttribute('xtype'));
                a[i].removeAttribute('xtype');
            };
        }
    }
}

jQuery.preloadImages = function () {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

