/*
Copyright 2010 Gerrit Veldman
All rights reserved. See license.txt.
*/
$(document).ready(function() {
	var $loadimage = $('<div></div>').addClass('ajaxloadimage');
	$context = $('<div id="ajaxcontext"></div>').css({
		'position':'absolute',
		'zIndex': '+100'
	})
	.hide()
	.appendTo('body');
	$contextTop = $('<div id="ajaxcontexttop"></div>').appendTo($context);
	$contextContainer = $('<div id="ajaxcontextcontainer"></div>').appendTo($context);
	$contextBottom = $('<div id="ajaxcontextbottom"></div>').appendTo($context);
	$person = $('a[href^=' + CF_BASE + 'person/]');
	$event = $('a[href^=' + CF_BASE + 'event/]');
	$note = $('a[href^=' + CF_BASE + 'note/]');
	$media = $('a[href^=' + CF_BASE + 'media/]');
	$source = $('a[href^=' + CF_BASE + 'source/]');
	$repository = $('a[href^='+ CF_BASE + 'repository/]');
	$.manageAjax.create('context',{
		queue:'clear',
		maxRequests: 1,
		abortOld: true,
		cacheResponse: true,
		preventDoubbleRequests: true
	});
	var linkIn = function(event){
		var border_top = $(window).scrollTop();
		var border_left = $(window).scrollLeft();
		var border_height = $(window).height();
		var border_width = $(window).width();
		var record = $(this).attr('href');
		$contextContainer.empty();
		$context.addClass('ajaxcontexthovering').css({
			'top': event.pageY+5,
			'left': event.pageX+5,
		}).show();
		$loadimage.appendTo($contextContainer);
		$.manageAjax.add('context',{
			'url': CF_BASE_PATH + 'index.php',
			'data': {'a':'ajaxcontext.xml','r':record.replace(CF_BASE,'')},
			'dataType': 'xml',
			'type': 'GET',
			'success': function(data) {
				$loadimage.remove();
				$('<h3></h3>').text($(data).find('title:first').text()).appendTo($contextContainer);
				$links = $('<ul></ul>').appendTo($contextContainer);
				$(data).find('link').each(function() {
					var html = '<a href="' + $(this).find('url').text() + '">' + $(this).find('title').text() + '</a>';
					$('<li></li>').html(html)
					.appendTo($links)
				});
				$stats = $('<ul></ul>').appendTo($contextContainer);
				$(data).find('stat').each(function() {
					var html = $(this).find('title').text() + ': ' + $(this).find('value').text();
					$('<li></li>').html(html)
					.appendTo($stats)
				});
				if (event.pageY-border_top > border_height/2) {
					$context.css('top',event.pageY-5-$context.height());
				}
				else {
					$context.css('top',event.pageY+5);
				}
				if (event.pageX-border_left > border_width/2) {
					$context.css('left',event.pageX-5-$context.width());
				}
				else {
					$context.css('left',event.pageX+5);
				}
			},
			'error': function() {
				$loadimage.remove();
				$contextContainer.empty();
				$context.hide();
			}
		});
		event.stopPropagation();
	};
	var linkOut = function(event){
		$context.removeClass('ajaxcontexthovering');
	};
	var linkOutIntent = function(event){
		if (!$context.hasClass('ajaxcontexthovering')) {
			$context.hide();
		}
	};
	var contextIn = function(event){
		$context.addClass('ajaxcontexthovering').show();
	};
	var contextOut = function(event){
		$context.removeClass('ajaxcontexthovering').hide();
	};
	var settings = {
		sensitivity:7,
		interval:100,
		over: linkIn,
		timeout:500,
		out: linkOutIntent
	};
	$person.hoverIntent(settings).mouseleave(linkOut);
	$event.hoverIntent(settings).mouseleave(linkOut);
	$note.hoverIntent(settings).mouseleave(linkOut);
	$media.hoverIntent(settings).mouseleave(linkOut);
	$source.hoverIntent(settings).mouseleave(linkOut);
	$repository.hoverIntent(settings).mouseleave(linkOut);
	$context.hoverIntent({
		sensitivity:100,
		interval:100,
		over: contextIn,
		timeout:100,
		out: contextOut
	});
	//$context.hover(contextIn,contextOut);
});
