 /*
 * @author Renan Vaz
 * @version 1.0
 * @example
 * $("element").focusInOut({
 *
 * 		textIn: 'texto inicial',
 *		textOut: 'texto com focus'
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param text textIn, text textOut
 * @contribution Renan Vaz
 *
 */
function $r(id){
	return document.getElementById(id);
}
jQuery.fn.focusInOut = function(params) {
		var options = {
			
			textIn: null,
			textOut: '',
			borderIn: '',
			borderOut: '',
			borderTopOut: '',
			borderTopIn: '',
			borderRightIn: '',
			borderRightOut: '',
			borderBottomIn: '',
			borderBottomOut: '',
			borderLeftIn: '',
			borderLeftOut: '',
			bgIn: '',
			bgOut: '',
			colorIn: '',
			colorOut: ''
		}
		
   return this.each(function(){
		var $self = jQuery(this);
		options.borderTopIn 	= $self.css('border-top-style')+' '+$self.css('border-top-width')+' '+toHex($self.css('border-top-color'));
		options.borderTopOut 	= $self.css('border-top-style')+' '+$self.css('border-top-width')+' '+toHex($self.css('border-top-color'));
		options.borderRightIn 	= $self.css('border-right-style')+' '+$self.css('border-right-width')+' '+toHex($self.css('border-right-color'));
		options.borderRightOut 	= $self.css('border-right-style')+' '+$self.css('border-right-width')+' '+toHex($self.css('border-right-color'));
		options.borderBottomIn 	= $self.css('border-bottom-style')+' '+$self.css('border-bottom-width')+' '+toHex($self.css('border-bottom-color'));
		options.borderBottomOut = $self.css('border-bottom-style')+' '+$self.css('border-bottom-width')+' '+toHex($self.css('border-bottom-color'));
		options.borderLeftIn 	= $self.css('border-left-style')+' '+$self.css('border-left-width')+' '+toHex($self.css('border-left-color'));
		options.borderLeftOut 	= $self.css('border-left-style')+' '+$self.css('border-left-width')+' '+toHex($self.css('border-left-color'));
		options.bgIn 			= toHex($self.css('background-color'));
		options.bgOut 			= toHex($self.css('background-color'));
		options.colorIn 		= toHex($self.css('color'));
		options.colorOut 		= toHex($self.css('color'));
		var op = jQuery.extend(options, params);
		
		//initializing variables
		
		if(op.textIn == null)	op.textIn = $self.val();
		 $self.attr('textIn',op.textIn);
		 $self.attr('textOut',op.textOut);
		 if($self.val()=='')
		 $self.val(op.textIn);
		 $self.focus(function (){
				if(op.borderOut != ''){
					$self.css('border',op.borderOut);
				}else{
					$self.css('border-top',op.borderTopOut);
					$self.css('border-right',op.borderRightOut);
					$self.css('border-bottom',op.borderBottomOut);
					$self.css('border-left',op.borderLeftOut);
				}
				$self.css('background-color',op.bgOut);
				$self.css('color',op.colorOut);
				if(jQuery.trim($self.val()) == op.textIn) $self.val(op.textOut)
			});
		 $self.blur(function (){
				if(op.borderIn != ''){
					$self.css('border',op.borderIn);
				}else{
					$self.css('border-top',op.borderTopIn);
					$self.css('border-right',op.borderRightIn);
					$self.css('border-bottom',op.borderBottomIn);
					$self.css('border-left',op.borderLeftIn);
				}
				$self.css('background-color',op.bgIn);
				$self.css('color',op.colorIn);
				if(jQuery.trim($self.val()) == op.textOut) $self.val(op.textIn)
			});
   });
};
/**
 * @author Renan Vaz
 * @version 1.0
 * @example
 * $("input[@type=checkbox]").check(); $("input[@type=radio]").uncheck();
 * 
 * @contribution Renan Vaz
 *
 */
jQuery.fn.extend({ 
	check: function() { 
	return this.each(function() { 
	this.checked = true; }); }, 
	uncheck: function() {
	return this.each(function() { 
	this.checked = false; }); } 
}); 
/**
 * @author Renan Vaz
 * @version 1.0
 * @example
 * $("input").countChars(40);
 * @obs With no arguments, the default is 30, the span have like id='id input'+limit
 * @license free
 * @param number p (max chars)
 * @contribution Renan Vaz
 *
 */
jQuery.fn.countChars = function(p) {
	var params = {
			limit: p
		}
	var options = {
			limit: 30
		}
		var fnCount = function($this){
				var tamanho = $this.value.length;
				var resto   = Number(jQuery($this).attr('MaxCh')) - tamanho;
				resto 		= resto > 0 ?resto : '0'; 
				var obj     = jQuery($this).attr('id')+'limit';
				jQuery('#'+obj).html(resto);
			};
		op = jQuery.extend(options, params);
   return this.each(function(){
		//initializing variables
		var $self   = jQuery(this);
		$self.attr('MaxCh',op.limit);
		$self.attr('maxlength',op.limit);
		fnCount(this);
	$self.keyup(function (){fnCount(this)});
	$self.blur(function (){fnCount(this)});
	$self.parents("form").each(function() {
		//Bind parent form submit
		$self.submit(function() {
			if($self.val()==text) {
				$self.val('');
			}
		});
	});
   });
};
jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = jQuery(this).offset().top;
      jQuery('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

/**
 * @author Renan Vaz
 * @version 1.0
 * @example
 * $("input").countChars(40);
 * @obs With no arguments, the default is 30, the span have like id='id input'+limit
 * @license free
 * @param borderColor String (hex) e.g default is '#c00'
 * @param borderOut   String (hex) e.g default is '2px'
 * @param borderOver  String (hex) e.g default is '4px'
 * @param clear		  String (hex) e.g default is 'both' clear float
 * @contribution Renan Vaz
 * usage:
 * $(function(){
 * $('a').innerBorder({clear:'right'});
 * });
 */

jQuery.fn.innerBorder = function(params) {
		var options = {
			borderColor: '#c00',
			borderOut: '2px',
			borderOver: '4px',
			clear: 'both'
		}
		op = jQuery.extend(options, params);
	 return this.each(function() {
		var $self = jQuery(this);
		$self.attr('innerBorder','yes');
		jQuery(this).css({float: 'left', overflow:'hidden',clear:op.clear});
		jQuery('[innerBorder=yes]').hover(function(){jQuery('img',this).css('margin','-'+op.borderOver);jQuery(this).css({border:op.borderOver+' solid '+op.borderColor})},function(){jQuery('img',this).css('margin','-'+op.borderOut);jQuery(this).css({border:op.borderOut+' solid '+op.borderColor})});
		jQuery('img',this).css('margin','-'+op.borderOut);
		jQuery(this).css({border:op.borderOut+' solid '+op.borderColor})
	});
}

jQuery.fn.isChrome = function(){
	return  navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
}

//-------------------------------------------------------------------------
//@param font = font path  ex: fonts/arial.swf
//@param css array [ font-style:italic; display: inline;] apenas adicionar mais atributos a classe principal 
//@param cssAdd array [em { font-style:italic; display: inline;}] adicionar novas classes
// depende do plugin flash do jquery
jQuery.fn.sifrRvaz = function(params) {
	var options = {
			css 	: [],
			cssAdd  : [],
			font 	: null,
			debug	:false
	}
	op = jQuery.extend(options, params);
	if(op.debug)
	jQuery('body').append('<div id="debugSifrRvaz"></div>');

	return this.each(function() {
		var $self 			= jQuery(this);
		var cssDefault 		= new String();
		var color 			= 'color:'			+ toHex($self.css('color'))		+';';
		var size  			= 'font-size:'		+ ($self.css('font-size'))		+';';
		var font_weight 	= 'font-weight:'	+ ($self.css('font-weight'))	+';';
		var font_style 		= 'font-style:'		+ ($self.css('font-style'))		+';';
		var line_height 	= 'line-height:'	+ ($self.css('line-height'))	+';';
		var margin_top 		= 'margin-top:'		+ ($self.css('margin-top'))		+';';
		var margin_right 	= 'margin-right:'	+ ($self.css('margin-right'))	+';';
		var margin_bottom 	= 'margin-bottom:'	+ ($self.css('margin-bottom'))	+';';
		var margin_left		= 'margin-left:'	+ ($self.css('margin-left'))	+';';
		var padding_top 	= 'padding-top:'	+ ($self.css('padding-top'))	+';';
		var padding_right 	= 'padding-right:'	+ ($self.css('padding-right'))	+';';
		var padding_bottom 	= 'padding-bottom:'	+ ($self.css('padding-bottom'))	+';';
		var padding_left	= 'padding-left:'	+ ($self.css('padding-left'))	+';';
		var index = -1;
		for(i = 0; i<op.css.length; i++){
			var str = op.css[i];
			if(str.indexOf('font-size') > -1){
				index = i;
				break;
			}
		}
		if(index > -1 )
		$self.css('font-size',op.css[index].replace(/\D/g,'')+'px');
		var height			= Number($self.css('font-size').replace(/\D/g,''));
		var margin 			= margin_top+'	'+margin_right+'	'+margin_bottom+'	'+margin_left;
		var padding 		= padding_top+'	'+padding_right+'	'+padding_bottom+'	'+padding_left;//'+margin+' '+padding+'
		cssDefault 			= '* { '+color+' '+size+' '+font_weight+' '+font_style+' '+line_height+'  '+(op.css.join(' '))+' }';
		if(op.debug)
		jQuery('#debugSifrRvaz').html(jQuery('#debugSifrRvaz').html()+'('+this.id+')<div>'+cssDefault+'</div>');
		$self.flash(
			{ 
				src: op.font || 'default.swf', 
				flashvars: { 
					css: cssDefault + 'em { font-style:italic; display: inline;} strong { font-weight:bold; display: inline; } ' + (op.cssAdd.join(' '))
				}
			},
			{ version: 8 },
			function(htmlOptions) {
				htmlOptions.flashvars.txt = jQuery(this).html();
				jQuery(this).html('<div>'+this.innerHTML+'</div>');
				var $alt = jQuery(this.firstChild);
				htmlOptions.height = ($alt.height() == 0 ? $alt.css('font-size'):$alt.height()) +15;
				htmlOptions.width  = $alt.width();
				htmlOptions.scale  = 'noscale';
				htmlOptions.wmode  = 'transparent';
				$alt.addClass('alt');
				jQuery(this)
					.addClass('flash-replaced')
					.prepend(jQuery.fn.flash.transform(htmlOptions));						
			}
   		 );
	});
};

/////////////////////////////////////////////////////////////////////
var toHex = function (c) {
				var h = function (n) {
					if (n === 0 || isNaN(n)) {
						return '00';
					}
					n = Math.round(Math.min(Math.max(0, n), 255));
					return '0123456789ABCDEF'.charAt((n - n % 16) / 16) + '0123456789ABCDEF'.charAt(n % 16);
				};
				c = (c) ? c.replace(/rgb|\(|\)|#$/g, '') : false;
				if (!c) {
					return false;
				}
				if (c.indexOf(',') > -1) {
					c = c.split(', ');
					return '#' + h(c[0]) + h(c[1]) + h(c[2]);
				}
				if (c.search('#') > -1 && c.length <= 4) {
					c = c.split('');
					return '#' + c[1] + c[1] + c[2] + c[2] + c[3] + c[3];
				}
				return c;
		}
