function emptyFunction() { }

var Try = {
    these: function() {
	var returnValue;
	
	for (var i = 0, length = arguments.length; i < length; i++) {
	    var lambda = arguments[i];
	    try {
		returnValue = lambda();
		break;
	    } catch (e) { }
	}
	return returnValue;
    }
};

var ShowEvent = {
    transport : null,
    is_busy : false,
    speed: 0.0,
    acceleration: 0.0,
    lost_energy: 0.0,
    y: -50.0,
    yminref: -50.0,
    ymin: -50.0,    
    ymaxref: 10.0,
    ymax: 10.0,
    growl_node: null,
    fodder: 0,    

	getTransport : function() {
	    return Try.these
		(function() {return new XMLHttpRequest()},
		 function() {return new ActiveXObject('Msxml2.XMLHTTP')},
		 function() {return new ActiveXObject('Microsoft.XMLHTTP')}
		 ) || false;
	},
	
    init: function() {
	if (!AJAX_Compatible) {
	    return false;
	}
	ShowEvent.update();
	return true;
    },
    fadeit_init: function() {
	var yoffset = 0.0;
	if (is_ie) {
	    yoffset =
		(window.pageYOffset || document.documentElement.scrollTop ||
		 document.body.scrollTop);
	};
	ShowEvent.speed = 0.0;
	ShowEvent.acceleration = 0.15;
	ShowEvent.lost_energy = 8.0;
	ShowEvent.ymin = ShowEvent.yminref + yoffset;
	ShowEvent.ymax = ShowEvent.ymaxref + yoffset;
	ShowEvent.y = ShowEvent.ymin;	
    },
    fadeit: function() {
	ShowEvent.speed += ShowEvent.acceleration;
	ShowEvent.y += ShowEvent.speed;
	if (ShowEvent.y > ShowEvent.ymax) {
	    ShowEvent.y = ShowEvent.ymax;
	    ShowEvent.speed = -ShowEvent.speed + ShowEvent.lost_energy;
	    if (-ShowEvent.speed < ShowEvent.lost_energy) {
		if (is_ie) {
		    ShowEvent.growl_node.style.top = Math.round(ShowEvent.y) + "px";
		} else {
		    ShowEvent.growl_node.style.bottom = Math.round(ShowEvent.y) + "px";
		}
		ShowEvent.acceleration = -ShowEvent.acceleration;
		ShowEvent.speed = 0.0;
		setTimeout(ShowEvent.fadeit, 1000 * 10);
		return;
	    }
	}
	if (ShowEvent.acceleration < 0.0 && ShowEvent.y < ShowEvent.ymin) {
	    ShowEvent.growl_node.parentNode.removeChild(ShowEvent.growl_node);
	    ShowEvent.growl_node = null;
	    setTimeout(ShowEvent.update, 1000 * 10);
	    return;
	}
	if (is_ie) {
	    ShowEvent.growl_node.style.top = Math.round(ShowEvent.y) + "px";
	} else {
	    ShowEvent.growl_node.style.bottom = Math.round(ShowEvent.y) + "px";
	}
//	ShowEvent.growl_node.style.left = (parseInt(ShowEvent.growl_node.style.left) + 1) + "px";
	setTimeout(ShowEvent.fadeit, 15);
    },
    growl: function(reply) {
	if ((ShowEvent.growl_node = document.getElementById("growl-alert"))) {
	    ShowEvent.growl_node.parentNode.removeChild(ShowEvent.growl_node);
	}
	ShowEvent.growl_node = document.createElement('div');
	ShowEvent.growl_node.id = "growl-alert";
	if (is_ie) {
	    ShowEvent.growl_node.style.position = "absolute";
	    ShowEvent.growl_node.style.filter = "alpha(opacity=75)";
	    ShowEvent.growl_node.style.top = "0px";	    
	} else {
	    ShowEvent.growl_node.style.position = "fixed";
	    ShowEvent.growl_node.style.opacity = 0.75;
	    ShowEvent.growl_node.style.bottom = "0px";	    
	}	
	ShowEvent.growl_node.style.left = "8px";
	ShowEvent.growl_node.innerHTML = reply.html;
	document.body.appendChild(ShowEvent.growl_node);
	ShowEvent.fadeit_init();
	ShowEvent.fadeit(ShowEvent.growl_node);
    },
    cb: function(ev) {
	if (typeof ShowEvent === "undefined") {
	    return;
	}
	if (ShowEvent.transport.readyState !== 4) {
	    return;
	}
	ShowEvent.is_busy = false;	
	ShowEvent.transport.onreadystatechange = emptyFunction;
	if (ShowEvent.transport.status !== 200) {
	    return;
	}	
	var reply_ = ShowEvent.transport.responseText;
	if (reply_.length <= 0) {
	    setTimeout(ShowEvent.update, 1000 * 20);
	    return;
	}
	var reply;
	try {
	    reply = eval('(' + reply_ + ')');
	    void(reply.uid);
	    void(reply.html);	    
	} catch (err) {
	    setTimeout(ShowEvent.update, 1000 * 15);
	    return;	    
	}
	var old_uid = fetch_cookie('events-last-uid');
	if (true && old_uid == reply.uid) {
	   setTimeout(ShowEvent.update, 1000 * 10);
	   return;
	}
	set_cookie('events-last-uid', reply.uid);
	ShowEvent.growl(reply);
    },
    update: function() {
	if (!ShowEvent.transport &&
	    !(ShowEvent.transport = ShowEvent.getTransport())) {
	    return;
	}
	if (ShowEvent.is_busy) {
	    ShowEvent.transport.onreadystatechange = emptyFunction();
	    ShowEvent.transport.abort();
	}
	ShowEvent.is_busy = true;	
	ShowEvent.transport.open("GET", "/events/latest?se=2&t=" + ShowEvent.fodder);
	ShowEvent.transport.setRequestHeader
	    ("Accept", "text/javascript, text/html, application/xml, text/xml, application/json, text/json, */*");
	ShowEvent.transport.setRequestHeader
	    ("X-Requested-With", "XMLHttpRequest");
	ShowEvent.transport.onreadystatechange = ShowEvent.cb;
	ShowEvent.transport.send(null);
	ShowEvent.fodder++;
    }
};

ShowEvent.init();
