﻿var updateInterval;

$(document).ready(function() {
    $(window).bind('unload', function() {
        if(updateInterval) { clearInterval(updateInterval); }
    });
    
    updateStatus();
    updateInterval = setInterval('checkStock()', 5000);
});

var stack = {
    delay: 5000,
    actions:[],
    run: function() {
        if(stack.actions.length) {
            stack.actions.shift()();
            setTimeout(stack.run, stack.delay);
        }
    }
};

function checkStock() {
    if(stack.actions.length == 0) { updateStatus(); }
}
	
// Twitterfeed 
function updateStatus() {
    var list = $('ul.socialmedialist');
    var numLi = $("ul.socialmedialist > li").size();
    $.get('ajax.ashx', {page: 'gettwitter', debug:true}, function(data) {
        var items = $(data).find('response:eq(0) item');	        
        items.each(function() {
            var item = $(this);
            stack.actions.push(function(){    
                var link = item.attr("link");
                var icon = item.attr("icon");
                var content = item.attr("content");
                var updatedtext = item.attr("updatedtext");
                var id = item.attr("id");
                var li = $("<li>" + "<a href='" + link + "'><img src=" + icon + " class='left' style='width: 25px; height: 25px;' ></a>" + "<span>" + content + "</span> <span class='right'>" + updatedtext + "</span> <div class='clear'></div> </li>");
                if(numLi > 3) {
                    list.children('li:last-child').fadeOut(500, function() {
                        list.children('li:last-child').remove();	            
                    });
                    li.insertBefore(list.children('li:first-child')).fadeIn(1000);
                } else if(numLi == 0) {
                    $('.socialmedialist').append(li).fadeIn(1000);
                } else {
                    li.insertBefore(list.children('li:first-child')).fadeIn(1000);
                }
                numLi = $("ul.socialmedialist > li").size();
            });
        });
        stack.run();
    }, 'xml');
}

//  Facebookfeed
//	function updateStatus() {
//	    var list = $('ul.socialmedialist');
//	    var numLi = $("ul.socialmedialist > li").size();
//	    $.get('<%=ResolveClientUrl("~/ajax.ashx")%>', {page: 'getfacebook'}, function(data) {
//	        var items = $(data).find('response:eq(0) item');	        
//	        items.each(function() {
//	            var item = $(this);
//	            stack.actions.push(function(){    
//	                var link = item.attr("link");
//                    var icon = item.attr("icon");
//                    var content = item.attr("content");
//                    var updatedtext = item.attr("updatedtext");
//                    var id = item.attr("id");
//                    var li = $("<li><span>" + content + "</span> <span class='right'>" + updatedtext + "</span> <a href='http://facebook.com" + link + "'>Read more</a> <div class='clear'></div> </li>");
//	                if(numLi > 2) {
//	                    list.children('li:last-child').fadeOut(500, function() {
//	                        list.children('li:last-child').remove();	            
//	                    });
//	                    li.insertBefore(list.children('li:first-child')).fadeIn(1000);
//	                } else if(numLi == 0) {
//	                    $('.socialmedialist').append(li).fadeIn(1000);
//	                } else {
//	                    li.insertBefore(list.children('li:first-child')).fadeIn(1000);
//	                }
//	                numLi = $("ul.socialmedialist > li").size();
//	            });
//	        });
//	        stack.run();
//	    }, 'xml');
//	}
