/**
 * Modals.js.php
 * modal windows for Jobs.cz
 *
 * @author Ploski ;)
 */

function JobsModalWindow(windowName, windowTitle) {
	this.create(windowName, windowTitle);
}
JobsModalWindow.prototype = {
	// DOMElement pro modal
	modalBgDiv : null,
	modalDiv : null,
	// creates the window
	create : function (windowName, windowTitle) {
		// init - create divs, etc
		// create background
		this.modalBgDiv = document.createElement('div');
		this.modalBgDiv.className = 'modal_bg';
		this.modalBgDiv.id = 'modal_bg';
	
		// append background to DOM of the page
		document.body.appendChild(this.modalBgDiv);
	
		// resize to fit the screen
		var parent = this.modalBgDiv.parentNode;
		this.modalBgDiv.style.width = Math.max( parent.offsetWidth, document.documentElement.scrollWidth)+'px';
		this.modalBgDiv.style.height = Math.max( Math.max( parent.offsetHeight, document.documentElement.scrollHeight), document.documentElement.clientHeight)+'px';
		
	
		// create modal window
		this.modalDiv = document.createElement('div');
		this.modalDiv.className = 'modal';
		this.modalDiv.id = windowName;
		this.modalContent = document.createElement('div');
		this.modalContent.className = 'modalCnt';
		this.modalContent.id = windowName + '_content';
		this.header = document.createElement('h5');
		this.header.innerHTML = windowTitle;
		this.closeLink = document.createElement('a');
		this.closeLink.className = 'close';
		this.closeLink.innerHTML = '&nbsp;';
		this.closeLink.myParentElement = this;
		this.closeLink.onclick = this.onCancel;
		this.header.myParentElement = this;
		this.header.onmousedown = function (e) {
			mouseMoveLocked = this.myParentElement.modalDiv;
			mouseOffset = getMouseOffset(this, e);
			return false;
		}
		this.header.onmouseup = function () {
			mouseMoveLocked = null;
			return false;
		}
		this.header.appendChild(this.closeLink);
		this.modalDiv.appendChild(this.header);
		this.modalDiv.appendChild(this.modalContent);
		this.modalBgDiv.appendChild(this.modalDiv);
	},
	
	setContentFrom : function(elementId) {
		if (p = getElement(elementId)) {
			this.modalContent.innerHTML = p.innerHTML;
		}
	},
	
	display : function () {
		this.modalDiv.style.visibility = 'hidden';
		this.modalDiv.style.display = 'block';
		this.modalBgDiv.style.display = 'block';
		this.centerModalWindow();
		this.modalDiv.style.visibility = 'visible';
	}, 
	
	hide : function () {
		this.modalDiv.style.display = 'none';
		this.modalBgDiv.style.display = 'none';
	},
	
	destroy : function () {
		this.modalBgDiv = null;
		this.modalDiv = null;
	},
	
	centerModalWindow : function () {
		// makes the window centered
		var scrollLeft = Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
		var winWidth = document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth;
		var scrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
		var winHeight = document.documentElement.clientHeight != 0 && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
	
		var divHeight = this.modalDiv.offsetHeight;
		var divWidth = this.modalDiv.offsetWidth;
		this.modalDiv.style.top = ((winHeight / 2) - (divHeight / 2) + scrollTop) + 'px';
		this.modalDiv.style.left = ((winWidth / 2) - (divWidth / 2) + scrollLeft) + 'px';
	},
	
	onCancel : function (par) {
		this.myParentElement.hide();
	}
	
};
	
var mouseMoveLocked = null;
	
function enableModals() {
	document.onmousemove = mouseMove;
	mouseMoveLocked = null;
}
	
function mouseMove(ev){
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
	if (mouseMoveLocked) {
		mouseMoveLocked.style.top = (mousePos.y - mouseOffset.y) + 'px';
		mouseMoveLocked.style.left = (mousePos.x - mouseOffset.x) + 'px';
		return false;
	}
}
	
function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:(ev.clientX + document.body.scrollLeft - document.body.clientLeft),
		y:(ev.clientY + document.body.scrollTop  - document.body.clientTop)
	};
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;
	return {x:left, y:top};
}
	
// fce getElement
function getElement(el) {
	return document.getElementById(el);
}