﻿
var curBanner;
var timeout;

$(function () {

    curBanner = $('.banner1');

    // hide all banners except the first
    $('.banner2,.banner3,.banner4').hide();

    // setup click events for icons
    $('img[banner]').click(function () {

        $('img[banner]').each(function () {
            $(this).attr("src", $(this).attr("src").replace("-selected", ""));
        });
        $(this).attr("src", $(this).attr("src").replace("icon.png", "icon-selected.png"));

        curBanner.fadeOut('slow');

        curBanner = $("." + $(this).attr("banner"));
        curBanner.fadeIn('slow');
        clearTimeout(timeout);
        //Rotate();
    })
    .hover(function () {
        $(this).attr("src", $(this).attr("src").replace("icon.png", "icon-selected.png"));
    }, function () {
        if (!curBanner.hasClass($(this).attr("banner")))
            $(this).attr("src", $(this).attr("src").replace("icon-selected.png", "icon.png"));
    })
    .css("cursor", "pointer");

    // start the rotation
    $('img[banner="banner1"]').attr("src", $('img[banner="banner1"]').attr("src").replace("icon.png", "icon-selected.png"));
    Rotate();

    // Load RSS Feed
    LoadRSSFeed();
});

function Rotate() {
    timeout = setTimeout(function () {
        curBanner.fadeOut('slow');
        var nextBanner;

        if (curBanner.hasClass('banner4')) {
            nextBanner = $('.banner1');
        }
        else {
            nextBanner = curBanner.next();
        }

        $(".icons img").each(function () {
            if ($(this).attr("banner") == nextBanner.attr("class")) {
                $(this).attr("src",$(this).attr("src").replace("icon.png", "icon-selected.png"));
            }
            else {
                $(this).attr("src", $(this).attr("src").replace("-selected", ""));
            }
        });

        nextBanner.fadeIn('slow', function () {
            curBanner = nextBanner;
            Rotate();
        });
    }, 5000);
}

function LoadRSSFeed() {
    $.jGFeed('http://blog.voiance.com/feed',
  function (feeds) {
      // Check for errors
      if (!feeds) {
          // there was an error
          return false;
      }
      // do whatever you want with feeds here
      for (var i = 0; i < feeds.entries.length; i++) {
          var entry = feeds.entries[i];
          // Entry title
          if (i > 2)
              return false;

          var articleTitle = entry.title;
          if (articleTitle.length > 41)
              articleTitle = entry.title.substring(0, 42) + "...";

          var li = '<li><a href="' + entry.link + '">' + articleTitle + '&nbsp;&nbsp;<img src="images/shell/blog-icon-box-orange-16.gif" alt="blog link" /></a>';
          li += '<p>' + entry.contentSnippet.substring(0, 90) + '...</p>';
          li += '</li>';
          $("#rss-feed").append(li);
      }
  }, 10);
}
