/* 
	jQuery addCaption plugin
	Copyright 2008 by Paul Boutin 
	Licensed under the Creative Commons Attribution 3.0 license
	Version 1.0.0
	http://www.redsgn.com/category/jquery/
	
*/
(function($){
 $.fn.addcaption = function(options) {
  var defaults = {
	  align:'display:block;float:left;',
	  textalign:';',
	  width:'width:auto;',
	  border:';',
	  margin:";",
	  marginleft:";",
	  marginright:";",
	  margintop:";",
	  marginbottom:";"
  };
  var options = $.extend(defaults, options);
    
  return this.each(function() {
   if($(this).attr('title')){
	   var p = $(this).parent();
	   var margin = options.margin;
	   if($(this).css('margin')){
	   		margin = 'margin:'+$(this).css('margin')+";";
			//$(this).css('margin', 0);
	   }
	   var marginleft = options.marginleft;
	   if($(this).css('margin-left') != '0px'){
	   		marginleft = 'margin-left:'+$(this).css('margin-left')+";";
			//$(this).css('margin-left', 0);
	   }
	   var marginright = options.marginright;
	   if($(this).css('margin-right') != -'0px'){
	   		marginright = 'margin-right:'+$(this).css('margin-right')+";";
			//$(this).css('margin-right', 0);
	   }
	   var margintop = options.margintop;
	   if($(this).css('margin-top') != '0px'){
	   		margintop = 'margin-top:'+$(this).css('margin-top')+";";
			//$(this).css('margin-top', 0);
	   }
	   var marginbottom = options.marginbottom;
	   if($(this).css('margin-bottom') != '0px'){
	   		marginlbottom = 'margin-bottom:'+$(this).css('margin-bottom')+";";
			//$(this).css('margin-bottom', 0);
	   }
	   var align = options.align;
	   if($(this).css('float') && ($(this).css('float').toLowerCase() == 'right' || $(this).css('float').toLowerCase() == 'left')){
	   		align = 'display:block;float:'+$(this).css('float').toLowerCase()+';';
	   }
	   if($(this).attr('align') && ($(this).attr('align').toLowerCase() == 'right' || $(this).attr('align').toLowerCase() == 'left')){
	   		align = 'display:block;float:'+$(this).attr('align').toLowerCase()+';';
	   		$(this).removeAttr('align');
	   }
	   var textalign = options.textalign;
	   if(p.css('text-align') && (p.css('text-align').toLowerCase() == 'right' || p.css('text-align').toLowerCase() == 'left' || p.css('text-align').toLowerCase() == 'center')){
	   		textalign = 'text-align:'+p.css('text-align').toLowerCase()+';';
	   		p.css('text-align', '');
	   }
	   var width = options.width;
	   if($(this).attr('width')){
	   		width = 'width:'+$(this).attr('width')+'px;';
	   }
	   var border = options.border;
	   if($(this).attr('border')){
	   		border = 'border:'+$(this).attr('border')+'px solid #000;';
			$(this).removeAttr('border');
	   }
	   $(this).attr('style','margin:auto;border:none;');
	   $(this).wrap('<span style="'+border+align+textalign+margin+marginleft+marginright+margintop+marginbottom+width+'loy"></span>');   
	   $(this).parent('span').append('<span class="imageCaption">' + $(this).attr('title') + '</span>');
   	   if($(this).parent('span').parent('a')){
			if($.browser.msie){
				$(this).parent('span').attr('style',$(this).parent('span').attr('style') + 'cursor:hand;');
				$(this).parent('span').parent('a').attr('style','text-decoration:none;cursor:hand;');
			}else{
				$(this).parent('span').attr('style',$(this).parent('span').attr('style') + 'cursor:pointer;');
				$(this).parent('span').parent('a').attr('style','text-decoration:none;cursor:pointer;');
			}
	   }
	}
  });
 };
})(jQuery);
