this.vtip = function() {    
    this.xOffset = -10; // x distance from mouse
    this.yOffset = 10; // y distance from mouse       
    
    $(".tooltip").unbind().hover(    
        function(e) {
            this.t = this.title;
            this.title = ''; 
            this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
            
            $('body').append( '<p id="tooltip">' + this.t + '</p>' );
            //  $('body').append( '<p id="tooltip"><img id="vtipArrow" />' + this.t + '</p>' );
			
            $('p#tooltip #vtipArrow').attr("src", 'images/vtip_arrow.png');
            $('p#tooltip').css("top", this.top+"px").css("left", this.left+"px").fadeIn("slow");
            
        },
        function() {
            this.title = this.t;
            $("p#tooltip").fadeOut("slow").remove();
        }
    ).mousemove(
        function(e) {
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
                         
            $("p#tooltip").css("top", this.top+"px").css("left", this.left+"px");
        }
    );            
    
};

var modal = null;
var tmpmodal = null;

jQuery(document).ready(function() {

	jQuery("div.bubble-navigation").hover(
 		function () {
  		    jQuery(this).addClass("bubble-navhover");
 		},
 		function () {
   			jQuery(this).removeClass("bubble-navhover");
 		}
	);
	
	jQuery("div.bubble-short").hover(
 		function () {
  		    jQuery(this).addClass("bubble-shorthover");
 		},
 		function () {
   			jQuery(this).removeClass("bubble-shorthover");
 		}
	);
	
	vtip(); 
	bubbleStartAni();
	
	$.fn.nyroModal.settings.minHeight = 200;
	$.fn.nyroModal.settings.endFillContent = modalImageGal;
	$.fn.nyroModal.settings.processHandler = bubbleStopAni;
	$.fn.nyroModal.settings.endRemove = bubbleStartAni;

});

function bubbleStopAni (settings)
{
	$('div.bubble').stop(true);
	$('div.bubble').unbind();
	$('div.bubble-navigation').stop(true);
	$('div.bubble-navigation').unbind();
}

function bubbleStartAni()
{
	$('div.bubble').each(function () {
		if ($(this).attr('origtop') == undefined)
		{
			var pos = $(this).position();
			$(this).attr('origtop',pos.top);
			$(this).attr('origleft',pos.left);
		}
		if(this.id!='centerbubble') {
			$(this).hover(function() {
				$(this).stop();
			},function() {
				var origtop = $(this).attr('origtop');
				var origleft = $(this).attr('origleft');
				$(this).animate({
					top: origtop,
					left: origleft
				},2000,easingmethod, function() {$(this).attract(true); });
			}).attract(true);
		}
	});
	$('div.bubble-navigation').each(function() {
		$(this).hover(function() {
			$(this).stop();
		}, function() {
			$(this).animnavi('init');
		}).animnavi('init');
	});
}

function modalImageGal (elts, settings)
{
	if ($('a.nyroModal',elts.content).length == 0)
		return;
	// create temp store
	$('body:last-child').after('<div id="modaltmp" style="display: hidden"></div>');
	modaltmp = $('#modaltmp');
	// register click event on modal images in a modal window
	$('a.nyroModal',elts.content).click(function() {
		// user clicked on a modal link, but a modal is allready loaded
		modal = settings.from;
		// save textpic element for later use in new modal window
		$('div.csc-textpic',elts.content).clone().appendTo(modaltmp);
		// register close handler
		$.fn.nyroModal.settings.endRemove = modelImageGalRestore;
	});
}

function modelImageGalRestore (elts, settings)
{
	// cleanup
	if (modaltmp)
	{
		$(modaltmp).remove();
		modaltmp = null;
	}
	// click on old modal
	if (modal)
	{
		$(modal).click();
		modal = null;
	}
	// deregister close handler
	$.fn.nyroModal.settings.endRemove = null;
	return;
}

$.fn.animnavi = function (goleft) {
	switch(goleft) {
		case 'left':
			$(this).animate({
				left: '13px'
			},2000,easingmethod,function() { $(this).animnavi('right'); });
			break;
		case 'right':
			$(this).animate({
				left: '18px'
			},2000,easingmethod,function() { $(this).animnavi('left'); });
			break;
		default:
			var pos = $(this).position();

			if (pos.left < 15) {
				$(this).animate({
					left: '18px'
				},1000,easingmethod,function() { $(this).animnavi('left'); });
			
			} else {
				$(this).animate({
					left: '13px'
				},1000,easingmethod,function() { $(this).animnavi('right'); });
			}
			break;
	}

}

$.fn.attract = function(attract) {
	if (!main) main = getCenter('div#centerbubble');
	var me = getCenter(this);
	if (attract) attract = false;
	else attract = true;

	var isleft = true;
	var istop = true;
	if (me.x > main.x) {
		isleft = false;
	} 
	if (me.y > main.y) {
		istop = false;
	} 


	//console.log(collision);

	if (!attract) {	
		var moveByX = '+=40px';
		var moveByY = '+=20px';
		if (isleft) moveByX = '-=50px';
		if (istop) moveByY = '-=15px';
	} else {
		var moveByX = '-=40px';
		var moveByY = '-=20px';
		if (isleft) moveByX = '+=50px';
		if (istop) moveByY = '+=15px';
	}
	
	var time = Math.random()*1000;
		$(this).animate({
				top: [moveByY, 'swing'],
				left: [moveByX, 'jswing']
			},time+2000,function() { $(this).attract(attract); 
		});

	

}

function getCenter(o) {
	var w = parseInt($(o).css('width'));
	var h = parseInt($(o).css('height'));
	var pos = $(o).position();
	//console.log([o,w,h,pos]);
	var myrad = h/2-20;
	var myx = w/2+pos.left;
	var myy = h/2+pos.top;
 	return ({x: myx,y: myy, rad: myrad,position: pos});
}

function getConfig (rel) {
	var j = Bubbles.length;
	for(var i=0;i<j;i++) {
		if (Bubbles[i].id==rel) return(Bubbles[i]);
	}
}

var main = false;
var animdone = false;
var easingmethod = 'jswing';

