﻿/* jQuery based IE9 Site Mode for Terra
Requisitos:   jQuery 1.5.2+ (http://www.jquery.com)
            jFeed plugin (http://hovinne.com/articles/jfeed-jquery-rss-atom-feed-parser-plugin)
            Pinify plugin 1.2+ (http://ie9ify.codeplex.com)
*/

var DynamicJumpListArray = []; // Array para las noticias de la JumpList Dinámica
var lastFeedShown, lastFeedUpdate; // Controlan las actualizaciones de la RSS
var rssCheckSeconds = 300; // Segundos para comprobar si hay actualizaciones.
var rssFeedPath = "http://www.invertia.com/rss/rss_discoverability.asp"; // Ruta del Rss para comprobar, por ejemplo "http://cine.terra.es/noticias/rss.htm"

/// <summary>
/// Main, default method. Called by $().ready(), so automatically ex
/// </summary>
function Main() {
    // Pinify HEAD. Generamos el modo pienado y la JumpList Estática.
    $('head').pinify({
        applicationName: 'Invertia',
        favIcon: 'http://www.invertia.com/favicon.ico',
		navColor: '#a92d2f',
        startUrl: 'http://www.invertia.com/portada_bis.asp',
        tooltip: 'El Portal Invertia.com',
        tasks: [
            { 'name': 'Cotizaciones de Ibex 35', 'action': 'http://www.invertia.com/mercados/bolsa/indices/acciones.asp?idtel=IB011IBEX35', 'icon': 'http://www.invertia.com/favicon.ico' },
            { 'name': 'Cotizaciones de toda la bolsa', 'action': 'http://www.invertia.com/mercados/bolsa/indices/acciones.asp?idtel=IB011CONTINU', 'icon': 'http://www.invertia.com/favicon.ico' },
            { 'name': 'Mis Valores', 'action': 'http://www.invertia.com/opener_in.htm', 'icon': 'http://www.invertia.com/favicon.ico' },
            { 'name': 'Comunidad y foros', 'action': 'http://servicios.invertia.com/foros/default.asp', 'icon': 'http://www.invertia.com/favicon.ico' },
        ]
    });
    // Thumbbar Buttons for Windows 7 taskbar preview
    $.pinify.createThumbbarButtons({
        buttons: [{
            icon: "http://www.invertia.com/images/ico_facebook.ico",
            name: "Facebook",
            click: function () { window.open("http://www.facebook.com/pages/Invertia/76224614701") }
        }, {
            icon: "http://www.invertia.com/images/ico_twitter.ico",
            name: "Twitter",
            click: function () { window.open("http://twitter.com/Invertia/") }
        }]
    });
    // Discoverability Teaser with close link and cookie handler
 try {
     if (window.external.msIsSiteMode()) {
		            // Continue initialization.
		        }
		        else {
		            // Display drag-to-pin image.
		            obj = document.getElementById("pinContainer");
		            obj.style.display = "block";
		        }
	}
		    catch (e) {
		        // Fail silently. Pinned Site API not supported.
		    }
    // Obtiene las noticias de la RSS y las publica en la JumpList dinámica.
    $.getFeed({
        url: rssFeedPath,
        success: function (feed) {
            PopulateDynamicJumpList(feed);
            lastFeedShown = lastFeedUpdate;
        }
    });
    // Comprobamos cada "rssCheckSeconds" segundos si hay noticias nuevas.
    // Si hay nuevos elementos, se mostrará un icono de notificación.
    setInterval(function () { NewRSSCheck() }, rssCheckSeconds * 1000);
}

/// <summary>
/// Populates the DynamicJumpListArray with items retrieved
/// from the RSS feed
/// </summary>
/// <param name="feed">jFeed's var with retrieved feed</param>
function PopulateDynamicJumpList(feed) {
    $.pinify.clearJumpList();
    for (var i = 0; i < feed.items.length && i < 5; i++) {
        var item = feed.items[i];
        var jpItem = {
            'name': item.title,
            'url': item.link,
            'icon': "http://www.invertia.com/favicon.ico",
            'date': item.updated
        };
        DynamicJumpListArray.push(jpItem);
    }
    $.pinify.addJumpList({
        title: "Noticias",
        items: DynamicJumpListArray
    });
    lastFeedUpdate = feed.updated; // Update last feed status, so we can check whatever there are new items or not
}

/// <summary>
/// Checks for new items in the RSS feed
/// </summary>
function NewRSSCheck() {
    $.pinify.clearOverlay();
    $.getFeed({
        url: rssFeedPath,
        success: function (feed) { PopulateDynamicJumpList(feed); }
    });

    //if (true) { // Always true just for testing pruporses
    if (lastFeedShown != lastFeedUpdate) {
        $.pinify.flashTaskbar();
        $.pinify.addOverlay({
            title: 'Nuevas Noticias en Invertia',
            icon: 'http://www.invertia.com/favicon.ico'
        });
        lastFeedShown = lastFeedUpdate;
    }
}

		

// Our particular BigBang, here starts everything
$().ready(function () {
    Main();
});

