$(document).ready(function() {
	// This is our colors classes in our stylesheet
	var colors = ['color-1', 'color-2', 'color-3', 'color-4', 'color-5', 'color-6'];
	// We need to count
	var i = 0;
	
	// Foreach block date
	$('input.info-date').each(function() {
		// First we add the color class
		$(this).next().addClass(colors[i]);
		// Next we iterate throught all post with the same post date and we add the same class
		$('input[class!="info-date"][value="' + $(this).attr('value') + '"]').each(function() {
			$(this).next().children('.block-icon').addClass(colors[i]);
		});
		// We need to increment now!
		i = (i > 5) ? i = 0 : i = i + 1;
	});
	
	// And now, we want an image for our video post
	$("div[class*=thumb]").each(function() {
        var thumb = $(this);
        var videoEle = $(this).children(".hide");
        var swf = "";

        if ($(videoEle).children("iframe").length === 0) {
            $(videoEle).children("object").children("param").each(function() {
                if ($(this).attr("name") == "movie") {
                    swf = $(this).attr("value");
                }
            });
        }
        else {
            swf = $(videoEle).children("iframe").attr("src");
        }

        if (swf.indexOf("youtube.com") >= 0) {
            // get youtube thumb
            swf = swf.replace("http://www.youtube.com", "");
            swf = swf.replace("http://youtube.com", "");
            var youtube_id = swf.substring(swf.indexOf("/v/") + 3, swf.indexOf("&"));
            var thumb_url = "http://i.ytimg.com/vi/" + youtube_id + "/0.jpg";
            $(thumb).attr('style', 'background: url(' + thumb_url + ') no-repeat center center;');
        } else if (swf.indexOf("vimeo.com") >= 0) {
            var vimeo_id = swf.substring(swf.lastIndexOf("/") + 1);
            $.getJSON("http://vimeo.com/api/v2/video/" + vimeo_id + ".json?callback=?", function(d) {
                var thumb_url = d[0].thumbnail_medium;
                $(thumb).attr('style', 'background: url(' + thumb_url + ') no-repeat center center;');
            });
        }
    });
});
