//Ajax Tooltip script: By JavaScript Kit: http://www.javascriptkit.com
//Last update (July 10th, 08'): Modified tooltip to follow mouse, added Ajax "loading" message.
// Modified Seafolk

var ajaxtooltip={
	
	useroffset: [10, 10], //additional x and y offset of tooltip from mouse cursor, respectively
	loadingHTML: '<div align="center"><img src="i/ajaxload.gif" /></div>',

	positiontip:function($tooltip, e){
		var docwidth=(window.innerWidth)? window.innerWidth-15 : ajaxtooltip.iebody.clientWidth-15
		var docheight=(window.innerHeight)? window.innerHeight-18 : ajaxtooltip.iebody.clientHeight-15
	
		var twidth=$tooltip.get(0).offsetWidth
		var theight=$tooltip.get(0).offsetHeight
		
		var tipx=e.pageX+this.useroffset[0]
		var tipy=e.pageY+this.useroffset[1]
		//tipx=(e.clientX+twidth>docwidth)? tipx-twidth-(2*this.useroffset[0]) : tipx //account for right edge
		//tipy=(e.clientY+theight>docheight)? tipy-theight-(2*this.useroffset[0]) : tipy //account for bottom edge
		$tooltip.css({left: tipx, top: tipy})
		
	},

	showtip:function($tooltip, e){
		$tooltip.hide().fadeIn(500)
	},

	hidetip:function($tooltip, e){
		$tooltip.fadeOut(500)	
	}
}

jQuery(document).ready(function(){
	
	ajaxtooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	var tooltips = [] //array to contain references to all tooltip DIVs on the page
	var CurTooltips = -1; // -1 так как массив tooltips имеет 0 индекс
	var TrigerClick = 0; // для того что бы попап не закрывался при первом клике 1 - первый клик 2- второй (можно закрывать)
	
	// закрываем попап по щелчку вне слоя попапа
	$("body").click(function(e){
		//onMouseout element
				var $tooltip=tooltips[CurTooltips]
				//alert(CurTooltips)
				if (TrigerClick == 2) {
					$tooltip.fadeOut(500);
					CurTooltips = -1;
				}
				TrigerClick = 2;
	})
	
	$('*[@ajaxpopap^="url:"]').each(function(index){ //find all links with "title=ajax:" declaration

		this.titleurl=jQuery.trim(this.getAttribute('ajaxpopap').split(':')[1]) //get URL of external file
		this.width=jQuery.trim(this.getAttribute('ajaxpopap').split(':')[2])

		this.titleposition=index+' pos' //remember this tooltip DIV's position relative to its peers
		tooltips.push($('<div class="ajaxtooltip" style="width:'+this.width+'px"><span class="draganddrop"></span></div>').appendTo('body'))
		
		var $target=$(this)
		$target.removeAttr('title')
		
		var $tooltip = tooltips[parseInt(this.titleposition)]
		
		$target.bind("click", function(e){

				if (!$tooltip.get(0).loadsuccess){ //first time fetching Ajax content for this tooltip?
					ajaxtooltip.positiontip($tooltip, e)
					$tooltip.html(ajaxtooltip.loadingHTML).show()
					$tooltip.load(this.titleurl, '', function(){
						ajaxtooltip.showtip($tooltip, e)
						$tooltip.get(0).loadsuccess=true
					})
					$tooltip.draggable()
				}
				else{
					ajaxtooltip.positiontip($tooltip, e)
					ajaxtooltip.showtip($tooltip, e)	
				}
				if (CurTooltips>=0) {
					tooltips[CurTooltips].fadeOut(500);
					CurTooltips = -1;
				}
				CurTooltips = parseInt(this.titleposition);
				TrigerClick = 1;
		})
/* закрытие при саскакивании мыши с окна подсказки
		$tooltip.bind("mouseleave", function(e){
			ajaxtooltip.hidetip($tooltip, e)
		})
*/
		$tooltip.bind("click", function(e){
			TrigerClick = 1;
		})

	})
})