
var mouseX = 0;
var mouseY = 0;

var isIE = (document.all ? true : false);
if (!isIE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

function getMouseX(evt) {
	
	if (evt.pageX) {
		return evt.pageX;
	} else if (evt.clientX) {
		return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	} else {
		return null;
	}

}
function getMouseY(evt) {
	
	if (evt.pageY) {
		return evt.pageY;
	} else if (evt.clientY) {
		return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else {
		return null;
	}

}

function getMouseXY(e) {

	mouseX = getMouseX(e ? e : event);
	mouseY = getMouseY(e ? e : event);
	
	if (mouseX < 0){mouseX = 0;}
	if (mouseY < 0){mouseY = 0;}  
	
	return true;
} // end mouse x/y detector

function venu_popup_class() {
	
	var venu_popup_div;
	var visible = false;
	var current_obj_ref = null;

	var use_transition = false;
	//var bound_by_screen = false;
	
	var initial_x = 0;
	var initial_y = 0;
	var screen_width = 0;
	var screen_height = 0;
	var scrollX = 0;
	var scrollY = 0;
	
	this.init = init;
	this.showpopup = showpopup;
	this.hidepopup = hidepopup;
	this.positionPopup = positionPopup;
	this.getScreenSize = getScreenSize;
	this.getScrollXY = getScrollXY;

	function getScreenSize() {
		
		if (typeof( window.innerWidth ) == 'number') {
			//Non-IE
			this.screen_width = window.innerWidth;
			this.screen_height = window.innerHeight;
		} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			this.screen_width = document.documentElement.clientWidth;
			this.screen_height = document.documentElement.clientHeight;
		} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			this.screen_width = document.body.clientWidth;
			this.screen_height = document.body.clientHeight;
		}
		
	}

	function getScrollXY() {
		
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			//alert('NS');
			this.scrollY = window.pageYOffset;
			this.scrollX = window.pageXOffset;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			//alert('IE');
			this.scrollY = document.documentElement.scrollTop;
			this.scrollX = document.documentElement.scrollLeft;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			//alert('DOM');
			this.scrollY = document.body.scrollTop;
			this.scrollX = document.body.scrollLeft;
		} else {
			this.scrollY = 0;
			this.scrollX = 0;
		}

	}

	function init(param_use_transition, param_bound_by_screen) {
		this.venu_popup_div = document.createElement("div");
		this.venu_popup_div.className = "venu_popup_container";
		document.body.appendChild(this.venu_popup_div);	
		
		if (param_use_transition != null) {
			this.use_transition = param_use_transition;
		}
		
		if (param_bound_by_screen != null) {
			if (param_bound_by_screen) {
				this.bound_by_screen = param_bound_by_screen;
				this.getScreenSize();
			}
		}
	}

	function positionPopup(popup_fixed, top, left,center_x_pos) {
		
		var divheight = this.venu_popup_div.offsetHeight;
		var divwidth = this.venu_popup_div.offsetWidth;
		
		this.getScrollXY();
		
		if (popup_fixed != null) {
			if (popup_fixed) {
				if (top != null) {
					this.venu_popup_div.style.top = '' + parseInt(top) + 'px';
				} else {
					this.venu_popup_div.style.top = '' + parseInt(this.initial_y) + 'px';
				}
				if (left != null) {
					if ((this.bound_by_screen) && (parseInt(left) + parseInt(divwidth) > parseInt(this.screen_width) - this.scrollX)) {
						this.venu_popup_div.style.left = '' + (parseInt(left) - parseInt(divwidth)) + 'px';
					} else {
						this.venu_popup_div.style.left = '' + parseInt(left) + 'px';
					}
				} else {
					if ((this.bound_by_screen) && (parseInt(this.initial_x) + parseInt(divwidth) > parseInt(this.screen_width) - this.scrollX)) {
						this.venu_popup_div.style.left = '' + (parseInt(this.initial_x) + 10 - parseInt(divwidth)) + 'px';
					} else {
						this.venu_popup_div.style.left = '' + (parseInt(this.initial_x) + 10) + 'px';
					}
				}

			} else {
				if (this.bound_by_screen) {
					
					if (center_x_pos!= null) {
						
						if (center_x_pos) {
							
							this.getScreenSize();
							centerXPos = (this.screen_width - divwidth)/2;
							centerYPos = (this.screen_height - divwidth)/2 + this.scrollY;
							this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
							this.venu_popup_div.style.left = '' + parseInt(centerXPos) + 'px';
							
						} else {
							
							this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
						
							if (parseInt(mouseX) + parseInt(divwidth) > parseInt(this.screen_width) - this.scrollX) {
								this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10 - parseInt(divwidth)) + 'px';
							} else {
								this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
							}
							
						}
						
					
					} else {
						this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
						
						if (parseInt(mouseX) + parseInt(divwidth) > parseInt(this.screen_width) - this.scrollX) {
							this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10 - parseInt(divwidth)) + 'px';
						} else {
							this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
						}
					}
					
				} else {
					if (center_x_pos!= null) {
						if (center_x_pos) {
							this.getScreenSize();
							centerXPos = (this.screen_width - divwidth)/2;
							centerYPos = (this.screen_height - divwidth)/2 + this.scrollY;
							this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
							this.venu_popup_div.style.left = '' + parseInt(centerXPos) + 'px';
							
						} else {
								this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
								this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
						}
													
					} else {
							this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
							this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
						
					}
						
				}
			}
		} else {
			if (this.bound_by_screen) {
				this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
				
				if (parseInt(mouseX) + parseInt(divwidth) > parseInt(this.screen_width) - this.scrollX) {
					this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10 - parseInt(divwidth)) + 'px';
				} else {
					this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
				}
			} else {
				this.venu_popup_div.style.top = '' + parseInt(mouseY) + 'px';
				this.venu_popup_div.style.left = '' + (parseInt(mouseX) + 10) + 'px';
			}
		}
	}

	function showpopup(obj_ref, code, popup_fixed, top, left, center_x_pos) {
		if (!this.visible || (this.current_obj_ref != obj_ref)) {
			
				if (popup_fixed != null) {
					if (popup_fixed) {
						if (top == null) {
							this.initial_y = mouseY;
						}
						if (left == null) {
							this.initial_x = mouseX;
						}
					}
				}

				if (this.use_transition && isIE) {
					this.venu_popup_div.innerHTML = "";
					this.venu_popup_div.filters[0].apply();
					this.venu_popup_div.innerHTML = code;
					this.venu_popup_div.filters[0].play();
					this.venu_popup_div.style.visibility = 'visible';
				} else {
					this.venu_popup_div.innerHTML = code;
					this.venu_popup_div.style.visibility = 'visible';
				}
				
			this.visible = true;
			this.current_obj_ref = obj_ref;
			this.positionPopup(popup_fixed, top, left, center_x_pos);

		} else {
			this.positionPopup(popup_fixed, top, left, center_x_pos);
		}

	}
	
	function hidepopup() {
		this.venu_popup_div.style.visibility = 'hidden';
		this.visible = false;
	}

} // end class

var venu_popup = new venu_popup_class();
