var BrowserDetect = { 
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) !== -1) return data[i].identity;
				}
			else if (dataProp) return data[i].identity;
			}
		},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
	dataBrowser: [
		{string: navigator.vendor, subString: "Apple", identity: "Safari"},
		{prop: window.opera, identity: "Opera"},
		{string: navigator.vendor, subString: "iCab", identity: "iCab"},
		{string: navigator.vendor, subString: "KDE", identity: "Konqueror"},
		{string: navigator.userAgent, subString: "Firefox", identity: "Firefox"},
		{string: navigator.userAgent, subString: "Netscape", identity: "Netscape"},	// for newer Netscapes (6+)
		{string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE"},
		{string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv"},
		{string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla"} // for older Netscapes (4-)
		],
	dataOS : [
		{string: navigator.platform, subString: "Win", identity: "Windows"},
		{string: navigator.platform, subString: "Mac", identity: "Mac"},
		{string: navigator.platform, subString: "Linux", identity: "Linux"}
		]
	};

BrowserDetect.init();


	   // Variables
       var modalDiv = null;
       var messageDiv = null;
       var messageIframe = null;
       var newcloseIcon = null;

       var ImgPath = "/runjindian/wireframe_images/";
      
	function showWebMessageBox(width,height,title,message,isUrl) 
       {
       if (messageDiv != null)
       	{return false;}
       
       var originalW = width;
       var originalH = height;
       
       
        var topPad=8;
	    var bottomPad=5;
       
       	var maxWidht=968;
       	//var maxHeight=508;
       	var maxHeight=600;
       	
       	var minWidht=300;
       	var minHeight=150;
              
            
        messageDiv = document.createElement('div');
        
        
		
	      
        
        if ( width < minWidht ) width = minWidht;
        if ( height < minHeight) height = minHeight;  
        
        var mainTable = document.createElement('table');
		mainTable.setAttribute('cellSpacing', '0');
		mainTable.setAttribute('cellPadding', '0');
		mainTable.setAttribute('border', '0');
		mainTable.setAttribute('width', '100%');
		mainTable.setAttribute('height', '100%');

		var tBodyM = document.createElement('tbody');
		var rowM = document.createElement('tr');
		var cellM = document.createElement('td');

		//*********** BEGIN Title TABLE ***********
		var titleTable = document.createElement('table');
		titleTable.setAttribute('cellSpacing', '0');
		titleTable.setAttribute('cellPadding', '0');
		titleTable.setAttribute('border', '0');
		titleTable.setAttribute('width', '100%');

		var tBodyT = document.createElement('tbody');
		var rowT = document.createElement('tr');
		var cellT = document.createElement('td');
		cellT.className = "tbLeft"; 			// CSS className
		rowT.appendChild(cellT);

		var titleCell = document.createElement('td');
		titleCell.className = "Title";			// CSS className
		rowT.appendChild(titleCell);

		cellT = document.createElement('td');
		cellT.style.paddingTop = topPad + "px";
		cellT.style.paddingBottom = bottomPad + "px";
		cellT.className = "tbRight";			// CSS className

		initCloseIcon()
		var closeIcon = document.createElement('img');
		closeIcon.src = newcloseIcon.src;
		closeIcon.setAttribute('border','0');
		closeIcon.dialogBox = this;
		
		//var closeText = document.createTextNode(Close_MsgBox);	
		//var closeText = document.createTextNode(Close_MsgBox);		
		var aLink = document.createElement('A');
		aLink.setAttribute('id','Close');
		if(typeof(Close_HD)!="undefined")			
			aLink.innerHTML = Close_HD;
		else
			aLink.innerHTML = 'X';
		aLink.setAttribute('href','#');
		//aLink.appendChild(closeText);
		//aLink.appendChild(closeIcon);
		cellT.appendChild(aLink);
		aLink.onclick=closeBox;

		rowT.appendChild(cellT);

		tBodyT.appendChild(rowT);
		titleTable.appendChild(tBodyT);
		//*********** END Title TABLE ***********

		cellM.appendChild(titleTable);
		rowM.appendChild(cellM);
		tBodyM.appendChild(rowM);

		rowM = document.createElement('tr');
		cellM = document.createElement('td');
		cellM.className = "MainPanel";			// CSS className	
		
		var contentArea = document.createElement('div');
		contentArea.className = "ContentArea";	// CSS className
		messageDiv.appendChild(contentArea);

		rowM.appendChild(cellM);
		tBodyM.appendChild(rowM);
		mainTable.appendChild(tBodyM);
		messageDiv.appendChild(mainTable);
        
        
        //***** Set the title 
        if (title== '') title="&nbsp";
		titleCell.innerHTML = title;
		

		if (isUrl)
			{border=2;
			urlIframe = document.createElement('iframe');
			urlIframe.style.width = width - border + "px";
			urlIframe.style.border = 0;
			urlIframe.setAttribute("frameborder", "no");
			urlIframe.setAttribute("frameBorder", "0");
			urlIframe.className = "urlIframe";			// CSS className
			urlIframe.allowTransparency = true; /* IE only */
			urlIframe.style.backgroundColor = 'transparent';
			urlIframe.style.height = height - newcloseIcon.height - topPad - bottomPad - border + "px";
			cellM.appendChild(urlIframe);			
			urlIframe.src = message;
			
			
	       
	        if(originalW==-1 && originalH==-1)
			{
	        	
	        	messageDiv.className = "divHidden MessageDivFull";
			}
	        else{
	        	messageDiv.className = "MessageDivFull"; // CSS className
	        }
	    	
			messageDiv.id = "MessageDivFull";
			
			
			
			
			bodyText=urlIframe;}
		else
			{
			//width=minWidht;
        	//height=minHeight;   
			border=10;
			textDiv = document.createElement('div');
			textDiv.style.border = 0;
			textDiv.style.margin = "6px";
			textDiv.style.overflow = 'auto';
			textDiv.style.width = width - border + "px";
			cellM.appendChild(textDiv);
			cellM.style.verticalAlign= 'middle';
			
			titleCell.style.cursor="move";
			
			textDiv.innerHTML = message;
			
			messageDiv.id = "MessageDivTrasp";
			messageDiv.className = "MessageDivTrasp"; // CSS className
			
			bodyText=textDiv;}
			
		messageDiv.style.width = width + "px";
		
        messageDiv.style.height = height + "px";
        	
		if (!isUrl){	
		//Messagebox Autosize
		var check = new  Array(2);
		
		while  (bodyText.scrollHeight > bodyText.clientHeight)
		//while  (bodyText.document.body.scrollHeight > bodyText.clientHeight)
			{widthDelta = parseInt(width/100*10);
			heightDelta = parseInt(height/100*10);
			
			width =  width + widthDelta;
			height = height + heightDelta;
			
			if (width < maxWidht)
				{messageDiv.style.width = width + "px";
				textDiv.style.width = width - border + "px";
				check[0]=true;
				}
			else
				{check[0]=false;}
				
			if (height < maxHeight)
        		{messageDiv.style.height = height + "px";
				textDiv.style.height = height - newcloseIcon.height - topPad - bottomPad - border + "px";
				check[1]=true;
				}
			else
				{check[1]=false;}
				
			if (check[0]==false && check[1]==false) 
				{break;}
			}
		}
		
		
		
		var TotH = document.body.clientHeight;
		var TotW = document.body.clientWidth;
		
		if(TotH==0){
		 TotH = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
		}
		
		if(TotW==0){
		 TotW = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
		}
		
		messageDiv.style.left=((TotW - width) / 2) + "px"; 
        messageDiv.style.top= ((TotH - height) / 2)  + "px";
	
        messageIframe = document.createElement('iframe');
        messageIframe.style.border = 0;
        messageIframe.id = "MessageIframe";	
        messageIframe.className = "MessageIframe";	// CSS className
        messageIframe.style.width = messageDiv.style.width;
        messageIframe.style.height = messageDiv.style.height;
        messageIframe.style.left= messageDiv.style.left; 
        messageIframe.style.top= messageDiv.style.top;
        messageIframe.src="javascript:false;";
        
        
        //modalDiv
        modalDiv = document.createElement('div');
        modalDiv.id = 'coverpanel' ;
        modalDiv.className = 'coverpanel' ;		// CSS className
        modalDiv.style.left = 0;
        modalDiv.style.top = 0;
        var mw= Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), document.body.scrollWidth);
        var mh= Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), document.body.scrollHeight);
        if (BrowserDetect.browser=='Explorer') 
        	{ mw = mw-25;
        	  mh = mh-25;
        	}
        modalDiv.style.width = '100%';
        modalDiv.style.height = '100%';
        
		// render to screen
		document.body.appendChild(modalDiv);
      	document.body.appendChild(messageDiv);
      	document.body.appendChild(messageIframe);
      
       if (BrowserDetect.browser=='Explorer')  
           {
           messageDiv.style.position = 'absolute';
           messageIframe.style.position = 'absolute';
           modalDiv.style.position = 'absolute'; 
           }                       
       else {
           messageDiv.style.position = 'fixed';
           messageIframe.style.position = 'fixed';
           modalDiv.style.position = 'fixed'; 
           }                       
       modalDiv.style.zIndex = 10000;
       messageDiv.style.zIndex = modalDiv.style.zIndex + 2;
	   messageIframe.style.zIndex = modalDiv.style.zIndex + 1;
	   
	   messageDiv.focus();
	   
	   if (!isUrl)
       		{Drag.init(titleCell, messageDiv , messageIframe, 0, null, 0);} 
       }        

/************ BEGIN: Private Methods ************/

 function initCloseIcon() {
	// pre-fetch this icon so it doesn't distort dialog box size
	if (newcloseIcon == null) {
		newcloseIcon = new Image();
		newcloseIcon.src = ImgPath + "Close.gif";
		}
	}


 closeBox= function (e) {
	if (!e) e = window.event; 
	
	
	if(document.getElementById("MessageDivTrasp"))
	{
		modalDiv = document.getElementById("coverpanel");
		messageDiv = document.getElementById("MessageDivTrasp");
		messageIframe = document.getElementById("MessageIframe");
		
	}
		
	
	document.body.removeChild(messageDiv);
	document.body.removeChild(messageIframe);
	document.body.removeChild(modalDiv);
	messageDiv=null;
	messageIframe=null;
    modalDiv =null;
	return false;
	}

  
      
