/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         * 
 *  @package messageBox                                                    * 
 *  @file messageBox                                                       * 
 *  @version 1.0                                                           * 
 *  @author Özkan KONAY <ozkankonay@gmail.com>                             * 
 *  @created 2011-07-28                                                    * 
 *  @lastUpdated 2011-07-29                                                * 
 *                                                                         * 
 *  Copyright (C) 2011 Özkan KONAY                                         * 
 *                                                                         * 
 *  This program is free software: you can redistribute it and/or modify   * 
 *  it under the terms of the GNU General Public License as published by   * 
 *  the Free Software Foundation, either version 3 of the License, or      * 
 *  (at your option) any later version.                                    * 
 *                                                                         * 
 *  This program is distributed in the hope that it will be useful,        * 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         * 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          * 
 *  GNU General Public License for more details.                           * 
 *                                                                         * 
 *  You should have received a copy of the GNU General Public License      * 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  * 
 *                                                                         * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

(function(){
	var messageBox = null, 
		messageBoxTimer = null,
		messageBoxLoaderImageUrl = "loader.gif",
		messageImageUrl = "news.jpg",
		messageBgOpacitiy = 0.75,
		messageBoxWaitingTime = 3000;
	
	function initMessageBox(){
		messageBox = document.createElement("div");
		messageBox.id = "messageBox";
		document.body.appendChild(messageBox);
		
		messageBox.background = document.createElement("div");
		messageBox.background.className = "bg";
		if("opacity" in messageBox.background.style){
			messageBox.background.style.opacity = messageBgOpacitiy;
		} else if("filter" in messageBox.background.style) {
			messageBox.background.style.filter = "alpha(opacity="+ (messageBgOpacitiy*100) +")";
		}
		messageBox.appendChild(messageBox.background);
		
		
		messageBox.content = document.createElement("div");
		messageBox.content.innerHTML = "<img alt=\"\" src=\""+ messageBoxLoaderImageUrl +"\" border=\"0\" />";		
		
		
		messageBox.box = document.createElement("table");
		messageBox.box.cellPadding = 0;
		messageBox.box.cellSpacing = 0;
		messageBox.box.border = 0;
		messageBox.box.className = "box";
		messageBox.appendChild(messageBox.box);
		
		var boxRow = messageBox.box.insertRow(-1), 
		boxCell=null, 
		boxCellCssNames = ["top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right"];;
		for(var i=0; i < 9; i++){			
			if(i > 0 && i % 3 == 0){
				boxRow = messageBox.box.insertRow(-1);
			}
		
			boxCell = boxRow.insertCell(-1);
			boxCell.className = boxCellCssNames[i];
			if(boxCell.className === "center"){
				boxCell.appendChild(messageBox.content);
			}else{
				boxCell.innerHTML = "&nbsp;"
			}
		}
		
		
		centerBox();
		
		setTimeout(loadContentImage, 200);
		
		messageBox.background.onclick = closeMessageBox;
	}
	
	function loadContentImage(){
		var img = new Image();
		img.border="0";
		img.onload = function(){
			if(!messageBox) return;
			messageBox.content.innerHTML = "";
			messageBox.content.appendChild(this);
			centerBox();
			
			setMessageBoxTimer();
		}
		img.src = messageImageUrl;
	}
	
	function centerBox(){
		if(!messageBox) return;
 		messageBox.box.style.top = ((messageBox.background.offsetHeight - messageBox.box.offsetHeight) / 2) + "px";
		messageBox.box.style.left = ((messageBox.background.offsetWidth - messageBox.box.offsetWidth) / 2) + "px"; 
	}
	
	function closeMessageBox(){
		if(messageBoxTimer) {
			clearTimeout(messageBoxTimer);
			messageBoxTimer = null;
		}
	
		if(!messageBox) return;
		document.body.removeChild(messageBox);
		messageBox = null;
	}
	
	function setMessageBoxTimer(){
		if(messageBoxWaitingTime>0){
			messageBoxTimer = setTimeout(function(){
				closeMessageBox();
			}, messageBoxWaitingTime);
		}
	}
	
	if(window.addEventListener){
		window.addEventListener("load", initMessageBox, false);
	} else if(window.attachEvent){
		window.attachEvent("onload", initMessageBox);
	} else {
		var onloadFunc = window.onload;
		window.onload = function(){
			if(typeof(onloadFunc) == "function"){
				onloadFunc();
			}
			initMessageBox();
		}
	}
	
})();
