(function($){
    //Set element ID
    var alertElementID = 'emerg-alert-system', pollInterval = 60000;
    
    //Function for getting, adding, and/or removing alert
    function get_alert(){
    
        $.ajax({url:'https://www.emory.edu/template_shared/alert-feed.cfm', cache:false}).success(function(data) {
    
        	//Assign variables for the target element, the presence of an alert message, and the presence of the alert element 
    		var $element = $('#' + alertElementID),
    			alert = $.trim(data).length === 0 ? false : true,
    			displayed = $element.length === 0 ? false : true,
    			styles = {
    				"background": "#b40000", 
    				"color": "white", 
    				"display": "block", 
    				"max-width": "100%", 
    				"font-family": "Arial,sans-serif",
    				"font-size": "1.2em",
    				"text-align": "center",
    				"text-transform": "uppercase",
    				"letter-spacing": "1px",
    				"line-height": "1.5",
                    "z-index": "9999",
                    "position":"relative"
    			};
                
    
    		//If there is an alert...
    		if( alert === true ){
    
    			//Check to see if the element is already displayed...
    			if ( displayed === false ){
    				
    				//If not, create and style the element and populate with the alert
    				$('<div/>', {id: alertElementID}).prependTo('body').css(styles).html(data);
    
    			} else if( displayed === true && $element.html() !== data ){
    				
    				//If the element is already present and the message has changed, change the content of the element
    				$element.html(data);
    
    			} else {
    				//Current message already displayed; do nothing.
    			}
    		} else if( alert === false && displayed === true ){
    			
    			//If there is no alert message and the element is currently displayed, remove the element
    			$element.remove();
    
    		} else {
    			//No alert and nothing displayed; do nothing.
    		}
    	});
    
    	//Repeat this function at an interval
    	//console.log('polling interval = '+ pollInterval/60/1000 + ' minutes (' + pollInterval + ' milliseconds)'); //Show polling interval
    	setTimeout(get_alert,pollInterval);
    }
    
    //When the document is ready, call the get_alert function
    $(document).ready(function(){
    	get_alert();		
    });    
    
    
})(jQuery);
