/*
 jQuery Plugin for Opening DHTML Windows
 (c) 2008-2009 Aleksander Ovcharenko

 version 1.1

 uses other jquery plugins:
 - jquery.ifixpng.js (for IE6)
 - ui.draggable, ui.core (optional)
 
 example:
 <div id="testdiv" style="background-color:#CCCCCC; width:400px; padding:15px; height:100px;">test</div>
 
<a href="javascript: void(0);" 
onclick="$('#testdiv').jMsgHint('red',
							 'testing message',
							 {
								 lifetime: 0,
								 drag: true
								});"> test </a>
<br />

<a href="javascript: void(0);" 
onclick="$('#testdiv').jMsgHint('green',
								 'All ok!',
								 {lifetime: 2000,
								 drag: false});"> test2 </a>
*/

(function($) {
	$.jMsgHint = function( hinttype , message, options, object ) {
		var jthis;
		if (object) jthis = $(object);
		hinttype || (hinttype='green');
		
		var jhint = $(object?'#'+jthis.attr('hintid'):(hinttype=='wait'?'#waitMsgHint':'#freeMsgHint'));
		var cfg = $.extend({
				lifetime: 5000,
				drag: false,
				xpos: undefined,
				ypos: undefined,
				width: undefined,
				dx: 0,
				dy: 0,
				closeonclick: false,
				onshow: null,
				onhide: null,
				zIndex: 0,
				animation: 1
			}, options || ( object && jhint.length && jhint[0].cfg ? jhint[0].cfg : {} ), (hinttype=='red' ? {lifetime: 0}: {}));
		
		if ($.browser.msie && $.browser.version<7 && jhint.length) jhint.remove();
		if ( jhint.size() == 0 ) {
			hintid = object?'MHint_'+Math.floor(Math.random()*10000):(hinttype=='wait'?'waitMsgHint':'freeMsgHint');
			var ie6class = ($.browser.msie && $.browser.version<7)?' jMsgHint_ie6':'';
			jhint = $('<div id="'+hintid+'" class="jMsgHint'+ie6class
					+'" style="display: none;"><div class="MH_box"><div class="MH_border_top fixpng"><div class="MH_border_line fixpng"><div class="MH_crossbar"></div></div><div class="MH_border_corner fixpng"><div class="MH_closer"></div></div></div><div class="MH_center"><div class="MH_border_line fixpng"></div><div class="MH_right fixpng"><div class="MH_border_line fixpng"><div class="MH_gradbg"></div></div><div class="MH_content"><div class="MH_gradbg"></div><div class="MH_message"><div class="MH_icon"></div><span class="MH_text"></span></div></div></div></div><div class="MH_border_bottom fixpng"><div class="MH_border_line fixpng"></div><div class="MH_border_corner fixpng"></div></div></div></div>'
				).appendTo(document.body);//.prependTo(jthis);
			if (object) jthis.attr('hintid', hintid);
		} else hintid = jhint.attr('id');
		
		jhint[0].cfg = cfg;
		
		var closer=$(($.browser.msie && $.browser.version<7)?'div.MH_border_top div.MH_border_corner':'div.MH_closer', jhint);
		
		if (cfg.closeonclick) {
			closer.hide();
			jhint.unbind('click').bind('click', function () {
				hint_close(this);
			});
		} else {
			closer.show();
			closer.unbind('click').bind('click', function () {
						hint_close($(this).parents('div.jMsgHint:first'));
			});
			jhint.unbind('click');
		}
		
		
		$('div.MH_box', jhint).removeClass('MH_green MH_red').addClass('MH_'+hinttype);
		if ($.browser.msie && $.browser.version<7) setTimeout("$('#"+hintid+" .fixpng').ifixpng()", 50);
		
		$('span.MH_text', jhint).html(message);
		
///////////////////////////// Position, Width, zIndex
		if ( object && ((!jthis.offset().left && !jthis.offset().top) || ($.browser.msie && jthis.offset().left<=0 && jthis.offset().top<=0)) && !jthis.width() && !jthis.height()) return false;//!jthis.is(':visible') ||
		var xpos = cfg.xpos, ypos = cfg.ypos, hintwidth = cfg.width;
		if (hintwidth == undefined) hintwidth = object?parseInt(jthis.outerWidth()*0.8):(hinttype=='wait'?233:333);
		if (xpos == undefined) xpos = object?parseInt(jthis.offset().left + jthis.width()*0.2+50):$(window).width()-hintwidth-(hinttype=='wait'?0:5);
		if (ypos == undefined) ypos = object?parseInt(jthis.offset().top - 30):(hinttype=='wait'?0:15);

		xpos += cfg.dx||0;
		ypos += cfg.dy||0;
		
		var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
		var scrollY = document.documentElement.scrollTop || document.body.scrollTop;

		if (cfg.width == undefined && hinttype!='wait' && hintwidth < 333) hintwidth=333;
		if (cfg.prev_coll) {
			var hheight = jhint.height(), maxx=0, miny=ypos+hheight, cflag=0;
			$(cfg.prev_coll).each(
				function () {
					var jthis = $(this), to = jthis.offset(), tw = jthis.width(), th = jthis.height();
					if (maxx < (to.left + tw)) maxx = (to.left + tw);
					if (miny > to.top) miny = to.top;
					if (
						( xpos <= to.left && to.left <= (xpos + hintwidth)
							|| to.left <= xpos && xpos <= (to.left + tw) )
						&& ( ypos <= to.top && to.top <= (ypos + hheight)
							|| to.top <= ypos && ypos <= (to.top + th) )
					   ) cflag = 1;
				});
			if (cflag) {
				ypos = miny - hheight;
				xpos = maxx;
			}
		}
		
		if (xpos > $(window).width()-hintwidth-(hinttype=='wait'?0:5)) xpos=$(window).width()-hintwidth-(hinttype=='wait'?0:5);
		if (xpos < scrollX) xpos = scrollX;
		if (ypos < scrollY) ypos = scrollY;

		jhint.css('width', hintwidth +'px').css('left', xpos + 'px').css('top', ypos + 'px');

		//Sorry mac-users ( Task #MAILUA-2819 )                
		//if (navigator.appVersion.indexOf("Mac") != -1) $('.MH_closer', jhint).css('margin-left', '-'+(hintwidth-26)+'px');
		
		if (cfg.zIndex != undefined && parseInt(cfg.zIndex) > 0) jhint.css('z-index', parseInt(cfg.zIndex));
		else if (object) {
			var zIndex = parseInt(jthis.css('z-index'));
			if (!zIndex) zIndex = calc_zIndex(jthis);
			jhint.css('z-index', (zIndex?zIndex+1:10001));
		}
		else jhint.css('z-index', window.window_maxzindex ? window.window_maxzindex+1 : 10001);
		
//////////////////////////// Dragable
		if (!cfg.closeonclick && cfg.drag && $.isFunction( $().draggable ) ){
			jhint.draggable({containment: 'document', scroll:false});
		}
		
		hint_show(jhint);
///////////////////////////// Lifetime				
		clearTimeout(jhint[0].lifetimeout);
		if (cfg.lifetime > 0) {
			jhint[0].lifetimeout = setTimeout(function(){hint_close(jhint)}, cfg.lifetime);
		}
		
		return jhint;
	};
	
	$.fn.jMsgHint = function( hinttype , message, options ) {
		if ( $.isFunction(this.each) ) {
			var args = arguments;
			return this.each(function() {
				return $.jMsgHint(hinttype , message, options, this);
			});
		}
	};
	
	$.hintWait = function(message, options) {
		message || (message = $.lang('Your request is processing now ...'));
		clearTimeout(window.hintWait_tout);
		window.hintWait_tout = setTimeout(
			function(){$.jMsgHint('wait', message, $.extend({lifetime: 0, drag: false, animation: 0}, options))},
			((options && options.timeout)?options.timeout:0)
		);
	}
	
	$.hintUnWait = function() {
		clearTimeout(window.hintWait_tout);
		if ($.browser.msie && $.browser.version<7) window.hintWait_tout = setTimeout("$.closehint('#waitMsgHint');", 100);
		else $.closehint('#waitMsgHint');
	}

//********************************** close hint
	$.closehint = function(hint) {
		hint_close(hint || '#freeMsgHint');
	};
	
	$.fn.closehint = function() {
		return this.each(function(){
				//hint_close($('.jMsgHint', $(this)).attr('id'));
                $.closehint('#'+$(this).attr('hintid'));
		});
	};

//********************************** show hint
	$.fn.showhint = function() {
		return this.each(function(){
				//hint_show($('.jMsgHint', $(this)).attr('id'));
                hint_show('#'+$(this).attr('hintid'));
		});
	};
	
//********************************** UTILS

	function hint_close(hint){
		if (typeof(hint) == 'undefined' || !hint) return;
		var jhint = $(hint);
		if (!jhint.is(':visible')) return;
		if ( jhint.length ) {
			if (!$.browser.msie && jhint[0].cfg.animation)
				jhint.stop(1,1).fadeOut('slow', function(){jhint=$(this); if (jhint[0].cfg && $.isFunction( jhint[0].cfg.onhide )) jhint[0].cfg.onhide(jhint)});
			else {
				jhint.hide();
				if (jhint[0].cfg && $.isFunction( jhint[0].cfg.onhide )) jhint[0].cfg.onhide(jhint);
				if ($.browser.msie && $.browser.version<7) jhint.remove();
			}
		}
	}
	
	function hint_show(hint){
		if (typeof(hint) == 'undefined' || !hint) return;
		var jhint = $(hint);
		if ( jhint.length ) {
			if (!$.browser.msie && jhint[0].cfg.animation)
				jhint.stop(1,1).fadeIn('slow', function(){jhint=$(this); if (jhint[0].cfg && $.isFunction( jhint[0].cfg.onshow )) jhint[0].cfg.onshow(jhint)});
			else if ($.browser.msie && $.browser.version < 7)
				setTimeout(function(){
					jhint.show();
					if (jhint[0].cfg && $.isFunction( jhint[0].cfg.onshow )) jhint[0].cfg.onshow(jhint);
				}, 100);
			else {
				jhint.show();
				if (jhint[0].cfg && $.isFunction( jhint[0].cfg.onshow )) jhint[0].cfg.onshow(jhint);
			}
		}
	}
    
    function calc_zIndex(currrenthis) {
        if( !currrenthis || currrenthis.size() == 0 ) return 0;
		var jparent = currrenthis.parent();
        if (jparent.is('body')) return 0;
        var zIndex = parseInt(jparent.css('z-index'));
        if (!zIndex) return calc_zIndex(jparent);
        return zIndex;
    }
    
    function hint_closeall() {
        var jhints = $('body > div.jMsgHint:visible');
        for (var i=0; i<jhints.length; i++) {
            hint_close(jhints[i]);
        }
    }
	
    $.extend({
	  'jMsgHintCloseAll': hint_closeall
    });
    
})(jQuery);
