/**
 * Popup class 
 * @package DotMedia.js.Popup
 * @author DotMedia
 * @url http://www.dotmedia.bg
 * @license Can be used only where authorized by Dot Media
 */


function Popup(backgroundImage, popupDiv, fadeOut) {
	this.attachedTo = null;
	this.backgroundImage = backgroundImage;
	this.popupId = popupDiv;
	this.backId = "id_"+popupDiv+"_back";
	this.popupDiv = this.$(popupDiv);
	this.offsetX = 0;
	this.offsetY = 0;
	this.showType = 0;
	this.debug = 0;
	this.fadeState = 100;
	this.fadeStep = 15;
	this.fade = false;
	this.fadeInterval = null;
	
	if (fadeOut == true) {
		this.fade = true;
	}
	
	var obj = this;
	if (!backgroundImage) {
		this.backgroundDiv = null;
	} else {
		try {
			this.backgroundDiv = document.createElement("div");
			this.backgroundDiv.style.width = "100%";
			this.backgroundDiv.style.height = "100%";
			this.backgroundDiv.style.display = "none";
			this.backgroundDiv.style.position = "fixed";
			this.backgroundDiv.style.zIndex = "1000";
			this.backgroundDiv.style.top = "0";
			this.backgroundDiv.style.backgroundImage = "url(" + backgroundImage + ")";
			this.backgroundDiv.setAttribute("id", this.backId);
			this.backgroundDiv.onclick = function() {
				obj.close();
			};
		} catch (e) {
			if (this.debug) {
				alert(e);
			}
		}
	}
}

Popup.prototype.close = function() {
	if (this.backgroundDiv) {
		this.$(this.backId).style.display = "none";
	}
	
	
	if (this.fade) {
		this.fadeInterval = 100;
		
		var obj = this;
		
		this.fadeInterval = setInterval(function() {
			var elm = obj.$(obj.popupId);
			
			if (obj.fadeState < 1) {
				
				clearInterval(obj.fadeInterval);
				elm.style.filter = "alpha(opacity=100)";
				elm.style.opacity = "1";
				elm.style.display = "none";
				elm.style.zoom = "1";
				obj.fadeState = 100;
				return;
			}
			
			elm.style.filter = "alpha(opacity=" + obj.fadeState + ")";
			elm.style.opacity = obj.fadeState / 100;
			obj.fadeState -= obj.fadeStep;
		}, 30);
	} else {
		this.$(this.popupId).style.display = "none";
	}
}

Popup.prototype.onKeyPress = function(obj, e) {
	if (e.keyCode == 27) {
		obj.close();
	}
}

Popup.prototype.onResize = function(obj) {
	if (obj.showType == 1) {
		var pos = obj.findPosition(obj.attachedTo);	
		var popupDiv = obj.$(obj.popupId);
	
		popupDiv.style.left = (pos[0] + obj.offsetX) + "px";
		popupDiv.style.top = (pos[1]  + obj.offsetY) + "px";
	} else if (obj.showType == 2) {
		var popupDiv = obj.$(obj.popupId);
		var w = Math.round(parseInt(popupDiv.scrollWidth) / 2);
		var h = Math.round(parseInt(popupDiv.scrollHeight) / 2);

		popupDiv.style.left = Math.round((obj.getScreenCenterX() - w)) + "px";
		popupDiv.style.top = Math.round((obj.getScreenCenterY_() - h)) + "px";	
	}
}

Popup.prototype.getScreenCenterX = function() {
	return(document.body.clientWidth/2);
}

Popup.prototype.getScreenCenterY_ = function() {
	if (!window.innerHeight) {
		return (document.documentElement.clientHeight / 2);
	}
	return (window.innerHeight / 2);
}

Popup.prototype.getScreenCenterY = function() {
	var y = 0;
	y = this.getScrollOffset()+(this.getInnerHeight()/2);

	return(y);
}

Popup.prototype.getInnerHeight = function() {
	var y;
	
	if (self.innerHeight) {
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		y = document.body.clientHeight;
	}
	
	return(y);
}

Popup.prototype.getScrollOffset = function() {
	var y;
	
	if (self.pageYOffset) {
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		y = document.documentElement.scrollTop;
	} else if (document.body) { 
		y = document.body.scrollTop;
	}

	return(y);
}

Popup.prototype.findPosition = function(elm) {
	var curleft = curtop = 0;
	
	if (elm.offsetParent) {
		do {
			curleft += elm.offsetLeft;
			curtop += elm.offsetTop;
		} while (elm = elm.offsetParent);

	}
	
	return [curleft,curtop];
}

Popup.prototype.$ = function(id) {
	var obj = null;
	try {
		obj = document.getElementById(id);
	} catch (e) {
		//
	}
	return obj;
}

Popup.prototype.showInCenter = function(elementToFocus) {
	var obj = this;
	this.showType = 2;
	
	if (!this.$(this.backId)) {
		if (this.backgroundDiv) {
			document.body.appendChild(this.backgroundDiv);
		}
		if (document.body.addEventListener) {
			document.body.addEventListener("keypress", function(e) {
				obj.onKeyPress(obj, e);
			}, false);
		} else {
			document.body.attachEvent("onkeypress", function(e) {
				obj.onKeyPress(obj, e);
			}, false);
		}
	}
	
	if (this.backgroundDiv) {
		this.backgroundDiv.style.display = "block";
	}
	
	if (this.fade) {
		this.popupDiv.style.opacity = "0";
		this.popupDiv.style.filter = "alpha(opacity=0)";
	}

	this.popupDiv.style.position = "fixed";
	this.popupDiv.style.display = "block";
	this.onResize(obj);

	if (this.fade) {
		this.fadeInterval = 100;
		
		var obj = this;
		obj.fadeState = 0;
		this.fadeInterval = setInterval(function() {
			var elm = obj.$(obj.popupId);
			
			if (obj.fadeState >= 100) {
				
				clearInterval(obj.fadeInterval);
				elm.style.filter = "alpha(opacity=100)";
				elm.style.opacity = "1";
				obj.fadeState = 100;
				return;
			}
			
			elm.style.filter = "alpha(opacity=" + obj.fadeState + ")";
			elm.style.opacity = obj.fadeState / 100;
			obj.fadeState += obj.fadeStep;
		}, 50);
	}
	
	if (elementToFocus) {
		var elmToFocus = this.$(elementToFocus);
		if (elmToFocus) {
			this.$(elementToFocus).focus();	
		} else {
			if (this.debug) {
				alert("Параметър 1: overElement, е подаден с грешна стойност!");
			}
		}
		
	}
	
	window.onresize = function() {
		obj.onResize(obj);
	}
}

Popup.prototype.show = function(overElement, offsetX, offsetY, elementToFocus) {
	var obj = this;
	this.showType = 1;
	
	if (!isNaN(offsetX)) {
		this.offsetX = offsetX;
	} else {
		this.offsetX = 0;
	}
	
	if (!isNaN(offsetY)) {
		this.offsetY = offsetY;
	} else {
		this.offsetY = 0;
	}
	
	this.attachedTo = this.$(overElement);
	
	if (!this.$(this.backId)) {
		
		if (this.backgroundDiv) {
			document.body.appendChild(this.backgroundDiv);
		}
		
		if (document.body.addEventListener) {
			document.body.addEventListener("keypress", function(e) {
				obj.onKeyPress(obj, e);
			}, false);
		} else {
			document.body.attachEvent("onkeypress", function(e) {
				obj.onKeyPress(obj, e);
			}, false);
		}
	}
	
	if (this.backgroundDiv) {
		this.backgroundDiv.style.display = "block";
	}
	
	this.onResize(obj);
	this.popupDiv.style.position = "absolute";
	this.popupDiv.style.zIndex = 1001;
	this.popupDiv.style.display = "block";
	if (elementToFocus) {
		var elmToFocus = this.$(elementToFocus);
		if (elmToFocus) {
			this.$(elementToFocus).focus();	
		} else {
			if (this.debug) {
				alert("Параметър 1: overElement, е подаден с грешна стойност!");
			}
		}
		
	}
	
	window.onresize = function() {
		obj.onResize(obj);
	}
	
}

function InitPopup(PopUpVariable, bgImage, PopUpDiv, fade) {
	if (window.addEventListener){ 
		window.addEventListener('load', function() {
			eval("window['"+PopUpVariable+"'] = new Popup('"+bgImage+"', '"+PopUpDiv+"', "+(fade ? "true":"false")+");");
		}, false); 
	} else if (window.attachEvent){ 
		window.attachEvent('onload', function() {
			eval("window['"+PopUpVariable+"'] = new Popup('"+bgImage+"', '"+PopUpDiv+"', "+(fade ? "true":"false")+");");
		}); 
	}
} 
