/*****************************************************************/
/*  Title:    Dialog Box Creator (dialogBox)                     */
/*  Author:   © Ján Šmatlík, 2010 (http://1ohnny.xatchat.sk)*/
/*  Any copying/using without permission of author is FORBIDDEN! */
/*****************************************************************/

function dialogBox(obj) {
this.obj = obj; this.allowedTypes = ["confirm","alert","info","form"]; this.type = null; this.dWidth  = 300; this.dHeight = 150; this.elDialog = null; this.elDialogTitle = null; this.elDialogBody = null; this.elShadow = null; this.elClass = ""; this.elID = ""; this.dragging = false; this.arrActions = { "ok": [0,"OK","",0], "cancel": [0,"Zrušiť","",0], "close": [0,"Zavrieť","",0] }; this.scheme = '<table id="{ID}_table" style="margin:0px auto;width:{WIDTH};height:{HEIGHT};"><tr><td style="width:60px; padding-right: 10px; vertical-align: top; text-align: center;"><img src="images/dialogBox/{TYPE}.png" alt="{TYPE}"></td> <td style="vertical-align: top;">{DATA}</td></tr><tr><td colspan="2" style="vertical-align: bottom; text-align: center;">{BUTTONS}</td></tr></table>'; this.init = function(type,dialogId,dialogClass) { var allowedTypesStr = this.allowedTypes.join(','); if(allowedTypesStr.indexOf(type)==-1) { this.type = this.allowedTypes[0]; } else { this.type = type; } if(!this.elDialog) {
/*@cc_on @if (@_jscript) 
this.elDialog      = document.createElement('<div style="z-index:50;width:'+this.dWidth+'px;height:'+this.dHeight+'px;position:absolute;"'+(dialogClass ? ' class="'+dialogClass+'"' : '')+(dialogId ? ' id="'+dialogId+'"' : '')+'>'); this.elDialogTitle = document.createElement('<div'+(dialogClass ? ' class="'+dialogClass+'_title"' : '')+(dialogId ? ' id="'+dialogId+'_title"' : '')+'>'); this.elDialogBody  = document.createElement('<div'+(dialogClass ? ' class="'+dialogClass+'_body"' : '')+(dialogId ? ' id="'+dialogId+'_body"' : '')+'>'); 
@else */ this.elDialog      = document.createElement('div'); this.elDialogTitle = document.createElement('div'); this.elDialogBody  = document.createElement('div'); this.elDialog.setAttribute('style', "width:"+this.dWidth+"px;height:"+this.dHeight+"px;position:absolute;z-index:50;"); if(dialogClass) { this.elDialog.setAttribute('class', dialogClass); this.elDialogTitle.setAttribute('class', dialogClass+"_title"); this.elDialogBody.setAttribute('class', dialogClass+"_body"); } if(dialogId) { this.elDialog.setAttribute('id', dialogId); this.elDialogTitle.setAttribute('id', dialogId+"_title"); this.elDialogBody.setAttribute('id', dialogId+"_body"); } /* @end @*/ 
this.elDialogTitle.onmousedown = new Function(this.obj+".mouseDown();"); document.onmouseup = new Function(this.obj+".mouseUp();");} if(!this.elShadow) { 
/*@cc_on @if (@_jscript) 
this.elShadow = document.createElement('<div style="position:absolute;z-index:49;width:'+document.documentElement.clientWidth+'px;height:'+document.documentElement.clientHeight+'px;"'+(dialogClass ? ' class="'+dialogClass+'_shadow"' : '')+(dialogId ? ' id="'+dialogId+'_shadow"' : '')+'>'); @else */ this.elShadow = document.createElement('div'); if(dialogClass) { this.elShadow.setAttribute('class', dialogClass+"_shadow"); } if(dialogId) { this.elShadow.setAttribute('id', dialogId+"_shadow"); } this.elShadow.setAttribute('style', "width:100%;height:100%;position:fixed;z-index:49;"); /* @end @*/ } this.elID    = dialogId; this.elClass = dialogClass; }; this.close = function() { if(this.elDialog) { (this.elDialog.parentNode).removeChild(this.elDialog); } if(this.elShadow) { (this.elShadow.parentNode).removeChild(this.elShadow); } }; this.setAction = function(action,call_func,prevent_close) { if(this.arrActions[action]) { this.arrActions[action][0] = 1; if(call_func) { this.arrActions[action][2] = call_func; } if(prevent_close) { this.arrActions[action][3] = 1; } } }; this.setData = function(title,data) { var dataScheme = this.scheme; var buttons = ''; var actionsArr = new Array(); var j = 0; for(var k in this.arrActions) { actionsArr[j] = k; j++; } for(var i=0; i<j; i++) { if(this.arrActions[actionsArr[i]][0]) { buttons += '<button onclick="'+this.arrActions[actionsArr[i]][2]+'; '+(this.arrActions[actionsArr[i]][3]!=1 ? this.obj+'.close();' : '')+' return false;">'+this.arrActions[actionsArr[i]][1]+'</button>'; } } dataScheme = dataScheme.replace(/\{TYPE\}/gi,this.type); dataScheme = dataScheme.replace(/\{DATA\}/gi,data); dataScheme = dataScheme.replace(/\{BUTTONS\}/gi,buttons); dataScheme = dataScheme.replace(/\{ID\}/gi,this.elID); dataScheme = dataScheme.replace(/\{CLASS\}/gi,this.elClass); dataScheme = dataScheme.replace(/\{WIDTH\}/gi,(this.dWidth-10)+"px"); dataScheme = dataScheme.replace(/\{HEIGHT\}/gi,(this.dHeight-35)+"px"); this.elDialogTitle.innerHTML = title; this.elDialogBody.innerHTML = dataScheme; };this.show = function(visibleShadow) { var elDialogLeft = Math.ceil(((window.innerWidth!==undefined ? window.innerWidth : document.body.offsetWidth)-this.dWidth)/2); var elDialogTop  = Math.ceil(((window.innerHeight!==undefined ? window.innerHeight : screen.height-200)-this.dHeight)/2); 
/*@cc_on @if (@_jscript)
var offTop  = document.documentElement.scrollTop; var offLeft = document.documentElement.scrollLeft; 
@else */ var offTop  = window.pageYOffset; var offLeft = window.pageXOffset; /* @end @*/ this.elDialog.style.left = (offLeft+elDialogLeft)+'px'; this.elDialog.style.top  = (offTop+elDialogTop)+'px'; if(visibleShadow) { 
/*@cc_on @if (@_jscript)
this.elShadow.style.left = (offLeft)+'px'; this.elShadow.style.top  = (offTop)+'px'; 
@else */ this.elShadow.style.left = '0px'; this.elShadow.style.top  = '0px'; /* @end @*/ document.body.appendChild(this.elShadow); } this.elDialog.appendChild(this.elDialogTitle); this.elDialog.appendChild(this.elDialogBody); document.body.appendChild(this.elDialog); if(this.elDialog.getElementsByTagName('button')[0]) { this.elDialog.getElementsByTagName('button')[0].focus(); } }; this.mouseCoords = function(ev){ if(!ev && !window.event) { return false; } ev = (ev ? ev : window.event); if(ev && (ev.pageX || ev.pageY)) { return {x:ev.pageX, y:ev.pageY}; } return { x: (window.event.clientX + document.body.scrollLeft - document.body.clientLeft), y: (window.event.clientY + document.body.scrollTop  - document.body.clientTop) }; }; this.mouseMove = function(ev) { if(!ev && !window.event) { return false; } if(this.dragging) { ev = (ev ? ev : window.event); var mousePos = this.mouseCoords(ev); if(mousePos.x+Math.floor(this.dWidth/2)>document.body.clientWidth) { mousePos.x = document.body.clientWidth-Math.floor(this.dWidth/2); } if(mousePos.x-Math.floor(this.dWidth/2)<0) {mousePos.x = Math.floor(this.dWidth/2); } if(mousePos.y+Math.floor(this.dWidth/2)-15>document.body.clientHeight) { mousePos.y = document.body.clientHeight-Math.floor(this.dWidth/2)+15; } if(mousePos.y-15<0) { mousePos.y = 15; } this.elDialog.style.top      = (mousePos.y - 15)+"px"; this.elDialog.style.left     = (mousePos.x - Math.floor(this.dWidth/2))+"px"; if(ev && ev.target) { ev.preventDefault(); } } return false; }; this.mouseDown = function(ev) { if(!ev && !window.event) { return false; } ev = (ev ? ev : window.event); document.onselectstart=function(event){ window.event.returnValue=false; }; this.dragging  = true; document.onmousemove = new Function(this.obj+".mouseMove();"); if(ev && ev.target) { ev.preventDefault(); } return false; }; this.mouseUp = function(ev) { if(!ev && !window.event) { return false; } if(this.dragging) { document.onmousemove = null; document.onselectstart=null; if(ev && ev.target) { ev.preventDefault(); } this.dragging  = false; } return false; }; 
}
