// popup.js
// Display/hide an element when hovering over another element

var popup_currrent_obj = null;
 
;(function($) {

	var ver = '1.0';
	var popup_in_focus = false;
	var parent_in_focus = false;

	$.fn.popup = function(options) {
		
		var defaults = {  
			delay: 500,
			fade_duration: 'fast',
			popup_item: ".popup",  
			onhover: true,
			onclick: false,
			offset: -2,
			offsetTop: -2,
			offsetLeft: -2,
			offsetRelative: false,
			callback: null
		};  
		var options = $.extend(defaults, options); 

		return this.each(function(){
			
			var obj = $(this);
			var popup_element = $( options.popup_item, obj );
			var popup_currrent_obj = null;
			
			if( options.onhover )
			{
				obj.hover(
					function(e) {
						if( !obj.parent_in_focus )
						{
							obj.parent_in_focus = true;
							if (popup_currrent_obj && popup_element != popup_currrent_obj )
								popup_currrent_obj.fadeOut( options.fade_duration );
							popup_currrent_obj = popup_element;
							if( options.offsetRelative && options.offsetLeft && options.offsetTop ) {
								popup_element.css('left', options.offsetLeft );
								popup_element.css('top', options.offsetTop );
							} else if( options.offsetLeft && options.offsetTop ) {
								popup_element.css('left', e.pageX - this.offsetLeft + options.offset );
								popup_element.css('top', e.pageY - this.offsetTop + options.offset );
							}
							zIndexFix(popup_element);
							popup_element.fadeIn(options.fade_duration);
							
							if( options.callback ) {
								if( this.id == '' ) {
									var link = $('a', this);
									var data = { href: link.attr('href'), title: link.attr('title'), id: this.id};
									options.callback.call( this, data );
								} else {
									var data = { href: '', title: '', id: this.id};
									options.callback.call( this, data );
								}
								//eval( options.callback + "('"+ this.id +"');");
							}
						}
					},
					function() {
						obj.parent_in_focus = false;
						
						setTimeout( function() {
							if( !obj.popup_in_focus && !obj.parent_in_focus )
							{
								popup_element.fadeOut(options.fade_duration);
								if( popup_element == popup_currrent_obj )
									popup_currrent_obj = null;
							}
						}, options.delay );
					}
				);
			}
			
			if( options.onclick )
			{
				obj.click(
					function(e) {
						
						if( popup_currrent_obj == popup_element )
							return; // we're already in this pop-up
						
						e.preventDefault();
						
						if (popup_currrent_obj && popup_element != popup_currrent_obj )
						{
							popup_currrent_obj.fadeOut(options.fade_duration);
							popup_currrent_obj = null;
							//return;
						}
						
						popup_currrent_obj = popup_element;
						
						if( options.offsetRelative && options.offsetLeft && options.offsetTop ) {
							popup_element.css('left', options.offsetLeft );
							popup_element.css('top', options.offsetTop );
						} else if( options.offsetLeft && options.offsetTop ) {
							popup_element.css('left', e.pageX - this.offsetLeft + options.offset );
							popup_element.css('top', e.pageY - this.offsetTop + options.offset );
						}
							
						//popup_element.css('left', e.pageX - this.offsetLeft + options.offsetLeft );
						//popup_element.css('top', e.pageY - this.offsetTop + options.offsetTop );
						zIndexFix(popup_element);
						popup_element.fadeIn(options.fade_duration);
						
						if( options.callback )
						{
							if( this.id == '' ) {
								var link = $('a', obj);
								var data = { href: link.attr('href'), title: link.attr('title'), id: ''};
								options.callback.call( this, data );
							} else {
								var data = { href: '', title: '', id: this.id};
								options.callback.call( this, data );
							}
						}
					}
				);
				
				obj.hover(
					function() {
						obj.parent_in_focus = true;
					},
					function() {
						obj.parent_in_focus = false;
						
						setTimeout( function() {
							if( !obj.popup_in_focus && !obj.parent_in_focus )
							{
								popup_element.fadeOut(options.fade_duration);
								if( popup_element == popup_currrent_obj )
										popup_currrent_obj = null;
							}
						}, options.delay );
					}
				);
			}
			
			
			// We want to hide the popup once the focus is lost
			popup_element.hover(
				function() { // hover in
					obj.popup_in_focus = true;
					//popup_currrent_obj = null;
				},
				function() { // hover out
					obj.popup_in_focus = false;
					//popup_currrent_obj = popup_element;
					setTimeout( function() {
							if( !obj.popup_in_focus && !obj.parent_in_focus )
							{
								popup_element.fadeOut(options.fade_duration);
								if( popup_element == popup_currrent_obj )
									popup_currrent_obj = null;
							}
						}, options.delay );
				}
			);
		
		})
	};
	
	function _popup_log(msg)
	{
		$('#log').prepend('<div>'+msg+'<div>');
	}
	
	function zIndexFix(obj) {
		
		var zmax = 0;
    $('*').each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
		zmax += 5;
		//alert(zmax);
		obj.css("z-index", zmax);
	}
	
})(jQuery);
