/**
 * @author: Hoby RAVELOMANANTSOA <h.ravelomanantsoa@devcommunication.com>
 * @param: content: the html content to use
 * @param: opacity: opacity to use
 *
 * This Object is used in to display a alert/prompt like screen : this will display the content specified
 */

HobyBox = function (content, opacity) {
	var id;
	var innerId;
	var windowWidth = 0;
	var windowHeight = 0;
	var me = this;
	this.element = document.createElement("div");
	me.element = $(me.element);
	me.id = Element.identify(me.element);
	/**
		* Initialize this object
		*/
	this.Initialize = function (contenta, opacity) {
		me.innerId = me.id + "_inner";
		me.getWindowSize();
		me.element.style.position='absolute';
		me.element.style.width = me.windowWidth + 'px';
		me.element.style.height = me.windowHeight + 'px';
		me.element.style.zIndex=99999;
		me.element.style.position='fixed';
		me.element.style.top='0px';
		me.element.style.left='0px';
		me.element.style.display='none';
		me.element.style.backgroundColor='transparent';
		me.element.style.cursor='not-allowed';
		me.element.setOpacity(opacity);
		Element.insert(me.element,
		"<table style='width:100%; height:100%;'><tr><td valign='middle' align='center'><div id='"+me.innerId+"'></div></td></tr></table>");
		document.body.appendChild(me.element);
		me.update(contenta);
/*		me.getContainer().style.zIndex = 99999;
		me.getContainer().style.position = 'fixed';*/
	}
	/**
		* show the box
		*/
	this.Show = function () {
		me.element.show();
		//me.element.scrollTo();
	};
	/**
		* hide the box
		*/
	this.Hide = function () {
		me.element.hide();
	};
	this.Delete = function () {
		me.element.remove();
	};
	/**
		* update the content of the box
		*/
	this.update = function (txt) {
		if ($(me.innerId)) {
			$(me.innerId).update(txt);
		}
	}
	/**
		* inset a content in the box
		*/
	this.insert = function (txt) {
		if ($(me.innerId)) {
			$(me.innerId).insert(txt);
		}
	}
	/**
		* return the box container
		*/
	this.getContainer = function () {
		return $(me.innerId);
	}
	/**
		* get the window size;
		*/
	this.getWindowSize = function () {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		me.windowWidth = myWidth;
		me.windowHeight = myHeight;
	}
	//Initialization
	var opaque = 1;
	if (opacity != null) {
		opaque = opacity;
	}
	
	me.Initialize(content, opaque);
};

/**
 * a dialog box panel
 * @author: Hoby RAVELOMANANTSOA <h.ravelomanantsoa@devcommunication.com>
 * @param : content: the content
 * @param: heightd: the heigth size
 * @param: widthd: the width size
 */
OxygenDialogBox = function (content, widthd, heightd) {
	var me = this;
	/*this.height = (heightd==null)?'auto':heightd;
	this.width = (widthd==null)?333:widthd;*/
	this.height = (heightd==null)?224:224;
	this.width = (widthd==null)?421:421;
	this.box = new HobyBox("", 1);
	this.frameId = me.box.id + "_frame";
	this.firstContentId = me.box.id + "_bxbox";
	this.closeId = me.firstContentId + "_close";
	this.titleId = me.firstContentId + "_title";
	this.titleBarId = me.firstContentId + "_titlebar";
	this.oxygenLogoId = me.firstContentId + "_oxygenLogo";
	//this.bodyId = me.firstContentId + "_body";
	this.footerId = me.firstContentId + "_footer";
	var firstContent_0 = "<div id=\""+ me.firstContentId +"\" style=\"color:#5a6a88;font-family:Verdana; font-size:10px; font-weight:normal;\"></div>";
	var firstContent_1 = "<table width=\"400px;\"><tr><td align=\"left\"><span style=\"padding-left:65px;\" id=\""+ me.titleId +"\"></span></td><td align=\"left\" style=\"width:60px;\"><a  id=\""+ me.closeId +"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></td></tr></table>";
	var logoContent = "<a id=\""+ me.oxygenLogoId +"\" href=\"http://www.squalesystems.com/fr/solutions/asterisk/\" target=\"_blank\" title='SSLL Sp&eacute;cialis&eacute;e VoIP / Asterisk'></a>";
	var footerContent = "<div align=\"left\" id=\"" + me.footerId + "\">AXE INTERNATIONAL<br/><a href=\"mailto:contact@axeinternational.com\">contact@axeinternational.com</a></div>";
	var firstContent = "<div id=\""+me.frameId+"\">"+ logoContent + "<div id=\""+ me.titleBarId +"\">" + firstContent_1 + "</div>" + firstContent_0 + footerContent + "</div>";	
	me.box.update(firstContent);
	$(me.frameId).className = "oxygenDialogBoxFrame";
	
	$(me.titleBarId).className = "oxygenDialogBoxTitleBar";
	
	$(me.titleId).className = "oxygenDialogBoxTitle";
	
	$(me.closeId).className = "oxygenDialogBoxClose";
	
	$(me.closeId).observe("click", function (evt) {me.Hide(true); delete me;});
	$(me.closeId).title = "close";
	$(me.closeId).style.cursor = 'default';
	
	$(me.oxygenLogoId).className = "oxygenDialogBoxLogoId";

	//$(me.bodyId).className = "oxygenDialogBoxBody";
	
	$(me.footerId).className = "oxygenDialogBoxFooter";
	/**
	 * when Action (event) occurs on the Element(id) 
	 * this box will be destroyed
	 * @param: id: the id of the element to register
	 * @param: event: the event type that is to be used to close this box
	 * note: event is all event that may be observable using the prototype framework observe function 
	 * 		like 'click', 'change', 'submit', ...
	 */
	this.registerCloseElement = function (id, event) {
		var elt = $(id);
		var evt = "click";
		if (event != null) {
			evt = event;
		}
		if (elt != null) {
			elt.observe(evt, function (evt) {
				me.Hide(true);
				delete me;
			});
		}
	};
	/**
	 * show or hide the close icon
	 * @param: bool: if true (default value) show the icon, if false hide the button
	 */
	this.setVisibleCloseIcon = function (bool) {
		if ((bool != null)&& (bool == false)) {
			$(me.closeId).hide();
		} else {
			$(me.closedId).show();
		}
	};
	this.onHide = function () {
	};
	/**
	 * this will set the title of this Box
	 * @param: titre: the title
	 * @param: class: optional parameters this is the class to apply to the title
	 */
	this.setTitle = function (titre, classo) {
		$(me.titleId).update(titre);
		if (classo != null) {
			$(me.titleId).className = classo;
		};
	};
	/**
	 * this Hide this Box;
	 * @param: deleteHTMLElement: if set to non null it will delete HTML elements
	 */
	this.Hide = function (deleteHTMLElement) {
		me.onHide();
		me.box.Hide();
		if (arguments.length == 1) {
			if ((deleteHTMLElement == true)&& (deleteHTMLElement != null)) {
				me.box.Delete();
			}
		}
	};
	/**
	*getting content from a file url
	*
	*/
	
	this.getContentFromUrl = function (Url,params) {
		me.getContainer().innerHTML = "<img src=\"img/spinner.gif\"/>";
		if(params ==""){
			new Ajax.Updater(me.getContainer(), Url, {evalScripts: true});
		}else{
			new Ajax.Updater(me.getContainer(), Url, {method:'post', parameters: params,evalScripts: true});
		}	
	};

	 /**
	 * this Show this Box;
	 */
	this.Show = function () {
		me.box.Show();
	};

	/**
	 * this returns the box container;
	 */
	this.getContainer = function () {
		return $(me.firstContentId);
	};

	this.getFrame = function () {
		return $(me.frameId);
	};
	/**
	 * this update the content of the container;
	 */
	this.update = function (txt) {
		me.getContainer().update(txt);
	};
	/**
	 * this insert child element to the container;
	 */
	this.insert = function (txt) {
		Element.insert(me.getContainer(), txt);
	};
	/**
	 * set the width
	 */
	this.setWidth = function (w) {
		if (w != null) {
			me.width = w;
		};
		var l = me.width;
		if (me.width != 'auto') {
			l = l + "px";
		} else {
			me.getFrame().setStyle("min-width: 300px;");
		};
		me.getFrame().style.width = l;	
	};
	this.setHeight = function (h) {
		if (h != null) {
			me.height = h;
		};	
		var l = me.height;
		if (me.height != 'auto') {
			l = l + "px";
		};
		me.getFrame().style.height = l;
	};
	try {
		new Draggable(me.frameId);
		me.getFrame().style.cursor = 'move';
	} catch (e) {};
	me.getContainer().setStyle("margin-left: 10px; margin-right: 10px; margin-top: 10px; margin-bottom: 10px;");
	me.getContainer().className = "oxygenDialogBoxContainer"
	me.update(content);
	me.setHeight(me.height);
	me.setWidth(me.width);
// 	me.Show();
};

/////////////////////////////////////////////



/**
 * function used to pause execution;
 */

/** constructor 

	@param duration integer seconds
	@param <optional> function to run while waiting.
	
*/
function Pause(duration, busy){
	this.duration= duration * 1000;
	this.busywork = null; // function to call while waiting.
	this.runner = 0;

	if (arguments.length == 2) {
		this.busywork = busy;
	}

	this.pause(this.duration);

} // Pause class

/** pause method 

	@param duration: integer in seconds
	
*/
Pause.prototype.pause = function(duration){
	if ( (duration == null) || (duration < 0)) {return;}

	var later = (new Date()).getTime() + duration;

	while(true){
		if ((new Date()).getTime() > later) {
		break;
		}

		this.runner++;

		if (this.busywork != null) {
		this.busywork(this.runner);
		}

	} // while

} // pause method



/**
 * object that display alert like msg
 * @author:  Hoby RAVELOMANANTSOA <h.ravelomanantsoa@devcommunication.com>
 * @param: msg: the message to display
 * @param: titre: titre to display optional: default = message
 * @param: width: width
 * @param: height: height
 */
objalert = function (msg, titre, width, height) {
	var me = this;
	this.title = (titre == null)?'Message':titre;
	this.alertbox = new OxygenDialogBox("", width, height);
	this.okbuttonId = me.alertbox.id + "_alertok";
	var msg_txt = "";
	if (typeof msg != "undefined") {
		msg_txt += msg.gsub('\n', '<br/>').gsub('\r', '');
	}
	var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div class=\"contentalert\" align=\"center\">";
	content += msg_txt;
	content += "</div></td></tr></table>";
	content += "<div align=\"middle\"><input type=\"button\"  style=\"position:absolute;left:310px;top:198px;z-index:50;\"  id=\""+me.okbuttonId+"\" value=\"OK\"/></div>";	
	me.alertbox.update("<span style=\"font-size: 12px;\">" + content + "</span>");
	$(me.okbuttonId).className = "oxygenDialogBoxButton";
	$(me.okbuttonId).observe("click", function (evt) {me.alertbox.Hide(true); try {delete me.alertbox;} catch (e) {}});
	me.alertbox.setTitle(me.title);
	me.alertbox.Show();
}

/*
 * dialogBoxForm submit a form identified by its id formId to the url,
 * and put the response content into a oxygenDialogBox
 *
 * @author: Arthur RATSIMBARILALA arthur@devcommunication.com
 * @param: formId: the id of the form to submit
 * @param: url: the url of server-side handler
 * @param: titre: the title of a oxygendialogbox where to put the response.
*/
objOutPutForm = function (formId, url, titre) {
	var me = this;
	this.title = (titre == null)?'OutPut':titre;
	this.outputbox = new OxygenDialogBox("", 100, 100);
	this.okbuttonId = me.outputbox.id + "_outputboxok";
	this.contentDiv = me.outputbox.id + "_contentdiv";
	
	var form_content = $(formId).serialize(true);

	var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div  id=\""+me.contentDiv+"\" class=\"contentalert\" align=\"center\">";
	//content += msg_txt;
	content += "</div></td></tr></table>";
	content += "<div align=\"middle\"><input type=\"button\"  style=\"position:absolute;left:310px;top:198px;z-index:50;\"  id=\""+me.okbuttonId+"\" value=\"OK\"/></div>";
	
	new Ajax.Updater(me.contentDiv, url, {parameters: form_content, evalScripts: true});
	
	me.outputbox.update("<span style=\"font-size: 12px;\">" + content + "</span>");
	$(me.okbuttonId).className = "oxygenDialogBoxButton";
	$(me.okbuttonId).observe("click", function (evt) {me.outputbox.Hide(true); try {delete me.outputbox;} catch (e) {}});
	me.outputbox.setTitle(me.title);	
	me.outputbox.Show();	
	//setTimeout("Effect.Fade('"+me.outputbox.frameId+"')",2500);
	delete_timeout = function () {
		try {
			me.outputbox.Hide(true);
			delete me.outputbox;
		} catch(e){}
	}
	setTimeout("delete_timeout()",5000);
}

objOutPutNotification = function (content_msg, titre ,alert_type) {
	var me = this;
	this.title = (titre == null)?'OutPut':titre;
	this.outputbox = new OxygenDialogBox("", 100, 100);
	this.okbuttonId = me.outputbox.id + "_outputboxok";
	this.contentDiv = me.outputbox.id + "_contentdiv";
	this.delay = 3000;
	
		if (alert_type == "success") {
			content_body = "<div class=\"successnotifbox\">"+content_msg+"</div>";		
		}else{
			if (alert_type == "error") {
			content_body = "<div class=\"errornotifbox\">"+content_msg+"</div>";
			this.delay = 30000;
			}
			else{
			content_body = "<div class=\"alertnotifbox\">"+content_msg+"</div>";
			this.delay = 5000;
			}
		}
		
	var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div  id=\""+me.contentDiv+"\" class=\"contentalert\" align=\"center\">";
	content += content_body;
	content += "</div></td></tr></table>";
	content += "<div align=\"middle\"><input type=\"button\"  style=\"position:absolute;left:310px;top:198px;z-index:50;\"  id=\""+me.okbuttonId+"\" value=\"OK\"/></div>";
	
	
	me.outputbox.update("<span style=\"font-size: 12px;\">" + content + "</span>");
	$(me.okbuttonId).className = "oxygenDialogBoxButton";
	$(me.okbuttonId).observe("click", function (evt) {me.outputbox.Hide(true); try {delete me.outputbox;} catch (e) {}});
	me.outputbox.setTitle(me.title);	
	me.outputbox.Show();	
	//setTimeout("Effect.Fade('"+me.outputbox.frameId+"')",2500);
	delete_timeout = function () {
		try {
			me.outputbox.Hide(true);
			delete me.outputbox;
		} catch(e){}
	}
	setTimeout("delete_timeout()",this.delay);
}
objManualOutPut = function (content_msg, titre ,alert_type) {
	var me = this;
	this.title = (titre == null)?'OutPut':titre;
	this.alert_type = (alert_type == null)?'alert':alert_type;
	this.outputbox = new OxygenDialogBox("", 100, 100);
	this.okbuttonId = me.outputbox.id + "_outputboxok";
	this.contentDiv = me.outputbox.id + "_contentdiv";
		if (alert_type == "success") {
			content_body = "<div class=\"successnotifbox\">"+content_msg+"</div>";		
		}else{
			if (alert_type == "error") {
			content_body = "<div class=\"errornotifbox\">"+content_msg+"</div>";
			}
			else{
			content_body = "<div class=\"alertnotifbox\">"+content_msg+"</div>";
			}
		}	
	var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div  id=\""+me.contentDiv+"\" class=\"contentalert\" align=\"center\">";
	content += content_body;
	content += "</div></td></tr></table>";
	content += "<div align=\"middle\"><input type=\"button\"  style=\"position:absolute;left:310px;top:198px;z-index:50;background-image:url('img/dialog_box/dialogBox_button.png');background-color:transparent;color:#5a6a88;font-family:Verdana;font-size:9px;font-weight:bold;width:73px;height:20px;border:none;\"  id=\""+me.okbuttonId+"\" value=\"OK\"/></div>";
	me.outputbox.update("<span style=\"font-size: 12px;\">" + content + "</span>");
	//$(me.okbuttonId).className = "oxygenButton";
	$(me.okbuttonId).observe("click", function (evt) {me.outputbox.Hide(true); try {delete me.outputbox;} catch (e) {}});
	me.outputbox.setTitle(me.title);
	/*this.initz = function () {
		var me = this;
		this.title = (titre == null)?'OutPut':titre;
		this.alert_type = (alert_type == null)?'alert':alert_type;
		this.outputbox = new OxygenDialogBox("", 100, 100);
		this.okbuttonId = me.outputbox.id + "_outputboxok";
		this.contentDiv = me.outputbox.id + "_contentdiv";
	}*/
	this.setContent = function (content_to_set) {
		if (me.alert_type == "success") {
			content_body = "<div class=\"successnotifbox\">"+content_to_set+"</div>";		
		}else{
			if (me.alert_type == "error") {
			content_body = "<div class=\"errornotifbox\">"+content_to_set+"</div>";
			}
			else{
			content_body = "<div class=\"alertnotifbox\">"+content_to_set+"</div>";
			}
		}
		var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div  id=\""+me.contentDiv+"\" class=\"contentalert\" align=\"center\">";
		content += content_body;
		content += "</div></td></tr></table>";
		content += "<div align=\"middle\"><input type=\"button\"  style=\"position:absolute;left:310px;top:198px;z-index:50;background-image:url('img/dialog_box/dialogBox_button.png');background-color:transparent;color:#5a6a88;font-family:Verdana;font-size:9px;font-weight:bold;width:73px;height:20px;border:none;\"  id=\""+me.okbuttonId+"\" value=\"OK\"/></div>";
		me.outputbox.update("<span style=\"font-size: 12px;\">" + content + "</span>");
	}
	this.setTitle = function (ttle) {
		me.title = ttle;
		me.outputbox.setTitle(me.title);
	}
	this.setType = function (ttyp) {
		me.alert_type = ttyp;
	}
	this.Show = function () {
		try {
			me.outputbox.Show();
		} catch(e){}
	}
	this.Hide = function () {
		try {
			me.outputbox.Hide();
		} catch(e){}
	}
	this.Close = function () {
		try {
			me.outputbox.Hide(true);
			delete me.outputbox;
		} catch(e){}
	}
}
objconfirm = function (msg, titre, action, width, height) {
	var me = this;
	this.hasresponse = false;
	this.value = false;
	this.title = (titre == null)?'Message':titre;
	this.alertbox = new OxygenDialogBox("", width, height);
	this.okbuttonId = me.alertbox.id + "_alertok";
	this.cancelbuttonId = me.alertbox.id + "_alertcancel";
	/**
	 * onAction is the function called when either cancel, ok button is clicked
	 * @param: response: boolean value(true if ok, false if cancel)
	 */
	this.onAction = function (response) {
	};
	//if action is given redefine it
	if (action != null) {
		me.onAction = action;
	}
	var confirmation_txt = "";
	if (typeof confirmation_txt != "undefined") {
		confirmation_txt += msg.gsub('\n', '<br/>').gsub('\r', '');
	}
	var content = "<table border=\"0\"><tr><td height=\"154px\" width=\"400px\"><div class=\"contentalert\" align=\"center\">";
	content += confirmation_txt;
	content += "</div></td></tr></table>";
	content += "<div style=\"vertical-align: bottom; height: 12%;\" align=\"middle\"><input class=\"oxygenDialogBoxButton\" style=\"position:absolute;left:210px;top:198px;z-index:50;\" type=\"button\" id=\"" + me.okbuttonId + "\" value=\"OK\"/>&nbsp;<input class=\"oxygenDialogBoxButton\" type=\"button\" value=\"Cancel\" style=\"position:absolute;left:310px;top:198px;z-index:50;\" id=\""+me.cancelbuttonId+"\"/></div>";
	me.alertbox.onHide = function () {
		me.hasresponse = true;
	};
	me.alertbox.update("<span style=\"font-size:12px\">"  + content +  "</span>");
	$(me.okbuttonId).onclick = function (evt) {
		me.onAction(true);
		me.value = true; 
		me.hasresponse = true; 
		me.alertbox.Hide(true);
	};
	$(me.cancelbuttonId).onclick = function (evt) {
		me.onAction(false);
		me.value = false; 
		me.hasresponse = true; 
		me.alertbox.Hide(true);
	};
	me.alertbox.setTitle(me.title);
	me.alertbox.setVisibleCloseIcon(false);
	me.alertbox.Show();
}
/**
 * a replacement of alert
 * @author: Hoby RAVELOMANANTSOA <h.ravelomanantsoa@devcommunication.com>
 * @param : msg: the message to display
 * @param: msg: the message to display
 * @param: titre: titre to display optional: default = message
 * @param: width: width
 * @param: height: height
 */
oalert = function (msg, titre, width, height) {
	new objalert(msg, titre, width, height);
};

if ( navigator.appName.indexOf('Microsoft') != -1 || navigator.appName.indexOf('MSIE') != -1) {
	true;
} else {
    alert = oalert;
}

/**
 * alternative to prompt
 * @param: msg: message to display
 * @param: titre: titre of the prompt
 * @param: action : function to call when user do action (either cancel or ok)
 *			the function must have this format
 *			function todo (bool) {
 *				....
 *			}
 *			todo(true) will be called when user action the ok button,
 *			todo(false) will be called when user action the cancel button.
 *
 * @param: width (optional): the width of the prompt
 * @param: height (optional): the height of the prompt
 */
oconfirm = function (msg, titre, action, width, height) {
	var u = new objconfirm(msg, titre, action, width, height);
};

oOutPutForm =  function(formId, url, title_dx) {
	new objOutPutForm(formId, url, title_dx);
};

oOutPutNotif =  function(content_corps, title_dx, alert_type) {
	new objOutPutNotification(content_corps, title_dx, alert_type);
};
oManualOutPut =  function(content_corps, title_dx, alert_type) {
	new objManualOutPut(content_corps, title_dx, alert_type);
};
