define([
    "jquery",
    "domReady!"
], function($) {
    "use strict";
    var mgtAdvancePreviewHelper = {};
    return mgtAdvancePreviewHelper = {
        version : '1.0.1',
        doRequest : function (url,data,divLoading,divMessageId,callback) {
            var self = this;
            self.showLoading(divLoading);
            var response = {
                status : 'error',
                message : 'System Error, Please check again'
            }
            $.ajax({
                url : url,
                dataType : 'json',
                type : 'POST',
                data : data,
                error : function () {
                    self.hideLoading(divLoading);
                    if(divMessageId !== '')
                    {
                        $('#'+divMessageId).css('color','red');
                        $('#'+divMessageId).html(response.message);
                        self.showMessage(divMessageId);
                    }
                    callback(response);
                },
                success : function (response) {
                    self.hideLoading(divLoading);
                    if(divMessageId !== '')
                    {
                        if(response.status === 'success') {
                            $('#'+divMessageId).css('color','green');
                        }
                        else {
                            $('#'+divMessageId).css('color','red');
                        }
                        $('#'+divMessageId).html(response.message);
                        self.showMessage(divMessageId);
                    }
                    callback(response);
                }
            })
        },
        uploadImages : function (url,data,divLoading,divMessageId,callback) {
            var self = this;
            self.showLoading(divLoading);
            $.ajax({
                url : url,
                dataType : 'json',
                type : 'POST',
                data : data,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success : function(response)
                {

                    if(divMessageId !== '')
                    {
                        if(response.status === 'success') {
                            $('#'+divMessageId).css('color','green');
                        }
                        else {
                            $('#'+divMessageId).css('color','red');
                        }
                        self.showMessage(divMessageId);
                        //self.hideMessage(divMessageId);
                    }
                    callback(response);
                    self.hideLoading(divLoading);

                },
                error : function()
                {
                    var response = {
                        status : 'error',
                        message : 'System Error, Please check again'
                    }
                    self.hideLoading(divLoading);
                    if(divMessageId !== '')
                    {
                        self.showMessage(divMessageId);
                        // self.hideMessage(divMessageId);
                    }
                    callback(response);
                }
            })
        },
        hideLoading : function (divLoading) {
            if($('#'+divLoading).length) {
                $('#'+divLoading).css('display','none');
            }
        },
        showLoading : function (divLoading) {
            if($('#'+divLoading).length) {
                $('#'+divLoading).css('display','block');
            }
        },
        translateText : function (objectText) {
            return JSON.parse(objectText);
        },
        hideMessage : function (divMessageId) {
            setTimeout(function () {
                $('#'+divMessageId).css('display','none');
            },3000)
        },
        showMessage : function (divMessageId,message) {
            var mMessage = message || '';
            if(mMessage !== '') {
                $('#'+divMessageId).html(mMessage);
            }
            $('#'+divMessageId).css('display','block');

        },
        checkMobile : function () {
            if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
                return true;
            }
            return false;
        },
        getFistItem : function(array,n) {
            if (array == null)
                return void 0;
            if (n == null)
                return array[0];
            if (n < 0)
                return [];
            return array.slice(0, n);
        },
        init : function () {

        }
    }
})
