//object definition
if (typeof YesNoDialog == "undefined") {

	function YesNoDialog(config){
		//function's private properties definition
		var panelId = Math.random();

		var patternPanelId = "yesNoDialog";

		var dialog = null;
		
		var panel = null;
		
		var yesButtonClass = "dialogYesButton";
		
		var noButtonClass = "dialogNoButton";

		var thirdButtonClass = "dialogThirdButton";

		var titleClass = "body-panel-title-content";

		var titleStyle = {};
		
		var textClass = "dialogText";
		
		var additionalTextClass = "additionalText";
		
		var title = "Confirmation";
		
		var text = "Are you sure?";
		
		var yesLabel  = "Yes";
		
		var noLabel  = "No";

		var thirdLabel  = "Third";

		var additionalText = null;

        var showYesButton = true;
        var showNoButton = true;
        var showThirdButton = false;

        var zIndex = 1000000;

        var maskBackgroundColor = "#000";

        //public method definitions
		this.show = show;
		this.hide = hide;
		this.setText = setText;
		this.setTitle = setTitle;
		this.setYesCallback = setYesCallback;
        this.setNoCallback = setNoCallback;
        this.setThirdCallback = setThirdCallback;
		this.setYesLabel = setYesLabel;
		this.setNoLabel = setNoLabel;
		this.setThirdLabel = setThirdLabel;
        this.getTextObject = getTextObject;

		//invoke constructor
		constructor(config);
		
		//constructor
		function constructor(config) {
			if(!config) {
				config = {};
			}
			
			panelId = getConfigParam(config, "panelId", panelId);
			var pattern = $("#" + patternPanelId);
		    panel = pattern.clone().appendTo("body");
            // remove previous version of dialog with specified ID if it exists
            $("#" + panelId).remove();
		    panel.attr("id", panelId);
		    
			title = getConfigParam(config, "title", title);
			text = getConfigParam(config, "text", text);
			yesButtonClass = getConfigParam(config, "yesButtonClass", yesButtonClass);
			noButtonClass = getConfigParam(config, "noButtonClass", noButtonClass);
			thirdButtonClass = getConfigParam(config, "thirdButtonClass", thirdButtonClass);
			titleClass = getConfigParam(config, "titleClass", titleClass);
			textClass = getConfigParam(config, "textClass", textClass);
		    additionalTextClass = getConfigParam(config, "additionalTextClass", additionalTextClass);
		    var yesCallback = getConfigParam(config, "yesCallback", function(){});
			var noCallback = getConfigParam(config, "noCallback", function(){});
			var thirdCallback = getConfigParam(config, "thirdCallback", function(){});
		    additionalText = getConfigParam(config, "additionalText", additionalText);
		    yesLabel = getConfigParam(config, "yesLabel", yesLabel);
		    noLabel = getConfigParam(config, "noLabel", noLabel);
		    thirdLabel = getConfigParam(config, "thirdLabel", thirdLabel);
            showYesButton = getConfigParam(config, "showYesButton", showYesButton);
            showNoButton = getConfigParam(config, "showNoButton", showNoButton);
            showThirdButton = getConfigParam(config, "showThirdButton", showThirdButton);
            titleStyle = getConfigParam(config, "titleStyle", titleStyle);
            zIndex = getConfigParam(config, "zIndex", zIndex);
            maskBackgroundColor = getConfigParam(config, "maskBackgroundColor", maskBackgroundColor);

            //init yahoo's panel params
			var yahooParams = getConfigParam(config, "yahooParams", {close: true});
            yahooParams.modal = true;
            yahooParams.visible = false;
            yahooParams.fixedcenter = true;
            yahooParams.underlay = "none";

		    $("." + titleClass, panel).text(title);
            $("." + textClass, panel).html(text);
			if (!additionalText) {
		    	$("." + additionalTextClass, panel).remove();
		    } else {
		    	$("." + additionalTextClass, panel).text(additionalText);
		    }

			if (titleStyle) {
				$("." + titleClass, panel).css(titleStyle);
			}

			setYesCallback(yesCallback);		
			setNoCallback(noCallback);		
			setThirdCallback(thirdCallback);

            Util.setButtonValue($("." + yesButtonClass, panel), yesLabel);
            Util.setButtonValue($("." + noButtonClass, panel), noLabel);
            Util.setButtonValue($("." + thirdButtonClass, panel), thirdLabel);

            if (!showYesButton) {
                $("." + yesButtonClass, panel).hide();
            } else {
                panel.keypress(function(e) {
                    if(!panel.is(":hidden") && (e.keyCode || e.which) == 13) {
                        $("." + yesButtonClass, panel).click();
                    }
                });
            }

            if (!showNoButton) {
                $("." + noButtonClass, panel).hide()
            }
            if (!showThirdButton) {
                $("." + thirdButtonClass, panel).hide()
            }

            if (zIndex != null) {
                yahooParams.zIndex = zIndex;
            }

            dialog = new YAHOO.widget.Panel(panel.attr("id"), yahooParams);
            if(yahooParams.width != undefined) {
                var $container = $(panel).parents(".yui-panel-container");
                $container.width(yahooParams.width);
            }

            dialog.render();
		    panel.hide();

            if (maskBackgroundColor) {
				$("#" + panel.attr("id") + "_mask").css("background-color", maskBackgroundColor);
			}

            $(".promo-block", panel).css("margin", "0");
        }

       	//function definitions
       	function getConfigParam(config, paramName, defVal){
			var confParam = config[paramName];
			if (confParam != undefined) {
				return confParam;
			} else {
				return defVal;
			}
		}
       
		function show(){
		    panel.show();
            dialog.show();
        }
		
		function hide(){
			dialog.hide();
		    panel.hide();
		}
		
		function setText(newText) {
			text = newText;
			$("." + textClass, panel).html(text);
		}
		
		function setTitle(newTitle) {
		    title = newTitle;
		    $("." + titleClass, panel).text(title);
		}

        function getTextObject() {
            return $("." + textClass, panel);
        }
		
		function setYesCallback(yesCallback) {
			setCallback(yesCallback, yesButtonClass);
		}

		function setNoCallback(noCallback) {
			setCallback(noCallback, noButtonClass);
		}

		function setThirdCallback(thirdCallback) {
			setCallback(thirdCallback, thirdButtonClass);
		}

		function setCallback(callback, buttonClass) {
			var button = $("." + buttonClass, panel);
			button.unbind("click");
		    button.click(function(){
                dialog.hide();
                callback();
                return false;
		    });
		}
		
		function setYesLabel(yesLabel) {
		    yesLabel = yesLabel;
		    $("." + yesButtonClass, panel).text(yesLabel);
		}
		
		function setNoLabel(noLabel) {
		    noLabel = noLabel;
		    $("." + noButtonClass, panel).text(noLabel);
		}

		function setThirdLabel(thirdLabel) {
		    thirdLabel = thirdLabel;
		    $("." + thirdButtonClass, panel).text(thirdLabel);
		}
	}
}
