function deletClass(source) {
	$(source).removeClass('show');
}
function addClass(source) {
	$(source).addClass('show');
}

// define new object
var jBalloon =  {
	cointainer: null,
	imageSelector: null,
	showingTime: 450,
	hidingTime: 450,
	tickTime: 300,
	intervalID: null,
	keepAliveCounter: 0,
	keepAliveHealth: 3,
	isClicked: 0,
	isLoad: 0,
	init: function(selector) {
		jBalloon.cointainer = selector;
		jBalloon.imageSelector = jBalloon.cointainer + ' img';
		jBalloon.closeIcon = jBalloon.cointainer + ' .tarif_close a';
		jQuery(jBalloon.closeIcon).bind('click',jBalloon.onClose);
		jQuery(jBalloon.cointainer).bind('mousemove',jBalloon.keepAlive)
					   .bind('mouseover',jBalloon.keepAlive)
					   .bind('mouseleave',jBalloon.startTimer)
					   .bind('mouseenter',jBalloon.stopTimer);
		
	},
	click:  function(obj) {
		jBalloon.isClicked = 1;
		jBalloon.stopTimer();
		jBalloon.loadImage(obj);
	},
	tickerHandler: function() {
		if(jBalloon.isClicked) return;
		if(!jBalloon.keepAliveCounter) {
			jBalloon.close();
		}
		if(jBalloon.intervalID && (jBalloon.keepAliveCounter < 0)) {
			jBalloon.stopTimer();
		}
		jBalloon.keepAliveCounter--;
	},
	stopTimer: function() {
		if(jBalloon.isClicked) return;
		if(!jBalloon.intervalID) {
			return ;
		}
		clearInterval(jBalloon.intervalID);
		jBalloon.intervalID = null;
	},
	startTimer: function() {
		if(jBalloon.isClicked) return;
		if(!jBalloon.intervalID) {
			jBalloon.intervalID = setInterval(jBalloon.tickerHandler,jBalloon.tickTime);
		}
	},
	mouseEnter: function(obj) {
		if(jBalloon.isClicked) return;
		jBalloon.isLoad = 1;
		jBalloon.keepAlive();
		jBalloon.stopTimer();
		jBalloon.loadImage(obj);
	},
	keepAlive: function() {
		jBalloon.keepAliveCounter = jBalloon.keepAliveHealth;
	},
	prepare: function(obj){
		jBalloon.setPosition(obj);
		jBalloon.inheriteAttributes(obj);
		
	},
	onClose: function() {
		jBalloon.isClicked = 0;
		jBalloon.close();
	},
	close: function() {
		jQuery(jBalloon.cointainer).fadeOut(jBalloon.hidingTime);
	},
	show: function()
	{
		jQuery(jBalloon.cointainer).fadeIn(jBalloon.showingTime);
	},
	setPosition: function(obj) { 
		var pos =  $(obj).offset();
		var offset = {top:0,left:0};
		with(Math) {
			offset.top = ceil($(obj).height()/2 + 2);
			offset.left = ceil($(obj).width()/2 + 1);
			pos.top = ceil(pos.top);
			pos.left = ceil(pos.left);
		}
		$(jBalloon.cointainer).css('top',pos.top + offset.top + 'px').css('left',pos.left + offset.left + 'px');
		
	},
	inheriteAttributes: function(obj) {
		$(jBalloon.imageSelector).attr('alt', $(obj).text());
		$(jBalloon.cointainer + ' .tarif_auto').html($(obj).text());
	},
	getImageSRC: function(obj) {
		return '/images/timg_' + ($(obj).attr('rel') ? $(obj).attr('rel') : 'noauto')  + '.jpg';
	},
	loadImage: function(obj) {
		if($.browser.msie) {
			jBalloon.doOperations(obj);
			return;
		}
		var i = new Image();
		i.src = jBalloon.getImageSRC(obj);
		jQuery(i).bind('load',function(){
			jBalloon.doOperations(obj);
		})
	},
	doOperations: function(obj) {
		jBalloon.prepare(obj);
		jBalloon.swapImage(obj);
		jBalloon.show(obj);
		
	},
	swapImage: function(obj)
	{
		if(!jBalloon.isLoad) return;		
		$(jBalloon.imageSelector).attr('src',jBalloon.getImageSRC(obj));
	}
}

$(document).ready(function() {
	jBalloon.init('#tarif_img');
	$('.price a').each(function() {
		$(this).bind('click', function(){ jBalloon.click(this); })
		  .bind('mouseenter',function(){ jBalloon.mouseEnter(this); })
		  .bind('mousemove',function(){ jBalloon.keepAlive(); })
		  .bind('mouseover',function(){ jBalloon.keepAlive(); })
		  .bind('mouseleave',function(){jBalloon.startTimer(); });
		
	});
});

