jQuery(function ($) {
	if (getCookie('entertainer') === '1') {
		$('body').addClass('entertainer-on');
	}
	// activate entertainer
	$('body').delegate('button.toggle-entertainer', 'click', function () {
		var bool = $('body').toggleClass('entertainer-on').is('.entertainer-on');
		$('#entertainer-tooltip').fadeOut();
		if ($('#entertainer-tab').is(':hidden')) {
			$('#entertainer-tab').show('slide', {direction:'right'});
		}
		setCookie('entertainer', bool ? 1 : 0, 7);
	})
	// close tab
	.delegate('#entertainer-tab a.close', 'click', function () {
		$(this).closest('#entertainer-tab').hide('slide', {direction:'right'});
		if ($('body').is('.entertainer-on')) {
			$('button.toggle-entertainer:first').click();
		}
	})
	// close tooltip
	.delegate('#entertainer-tooltip a.close', 'click', function () {
		$(this).closest('#entertainer-tooltip').fadeOut('fast');
	})
	// show tooltip
	.delegate('span.entertainer-hotpoint', 'click', function () {
		var $t = $(this),
			$tt = $('#entertainer-tooltip'),
			$img;
		
		if ($t.is('.current')) {
			$tt.fadeIn('slow');
			return;
		}
		$('span.entertainer-hotpoint').removeClass('current');
		$t.addClass('current');
		// set content and find images
		$img = $tt.find('#entertainer-tooltip-content').html($t.html()).find('img');
		
		// find out if it is gallery
		if ($img.length > 1) {
			$img.wrapAll('<div id="tooltip-gallery"></div>').last().after('<a class="prev"><span>&lt;</span></a><a class="next"><span>&gt;</span></a>');
			$img.hide().eq(0).show();
		}
		
		// find out if there is a flash
		$tt.find('div.flash').each(function () {
			var $flash			= $(this).attr('id', ['flash-', new Date().getTime()].join('')),
				$input			= $flash.children('input:hidden'),
				swfUrl			= $input.filter('[name=swfUrl]').val(),
				divId			= $flash.attr('id'),
				width			= $input.filter('[name=width]').val() || 265,
				height			= $input.filter('[name=height]').val() || width * 2/3,
				version			= $input.filter('[name=version]').val() || '8.0.0',
				expressInstall	= $input.filter('[name=expressInstall]').val() || false,
				flashvars		= false,
				parameters		= false,
				attributes		= false;
			$flash.width(width).height(height);
			// try evaluating the objects:
			try {
				eval('flashvars=' + $input.filter('[name=flashvars]').val());
			} catch(e) {};
			try {
				eval('parameters=' + $input.filter('[name=parameters]').val());
			} catch(e) {};
			try {
				eval('attributes=' + $input.filter('[name=attributes]').val());
			} catch(e) {};
			// insert the code:
			swfobject.embedSWF(swfUrl, divId, width, height, version, expressInstall, flashvars, parameters, attributes);
		});
		
		// show and position
		$tt.fadeIn('slow', function () {
			clearInterval(window.entertainerTimer);
			delete window.entertainerTimer;
		})
		window.entertainerTimer = setInterval(function () {
			$tt.css({
				'left':$t.offset().left + 'px',
				'top':$t.offset().top - ($tt.height() + parseInt($tt.css('paddingBottom'), 10) + parseInt($tt.css('paddingTop'), 10)) + 'px'
			});
		}, 13);
		
	})
	// gallery events
	.delegate('#tooltip-gallery a', 'click', function(){
		var $t = $(this);
		$t.siblings('img:visible').hide()[$t.is('.prev') ? 'cyclePrev' : 'cycleNext']('img').show();
	});	
});
