/*
 * Animated multilevel right-click context menu
 * (C) 2011 Sawanna Team (http://sawanna.org)
 */

$(document).ready(function(){
	contextMenu.init();
});

contextMenu={
	handler: document,
	elem: 'ul#context-menu',
	locked: 0,
	state: 0,
	speed: 300,
	offset: 10,
	init: function() {
		$(this.elem).appendTo('body');
		
		$(this.handler).bind('contextmenu',function(e){
			if (e.preventDefault) {
				e.preventDefault();
			} else {
				e.returnValue= false;
			}
			if (!contextMenu.state && !contextMenu.locked) {
				contextMenu.show(e);
			}
			
			return false;
		});
		$(document).bind('click',function(e){
			if (contextMenu.state && !contextMenu.locked) {
				contextMenu.hide();
				return false;
			}
			return true;
		});
		$(this.elem).bind('mousedown',function(e){
			contextMenu.locked=1;
		});
		$(this.elem).bind('mouseup',function(e){
			window.setTimeout("contextMenu.locked=0;",500);
		});
		$(this.elem).find('a').bind('click',function(e){
			contextMenu.hide();
			return true;
		});
		$(this.elem).find('a').bind('mouseover',function(e){
			contextMenu.showChild($(this));
		});
		$(this.elem).find('li').each(function(){
			if ($(this).children('ul').length) {
				$(this).addClass('parent');
				$(this).children('a').addClass('parent');
			}
		});
	},
	show: function(e) {
		$(this.elem).find('ul').css('display','none');
		$(this.elem).find('li').children('a').removeClass('active');
		
		this.locked=1;
		this.fixPos(e);
		$(this.elem).fadeIn(this.speed,function(){
			contextMenu.state=1;
			contextMenu.locked=0;
		});
	},
	hide: function() {
		this.locked=1;
		$(this.elem).fadeOut(this.speed,function(){
			contextMenu.state=0;
			contextMenu.locked=0;
			$(this).find('ul').css('display','none');
		});
	},
	fixPos: function(e) {
		var deltaX=0;
		var deltaY=0;
		if (e.pageX+this.offset+$(this.elem).width() >= $(window).width()+$(window).scrollLeft()) {
			deltaX=-($(this.elem).width()+2*this.offset);
		}
		if (e.pageY+this.offset+$(this.elem).height() >= $(window).height()+$(window).scrollTop()) {
			deltaY=-($(this.elem).height()+2*this.offset);
		}
		$(this.elem).css({
			'left':e.pageX+this.offset+deltaX,
			'top':e.pageY+this.offset+deltaY
		});
	},
	showChild: function(el) {
		$(this.elem).find('ul').css('display','none');
		$(el).parents('ul').css('display','block');
		$(this.elem).find('li').children('a').removeClass('active');
		$(el).parents('li').children('a').addClass('active');
		if ($(el).parent('li').children('ul').length) {
			this.fixChildPos($(el).parent('li').children('ul'));
			$(el).parent('li').children('ul').fadeIn(this.speed);
		}
	},
	fixChildPos: function(elem) {
		var deltaX=0;
		var deltaY=0;
		if ($(elem).parent('li').parent('ul').offset().left+$(elem).parent('li').parent('ul').width()+$(elem).width() >= $(window).width()+$(window).scrollLeft()) {
			deltaX=-($(elem).width()+$(elem).parent('li').parent('ul').width());
		}
		if ($(elem).parent('li').offset().top+$(elem).height() >= $(window).height()+$(window).scrollTop()) {
			deltaY=-($(elem).height())+$(elem).parent('li').height();
		}
		$(elem).css({
			'left':$(elem).parent('li').parent('ul').width()+deltaX,
			'top':$(elem).parent('li').offset().top-$(elem).parent('li').parent('ul').offset().top+deltaY
		});
	}
}

