/**
 * Wrapper para o qTip 1.0
 * 
 * Algumas das features implementadas nesse wrapper sao:
 * 
 * - escolher um elemento para conter todo o conteudo da tooltip
 *     o wrapper tentará pegar o conteudo do elemento de classe 'tooltip-content-wrapper'
 *     e dentro dele, poderão existir elementos com as classes tooltip-title e tooltip-content,
 *     para que o wrapper extraia o html do title e o content, respectivamente.
 * - especificar content e title em string, nao necessitando passar para objeto.
 * 
 * @version 0.9
 * @author rodolfo42
 */
(function($){
	$.fn.tooltip = function(options){
		// o default é uma tooltip
		// ao lado direito superior do elemento, com o theme padrao
		var config = {
				contentSelector: '.tooltip-content-wrapper',
				contentElement: true
			},
			defaults = {
				content: '',
				style: {
			        tip: 'bottomLeft'
			    },
			    position: {
			        corner: {
			            target: 'topRight',
			            tooltip: 'bottomLeft'
			        },
			    	adjust: {
			    		screen: true
			        }
			    },
			    hide: {
			    	fixed: true
			    }
		};
		
		// faz o padrao de deixar um elemento com o content
		if(!options.contentElement) {
			options.contentElement = config.contentElement;
		}
		
		if( typeof options.position == 'string' ) {
			var trans = {};
			switch(options.position) {
				case 'top':
					trans = {
						style: {
							tip: 'bottomMiddle'
						},
						position: {
							corner: {
								target: 'topMiddle',
					            tooltip: 'bottomMiddle'
							}
						}
					};
					break;
				case 'bottom':
					trans = {
						style: {
							tip: 'topMiddle'
						},
						position: {
							corner: {
								target: 'bottomMiddle',
					            tooltip: 'topMiddle'
							}
						}
					};
					break;
				case 'right':
					trans = {
						style: {
							tip: 'leftTop'
						},
						position: {
							corner: {
								target: 'rightMiddle',
					            tooltip: 'leftTop'
							}
						}
					};
					break;
				default:
					break;
			}
			options = $.extend(true, options, trans);
		}
		
		// merge as options com os defaults
		
		
		// aplica a tooltip para cada elemento
		this.each(function(){
			var opt = $.extend(true, {}, options);
			if(opt.contentElement && (!opt.title && !opt.content)) {
				var contentWrapper = (typeof opt.contentElement == 'boolean' ?
						$(config.contentSelector, this) :
						$(opt.contentElement));
				
				if(!opt.content) {
					opt.content = $('.tooltip-content', contentWrapper).html();
				}
				
				if(!opt.title) {
					opt.title = $('.tooltip-title', contentWrapper).html();
				}
				
				contentWrapper.remove();
				delete opt.contentElement;
				
				if( typeof opt.title == 'string' ) {
					if( typeof opt.content == 'string' ) {
						opt.content = {
							text: opt.content
						};
					}
					opt.content.title = {
						text: opt.title
					};
					delete opt.title;
				}
			}
			
			opt = $.extend(true, {}, defaults, opt);
			
			$(this).qtip(opt);
		});
	};
})(jQuery);

/**
 * Script de inicialização automática das tooltips em
 * - formulários (inputs e buttons)
 * - links
 * - e qualquer elemento que tenha os atributos de inicialização
 * 
 * @author rodolfo42
 */
initFormTooltips = function(context){
	context = context || document;
	
	// preparar os inputs para exemplos de preenchimento
	$('[tooltip-content]', context).each(function(){
		var text = $(this).attr('tooltip-content');
		var options = {};
		var self = $(this);
		
		text = text.split('%br%').join('<br />');
		if(!text.length) return;
		
		options.content = text;
		
		
		if($(context).is('#fancybox-wrap')) {
			options.style = {
					classes: {
						tooltip: 'qtip-calangomusic qtip-fancybox'
				}
			};
		}
		
		if(self.is(':input')) {
			if(self.is(':text,:password')) {
				$(this).addClass('help-available');
			}
			
			if(self.is(':text, :password, select')) {
				options = $.extend(true, {}, options, {
					hide: {
						delay: 1,
						when: {
							event: 'blur'
						}
					},
					show: {
						delay: 1,
						when: {
							event: 'focus'
						}
					},
					style: {
						name: 'gr1Dark'
					}
				});
			}
		}
		
		if($(this).is('[tooltip-theme]')) {
			options.style = { name : $(this).attr('tooltip-theme') };
		}
		
		if($(this).is('[tooltip-width]')) {
			options.style.width = $(this).attr('tooltip-width');
			debugger;
		}
		
		$(this).tooltip(options);
	});
	
};

$(function(){
	initFormTooltips(document);
});
