/*
Ext.setup ({
    tabletStartupScreen: 'resources/images/splash-2.png',
    phoneStartupScreen: 'resources/images/splash-1.png',
    //icon: 'icon.png',
});
*/
Ext.regApplication({
	name: 'cm',
	//baseUrl : 'http://imars.info/cesarmaia/',
	baseUrl : 'http://localhost/cesarmaia_backend/',
	//baseUrl : 'http://www.orientexports.net/cesarmaia/',
	profiles: {
	    tabletPortrait: function() {
	        return Ext.is.Tablet && Ext.orientation == 'portrait';
	    },
	    tabletLandscape: function() {
	        return Ext.is.Tablet && Ext.orientation == 'landscape';
	    },
	    phonePortrait: function() {
	        return !Ext.is.Tablet && Ext.orientation == 'portrait';//Ext.is.Phone does not work
	    },
	    phoneLandscape: function() {
	        return !Ext.is.Tablet && Ext.orientation == 'landscape';//Ext.is.Phone does not work
	    }
	},
	launch: function() {
		this.launched = true;
		this.mainLaunch();
	},
	mainLaunch: function() {

		/*Open following lines if you want to use phonegap*/
		if (!device || !this.launched) {
			return;
		}
		console.log("*****************************    We have entered the app code here  ***************************");

/********************************************************** |||||  PUSH NOTIFICATIONS CODE  START |||||****************************************************************************************************/
		// Customized callback for receiving notification
	    PushNotification.prototype.notificationCallback = function (notification) {
	        window.plugins.pushNotification.log("Received a notification.");
	        var msg = '';
	        for (var property in notification) {
	            msg += property + ' : ' + notification[property] + '<br>';
	        }
	        window.plugins.pushNotification.log(msg);
			console.log(msg);	
	    };		

		//Get device token and upload to the database if not already there.
		PushToken.getToken(     
		    ["getToken"] , 			
		    function(token) {
		        console.log("Got device token successfully --- Token : "+token);
				if(localStorage.hasOwnProperty("deviceToken")){ // If the device token is in the server side database
					console.log("Token is in the db...");
					//As device token can change, we must check if it has changed
					var dt = localStorage.getItem("deviceToken");
					var dtId = localStorage.getItem("tokenId");
					console.log("Token ID...:" +dtId);
					if(dt != token){//Device token has changed
						console.log("Token has changed...");
						cm.util.ajaxRequest('updateDeviceToken', function(result){
							cm.util.hideLoadMask();
							if(result){
								console.log("Device Token added to database...");
								localStorage.setItem("deviceToken", token);								
							}
							else{
								alert("Cannot add device to push notification database. You will not receive push notifications for this application!");
							}
							
						}, token, dtId);														
					}
					//else ... Device token has not changed... Do nothing
					window.plugins.pushNotification.startNotify();					
				}
				else{//If the device token is not in the server db
				console.log("Token is not in the db...");
					cm.util.ajaxRequest('setDeviceToken', function(result){
						cm.util.hideLoadMask();
						if(result){
							console.log("Device Token added to database...");
							localStorage.setItem("tokenId", result.token_id);
							localStorage.setItem("deviceToken", token);
							window.plugins.pushNotification.startNotify();
						}
						else{
							alert("Cannot add device to push notification database. You will not receive push notifications for this application!");
						}
						
					},token);								
				}
				
		    },
		    function(error) {
		        console.log("Cnnot get device token --- Error : \r\n"+error);      
		    }
		);

/********************************************************** |||||  PUSH NOTIFICATIONS CODE  END |||||****************************************************************************************************/
	    
		this.loadingMask = new Ext.LoadMask(Ext.getBody(), {
			msg: 'Loading...'
		});

		// apple likes when you tell the user they have no internets
		//phonegap required
		if (!navigator.onLine) {
			navigator.notification.alert('You must be online to use the CesarMaia Mobile Application');
		}
		else{
			this.viewport = new cm.views.ViewPort();
		}

	},
	
	util: {
				
		hideLoadMask : function(){
			cm.loadingMask.hide();
		},
		
		/*
		 * Ajax request via JSONP
		 */
		ajaxRequest : function(funcName, callback, param, extra) {
			
			cm.loadingMask.show();
			
			Ext.util.JSONP.request({
				url : cm.baseUrl + 'index.php',
				params : {
					func : funcName,
					param : param,
					extra : extra
				},
				callbackKey : 'callback',
				callback : callback
			});		
		}
	}
});

