/**
 * Created by florinaie on 2/17/2016.
 */
AUI().ready(function(){

    if(typeof documentDetailActionsView_loadJavascript != 'undefined'){
        Liferay.provide(
            window,
            documentDetailActionsView_portletNamespace + 'showActionPopUp',
            function (selectedPopUpUrl, title, dialogHeight, actionName) {
                showPopUp(selectedPopUpUrl, title, dialogHeight,actionName);
            },
            []
        );

        AUI().use('node','io-base','aui-modal', function (A) {
            var metadataRDF = A.one("#" + documentDetailActionsView_portletNamespace + "metadataRdf");
            if(metadataRDF != null) {
                metadataRDF.on('click', function (e) {
                    e.preventDefault();
                    var anchor = A.one(e.currentTarget);
                    downloadURL(anchor.get("href"));
                });
            }
        });
    }
});
AUI().ready(function () {

    var POP_UP_CONTENT_ID = 'popup-content';
    var dialogCloseButton = "<button onclick='closeDialog();'>Close</button>";

    showPopUp = function(renderUrl, title, dialogHeight) {
        AUI().use('node', 'opportal-dialog', function(A){
            A.opportalDialog.show(renderUrl, title, 'auto');
            try {
                YUI.one(".myportalwrapper ul.worpmenu").addClass('colapsed');
            } catch (exception) {

            }
        });
    }

    asyncSubmitDetailActionsForm = function(portletNamespace, actionName, actionURL) {
        var emailValid = false;
        AUI().use('node', function(A){
            var emailInput = A.one(".form_user_email input[type='text']");
            if (!emailInput) {
                emailInput = A.one(".form_user_email .control-group input[type='text']");
            }
            if (!emailInput) {
                emailInput = A.one(".form_user_email .control-group.error input[type='text']");
            }

            if(emailInput != null && emailInput != "null") {
                var email = emailInput.get('value');
                if(email == '') {
                    emailValid = false;
                    A.one(".form_user_email .control-group").addClass('error');
                    emailInput.focus();
                } else {
                    var emailRegexStr = /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?$/;
                    emailValid = emailRegexStr.test(email);
                }
            } else {
                emailValid = true;
            }

        });

        AUI().use('aui-io-plugin', 'io-form', 'aui-io-request', 'opportal-dialog', function (A) {
            if(emailValid) {
                A.io.request(actionURL, {
                    method: 'POST',
                    data: {actionName: actionName},
                    form: {
                        id: portletNamespace + 'sa_fm'
                    },
                    on: {
                        success: function () {
                            var responseData = this.get('responseData');
                            if (A.opportalDialog.getDialog() != undefined) {
                                A.opportalDialog.getDialog().set('bodyContent', "<br/><p>" + responseData + "</p><p>" + dialogCloseButton + "</p>");
                            }
                        }
                    }
                });
            }
        });
    }

    closeDialog = function() {
        AUI().use('node', 'opportal-dialog', function(A){
            if (A.opportalDialog.getDialog() != undefined) {
                A.opportalDialog.getDialog().hide();
            }
        });
    }

    checkFrequencyType = function(element) {
        if(element.value == "WEEKLY") {
            document.getElementById("dayOfWeek_Selector").style.display = "block";
        } else {
            document.getElementById("dayOfWeek_Selector").style.display = "none";
        }
    }

    AUI().use('event', 'node', 'opportal-dialog', 'array-extras', 'querystring-stringify', function (Y) {
        if(typeof documentDetailActionsView_portletNamespace==='undefined')
            return;
        var previewWidgetLink = Y.one('#' + documentDetailActionsView_portletNamespace + 'previewWidgetLink');
        if (previewWidgetLink) {
            previewWidgetLink.on('click', function (e) {
                e.preventDefault();
                var previewDiv = Y.one('#' + documentDetailActionsView_portletNamespace + 'widgetFrame');
                if (previewDiv) {
                    if (previewDiv.getStyle("display") === "none") {
                        previewDiv.setStyle("display", "block");
                    }
                    else {
                        previewDiv.setStyle("display", "none");
                    }
                }
            });
        }
    });

});
