﻿
/// <reference path="jquery-1.4.1-vsdoc.js" />
function ConfirmationDialog() {
    var scrollTop = $(window).scrollTop();
    var confirmDialog = document.createElement('div');
    confirmDialog.setAttribute('id', 'confirmDialog');
    document.body.appendChild(confirmDialog);
    this.title = null;
    this.text = null;
    this.btnOkName = 'Да';
    this.btnCancelName = 'Нет';
    var title = this.title;
    var dialog_buttons = {};
    this.OpenDialogWithSumbit = function (formID) {
        function action() {
            $('#' + formID).submit();
        }
        OpenDialog(action, this);
    }
    this.OpenDialogWithHref = function (id) {
        function action() {
            var link = $('#' + id).attr('href');
            window.location = link;
            return false;
        }
        OpenDialog(action, this);
    }
    this.OpenSimpleDialog = function (action) {
        OpenDialog(action, this);
    }
    this.OpenDialogWithActionLink = function (id) {
        function action() {
            $('#' + id).click();
        }
        OpenDialog(action, this);
    }
    this.OpenAlertDialog = function () {
        OpenDialog(null, this, true);
    }
    function OpenDialog(action, obj, isAlertDialog) {

        if (isAlertDialog) {
            SetAlertButtonActions('Закрыть');
        } else {
            SetButtonsActions(obj.btnOkName, obj.btnCancelName, action);
        }

        LoadComponents(obj.title);

        // $('.ui-dialog .ui-dialog-buttonpane button').css('float', 'none');
        // $('.ui-dialog .ui-dialog-content').css('overflow', 'hidden');
        // $('.ui-dialog .ui-dialog-buttonpane').css('text-align', 'center');
        //$('.ui-widget-header').css('background', '#e5e5dd none repeat-x scroll 50% 50%');
        //$('.ui-widget-header').css('border', '1px solid #e5e5dd');
        //$('.ui-widget-overlay').css('background','black');
        $('#confirmDialog').html(obj.text);
        $('#confirmDialog').css('text-align', 'center');
        $('#confirmDialog').css('margin-top', '15px');
        $('#confirmDialog').next().removeClass('ui-widget-content');
        $(window).scrollTop(scrollTop);
        //$('#confirmDialog').dialog('open');
        //remove class  ui-widget-content
    }
    function SetButtonsActions(btnOk, btnCancel, action) {
        dialog_buttons[btnOk] = function () {
            //$(this).dialog('close');
            $(this).dialog('destroy');
            $('#confirmDialog').remove();
            action();
        }
        dialog_buttons[btnCancel] = function () {
            //$(this).dialog('close');
            $(this).dialog('destroy');
            $('#confirmDialog').remove();
        }
    }
    function SetAlertButtonActions(btnCancel) {
        dialog_buttons[btnCancel] = function () {
            //$(this).dialog('close');
            $(this).dialog('destroy');
            $('#confirmDialog').remove();
        }
    }

    function LoadComponents(title) {

        $('#confirmDialog').dialog(
            {
                bgiframe: true,
                resizable: false,
                title: title,
                modal: true,
                draggable: false,
                closeOnEscape: true,
                overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
                },
                buttons: dialog_buttons,
                close: function () {
                    $(this).dialog('destroy');
                    $('#confirmDialog').remove();
                }
            });

    }

}

function SendRecommendationDialog() {
    var SendRecommendationDialog = document.createElement('div');
    SendRecommendationDialog.setAttribute('id', 'SendRecommendationDialog');
    document.body.appendChild(SendRecommendationDialog);
    this.title = 'Порекомендовать улучшение';
    this.btnSend = 'Отправить';
    this.OpenDialog = function (url, obj) {
        $('#SendRecommendationDialog').html(
        '<span id="validationMessage" style="color:red;"></span>' +
	    '<label for="themeName">Тема</label><br/>' +
	    '<input type="text" style="width:100%;" name="themeName" id="themeName" class="text ui-widget-content ui-corner-all" />' +
	    '<label for="offerText">Предложение</label><br/>' +
	    '<textarea style="width:100%; height:235px; overflow:hidden;" name="offerText" id="offerText" value="" class="text ui-widget-content ui-corner-all" </textarea>');
        $('#SendRecommendationDialog').dialog(
             {
                 bgiframe: true,
                 resizable: false,
                 title: 'Порекомендовать улучшение',
                 modal: true,
                 height: 400,
                 width: 600,
                 draggable: false,
                 closeOnEscape: true,
                 overlay: {
                     backgroundColor: '#000',
                     opacity: 0.5
                 },
                 buttons:
            {
                'Отправить': function () {
                    if ($('#themeName').val() != "" && $('#offerText').val() != "") {
                        if (($('#themeName').val()).length > 4 && ($('#themeName').val()).length < 75) {
                            if (($('#offerText').val()).length > 10 && ($('#offerText').val()).length < 750) {
                                var ajaxData = { Message_Theme: ($('#themeName')).val(), Message_Text: ($('#offerText')).val() };
                                $.ajax({
                                    type: 'post',
                                    url: url,
                                    data: ajaxData,
                                    success: function () {

                                        //var firstButton=$('.ui-dialog-buttonpane button:first');
                                        var parentWidth = $('#SendRecommendationDialog').parent().parent().width();
                                        //firstButton.css('text', 'none');
                                        var cssObj = {
                                            'text-align': 'center',
                                            'vertical-align': 'middle',
                                            'height': '100%',
                                            'width': parentWidth,
                                            'display': 'table-cell'

                                        }
                                        $('#SendRecommendationDialog').dialog('option', 'buttons', { "Закрыть": function () { $(this).dialog("close"); $('#SendRecommendationDialog').remove(); } });
                                        $('#SendRecommendationDialog').css(cssObj).html('Спасибо за рекомендацию.');
                                        $('#SendRecommendationDialog').next().removeClass('ui-widget-content');
                                    },
                                    error: function () {
                                        //alert('err');
                                    }
                                });

                            }
                            else {
                                $('#validationMessage').html('* Не введен Текст Сообщения или Текст Сообщения превышает 750 символов.<br/>');
                            }
                        }
                        else {
                            $('#validationMessage').html('* Не введена Тема Сообщения или Тема Сообщения превышает 75 символов.<br/>');
                        }
                    }
                    else {
                        $('#validationMessage').html('* Все поля должны быть заполнены<br/>');
                    }
                }
            },
                 close: function () {
                     $(this).dialog('destroy');
                     $('#SendRecommendationDialog').remove();
                 }
             });
        $('#SendRecommendationDialog').next().removeClass('ui-widget-content');
    }

}

//    function Button(id, appendToId) {
//    uiDialogButtonPane = $('<div></div>')
//				.addClass(
//					'ui-dialog-buttonpane ' +
//					'ui-helper-clearfix'
//				);
//		// if we already have a button pane, remove it
//		//$(this).find('.ui-dialog-buttonpane').remove();

//				$('<button type="button" id='+ id +'></button>')
//					.addClass(
//						'ui-state-default ' +
//						'ui-corner-all'
//					)
//					.text(name)
//					.click(function() {alert();})
//					.hover(
//						function() {
//							$(this).addClass('ui-state-hover');
//						},
//						function() {
//							$(this).removeClass('ui-state-hover');
//						}
//					)
//					.focus(function() {
//						$(this).addClass('ui-state-focus');
//					})
//					.blur(function() {
//						$(this).removeClass('ui-state-focus');
//					}).appendTo(uiDialogButtonPane);
//					uiDialogButtonPane.appendTo($('#divToAppend'));

//	}
