/*jslint browser:true*/
/*global angular, */
/* exported appRoot */
// Main configuration file. Sets up AngularJS module and routes and any other config objects
var appRoot = angular.module("main", ["ngResource", "ngRoute", "icisPatternRestrict", "ui.utils", "ui.router", "ui.bootstrap", "ui.bootstrap.tpls", "ui.mask", "icisDatepicker", "showErrors", "CustomFilters", "CustomSource"]); //Define the main module
appRoot.config(['$tooltipProvider', '$locationProvider', function ($tooltipProvider) {
    "use strict";
    $tooltipProvider.setTriggers({
        'mouseenter': 'mouseleave',
        'click': 'click',
        'blur': 'focus',
        'never': 'mouseleave' // <- This ensures the tooltip will go away on mouseleave
    });
    //$locationProvider.html5Mode(true);
    //disables the debug info for angular code and boost the performance. The line should be there for higher environment.The feature is available for Angular version 1.3 and higher
    //$compileProvider.debugInfoEnabled( false );

    //$provide.decorator( "$exceptionHandler", function ( $delegate,$window,$log)
    //{
    //    return function ( exception, cause )
    //    {
    //        $delegate( exception, cause );

    //        try
    //        {
    //            var errorMessage = "[AngularException]:" + exception.message;
    //            // Refine the error object with stack trace
    //            $.ajax( {
    //                type: "POST",
    //                url: $window.baseApiUrl + "common/clientexception/",
    //                contentType: "application/json",
    //                data: angular.toJson( {
    //                    errorUrl: $window.location.href,
    //                    errorMessage: errorMessage,
    //                    stackTrace: "Not yet captured",
    //                    cause: ( cause || "" )
    //                } )
    //            } );          

    //        }
    //        catch ( e )
    //        {
    //            $log.warn( "Error logging failed" );
    //            $log.log(e);
    //        }
    //    };
    //} );

    //$locationProvider.html5Mode(true); 

}]);
;
/*jslint browser:true*/
/*global appRoot,angular*/
(function () {
    "use strict";
    appRoot.service('ApiResourceService', ['$resource', "UtilService", function ($resource, utilService) {
        return {
            getHttpResourceObject: function (targetApiUrl, paramDefaults) {
                if (utilService.isUndefinedOrNull(paramDefaults)) {
                    paramDefaults = {};
                }
                var actions = {
                    'get': { method: 'GET', withCredentials: true, headers: { 'X-languageId': this.getCurrentLanguageCode() } },
                    'save': { method: 'POST', withCredentials: true, headers: { 'X-languageId': this.getCurrentLanguageCode() } },
                    'query': { method: 'GET', isArray: true, withCredentials: true, headers: { 'X-languageId': this.getCurrentLanguageCode() } },
                    'remove': { method: 'DELETE', withCredentials: true, headers: { 'X-languageId': this.getCurrentLanguageCode() } },
                    'delete': { method: 'DELETE', withCredentials: true, headers: { 'X-languageId': this.getCurrentLanguageCode() } }
                };
                var options = {};
                return $resource(targetApiUrl, paramDefaults, actions, options);
            },
            getHttpResourceObjectWithHeaders: function (targetApiUrl, paramDefaults, headers) {
                if (utilService.isUndefinedOrNull(headers)) {
                    headers = { 'X-languageId': getCurrentLanguageCode() };
                }
                //else {
                //    headers.push({ 'X-languageId': getCurrentLanguageCode() });
                //}

                if (utilService.isUndefinedOrNull(paramDefaults)) {
                    paramDefaults = {};
                }
                var actions = {
                    'get': { method: 'GET', withCredentials: true, headers: headers },
                    'save': { method: 'POST', withCredentials: true, headers: headers },
                    'query': { method: 'GET', isArray: true, withCredentials: true, headers: headers },
                    'remove': { method: 'DELETE', withCredentials: true, headers: headers },
                    'delete': { method: 'DELETE', withCredentials: true, headers: headers }
                };
                var options = {};
                return $resource(targetApiUrl, paramDefaults, actions, options);
            },
            getCurrentLanguageCode: function () {
                var languageId = "1"; // 1 = English
                var langulageIndicator = 'EN';

                //languageCode comes from Desktop View which will have value as 1 or 2.
                if (!utilService.isUndefinedOrNull(languageCode) && languageCode !== '') {
                    langulageIndicator = languageCode;
                    //langCode comes from Mobile Web View which will have value as EN or ES.
                } else if (!utilService.isUndefinedOrNull(langCode) && langCode !== '') {
                    langulageIndicator = langCode;
                }

                if (langulageIndicator === 'ES' || langulageIndicator === '2') {
                    languageId = "2"; // 2 = Spanish
                }

                return languageId;
            }
        };
    }]);
}());
;
