/**
 * newsflash_alertbox jquery plugin 
 * @usage:  jQuery("#SELECTOR#").newsflash_alertbox();
 * @description: checks given url for changes to the file (ie: a CFM file that polls automatically for new content by
 * given number of milliseconds).
 * @requires: jQuery 1.3+  (uses live() method)
 * @author: mcn 
 * @usage-extended:
  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="/lib/js/jQuery/jquery.newsflash_alertbox.js"></script>
<script type="text/javascript" language="javascript">
jQuery(function(){
	jQuery("#testnewsflash").newsflash_alertbox();
});	
</script>
</head>
	<body>
		<div id="testnewsflash" style="display: none;">
			Alert box here!
		</div>
		<hr>
		<a href="#" id="alertboxToggle">[--NEWS FLASH--]</a>
	</body>
 */
;(function(jQuery) {
	jQuery.fn.newsflash_alertbox = function(options){
		var _this = jQuery(this);
		if (typeof(options) == 'undefined') {
			var options = {};
		}
		// initialize defaults
		var defaultOptions = {
				url: '/scratch/removeme.html',
				dataType: 'html',
				boxHeader: '<div id="newsflash-header"><img src="images/newsflash-icon.png" alt="news flash"/><h4>News Flash</h4><a href="#" id="newsflash-x-close">[X]</a></div>',
				pollTime: 5000,
				alertboxToggleElement: '#alertboxToggle',
				autoFlash: true,
				autoFlashTime: 3000
		};
		// override defaults
		for (var key in defaultOptions) {
			if (typeof options[key] == 'undefined') {
				options[key] = defaultOptions[key];
			}
		}
		var checkContent = function() {
			jQuery.ajax({
				url: options.url,
				ifModified: false,
				dataType: options.dataType,
				success: function(data,status){
					if (typeof data !== 'undefined'){
						_this.html("");
						_this.prepend(options.boxHeader);
						_this.append(data);
						if (this.ifModified == true && status !="notmodified" ) {
							if (_this.not(":visible")){
								_this.fadeIn("slow");
							}
						}
					}
				}	
			});
		}
		function longPoll(){
			checkContent();
			setTimeout(arguments.callee, options.pollTime);
		}
		function startPolling(){
			longPoll();
		}
		
		// set up behaviors of alertbox
		jQuery("#newsflash-x-close").live('click', function(){
			_this.fadeOut("slow");
		});
		//
		jQuery(options.alertboxToggleElement).click(function(){
			if ( _this.is(":visible")) {
				_this.fadeOut("slow");
			}
			else {
				_this.fadeIn("slow");
			}
			return false;
		});
		
		startPolling();
	};
})(jQuery);
