function dim(){
/*	if($(this).css('position') == "static" || $(this).css('position') == ""){
		$(this).css('position', 'relative')	;
	}
*/	
	backCover = $("<div id='backCoverDimmer'></div>");
	backCover.css({	
		background: 'black',
		position: 'fixed',
		top: '0px',
		left: '0px',
		height: '100%',
		width: '100%',
		opacity: '.8',
		display: 'none',
		zIndex: '0'
	});			  
	backCover.appendTo($('body')).fadeIn('slow');
}

function undim(){
	$('#backCoverDimmer').fadeOut('slow',function(){$(this).remove()});	
}

$.fn.maintainSize = function(){
	$(this).css('width',$(this).css('width'));
	$(this).css('height',$(this).css('height'));	
}

$.fn.editable = function(options){
	settings = {
		editEvents 		: 'click',
		finishedEvents 	: 'blur mouseleave',
		editScript 		: false,
		editName		: 'edited',
		scriptData 		: '',
		scriptFunc 		: null,
	}
	$.extend(settings, options);
	/*if(typeof(opts) != 'undefined'){
		e.editEvents = typeof(opts.editEvents) == 'undefined'
				? e.editEvents : opts.editEvents;
		e.finishedEvents = typeof(opts.finishedEvents) == 'undefined'
				? e.finishedEvents : opts.finishedEvents;
		e.editScript  = typeof(opts.editScript) == 'undefined'
				? e.editScript : opts.editScript;
		e.editName  = typeof(opts.editName) == 'undefined'
				? e.editName : opts.editName;
		e.scriptData  = typeof(opts.scriptData) == 'undefined'
				? e.scriptData : opts.scriptData;
		e.scriptFunc  = typeof(opts.scriptFunc) == 'undefined'
				? e.scriptFunc : opts.scriptFunc;
	}*/
	
	var orig = $(this);
	orig.bind(settings.editEvents, 
		function(){
			textBox = $('<input type="text" style="display:block"/>');	
			textBox.val(orig.text());
			textBox.bind(settings.finishedEvents, 
				function(){
					orig.text($(this).val());
					//AJAX out the edit
					if (settings.editScript){
						eval ('data = {' + settings.editName + ' : "' +  $(this).val() + '"}');	
						if(settings.scriptData != '')
							$.extend(data, settings.scriptData);
						$.get(settings.editScript,
							  data,
							  settings.scriptFunc
						);						
					}					
					$(this).replaceWith(orig.editable(settings));
				}
			);
			orig.replaceWith(textBox);
		}
	);
	
	return orig;
}

$.fn.deletable = function (options){
	settings = {
		deleteEvents 	: 'mouseover',
		offEvents		: 'mouseleave',
		warning 		: true,
		deleteScript	: 'delete.php',
		scriptData 		: '',
		scriptFunc 		: null,
		deletePic		: '../images/deletePic.gif'
	}
	$.extend(settings, options);
	
	var wrap = $('<div style="position:relative"></div>').attr('class',$(this).attr('class'));
	$(this).attr('class','');
	//put on the delete img
	var deleter = $('<div></div>');
	deleter.css({
		position 	: 'absolute',
		top 		: '-10px',
		left		: '-10px',
		cursor		: 'pointer',
		border 		: '0'
	});

	deleter.html('<img src="' + settings.deletePic + '" alt="delete" style="width:25px;border:0"/>');	
	deleter.css('display','none');
	deleter.click(
		function(){
			var conf = false;
			if(settings.warning){				
				conf = confirm('Are you sure you want to delete this?');
			}else{
				conf = true;	
			}
			if(conf){
				wrap.fadeOut('slow', function(){
					$(this).remove();
				});
				if(settings.deleteScript){
					$.get(settings.deleteScript,
						  settings.scriptData,
						  settings.scriptFunc
					);	
				}
			}
		}
	)
	wrap.append(deleter);
	
	wrap.bind(settings.deleteEvents,
		function(){
				deleter.fadeIn('fast');
		}
	);
	
	wrap.bind(settings.offEvents, 
		function(){
			deleter.fadeOut('fast');	
		}
	);
	
	wrap.append($(this));
	return wrap;
}

$.fn.closable = function(options){
	settings = {
		callBackFunc 	: function(wrap){
									wrap.fadeOut('slow', function(){
										$(this).remove();
									});
								},
		deletePic		: '../images/deletePic.gif',
		css				: {}
	}
	$.extend(settings, options);
	var wrap = $('<div></div>')
		.attr('class',$(this).attr('class'))
		.css(settings.css);
	
	$(this).attr('class','');
	//put on the delete img
	var deleter = $('<div id="deleter"></div>');
	deleter.css({
		position 	: 'absolute',
		top 		: '-10px',
		left		: '-10px',
		cursor		: 'pointer',
		border 		: '0'
	});

	deleter.html('<img src="' + settings.deletePic + '" alt="delete" style="width:25px;border:0"/>');	
	deleter.click(function(){	wrap.fadeOut('slow', function(){
									$(this).remove();
								});
	});
	$(this).wrap(wrap).before(deleter);
	return wrap;
}

$.fn.fitIn = function(options){
	var settings = { 	container: 'parent',
	                 	pct : .9,
						horizontal : true,
						vertical   : true,
						resize	   : true
					 };
    $.extend(settings, options);	
	return this.each(function(i){
		if(!settings.horizontal && !settings.vertical)return;
		var el = $(this);
		var ew = el.width();
		var eh = el.height();
		var ratio = ew / eh;
		if(settings.container == 'parent')settings.container = el.parent();
		var ph = settings.container.height() * settings.pct;
		var pw = settings.container.width() * settings.pct;
		
		//don't care about vertical and horizontal is good
		if(!settings.vertical && ew < pw)return;
		//don't care about horizontal and vertical is good
		if(!settings.horizontal && eh < ph) return;
		
		var nh, nw;
		if(ratio < pw/ph){
			if(eh > ph){
				nh = ph; 
				nw = nh * ratio;
			}
		}else if(ew > pw){
			nw = pw; 
			nh = nw * ratio;
		}
		el.css({
			height : nh + 'px', 
			width  : nw + 'px'					
		});
		
	//	$(window).resize(el.fitIn(settings));
		
	});

}

$.fn.center = function(options) {
    var opt = {	vertical : true,
				horizontal : true };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var p = el.parent();
		var cp = el.css('position');
		var isFixed = cp == 'fixed';

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
		if(p.css('position') == 'static' && p.get(0).tagName != "BODY") p.css('position', 'relative');
        
		// have to make absolute
        if(cp != 'absolute' && !isFixed)
			el.css("position", "absolute");
		
		/*var x = (p.width() - el.outerWidth()) / 2;
		var y = (p.height() - el.outerHeight()) / 2;
		
		x += p.scrollLeft();
		y += p.scrollTop();
		*/
		var ml = '-' + el.outerWidth() / 2 + 'px';
		var mt = '-' + el.outerHeight() / 2 + 'px';		

		if(opt.horizontal)
			el.css("left", "50%").css('marginLeft',ml);
    	if(opt.vertical)
			el.css("top", "50%") .css('marginTop', mt);

    });
}

String.prototype.trim = function(){
	this.replace(/^\s+|\s+$/g,"");
	return this;
}	
