﻿/* jQuery based IE9 Site Mode for Grupo Estadâo 
Requires:   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 = []; // RSS news array for DynamicJumpList
var lastFeedShown, lastFeedUpdate; // Controls RSS updates
var rssCheckSeconds = 300; // Seconds to check for new RSS items
var rssFeedPath = "/xml/manchetes.xml?c="; // Path for the RSS feed to check

/// <summary>
/// Main, default method. Called by $().ready(), so automatically ex
/// </summary>
function Main() {
    // Pinify HEAD. Generates SiteMode and STATIC Jumplist
	
	    $.getFeed({
			url: rssFeedPath,
			cache: false,
			success: function (feed) {
				//PopulateDynamicJumpList(feed);
				$('head').pinify({
					applicationName: 'Estadâo',
					favIcon: 'ico/favicon.ico',
					startUrl: 'http://www.estadao.com.br/home/index.shtm',
					tooltip: 'Estadão',
			        tasks: [
			            { 'name': 'Política', 'action': 'http://politica.estadao.com.br', 'icon': 'ico/cav_politica.ico' },
			            { 'name': 'Economia & Negócios', 'action': 'http://economia.estadao.com.br', 'icon': 'ico/cav_economia.ico' },
			            { 'name': 'Esportes', 'action': 'http://esportes.estadao.com.br', 'icon': 'ico/cav_esportes.ico' },
			            { 'name': 'Tecnologia', 'action': 'http://blogs.estadao.com.br/link', 'icon': 'ico/cav_link.ico' },
			            { 'name': 'Blogs', 'action': 'http://blogs.estadao.com.br', 'icon': 'ico/cav_geral.ico' },
			            { 'name': 'Opinião', 'action': 'http://www.estadao.com.br/opiniao', 'icon': 'ico/cav_politica.ico' },
			            { 'name': 'Galerias do Estadão', 'action': 'http://fotos.estadao.com.br/', 'icon': 'ico/imagem.ico' }
			        ]
			//		tasks: PopulateDynamicJumpList2(feed)
				});
					
				//console.log(feed);
				lastFeedShown = lastFeedUpdate;
			}
		});
	

    // Thumbbar Buttons for Windows 7 taskbar preview
    $.pinify.createThumbbarButtons({
        buttons: [{
            icon: "ico/face.ico",
            name: "Facebook",
            click: function () { window.open("http://www.facebook.com/estadao/") }
        }, {
            icon: "ico/twtt.ico",
            name: "Twitter",
            click: function () { window.open("http://twitter.com/estadao/") }
        }]
    });
    // Discoverability Teaser with close link and cookie handler
//	if(!$.pinify.isPinned()) $("body").css("background-position",  "0px 40px");
//    $("#divPinSite").pinify('pinTeaser', {
//        type: 'topHat',
//        icon: '/favicon.ico',
//        addStartLink: false,
//        pinText: "Descubra um jeito completamente novo de ver o Estadão!",
//        cookieLength: 30,
//        useCookie: true,
//        style: {
//            backgroundImage: '/ie9-estadao/images/discoverability/toolbar_bg.png',
//            closeButtonImage: '/ie9-estadao/images/discoverability/close.png'
//        }
//    });
    // Retrieve RSS feeds and populate Dynamic JumpList
    $.getFeed({
        url: rssFeedPath,
		cache: false,
        success: function (feed) {
            PopulateDynamicJumpList(feed);
            lastFeedShown = lastFeedUpdate;
        }
    });
    // Cheking the RSS feed each "rssCheckSeconds" seconds for new items
    // If new items are detected, flash taskbar and show overlay icon
    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': "ico/cav_geral.ico",
            'date': item.updated
        };
        DynamicJumpListArray.push(jpItem);
    }
    $.pinify.addJumpList({
        title: "Manchetes",
		items: DynamicJumpListArray
    //    items: [
	//				{ 'name': 'Galerias do Estadão', 'url': 'http://fotos.estadao.com.br/', 'icon': 'ico/imagem.ico' },
	//				{ 'name': 'Blogs', 'url': 'http://blogs.estadao.com.br', 'icon': 'ico/cav_geral.ico' },
	//				{ 'name': 'Opinião', 'url': 'http://www.estadao.com.br/opiniao', 'icon': 'ico/cav_politica.ico' },
	//				{ 'name': 'Tecnologia', 'url': 'http://blogs.estadao.com.br/link', 'icon': 'ico/cav_link.ico' },
	//				{ 'name': 'Esportes', 'url': 'http://esportes.estadao.com.br', 'icon': 'ico/cav_esportes.ico' },
	//				{ 'name': 'Economia & Negócios', 'url': 'http://economia.estadao.com.br', 'icon': 'ico/cav_economia.ico' },
	//				{ 'name': 'Política', 'url': 'http://politica.estadao.com.br', 'icon': 'ico/cav_politica.ico' }
	//			]
    });
    lastFeedUpdate = feed.updated; // Update last feed status, so we can check whatever there are new items or not
}

function PopulateDynamicJumpList2(feed) {
	var tmpArr = [];
	for (var i = 0; i < feed.items.length && i < 5; i++) {
		var item = feed.items[i];
        var jpI = {
            'name': item.title,
            'action': item.link,
            'icon': "ico/cav_geral.ico"
        };
        tmpArr.push(jpI);
    }
	return tmpArr;
}

/// <summary>
/// Checks for new items in the RSS feed
/// </summary>
function NewRSSCheck() {
    $.pinify.clearOverlay();
    $.getFeed({
		cache: false,
        url: rssFeedPath + Math.random(),
        success: function (feed) { PopulateDynamicJumpList(feed); }
    });

    //if (true) { // Always true just for testing pruporses
    if (lastFeedShown != lastFeedUpdate) {
        $.pinify.flashTaskbar();
        $.pinify.addOverlay({
            title: 'Novas noticias en Estadâo',
            icon: 'ico/notify.ico'
        });
        lastFeedShown = lastFeedUpdate;
    }
}

// Our particular BigBang, here starts everything
$().ready(function () {
    Main();
	if($.pinify.firstRunState() > 0)
	{
		$('.instrucoes .instrucao-geral').html("<img src=\"http://www.estadao.com.br/estadao/novo/img/instrucao-geral-pinnedsite.png\" border=\"0\" alt=\"\" align=\"right\" /><div class=\"tit\">Parabéns! Agora você poderá acessar o Estadão com apenas um clique.</div><p>Basta usar o ícone do Estadão que você colocou na barra do Windows.</p>");
		$('.instrucoes').fadeIn();
	}
});
