Windows.maxZIndex = 300;

Windows.addModalWindow = function(win) {
	if (this.modalWindows.length == 0)
	{
		WindowUtilities.disableScreen(win.options.className, 'overlay_modal', win.overlayOpacity, win.getId(), win.options.parent);
	}
	else
	{
		if (win == this.modalWindows[this.modalWindows.length - 1])
		{
			return;
		}

		if (Window.keepMultiModalWindow)
		{
			$('overlay_modal').style.zIndex = Windows.maxZIndex + 1;
			Windows.maxZIndex += 20;
			WindowUtilities._hideSelect(this.modalWindows.last().getId());
		}
		else
		{
			this.modalWindows.last().element.hide();
		}

		WindowUtilities._showSelect(win.getId());
	}

	this.modalWindows.push(win);
};

Window.prototype.setAjaxContent = function(url, options, showCentered, showModal) {
		this.url = url;
		this.centered = showCentered ? true : false;

	    this.showFunction = showCentered ? "showCenter" : "show";
	    this.showModal = showModal || false;

	    options = options || {};
	    options['evalJS'] = false;

	    this.onComplete = options.onComplete;
	    if (! this._onCompleteHandler)
	      this._onCompleteHandler = this._setAjaxContent.bind(this);
	    options.onComplete = this._onCompleteHandler;

	    new Ajax.Request(url, options);
	    options.onComplete = this.onComplete;
		//this.setAjaxContentOrig(url, options, showCentered, showModal);
};

Window.prototype._setAjaxContent =  function(originalRequest) {
	var content = originalRequest.responseText;

	if (content.isJSON() && (data = content.evalJSON()))
	{
		if (data.title)
		{
			this.setTitle(data.title);
		}

		content = data.content;
	}

	Element.update(this.getContent(), content);

	if (this.onComplete)
	{
		this.onComplete(originalRequest);
	}

	this.onComplete = null;

	this[this.showFunction](this.showModal);
  };

	Dialog.info2 = function(content, parameters) {
		// Get Ajax return before
		if (content && typeof content != "string") {
		  Dialog._runAjaxRequest(content, parameters, Dialog.info);
		  return
		}
		content = content || "";

		// Backward compatibility
		parameters = parameters || {};
		parameters = Object.extend(parameters, parameters.windowParameters || {});
		parameters.windowParameters = parameters.windowParameters || {};

		parameters.className = parameters.className || "alert";

		var content = "<div id='modal_dialog_message' class='InnerPopup'>" + content  + "</div>";
		if (parameters.showProgress)
		  content += "<div id='modal_dialog_progress' class='" + parameters.className + "_progress'>  </div>";

		var cancelLabel = parameters.cancelLabel ? parameters.cancelLabel : "Cancel";
		if (parameters.cancel)
		{
		  var buttonClass = "class ='formButton'";
		  content += "\
			 <div class='buttonsRightRow'>\
		      <input type='button' value='" + cancelLabel + "' onclick='Dialog.cancelCallback()' " + buttonClass + "/>\
		        </div></div>";
		}

	    parameters.ok = null;

	    return this._openDialog(content, parameters);
	};

	Dialog.confirm = function(content, parameters)
	{
	    // Get Ajax return before
		if (content && typeof content != "string")
		{
		  Dialog._runAjaxRequest(content, parameters, Dialog.confirm);
		  return;
		}
		content = content || "";

		parameters = parameters || {};
		var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";
		var cancelLabel = parameters.cancelLabel ? parameters.cancelLabel : "Cancel";

		// Backward compatibility
		parameters = Object.extend(parameters, parameters.windowParameters || {});
		parameters.windowParameters = parameters.windowParameters || {};

		parameters.className = parameters.className || "alert";

		var buttonClass = "class ='formButton'";

		var content = '\
			<div class="InnerPopup window alert">\
				<div class="ContentBoxArea">\
					<p style="text-align: center;">' + content + '</p>\
					<div class="buttonsRightRow">\
						<input type="button" name="back" value="' + okLabel + '" onclick="Dialog.okCallback();" ' + buttonClass + ' />\
						<input type="button" name="back" value="' + cancelLabel + '" onclick="Dialog.cancelCallback();" ' + buttonClass + ' />\
					</div>\
				</div>\
			</div>\
		';

		return this._openDialog(content, parameters)
	};

// IE onReady creation problem
Window.prototype._createWindow = function(id) {
    var className = this.options.className;
    var win = document.createElement("div");
    win.setAttribute('id', id);
    win.className = "dialog";

    var content;
    if (this.options.url)
      content= "<iframe frameborder=\"0\" name=\"" + id + "_content\"  id=\"" + id + "_content\" src=\"" + this.options.url + "\"> </iframe>";
    else
      content ="<div id=\"" + id + "_content\" class=\"" +className + "_content\"> </div>";

    var closeDiv = this.options.closable ? "<div class='"+ className +"_close' id='"+ id +"_close' onclick='Windows.close(\""+ id +"\", event)'> </div>" : "";
    var minDiv = this.options.minimizable ? "<div class='"+ className + "_minimize' id='"+ id +"_minimize' onclick='Windows.minimize(\""+ id +"\", event)'> </div>" : "";
    var maxDiv = this.options.maximizable ? "<div class='"+ className + "_maximize' id='"+ id +"_maximize' onclick='Windows.maximize(\""+ id +"\", event)'> </div>" : "";
    var seAttributes = this.options.resizable ? "class='" + className + "_sizer' id='" + id + "_sizer'" : "class='"  + className + "_se'";
    var blank = "../themes/default/blank.gif";

    win.innerHTML = closeDiv + minDiv + maxDiv + "\
      <table id='"+ id +"_row1' class=\"top table_window\">\
        <tr>\
          <td class='"+ className +"_nw'></td>\
          <td class='"+ className +"_n'><div id='"+ id +"_top' class='"+ className +"_title title_window'>"+ this.options.title +"</div></td>\
          <td class='"+ className +"_ne'></td>\
        </tr>\
      </table>\
      <table id='"+ id +"_row2' class=\"mid table_window\">\
        <tr>\
          <td class='"+ className +"_w'></td>\
            <td id='"+ id +"_table_content' class='"+ className +"_content' valign='top'>" + content + "</td>\
          <td class='"+ className +"_e'></td>\
        </tr>\
      </table>\
        <table id='"+ id +"_row3' class=\"bot table_window\">\
        <tr>\
          <td class='"+ className +"_sw'></td>\
            <td class='"+ className +"_s'><div id='"+ id +"_bottom' class='status_bar'><span style='float:left; width:1px; height:1px'></span></div></td>\
            <td " + seAttributes + "></td>\
        </tr>\
      </table>\
    ";
    Element.hide(win);
    $(this.options.parent).insert({'top': win});
    Event.observe($(id + "_content"), "load", this.options.onload);
    return win;
}

Dialog.alert = function(content, parameters) {
	// Get Ajax return before
	/*if (content && ('string' != typeof content) && ('array' != typeof content)) {
		Dialog._runAjaxRequest(content, parameters, Dialog.alert);
 		return
	}*/

	parameters = parameters || {};
	var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";

	// Backward compatibility
	parameters = Object.extend(parameters, parameters.windowParameters || {});
	parameters.windowParameters = parameters.windowParameters || {};

	parameters.className = parameters.className || "alert";

	var okButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " ok_button'"

	var getContent = function(okLabel, content) {
		var lines = '', content = content || '';

		if (content)
		{
			if (!Object.isArray(content))
			{
				content = [content];
			}

			$A(content).each(function(line) {
				lines += '<p style="text-align: center;">' + line + '</p><br />';
			});
		}

		content = '\
			<div class="InnerPopup">\
				<div class="ContentBoxArea">\
				' + lines + '\
					<div class="buttonsRightRow">\
						<input type="button" name="back" value="' + okLabel + '" onclick="Dialog.okCallback();" class="formButton" />\
					</div>\
				</div>\
			</div>\
		';

		return content;
	}.curry(okLabel)

	var dlg  = this._openDialog(getContent(content), parameters);

	dlg.setAlertContent = function(getContent, content) {
		this.setHTMLContent(getContent(content));
	}.bind(dlg, getContent);

	return dlg;
}

// IE autoheight problem
Window.prototype.setSize = function(width, height, useEffect) {
    width = parseFloat(width);
    height = parseFloat(height);

    // Check min and max size
    if (!this.minimized && width < this.options.minWidth)
      width = this.options.minWidth;

    if (!this.minimized && height < this.options.minHeight)
      height = this.options.minHeight;

    if (this.options. maxHeight && height > this.options. maxHeight)
      height = this.options. maxHeight;

    if (this.options. maxWidth && width > this.options. maxWidth)
      width = this.options. maxWidth;


    if (this.useTop && this.useLeft && Window.hasEffectLib && Effect.ResizeWindow && useEffect) {
      new Effect.ResizeWindow(this, null, null, width, height, {duration: Window.resizeEffectDuration});
    } else {
      this.width = width;
      this.height = height;
      var e = this.currentDrag ? this.currentDrag : this.element;

      e.setStyle({width: width + this.widthW + this.widthE + "px"})

      if (height)
      {
		e.setStyle({height: height  + this.heightN + this.heightS + "px"})
      }

      // Update content size
      if (!this.currentDrag || this.currentDrag == this.element) {
        var content = $(this.element.id + '_content');
        if (height)
        {
        	content.setStyle({height: ('auto' == height) ? height : height  + 'px'});
        }
        if (width)
        {
        	content.setStyle({width: ('auto' == width) ? width : width  + 'px'});
        }
      }
    }
}

// IE open problem
Window.prototype._checkIEOverlapping = function() {
	if(!this.iefix && (Prototype.Browser.IE) && this.element.getStyle('position') == 'absolute' && this.element.id == 'overlay_modal') {
        new Insertion.After(this.element.id, '<iframe id="' + this.element.id + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
        this.iefix = $(this.element.id+'_iefix');
    }
    if(this.iefix)
      setTimeout(this._fixIEOverlapping.bind(this), 50);
}