//=========================================
//Override browser dialogs
//Copyright DominosMania.com
//=========================================

var dialogType = {
	 INFO:0
	,WARNING:1
	,ALERT:2
	,CONFIRM:3
	,POPUP:4
}

var dialogSize = {
	 w:300
	,h:130
	,titleH:20
	,messageH:70
}

var appTitle = 'DominosMania';

function alert(sMsg) {
	showDialog(sMsg, dialogType.WARNING);
}

function confirm(sMsg,yesCB,noCB) {
	var dialog = showDialog(sMsg, dialogType.CONFIRM);
	dialog.yesCB = yesCB;
	dialog.noCB = noCB;
}

function showDialog(sMsg, dlgType, sTitle) {
	var dialogW = dialogSize.w;
	var dialogH = dialogSize.h;
	var dialogTitleH = dialogSize.titleH;
	var dialogBodyH = dialogH - dialogTitleH - (!_ie?2:0);
	var dialogMsgH = dialogSize.messageH;
	
	sTitle = (sTitle != null)? sTitle : appTitle;
	
	dlgType = (dlgType != null)? dlgType : dialogType.WARNING;
	var iconSrc = 'site/skin/';
	switch(dlgType) {
		case dialogType.INFO:
			iconSrc += 'info.png';
			break;
		case dialogType.WARNING:
			iconSrc += 'warning.gif';
			break;
		case dialogType.ALERT:
			iconSrc += 'alert.gif';
			break;
		case dialogType.CONFIRM:
			iconSrc += 'question.png';
			break;
	}
	
	var getHTMLMsg = function() {
		var sHTML = '';
		if (dlgType == dialogType.POPUP) {
			sHTML = sMsg;
		}
		else {
			var h = new Array();
			var j = 0;
			h[j++] = '<table border="0" height="' + dialogMsgH + '" cellpadding="0" cellspacing="0">'
			h[j++] =  '<tr>';
			h[j++] =   '<td width="50" align="center" valign="middle">';
			h[j++] =    '<img id="dialogIcon" src="' + iconSrc + '" border="0" vspace="0" hspace="0" width="24" height="24"></img>';
			h[j++] =   '</td>';
			h[j++] =   '<td valign="middle">';
			h[j++] =    '<span id="dialogMsg" style="width:' + (dialogW - 50 - 2)+ 'px;font-size:12px">' + sMsg + '</span>';
			h[j++] =   '</td>';
			h[j++] =  '</tr>';
			h[j++] = '</table>';
			sHTML = h.join('');
		}
		return sHTML;
	}
	
	var dialog = document.getElementById("dialogBox");
	if (dialog == null) {
		//Create BG image
		var bgImg = document.createElement("IMG");
		bgImg.id = "dialogBGImage";
		bgImg.src = 'site/skin/flou.png';
		bgImg.onselectstart = doNothing;
		bgImg.ondragstart = doNothing;
		bgImg.height = getWinHeight();
		bgImg.width = getWinWidth();
		var imgSt = bgImg.style;
		imgSt.position = "absolute";
		imgSt.top = getScrollY();
		imgSt.left = getScrollX();
		imgSt.zIndex = 900;
		document.body.appendChild(bgImg);
		
		//Create BG iframe
		var bgiFrame = document.createElement("IFRAME");
		bgiFrame.id = "dialogBGiFrame";
		bgiFrame.src = "site/html/empty.html";
		bgiFrame.setAttribute("frameBorder", "0");
		bgiFrame.setAttribute("scrolling", "no");
		var iFrameSt = bgiFrame.style;
		iFrameSt.position = "absolute";
		iFrameSt.width = dialogW;
		iFrameSt.height = dialogH;
		iFrameSt.zIndex = 950;
		
		//Create div dialog
		dialog = document.createElement("DIV");
		dialog.id = "dialogBox";
		dialog.setAttribute("dlgType", dlgType);
		var dialogSt = dialog.style;
		dialogSt.position = "absolute";
		dialogSt.visibility = "visible";
		dialogSt.width = dialogW;
		dialogSt.height = dialogH;
		dialogSt.zIndex = 1000;
		
		var posX = (getWinHeight()/2) - (dialogH/2);
		var posY = (getWinWidth()/2) - (dialogW/2);
		
		//Center dialog
		dialogSt.top = posX;
		dialogSt.left = posY;
		//Center BG iFrame
		iFrameSt.top = posX;
		iFrameSt.left = posY;
		
		document.body.appendChild(bgiFrame);
		document.body.appendChild(dialog);
		
		var s = new Array();
		var i = 0;
		s[i++] = '<table border="0" width="100%" height="100%" cellpadding="0" cellspacing="0">';
		s[i++] =  '<tr>';
		s[i++] =   '<td valign="top" style="border:1px solid orange">';
		s[i++] =    '<table border="0" width="100%" cellpadding="0" cellspacing="0" style="background-color:white">';
		s[i++] =     '<tr>';
		s[i++] =      '<td style="background-color:orange">';
		s[i++] =       '<table border="0" width="100%" height="' + dialogTitleH + '" cellpadding="0" cellspacing="0">';
		s[i++] =        '<tr>';
		s[i++] =         '<td align="left" valign="middle">';
		s[i++] =          '<span id="dialogTitle" style="padding-left:10px;font-size=12px;color:white">' + sTitle + '</span>';
		s[i++] =         '</td>';
		s[i++] =         '<td align="right" valign="middle" style="padding-right:2px">';
		s[i++] =          '<a href="javascript:dialogBox_close()">';
		s[i++] =           '<img id="dialogCrossImg" src="site/skin/close.png" width="16" height="16" title="Fermer" border="0" vspace="0" hspace="0"></img>';
		s[i++] =          '</a>';
		s[i++] =         '</td>';
		s[i++] =        '</tr>';
		s[i++] =       '</table>';
		s[i++] =      '</td>';
		s[i++] =     '</tr>';
		s[i++] =     '<tr>';
		s[i++] =      '<td>';
		s[i++] =       '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
		s[i++] =        '<tr>';
		s[i++] =         '<td id="dialogBody" valign="top" style="height:' + dialogBodyH + 'px">';
		s[i++] =          '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
		s[i++] =           '<tr>';
		s[i++] =            '<td>';
		s[i++] =             '<img src="site/skin/blanc.gif" width="10" height="10">';
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =           '<tr>';
		s[i++] =            '<td id="dialogMsgZone">';
		s[i++] =             getHTMLMsg();
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =           '<tr>';
		s[i++] =            '<td>';
		s[i++] =             '<img src="site/skin/blanc.gif" width="10" height="10">';
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =           '<tr id="dialogAlertCmd"' + ((dlgType == dialogType.CONFIRM)?' style="display:none"':'') + '>';
		s[i++] =            '<td align="center">';
		s[i++] =             '<input id="dialogCloseBtn" type="button" class="button" name="btnDlgClose" value="Fermer" onclick="dialogBox_close()" onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'auto\'">';
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =           '<tr id="dialogConfirmCmd"' + ((dlgType != dialogType.CONFIRM)?' style="display:none"':'') + '>';
		s[i++] =            '<td align="center">';
		s[i++] =             '<table border="0" height="0" cellpadding="0" cellspacing="0">'
		s[i++] =              '<tr>';
		s[i++] =               '<td align="right">';
		s[i++] =                '<input id="dialogYesBtn" type="button" class="button" style="width:50px" name="btnDlgYes" value="Oui" onclick="dialogBox_confirm(\'Y\')" onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'auto\'">';
		s[i++] =               '</td>';
		s[i++] =               '<td width="10"></td>';
		s[i++] =               '<td align="left">';
		s[i++] =                '<input id="dialogNoBtn" type="button" class="button" style="width:50px" name="btnDlgNo" value="Non" onclick="dialogBox_confirm(\'N\')" onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'auto\'">';
		s[i++] =               '</td>';
		s[i++] =              '</tr>';
		s[i++] =             '</table>';
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =           '<tr>';
		s[i++] =            '<td>';
		s[i++] =             '<img src="site/skin/blanc.gif" width="10" height="10">';
		s[i++] =            '</td>';
		s[i++] =           '</tr>';
		s[i++] =          '</table>';
		s[i++] =         '</td>';
		s[i++] =        '</tr>';
		s[i++] =       '</table>';
		s[i++] =      '</td>';
		s[i++] =     '</tr>';
		s[i++] =    '</table>';
		s[i++] =   '</td>';
		s[i++] =  '</tr>';
		s[i++] = '</table>';
		
		dialog.innerHTML = s.join('');
	}
	else {
		//Show background image
		var bgImg = document.getElementById("dialogBGImage");
		bgImg.height = getWinHeight();
		bgImg.width = getWinWidth();
		var imgSt = bgImg.style;
		imgSt.top = getScrollY();
		imgSt.left = getScrollX();
		
		//Dialog position
		var posX = (getWinHeight()/2) - (dialogH/2);
		var posY = (getWinWidth()/2) - (dialogW/2);
		
		//Show background iFrame
		var bgiFrame = document.getElementById("dialogBGiFrame");
		bgiFrame.style.top = posX;
		bgiFrame.style.left = posY;
		bgiFrame.style.visibility = "visible";
		
		//Set title
		var dlgTitle = document.getElementById("dialogTitle");
		dlgTitle.innerHTML = sTitle;
		
		//Set message HTML
		var dlgMsg = document.getElementById("dialogMsgZone");
		dlgMsg.innerHTML = getHTMLMsg();
		
		//Set Icon
		var dlgIcon = document.getElementById("dialogIcon");
		if (dlgIcon) {
			dlgIcon.src = iconSrc;
		}
		
		//Select buttons
		var alertCmd = document.getElementById("dialogAlertCmd");
		alertCmd.style.display = (dlgType == dialogType.CONFIRM)?'none':'';
		var confirmCmd = document.getElementById("dialogConfirmCmd");			
		confirmCmd.style.display = (dlgType == dialogType.CONFIRM)?'':'none';
		
		//Center dialog
		dialog.style.top = posX;
		dialog.style.left = posY;
		
		//Show dialog
		dialog.style.visibility = "visible";
	}
	//Keep dlgType info
	dialog.setAttribute("dlgType", dlgType);
	//Init dialog events
	setTimeout("dialogBox_events()", 100);
	
	return dialog;
}

function dialogBox_events() {
	var dialog = document.getElementById("dialogBox");
	var dlgType = dialog.getAttribute("dlgType");
	if (dlgType == dialogType.CONFIRM) {
		var btn = document.getElementById("dialogYesBtn");
		if (btn) {
			btn.focus();
		}
	}
	else {
		dialog.focus();
	}
	dialog.onkeypress = dialogBox_keypress;
}

function dialogBox_keypress(e) {
	//Get keyCode
	var k = _ie?window.event.keyCode:e.keyCode;
	if (k == 27) {	//Escape
		dialogBox_close();	
	}
	else if (k == 13) { //Enter
		var dialog = document.getElementById("dialogBox");
		var dlgType = dialog.getAttribute("dlgType");
		if (dlgType != dialogType.CONFIRM) {
			dialogBox_close();
		}
	}
}

function dialogBox_close() {
	//hide dialo
	var dialog = document.getElementById("dialogBox");
	var dialogSt = dialog.style;
	dialogSt.visibility = "hidden";
	dialogSt.top = 0;
	dialogSt.left = 0;
	//BG iFrame
	var bgiFrame = document.getElementById("dialogBGiFrame");
	var iFrameSt = bgiFrame.style;
	iFrameSt.visibility = "hidden";
	iFrameSt.top = 0;
	iFrameSt.left = 0;
	//BG Image
	var bgImg = document.getElementById("dialogBGImage");
	bgImg.style.visibility = "visible";
	bgImg.width = 1;
	bgImg.height = 1;
	var imgSt = bgImg.style;
	imgSt.top = 0;
	imgSt.left = 0; 
}

function dialogBox_confirm(r) {
	var dialog = document.getElementById("dialogBox");
	dialogBox_close();
	switch(r) {
		case 'Y':
			if (dialog.yesCB != null) {
				dialog.yesCB();
			}
			break;
		case 'N':
			if (dialog.noCB != null) {
				dialog.noCB();
			}
			break;
	}
}

function doNothing() {
	return false;
}


//Listen to resize event
addListener("onresize", onDialogBox_resize);

function onDialogBox_resize() {
	var dialog = document.getElementById("dialogBox");
	if (dialog != null) {
		var dialogSt = dialog.style;
		if (dialogSt.visibility == "visible") {
			//Resize background image		
			var bgImg = document.getElementById("dialogBGImage");
			bgImg.height = getWinHeight();
			bgImg.width = getWinWidth();
			var imgSt = bgImg.style;
			imgSt.top = getScrollY();
			imgSt.left = getScrollX();
			//Dialog position
			var posX = (getWinHeight()/2) - (dialog.offsetHeight/2);
			var posY = (getWinWidth()/2) - (dialog.offsetWidth/2);
			//Center dialog
			dialogSt.top = posX;
			dialogSt.left = posY;
			//Center BG iFrame
			var bgiFrame = document.getElementById("dialogBGiFrame");
			var iFrameSt = bgiFrame.style;
			iFrameSt.top = posX;
			iFrameSt.left = posY;
		}
	}
}

//Listen to scroll event
addListener("onscroll", onDialogBox_scroll);

function onDialogBox_scroll() {
	var bgImg = document.getElementById("dialogBGImage");
	if (bgImg != null) {
		var imgSt = bgImg.style;
		imgSt.top = getScrollY();
		imgSt.left = getScrollX();
	}
}

function getWinWidth() {
	return _ie?window.document.body.clientWidth:window.innerWidth;
}

function getWinHeight() {
	return _ie?window.document.body.clientHeight:window.innerHeight;
}


function getScrollX() {		
	return _ie?window.document.body.scrollLeft:Math.max(0,window.scrollX-18);
}

function getScrollY() {
	return _ie?window.document.body.scrollTop:Math.max(0,window.scrollY-18);
}

