var tooltips = {
	//off: true,
	
	create: function(id, callback) {
		var run = false;
		
		tooltips[id] = function(thisObj, options) {
			if (tooltips.off === true || run) {
				return;
			}
			run = true;
			options = options || {};
			
			var ret = callback.call(thisObj, options);
			_.extend(options, ret);
			options.el = $(options.el);
			
			if (options.remove) {
				var removeEl = $(options.remove[0]),
					event = options.remove[1],
					check = options.remove[2];
				
				setTimeout(function() {
					removeEl.bind(event + '.' + id, function(e) {
						if (check && !check(e)) {
							return;
						}
						
						options.el.tipsy('hide');
						
						if (options.count != undefined && --options.count != 0) {
							return;
						}
						
						removeEl.unbind(event + '.' + id);
					});
				}, 20);
			}
			
			options.el.tipsy(options.tipsy);
		}
	},
	
	list: {}
}
tooltips.create('unstable', function(options) {
	return {
		el: $('#title'),
		remove: [document, 'mousedown'],
		tipsy: {
			trigger: 'now',
			gravity: 'n',
			fallback: 'Alpha version, program is still under heavy development and most features are not implemented',
			html: true
		}
	};
});

tooltips.create('start', function(options) {
	tooltips.unstable();
	
	var search = $('#search-bar');
	return {
		el: search,
		remove: [search, 'focus'],
		tipsy: {
			trigger: 'now',
			gravity: 'n'
		}
	};
});

tooltips.create('linksA', function(options) {
	var text = 
			'You can create shortcut links for artists, albums, tracks and tags that\'ll ' +
			'make it easier for you to look up the artist/album/etc. on external sites',
		$button = this.artistLinks.$button,
		once = true;
	
	return {
		el: this.artistLinks.el,
		count: 2,
		remove: [document, 'mousedown', function(e) {
			var isButton = $(e.target).closest($button).length,
				ret = !isButton && once;
			
			if (isButton && once) {
				once = false;
			}
			
			//console.log(!isButton);
			return !isButton;
		}],
		tipsy: {
			trigger: 'now',
			gravity: 'e',
			fallback: text
		}
	}
});

tooltips.create('linksB', function() {
	var text = 'Create your own links by going to the Links tab on the control box on the right',
		tip = $(this.artistLinks.el).tipsy(true);
	
	tip.change(text);
	!tip.visible && tip.show();
	
	setTimeout(tip.hide, 5000);
	
	return {
		el: controlBox.tab[1],
		remove: [document, 'mousedown'],
		tipsy: {
			trigger: 'now',
			gravity: 's',
			className: 'linksB',
			fallback: 'Here!',
			offset: 32
		}
	}
});

tooltips.create('playlist', function() {
	controlBox.switchTab(controlBox.tab[0]);
	
	var text =
		'You can order the tracks by dragging them up and down' + '<br/>' +
		'Remove a track by dragging it off the playlist';
	
	return {
		el: controlBox.playlist.row[0],
		remove: [document, 'mousedown'],
		tipsy: {
			trigger: 'now',
			gravity: 'e',
			fallback: text,
			html: true
		}
	}
});

tooltips.create('drag', function() {
	var text =
		'You can scroll around by dragging on an empty space like here' + '<br/>' +
		'Or by using the movement arrows that apear near the edges of this area';
	
	return {
		el: controlBox.el,
		remove: [document, 'mousedown'],
		tipsy: {
			trigger: 'now',
			gravity: 'w',
			offset: -400,
			fallback: text,
			html: true
		}
	}
})

tooltips.list.linksA = function(self) {
	if (self.consts.id++ != 0) {
		return;
	}
	var text = 'You can create shortcut links for artists, albums, tracks and tags that\'ll make it easier for you to look up the artist/album/etc. on external sites';
	
	var $artistLinks = $(self.artistLinks.el),
		$button = self.artistLinks.$button;
	
	$artistLinks.tipsy({
		trigger: 'now',
		gravity: 'w',
		fallback: text
	});
	
	$button.bind('click.tooltip', function() {
		$(this).unbind('.tooltip');
		tooltips.create('linksB', $artistLinks);
	});
	
	$(document).bind('mousedown.tooltip', function(e) {
		if (!$(e.target).closest($button).length) {
			$artistLinks.tipsy('hide');
			$(document).unbind('.tooltip');
		}
	});
	
	$(self.tab[1]).click(function() {
		$(self.el).tipsy({
			trigger: 'now',
			gravity: 'w',
			html: true,
			fallback: 'Click on a track to add it to the playlist<br/>MusicTree uses youtube to find and play songs'
		});
	});
}

tooltips.list.linksB = function($artistLinks) {
	var tip = $artistLinks.tipsy(true);
	tip.change('Create your own links by going to the Links tab on the control box on the right');
	
	if (!tip.visible) {
		tip.show();
	}
	
	var $links = $(controlBox.tab[1]).tipsy({
		trigger: 'now',
		gravity: 's',
		className: 'linksB',
		fallback: 'Here!',
		offset: 32
	});
		
	$(document).bind('mousedown.tooltip', function() {
		tip.hide();
		$links.tipsy('hide');
		
		$(document).unbind('.tooltip');
	});
}
