
var HoverManager = {

	_inited: false,
	_object: null,
	_current_pos: {x: 0, y: 0},
	_start_pos: {x: 0, y: 0},
	_opacity_now: 0,
	_opacity_prev: 1,
	_blend_stepper: false,
	_xml_request: false,

	/**
	*	Initializing
	*/
	init: function() {
		var self = HoverManager;

		if (self._inited) return;
		self._inited = true;

		// IE, Mozilla, Konqueror compatible approach
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id", "hover");
		newDiv.setAttribute("style", "position: absolute; z-index: 100; visibility: hidden; overflow: visible; float: left; margin: none; border: none;");
		document.body.appendChild(newDiv);

		self._object = getObject("hover");

		self._object.style.position		= "absolute";
		self._object.style.zIndex		= 100;
		self._object.style.visibility	= "hidden";
		self._object.style.overflow		= "visible";
		self._object.style.cssFloat		= "left";

		if (GeneralEvents) {
			GeneralEvents.addCursorMoveEventListener(HoverManager.updatePosition);
			GeneralEvents.addPageScrollEventListener(HoverManager.updateScroll);
		}

		self._setHoverOpacity(-1);
	},

	/**
	*	Repaint hover
	*/
	_repaintHover: function() {
		var self = HoverManager;

		// Hover visible ?
		if (self._object.style.visibility != "visible") return;

		// Hover not too far away ?
		if ((Math.abs(self._start_pos.x - self._current_pos.x) > 160) || (Math.abs(self._start_pos.y - self._current_pos.y) > 160)) {
			self._hideHover();
			return;
		}

		// Check window constraints
		var constraints		= getWindowSize();
		var scroll			= getScrollXY();
		var limit_width		= constraints.w + scroll.x;
		var limit_height	= constraints.h + scroll.y;

		var content			= getObject("hover_content")
		var mouse_diff		= 15;
		var border_diff		= 15;

		var x = (self._current_pos.x + mouse_diff);
		var y = (self._current_pos.y + mouse_diff);

		if (content) {
			if (x + content.offsetWidth + border_diff > limit_width) { x = limit_width - content.offsetWidth - border_diff; }
			if (y + content.offsetHeight + border_diff > limit_height) { y = limit_height - content.offsetHeight - border_diff; }
			if ((x < self._current_pos.x + mouse_diff) && (y < self._current_pos.y + mouse_diff)) {
				//x = self._current_pos.x - mouse_diff - content.offsetWidth;
				y = self._current_pos.y - mouse_diff - content.offsetHeight;
			}
		}

		self._object.style.left	= x + 'px';
		self._object.style.top	= y + 'px';
	},

	/**
	*	Hover position updating through cursor moving
	*/
	updatePosition: function(cursor_x, cursor_y) {
		var self = HoverManager;

		if (!self._inited) return;

		// Update current position
		self._current_pos.x = cursor_x;
		self._current_pos.y = cursor_y;

		self._repaintHover();
	},

	/**
	*	Hover position updating through
	*/
	updateScroll: function(scroll_x, scroll_y) {
		var self = HoverManager;

		if (!self._inited) return;

		self._repaintHover();
	},

	/**
	*	Set hover opacity
	*/
	_setHoverOpacity: function(step) {
		var self = HoverManager;

		self._opacity_now += step;
		if (self._opacity_now > 1) self._opacity_now = 1;
		if (self._opacity_now < 0) self._opacity_now = 0;

		// If new opacity differs from previous
		if (self._opacity_now != self._opacity_prev) {
			self._opacity_prev = self._opacity_now;

			self._object.style.opacity = self._opacity_now.toFixed(2);
			if (iex) self._object.style.filter = "alpha(opacity="+Math.round(self._opacity_now * 100)+")";
			if (nav || n_6) self._object.style.MozOpacity = self._opacity_now.toFixed(2);
		}

		// If done - fully visible
		if ((self._opacity_now >= 1) && (self._opacity_prev < 1)) {
			clearInterval(self._blend_stepper);
		}

		// If done - fully hidden
		if ((self._opacity_now <= 0) && (self._opacity_prev > 0)) {
			clearInterval(self._blend_stepper);
			self._hideHover();
		}
	},

	/**
	*	Inside hover displaying function
	*/
	_showHover: function(content, width, height) {
		var self = HoverManager;

		writeObjectContent(self._object, content);

		self._object.style.width		= (width ? width + "px" : "auto");
		self._object.style.height		= (height ? height + "px" : "auto");
		self._object.style.visibility	= "visible";

		self._start_pos.x	= self._current_pos.x;
		self._start_pos.y	= self._current_pos.y;

		self.updatePosition(self._start_pos.x, self._start_pos.y);
	},

	/**
	*	Inside hover hiding function
	*/
	_hideHover: function() {
		var self = HoverManager;

		writeObjectContent(self._object, "");

		self._object.style.visibility	= "hidden";
		self._object.style.left			= "-1000px";
		self._object.style.top			= "-1000px";
	},

	/**
	*	Hover displaying without blend
	*/
	showHoverWithoutBlend: function(content, width, height) {
		var self = HoverManager;

		if (!self._inited) return;

		clearInterval(self._blend_stepper);
		self._showHover(content, width, height);
		self._setHoverOpacity(1);
	},

	/**
	*	Hover displaying with blend
	*/
	showHoverWithBlend: function(content, width, height) {
		var self = HoverManager;

		if (!self._inited) return;

		clearInterval(self._blend_stepper);
		self._showHover(content, width, height);
		self._blend_stepper = setInterval("HoverManager._setHoverOpacity(+0.25)", 40);
	},

	/**
	*	Hover hiding without blend
	*/
	hideHoverWithoutBlend: function() {
		var self = HoverManager;

		if (!self._inited) return;

		clearInterval(self._blend_stepper);
		self._hideHover();
		self._setHoverOpacity(-1);
	},

	/**
	*	Hover hiding with blend
	*/
	hideHoverWithBlend: function() {
		var self = HoverManager;

		if (!self._inited) return;

		clearInterval(self._blend_stepper);
		self._blend_stepper = setInterval("HoverManager._setHoverOpacity(-0.25)", 40);
	},

	/**
	*	Get hover through XML request and show it
	*/
	showXMLRequestHover: function(url, url_params, delay, width, height) {
		var self = HoverManager;

		if (!XMLRequestHandler) return;

		if (self._xml_request) {
			XMLRequestHandler.abort(self._xml_request);
			self._xml_request = false;
			self.hideHoverWithoutBlend();
		}

		self._xml_request = XMLRequestHandler.post(url, url_params, HoverManager.showHoverWithBlend, delay);
	},

	/**
	*	Hide XML requested hover
	*/
	hideXMLRequestHover: function() {
		var self = HoverManager;

		if (self._xml_request && XMLRequestHandler) {
			XMLRequestHandler.abort(self._xml_request);
			self._xml_request = false;
		}

		self.hideHoverWithBlend();
	}
};


/**************************************************************************************/


/**
*	Show common text hover
*/
function showTextHover(text, width, height) {
	var content = '<div class="text_hover" id=\"hover_content\"><div class="hover_text">'+text+'</div></div>';
	HoverManager.showHoverWithBlend(content, width, height);
}

/**
*	Hide common text hover
*/
function hideTextHover() {
	HoverManager.hideHoverWithBlend();
}

/**
*	Show user hover box by XML request
*/
function showReqUserHover(user) {
	HoverManager.showXMLRequestHover("index.php", "action=hover_user&user="+user, 150);
}

function hideReqUserHover() {
	HoverManager.hideXMLRequestHover();
}

/**
*	Show picture hover box by XML request
*/
function showReqPictureHover(picture) {
	HoverManager.showXMLRequestHover("index.php", "action=hover_picture&picture="+picture, 150);
}

function hideReqPictureHover() {
	HoverManager.hideXMLRequestHover();
}

/**
*	Show RSS news hover box by XML request
*/
function showReqRSSHover(feed, item) {
	HoverManager.showXMLRequestHover("index.php", "action=hover_rss&feed="+feed+"&item="+item, 150, 300);
}

function hideReqRSSHover() {
	HoverManager.hideXMLRequestHover();
}

/**
*	Show user hover box by direct call
*/
function showUserHover(notice, pic_src, flag_src, level, vip, gender, age, num_pics, online) {
	var s = "";

	s = s + "<div class=\"user_hover\" id=\"hover_content\">";
	if (notice) {
		s = s + "<div class=\"hover_notice\">" + notice + "</div>";
	}
	if (pic_src) {
		s = s + "<div class=\"hover_picture\"><img src=\"" + pic_src + "\" alt=\"\" border=\"0\" class=\"hover_picture\" /></div>";
	}
	if (flag_src) {
		s = s + "<div class=\"hover_flag\"><img src=\"" + flag_src + "\" alt=\"\" border=\"0\" class=\"hover_flag\" /></div>";
	}
	s = s + "<div class=\"hover_text\">";
	if (level) {
		s = s + "<b>" + level + "</b><br/>";
	}
	if (vip) {
		s = s + "<b>" + vip + "</b><br/>";
	}
	s = s + gender + "<br/>";
	s = s + age + "<br/>";
	s = s + num_pics + "<br/>";
	s = s + online + "<br/>";
	s = s + "</div>";
	s = s + "</div>";

	HoverManager.showHoverWithoutBlend(s);
}

function hideUserHover() {

	HoverManager.hideHoverWithoutBlend();
}

/**
*	Show picture hover box by direct call
*/
function showPictureHover(heading, comment, rating, time, views, rates) {
	var s = "";

	s = s + "<div class=\"picture_hover\" id=\"hover_content\">";
	if (heading) {
		s = s + "<div class=\"hover_name\">" + heading + "</div>";
	}
	if (comment) {
		s = s + "<div class=\"hover_comment\">" + comment + "</div>";
	}
	s = s + "<div class=\"hover_text\">";
	if (rating) {
		s = s + rating + "<br/>";
	}
	s = s + time + "<br/>";
	s = s + views + "<br/>";
	s = s + rates + "<br/>";
	s = s + "</div>";

	HoverManager.showHoverWithoutBlend(s);
}

function hidePictureHover() {

	HoverManager.hideHoverWithoutBlend();
}
