﻿/**
	Description:
		
		
	Usage:
		var popup = new PopupDialog(dvxPopup);
		
		popup.Alert("Hello!")										// will show popup with message and "continue" button
		popup.Confirm("Are you sure?", onYes, onNo)					// will show popup with message and "yes"/"no" buttons
																				// true/false means show or not show grey background
		popup.TextInput("Please type notes:", "Default value", onYes, onNo);	// will show popup with text area for typing something
  */

function PopupDialogDevEx(width)
{
	this._function = 'function';
	
	this.dlg = null;
	this.btnContinue = null;
	this.btnYes = null;
	this.btnNo = null;
	this.label = null;
	this.labelInput = null;
	this.popupDiv = null;
	this.txtInput = null;

	this.Init = function() {
	    this.dlg = dvxPopup;
	    this.btnContinue = dvxButtonPopupContinue;
	    this.btnYes = dvxButtonPopupYes;
	    this.btnNo = dvxButtonPopupNo;
	    this.label = dvxLabelPopup;
	    this.labelInput = dvxTextLabelPopup;
		this.popupDiv = dvxPopupHtmlDiv;
	    this.txtInput = dvxTextInputPopup;
	    if (typeof (width) != "undefined")
	        dvxPopup.SetWidth(width);
	    else
	        dvxPopup.SetWidth(250);
	}
	
	this.Show = function()
	{
		if (jQuery.browser.msie)
		{
			this.dlg.SetHeight(1);
			this.dlg.SetWidth(350);
		}

		this.dlg.ShowWindow();
	}
	
	this.Hide = function()
	{
		this.dlg.HideWindow()
	}

	this.Alert = function (message, onContinueCallback) {
	    this._SetMessage(message);
	    this._SetContinueMode();
	    this._SetAlertMode();

	    this.btnContinue.OnClick = typeof (onContinueCallback) == this._function ?
			function () {
			    this.Hide();
			    onContinueCallback();
			}.bind(this)
            :
            function () {
                this.Hide();
            }.bind(this);
	    this.Show();
	}

	this.AlertHtml = function(htmlElementID, onContinueCallback) {
		this._SetHtml(jQuery('#' + htmlElementID).html());
	    this._SetContinueMode();
	    this._SetHtmlMode();

		this.btnContinue.OnClick = typeof(onContinueCallback) == this._function ?
			function(){ this.Hide(); onContinueCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);

		this.Show();
	}

	this.SetWidth = function (width) {
	    this.dlg.SetSize(width, this.dlg.GetHeight())
	}

	this.Confirm = function(message, onYesCallback, onNoCallback)
	{
		this._SetMessage(message);
		this._SetYesNoMode();
		this._SetAlertMode();
		
		this.btnYes.OnClick = typeof(onYesCallback) == this._function ? 
			function(){ this.Hide(); onYesCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);
		this.btnNo.OnClick = typeof(onNoCallback) == this._function ? 
			function(){ this.Hide(); onNoCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);

		this.Show();
	}

	this.ConfirmHtml = function(htmlElementID, onYesCallback, onNoCallback)
	{
		this._SetHtml(jQuery('#' + htmlElementID).html());
		this._SetYesNoMode();
		this._SetHtmlMode();
		
		this.btnYes.OnClick = typeof(onYesCallback) == this._function ? 
			function(){ this.Hide(); onYesCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);
		this.btnNo.OnClick = typeof(onNoCallback) == this._function ? 
			function(){ this.Hide(); onNoCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);

		this.Show();
	}
		
	this.TextInput = function(message, value, onYesCallback, onNoCallback)
	{
		this._SetTextInputValue(value);
		this._SetTextInputLabel(message);
		this._SetYesNoMode();
		this._SetInputMode();
		
		this.btnYes.OnClick = typeof(onYesCallback) == this._function ? 
			function(){ this.Hide(); onYesCallback(this, this.txtInput.GetValue()); }.bind(this) : function(){ this.Hide() }.bind(this);
		this.btnNo.OnClick = typeof(onNoCallback) == this._function ? 
			function(){ this.Hide(); onNoCallback(this, this.txtInput.GetValue()); }.bind(this) : function(){ this.Hide() }.bind(this);

		this.Show();
	}
	
	this.TextInputReadony = function(message, value, onContinueCallback)
	{
		this._SetTextInputValue(value);
		this._SetTextInputLabel(message);
		this._SetContinueMode();
		this._SetInputMode();

		this.btnContinue.OnClick = typeof(onYesCallback) == this._function ?
			function(){ this.Hide(); onContinueCallback(); }.bind(this) : function(){ this.Hide() }.bind(this);

		this.Show();
	}

	this._SetMessage = function(message)
	{
		this.label.SetValue(message);
	}

	this._SetHtml = function(html)
	{
		this.popupDiv.SetContentHtml(html);
	}
	
	this._SetTextInputLabel = function(message)
	{
		this.labelInput.SetValue(message);
	}
	
	this._SetTextInputValue = function(text)
	{
		this.txtInput.SetValue(text);
	}
	
	this._SetYesNoMode = function()
	{
		this.btnContinue.SetClientVisible(false);
		this.btnYes.SetClientVisible(true);
		this.btnNo.SetClientVisible(true);
	}
	
	this._SetContinueMode = function()
	{
		this.btnContinue.SetClientVisible(true);
		this.btnYes.SetClientVisible(false);
		this.btnNo.SetClientVisible(false);
	}
	
	this._SetAlertMode = function()
	{
		this.label.SetClientVisible(true);
		this.labelInput.SetClientVisible(false);
		this.txtInput.SetClientVisible(false);
		this.popupDiv.SetClientVisible(false);
	}
	
	this._SetInputMode = function()
	{
		this.label.SetClientVisible(false);
		this.labelInput.SetClientVisible(true);
		this.txtInput.SetClientVisible(true);
		this.popupDiv.SetClientVisible(false);
	}

	this._SetHtmlMode = function()
	{
		this.popupDiv.SetClientVisible(true);
		this.label.SetClientVisible(false);
		this.labelInput.SetClientVisible(false);
		this.txtInput.SetClientVisible(false);
	}
	
	this.Init();
}

// Fix for FireFox
if (jQuery.browser.mozilla)
{
	setTimeout(function(){
		window._aspxGetDocumentClientHeight = function(){
			if (__aspxSafari || __aspxFirefox) 
				return window.innerHeight;
			if(__aspxIE55 || __aspxOpera || document.documentElement.clientHeight == 0)
				return document.body.clientHeight;
			return document.documentElement.clientHeight;
		}
	}, 100);
}
