var ie9 = {
    last_news: {
        id: 0,
        section: "",
        time: ""
    },

    meta: {
        applicationName: "Проект iXBT.com",
        favIcon: "/favicon.ico",
        navColor: "#8d83c4",
        startUrl: window.location.href,
        tooltip: "iXBT.com — сайт о высоких технологиях, оперативные новости индустрии, тестовые испытания и обзоры оборудования",
        window: "width=1024;height=768",
        tasks: [
            {
                'name': "Новости hardware",
                'action': "http://www.ixbt.com/news/hard/index.shtml",
                'icon': "/favicons/hard.ico"
            },
            {
                'name': "Новости software",
                'action': "http://www.ixbt.com/news/soft/index.shtml",
                'icon': "/favicons/soft.ico"
            },
            {
                'name': "Конференция iXBT",
                'action': "http://forum.ixbt.com",
                'icon': "/favicons/forum.ico"
            },
            {
                'name': "Теги",
                'action': "http://www.ixbt.com/tag/",
                'icon': "/favicons/tags.ico"
            },
            {
                'name': "Поиск",
                'action': "http://www.ixbt.com/news/search/",
                'icon': "/favicons/search.ico"
            }
        ]
    },

    setLastArticles: function(items){
        window.external.msSiteModeClearJumpList();
        window.external.msSiteModeCreateJumpList("Свежие статьи");

        items.reverse();

        for(var i = 0, l = items.length; i < l; i++){
            window.external.msSiteModeAddJumpListItem(
                items[i].text,
                items[i].url,
                "/favicon.ico",
                "self"
            );
        };
    },

    setThumbnailButtons: function(){
        $.pinify.createThumbbarButtons({
            buttons: [{
                icon: "/favicons/twitter.ico",
                name: "Twitter",
                click: function () {
                    document.location.href = 'http://twitter.com/#!/ixbtcom'
                }
            },
            {
                icon: "/favicons/facebook.ico",
                name: "Facebook",
                click: function () {
                    document.location.href = 'http://www.facebook.com/ixbtlabs'
                }
            }]
        });
    },

    checkNews: function(notify){
        $.ajax({
            url: "/include/json/news_for_pinned_sites.json",
            type: "get",
            cache: false,
            dataType: "json",
            success: function(data){
                if(data.id > ie9.last_news.id && notify){
                    window.external.msSiteModeSetIconOverlay("/favicons/ast.ico", "На сайте обновился раздел новостей");
                    window.external.msSiteModeActivate();
                };

                ie9.last_news.id = data.id;
            }
        });
    },

    setNewsListener: function(){
        ie9.checkNews(false);
        window.setInterval("ie9.checkNews(true)", 1000*30);
    }
}

$(function(){
    $("head").pinify(ie9.meta);

    try {
        if(window.external.msIsSiteMode()){
            ie9.setLastArticles(pin_articles);
            ie9.setNewsListener();
            ie9.setThumbnailButtons();
        };
        
    }catch(ex){

    };
});
