/********************************************************
Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved. 
 创建人   : joe Song
 创建时间 : 2016-03-10
 说明     : 应用的全局路由和全局配置项目
*********************************************************/

(function (win) {
	'use strict';
	win.xrmApp = angular.module('xrmApp', ['ngIOS9UIWebViewPatch', 'ionic'])
        .config([
            '$compileProvider',
            function ($compileProvider) {
            	//$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|local|data|wxlocalresource):/);
            	//$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|sms|app):/);
            }
        ])
        .config([
            '$locationProvider',
            function ($locationProvider) {

            }
        ])
        .config([
            "$httpProvider",
            function ($httpProvider) {
            	$httpProvider.defaults.useXDomain = true;
            	delete $httpProvider.defaults.headers.common['X-Requested-With'];
            }
        ])
        .config([
            "$ionicConfigProvider",
            function ($ionicConfigProvider) {
            	$ionicConfigProvider.backButton.previousTitleText(false).text('');
            	$ionicConfigProvider.templates.maxPrefetch(0);
            	$ionicConfigProvider.scrolling.jsScrolling(true);
            }
        ])
        .config([
            '$stateProvider', '$urlRouterProvider',
            function ($stateProvider, $urlRouterProvider) {
            	$stateProvider
                    .state('app-close', {
                    	url: '/app/close',
                    	templateUrl: 'app/blank/blankView.html',
                    	controller: 'blankController'
                    });
            	$urlRouterProvider.otherwise("/");
            }
        ])
        .run([
            '$rootScope', '$state', '$ionicHistory', '$http', 'rtRestClient', 'rtUtils',
            function ($rootScope, $state, $ionicHistory, $http, rtRestClient, rtUtils) {
            	$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
            		console.log("XrmMenuUrl : " + toState.templateUrl);
            	});

            	//当前打开的弹出对话框
            	var openedDialog = null;
            	$rootScope.$on('xrm.modal.shown', function (event, modal) {
            		openedDialog = modal;
            	});
            	$rootScope.$on('xrm.modal.hidden', function () {
            		openedDialog = null;
            	});

            	//当前打开的弹出菜单
            	var openedPopover = null;
            	$rootScope.$on('xrm.popover.shown', function (event, popover) {
            		openedPopover = popover;
            	});
            	$rootScope.$on('xrm.popover.hidden', function () {
            		openedPopover = null;
            	});

            	//当前打开的 是 否 选择的对话框
            	var openedPopup = null;
            	$rootScope.$on('xrm.popup.shown', function (event, popup) {
            		openedPopup = popup;
            	});
            	$rootScope.$on('xrm.popup.hidden', function () {
            		openedPopup = null;
            	});

            	document.addEventListener("xrmandroidbackclick", function () {
            		if (openedPopup !== null) {
            			openedPopup.close();
            			openedPopup = null;
            			return;
            		}

            		if (openedPopover !== null) {
            			openedPopover.hide();
            			openedPopover = null;
            			return;
            		}

            		if (openedDialog !== null) {
            			openedDialog.hide();
            			openedDialog = null;
            			return;
            		}

            		if ($ionicHistory.backView() == null) {
            			$state.go("app-close");
            			return;
            		}

            		$ionicHistory.goBack();
            	}, false);

            	window.fireAndroidBackClick = function () {
            		var event; // The custom event that will be created

            		if (document.createEvent) {
            			event = document.createEvent("HTMLEvents");
            			event.initEvent("xrmandroidbackclick", true, true);
            		} else {
            			event = document.createEventObject();
            			event.eventType = "xrmandroidbackclick";
            		}

            		event.eventName = "xrmandroidbackclick";

            		if (document.createEvent) {
            			document.dispatchEvent(event);
            		} else {
            			document.fireEvent("on" + event.eventType, event);
            		}
            		return 0;
            	};

            	window.fireNativeRefresh = function () {
            		$rootScope.$broadcast('rt-native-refresh');
            	};
            }
        ]);
})(window);