/*

Environment Badge - jQuery Plugin
http://www.brothersroloff.com/labs/environment-badge/

Copyright (c) 2010 Cory Roloff, Jay Roloff
http://www.brothersroloff.com

The terms of this software are described in the file LICENSE.txt, which you should have received as 
part of this distribution. Redistributions of files must retain the above copyright notice.

*/

(function($) {
	$.environmentBadge = function(options) {
		if(this.initialized) return;
	
		this.initialized = true;
	
		var defaults = {
			name: '',
			message: '',
			details: []
		};
		
		if(options) $.extend(defaults, options);
	
		var prefix = 'environment_badge';
	
		$('head').prepend('\
			<style type="text/css">\
			div#environment_badge {\
				width: 150px;\
				padding: 8px 12px;\
				background: #ff9900;\
				position: fixed;\
				top: 10px;\
				right: 0px;\
				z-index: 9999999;\
				color: #000;\
				font: 10px/12px "lucida grande", tahoma, verdana, sans-serif;\
				border-bottom-left-radius: 10px;\
				-moz-border-radius-bottomleft: 10px;\
				-webkit-border-bottom-left-radius: 10px;\
				border-top-left-radius: 10px;\
				-moz-border-radius-topleft: 10px;\
				-webkit-border-top-left-radius: 10px;\
				-box-shadow: 10px 2px 10px rgba(0, 0, 0, 0.3);\
				-moz-box-shadow: 10px 2px 10px rgba(0, 0, 0, 0.3);\
				-webkit-box-shadow: 10px 2px 10px rgba(0, 0, 0, 0.3);\
				text-shadow: 0px 1px 0px rgba(255, 255, 255, 0.25);\
				background: -webkit-gradient(linear, left top, left bottom, from(#ffb74b), to(#ff9900));\
				background: -moz-linear-gradient(top,  #ffb74b,  #ff9900);\
				opacity: .70;\
				filter:alpha(opacity=70) progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffb74b", endColorstr="#ff9900");\
			}\
			div#environment_badge_name {\
				font-weight: bold;\
				text-transform: uppercase;\
				cursor: pointer;\
			}\
			div.environment_badge_hover {\
				opacity: 1 !important;\
				filter:alpha(opacity=100) progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffb74b", endColorstr="#ff9900") !important;\
			}\
			div#environment_badge_details {\
				padding-top: 8px;\
			}\
			div#environment_badge_message {\
				padding-bottom: 8px;\
			}\
			div#environment_badge table {\
				width: 100%;\
				border-collapse: collapse;\
				margin: 0px;\
				padding: 0px;\
				border: none;\
			}\
			div#environment_badge td {\
				border: none;\
				border-top: solid 1px #cc8212;\
				padding: 4px 10px 4px 0px;\
				text-align: left;\
				vertical-align: top;\
			}\
			div#environment_badge tr:first-child td {\
				border: none;\
			}\
			div#environment_badge a:link, div#environment_badge a:visited, div#environment_badge a:hover, div#environment_badge a:active {\
				color: #000;\
				text-decoration: underline;\
			}\
			</style>\
		');
		
		$('body').prepend('<div id="' + prefix + '"></div>');
		
		var badge = $('div#' + prefix);
		
		badge.append('<div id="' + prefix + '_name">' + options.name + '</div>');
		badge.append('<div id="' + prefix + '_details_wrapper" style="display: none;"></div>');
		
		var detailsWrapper = $('div#' + prefix + '_details_wrapper');
		
		detailsWrapper.append('<div id="' + prefix + '_details"></div>');
		
		var details = $('div#' + prefix + '_details');
		
		if(options.message) details.append('<div id="' + prefix + '_message">' + options.message + '</div>');
		
		if(options.details && options.details.length) {
			details.append('<table border="0" cellspacing="0" cellpadding="0"></table>');
			var table = details.find('table');
			for(var i=0, length=options.details.length; i<length; ++i) {
				var detail = options.details[i];
				table.append('\
					<tr>\
						<td>' + detail.label + '</td>\
						<td>' + detail.value + '</td>\
					</tr>\
				');
			}
		}
		
		var name = $('div#' + prefix + '_name');
		
		badge.mouseover(function() {
			if(!detailsWrapper.is(':visible')) badge.addClass(prefix + '_hover');
		});
		
		badge.mouseout(function() {
			if(!detailsWrapper.is(':visible')) badge.removeClass(prefix + '_hover');
		});
		
		name.click(function() {
			if(!detailsWrapper.is(':visible')) {
				detailsWrapper.slideDown('fast');
				if(!badge.hasClass(prefix + '_hover')) badge.addClass(prefix + '_hover');
			}
			else {
				detailsWrapper.slideUp('fast', function() {
					if(badge.hasClass(prefix + '_hover'))
						badge.removeClass(prefix + '_hover');
				});
			}
		});
		
		return this;
	};
})(jQuery);
