function showFML(doc){
    var url = "http://api.betacie.com/view/random?key=4a1843aad171f&language=en";
    _doc = $(doc);
    $.get(url, function(xml){
        var nFML = $(xml).find("text").get(0).textContent;
        nFML = nFML.replace("FML", "<b>FML</b>");
		var color = new Array();
		color[0]="#E5FFDB";color[1]="#F5DBFF";color[2]="#FFE0B3";
		color[3]="#FAFFCC";color[4]="#FFCCCC";color[5]="#E5ECFF";
		color[6]="#E5FFDB";color[7]="#F5DBFF";color[8]="#FFE0B3";
		color[9]="#FAFFCC";color[10]="#FFCCCC";color[11]="#E5ECFF";
		var _color = Math.floor(Math.random()*12);
        $(jetpack.tabs.focused.contentDocument).find("#i-am-a-new-fml3049").css({
            "background-color": color[_color],
            "padding": "10px",
            "color": "#000000",
            "font-weight": "500",
            "margin": "5px",
            "font-size": '13px',
            "font-family": 'Arial, Helvetica, sans-serif',
            "z-index": "100",
            "position": "relative"
        });     
        //************************
        //This is a bug, scroll sometimes doesn't work.
        _doc.find('body').animate({
            scrollTop: 0
        }, 'fast');
        //************************    
		//************************
		 //Hack to make scroll works
		_doc.find('html').animate({
            scrollTop: 0
        }, 'fast'); 
		//************************  
        _doc.find("#i-am-a-new-fml3049").html(nFML).stop(true, true);
        _doc.find("#i-am-a-new-fml3049").html(nFML).fadeIn("slow").fadeTo(15000, 1).fadeOut(1500);
        return false;
    });
}

function FML(doc){
    $(doc).click(function(){
        var body = jetpack.tabs.focused.contentDocument;
        if ($(body).find('#i-am-a-new-fml3049').length == 0) {
            $(body).find('body').prepend('<div id = "i-am-a-new-fml3049" style = display:none> </div>');
        }
        showFML(body);
    });
}

jetpack.statusBar.append({
    html: '<img style = "cursor:pointer" src="http://steveram.info/fml.ico"/>',
    onReady: function(doc){
        var fml = new FML(doc);
    },
    width: 30
});
