/**
 * 	@author 
 *	Aravintha Krishnan Jagannathan
 *	Althaf Hussain
 *  Ramesh N P	
 * 	Popup Window
 */

 /*	Global Variables  Used for popup window*/
var popupMask = null;
var popupDivMask = null;
var popupContainer = null;
var popupIsShown = false;
var popFrame = null;
var returnFunction = null;
var btn1 = "btn1";  // btn1 - OK (Alert popup) or Yes (Confirm popup)
var btn2 = "btn2";  // btn2 - No (Confirm popup)
var lastButtonFocus = btn1;

//---------------------------For Popup window Drag-------------------------
if  (document.getElementById){
	(function(){
		var n = 500;
		var dragok = false;
		var y,x,dragObject,dy,dx;
		
		function move(e){
			 if (!e) e = window.event;
			 if (dragok){
				  dragObject.style.left = dx + e.clientX - x + "px";
				  dragObject.style.top  = dy + e.clientY - y + "px";
				  return false;
			 }
		}
		function down(e){			
			if(popupIsShown){
				if (!e) e = window.event;
				var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
				if (temp.tagName != "HTML"|"BODY" && temp.className != "dragClass"){
					 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
				}
				if (temp.className == "dragClass"){
					 temp=document.getElementById("popupContainer");				 
					 dragok = true;
					 dragObject = temp;
					 dx = parseInt(temp.style.left+0);
					 dy = parseInt(temp.style.top+0);
					 x = e.clientX;
					 y = e.clientY;
					 document.onmousemove = move;
					 return false;
				 }
			 }
		}	
		function up(){
			if(popupIsShown){
				dragok = false;
				document.onmousemove = null;
			}
		}
		document.onmousedown = down;
		document.onmouseup = up;
		})();
}

function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}

function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Sets the size of the popup mask.
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];		
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}	
	popupMask.style.height = popHeight + "px";
	popupMask.style.width = popWidth + "px";

	popupDivMask.style.height = popHeight + "px";
	popupDivMask.style.width = popWidth + "px";
}

function getScrollTop() {
	if (self.pageYOffset) {  // all except Explorer
		return self.pageYOffset;
	}else if (document.documentElement && document.documentElement.scrollTop) {		// Explorer 6 Strict
		return document.documentElement.scrollTop;
	}else if (document.body) { // all other Explorers
		return document.body.scrollTop;
	}
}

/* To Set Popup window in center of Screen */
function centerPopWin(width, height) {
	if (popupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = popupContainer.offsetWidth;
		}
		if (height == null) {
			height = popupContainer.offsetHeight;
		}
		var theBody = document.getElementsByTagName("BODY")[0];
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		setMaskSize();
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		popupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		popupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}

function hidePopWin(btnName){
	parent.document.getElementById("popupMask").style.display = "none";
	parent.document.getElementById("popupDivMask").style.display = "none";
	parent.document.getElementById("popupContainer").style.display = "none";

	parent.document.body.removeChild(parent.document.getElementById("popupMask"));
	parent.document.body.removeChild(parent.document.getElementById("popupDivMask"));
	parent.document.body.removeChild(parent.document.getElementById("popupContainer"));

	popupIsShown = false;
	if(returnFunction!=null)
		returnFunction(btnName);
}

function setButtonFocus(btnName){
	document.getElementById(btnName).focus();	
	lastButtonFocus = btnName;
}

function setPopupButtonEvent() {
	if(popupIsShown) {
		var e = window.event;
		var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
		if(window.event.keyCode == 9 || (window.event.keyCode >= 37 && window.event.keyCode <= 40)) {
			if(temp.name == btn1 && document.getElementById(btn2)) {
				setButtonFocus(btn2);
			}
			else {
				setButtonFocus(btn1);
			}
		}
		else {
			setButtonFocus(lastButtonFocus);
		}
		return false;
	}
}
/**
 * For toggle button image on on mouse over and out
 */
function setButtonStyle(btnObj,mousePos){
	if(mousePos==1){
		btnObj.className='alert_btn_o';
	}else{
		btnObj.className='alert_btn';
	}
}

/**
 * Initializes popup code on load.	
 */
function initMessageBox(messageBoxType) {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('iframe');
	popmask.src = "javascript:'<html/>';";
	popmask.id = 'popupMask';

	popupdivmask = document.createElement('div');
	popupdivmask.id = 'popupDivMask';

	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.className='dragClass';
	var popupHTMLString = '' +
		'<table cellspacing="0" cellpadding="0" class="popupTable">'+
        '<tr>'+
		'<td class="popupTableTD">'+
			'<div id="popupInner">'+
			'<span id="popupTitleBar" class="dragClass" >'+
				'<div id="popupTitle" style="width:100%;"></div>'+
			'</span>'+
			'<div style="width:100%;height:100%;cursor:auto;" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%">';
			
				switch(messageBoxType) {
					case 1:
						popupHTMLString += 
						'<table style="width:100%;height:100%;" cellpadding="1" border="0">'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr>'+
								'<td width="100%" class="regularText" height="100%" colspan="2" align="center">'+
									'<div style="width:100%;height:100%;cursor:auto;" allowtransparency="true" id="popupMessage" name="popupMessage"/>'+
								'</td>'+
							'</tr>'+
							'<tr>'+
								'<td width="100%" class="regularText" height="100%" colspan="2" align="center">'+
									'<img src="/mtracker/resources/images/loading_bar.gif" />'+
								'</td>'+
							'</tr>'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr><td colspan="2"><table cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#1B276A" style="height:2px"><td/></tr></table></td></tr>'+
							'<tr>'+
								'<td colspan="2" align="right">'+
									'<input type="button" class="alert_btn" onMouseOver="setButtonStyle(this,1)" onMouseOut="setButtonStyle(this,2)"  value="Hide" name="btn1" id="btn1" onclick="hidePopWin('+ btn1 + ')">'+
								'</td>'+
							'</tr>'+
						'</table>';
						break;
					case 2:
						popupHTMLString += 
						'<table style="width:100%;height:100%;" cellpadding="1" border="0">'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr>'+
								'<td width="15%" align="center" valign="top">'+
									'<div id="alertImage"/>'+
								'</td>'+
								'<td width="85%" class="regularText" height="100%">'+
									'<div style="width:100%;height:100%;cursor:auto;" allowtransparency="true" id="popupMessage" name="popupMessage"/>'+
								'</td>'+
							'</tr>'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr><td colspan="2"><table cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#1B276A" style="height:2px"><td/></tr></table></td></tr>'+
							'<tr>'+
								'<td colspan="2" align="right">'+
									'<input type="button" class="alert_btn" onMouseOver="setButtonStyle(this,1)" onMouseOut="setButtonStyle(this,2)" value="Ok" name="btn1" id="btn1" onclick="hidePopWin('+btn1+')">'+
								'</td>'+
							'</tr>'+
						'</table>';
						break;
					case 3:
						popupHTMLString += 
						'<table style="width:100%;height:100%;" cellpadding="1" border="0">'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr>'+
								'<td width="15%" align="center" valign="top">'+
									'<div  id="alertImage"/>'+
								'</td>'+
								'<td width="85%" class="regularText" height="100%">'+
									'<div style="width:100%;height:100%;cursor:auto;" allowtransparency="true" id="popupMessage" name="popupMessage"/>'+
								'</td>'+
							'</tr>'+
							'<tr style="height:10px;"><td colspan="2"/></tr>'+
							'<tr><td colspan="2"><table cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#1B276A" style="height:2px"><td/></tr></table></td></tr>'+
							'<tr>'+
								'<td colspan="2" align="right">'+
									'<table cellspacing="0" cellpadding="1" border="0">'+
										'<tr>'+
											'<td>'+
												'<input type="button" class="alert_btn" onMouseOver="setButtonStyle(this,1)" onMouseOut="setButtonStyle(this,2)"  value="No" name="btn2" id="btn2" onclick="hidePopWin('+btn2+')">'+
											'</td>'+
											'<td width="2px">'+
											'</td>'+
											'<td>'+
												'<input type="button" class="alert_btn" onMouseOver="setButtonStyle(this,1)" onMouseOut="setButtonStyle(this,2)"  value="Yes" name="btn1" id="btn1" onclick="hidePopWin('+btn1+')">'+
											'</td>'+
										'</tr>'+
									'</table>'+
								'</td>'+
							'</tr>'+
						'</table>';
						break;
				}

		popupHTMLString +='</div>'+
		'</td></tr></table>';
	popcont.innerHTML = popupHTMLString;	
	theBody.appendChild(popmask);
	theBody.appendChild(popupdivmask);
	theBody.appendChild(popcont);
	
	popupMask = document.getElementById("popupMask");
	popupMask.oncontextmenu=new Function("return false");
	
	popupDivMask = document.getElementById("popupDivMask");
	popupDivMask.oncontextmenu=new Function("return false");
	
	popupContainer = document.getElementById("popupContainer");
	popFrame = document.getElementById("popupFrame");	
}

function setMessageBoxProperty(dialogTitle, width, height, retFunction){
		returnFunction=retFunction;	
		popupMask.style.display = "block";
		popupDivMask.style.display = "block";
		popupContainer.style.display = "block";
		
		// calculate where to place the window on screen
		centerPopWin(width, height);
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		popupContainer.style.width = width + "px";
		popupContainer.style.height = (height+titleBarHeight) + "px";
		setMaskSize();
		popFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
		popFrame.style.height = (height) + "px";
			
		document.getElementById("popupTitle").innerText=dialogTitle;	
		setButtonFocus(btn1);	
}
/***
*	To Set Popup box Alert Image & set message
***/
function displayMessageBox(msgAlertType,message){
	var alertImg=document.getElementById("alertImage");
	if(msgAlertType=="ERROR")
		alertImg.innerHTML='<img src="/mtracker/resources/images/error.bmp">';
	else if(msgAlertType=="HELP")
		alertImg.innerHTML='<img src="/mtracker/resources/images/ico_help.gif">';
	else if(msgAlertType=="CONFIRM")
		alertImg.innerHTML='<img src="/mtracker/resources/images/question.png" >';
	else	
		alertImg.innerHTML='<img src="/mtracker/resources/images/info.bmp">';		
	document.getElementById("popupMessage").innerHTML = message;		
}

/**
	* @argument dialogTitle - String - MessageBox Title
	* @argument alertMessage - String (Display Meassage)
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument retFunction - function (to be executed while hiding the loading bar)
*/
function showLoadingBar(dialogTitle, message, width, height, retFunction) {
	if (popupIsShown == false) {
		popupIsShown = true;
		initMessageBox(1);
		setMessageBoxProperty(dialogTitle, width, height, retFunction);		
		document.getElementById("popupMessage").innerHTML = message;
		setButtonFocus(btn1);	
	}
}

/**
	* @argument msgAlertType - String (ERROR,INFO,HELP)	
	* @argument dialogTitle - String - MessageBox Title
	* @argument alertMessage - String (Display Meassage)
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument retFunction - function (to be executed while hiding the alert box)
*/
function showAlertBox(msgAlertType, dialogTitle, alertMessage, width, height, retFunction) {
	if (popupIsShown == false) {
		popupIsShown = true;
		initMessageBox(2);
		setMessageBoxProperty(dialogTitle, width, height, retFunction);		
		displayMessageBox(msgAlertType,alertMessage);		
		setButtonFocus(btn1);	
	}
}

/**
	* @argument dialogTitle - String - MessageBox Title
	* @argument alertMessage - String (Display Meassage)
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument retFunction - function (to be executed while hiding the confirm box)
*/
function showConfirmBox(dialogTitle, alertMessage, width, height, retFunction) {
	if (popupIsShown == false) {
		popupIsShown = true;
		initMessageBox(3);
		setMessageBoxProperty(dialogTitle, width, height, retFunction);		
		displayMessageBox("CONFIRM",alertMessage);	
		setButtonFocus(btn1);	
	}
}

//----------------function for method overloading--------------------
function addMethod(object, name, fn){
	var old = object[name];		
	if ( old )
	    object[ name ] = function(){
	        if ( fn.length == arguments.length )
	            return fn.apply( this, arguments );
	        else if ( typeof old == 'function' )
	            return old.apply( this, arguments );
	    };
	else
	    object[ name ] = fn;			
}

function MoveoMessageBox() {
	var defWidth=300;
	var defHeight=100;

	addMethod(this, "loadingBar", function(){
		showLoadingBar("Status","Loading...<br/><br/>",defWidth,defHeight,null);
	});
	addMethod(this, "loadingBar", function(dialogTitle,alertMessage,width,height,retFunction){
		showLoadingBar(dialogTitle,alertMessage,width,height,retFunction);
	});
	addMethod(this, "loadingBar", function(dialogTitle,alertMessage,retFunction){
		showLoadingBar(dialogTitle,alertMessage,defWidth,defHeight,retFunction);
	});
	addMethod(this, "loadingBar", function(dialogTitle,alertMessage){
		showLoadingBar(dialogTitle,alertMessage,defWidth,defHeight,null);
	});	
	
	addMethod(this, "hideLoadingBar", function(){
		if(popupIsShown){
			hidePopWin(btn1);
		}
	});
	
	addMethod(this, "alert", function(){
		showAlertBox("HELP","Alert","This place is for alert message",defWidth,defHeight,null);
	});
	addMethod(this, "alert", function(msgAlertType,dialogTitle,alertMessage,width,height,retFunction){
		showAlertBox(msgAlertType,dialogTitle,alertMessage,width,height,retFunction);
	});
	addMethod(this, "alert", function(msgAlertType,dialogTitle,alertMessage,retFunction){
		showAlertBox(msgAlertType,dialogTitle,alertMessage,defWidth,defHeight,retFunction);
	});
	addMethod(this, "alert", function(msgAlertType,dialogTitle,alertMessage){
		showAlertBox(msgAlertType,dialogTitle,alertMessage,defWidth,defHeight,null);
	});	

	addMethod(this, "confirm", function(){
		showConfirmBox("Confirm","This place is for confirm message",defWidth,defHeight,null);
	});
	addMethod(this, "confirm", function(dialogTitle,alertMessage,width,height,retFunction){
		showConfirmBox(dialogTitle,alertMessage,width,height,retFunction);
	});
	addMethod(this, "confirm", function(dialogTitle,alertMessage,retFunction){
		showConfirmBox(dialogTitle,alertMessage,defWidth,defHeight,retFunction);
	});
	addMethod(this, "confirm", function(dialogTitle,alertMessage){
		showConfirmBox(dialogTitle,alertMessage,defWidth,defHeight,null);
	});	
}

var mMsgBox = new MoveoMessageBox();
