
/*var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() {

        IAP.initialize();
    }
};*/
/*window.onerror = function(errorMsg, url, lineNumber) {
	alertify.alert('The application has encountered an unknown error. It doesn\'t appear to have affected your data, but our technical staff have been automatically notified and will be looking into this with the utmost urgency.');
    Request.send({object: 'auth', action: 'log', msg:errorMsg, url:url, line:lineNumber});
};*/



window.onerror = function (message, filename, lineno, colno, error) {
    if ("\v" === "v") {
        return;
    }
    /*if (message === "Script error.") {
        return;
    }*/
    message = (message || '').toString();
    filename = (filename || '').toString();
    lineno = (lineno || 0).toString();
    colno = (colno || 0).toString();
    agent = (navigator.userAgent || '').toString();
    var stack = error ? (error.stack || '').toString() : '';

    Request.send({object: 'auth', action: 'log', msg:message, url:filename, line:lineno, col:colno, stack:stack, agent:agent});           
};



var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};




$(document).ready(function() {
    
    $(window).resize(function() {
	    var item = $('.open-left');
		var score = $('.header-bg-right');
		var itemWidth = item.width();
		var scoreWidth = score.width();
	
		var offset1 = item.offset().left;
		var offset2 = score.offset().left;
		var fillerWidth = (offset2 - offset1) - (itemWidth + scoreWidth);
	
		$('.header-bg-middle').css('width', fillerWidth+5);
    });

    window.isphone = false;
    if(document.URL.indexOf("http://") == -1 && document.URL.indexOf("Desktop") == -1 && document.URL.indexOf("Storage") == -1) {
        window.isphone = true;
    }
	//alert('deviceready done');
    if(window.isphone) {
    	//alert('is device');
        document.addEventListener("deviceready", onDeviceReady, false);
    } else {
        onDeviceReady();
    }
});
function onDeviceReady() {
	//alert('deviceready done');
	
	if (typeof StatusBar !== 'undefined' && !Check.isEmpty(StatusBar)) {
		
		if(!Check.isEmpty(window) && !Check.isEmpty(window.device) && !Check.isEmpty(window.device.platform) && !Check.isEmpty(window.device.version)){
			if (window.device.platform === 'iOS' && parseFloat(window.device.version) >= 7.0){
		    	//object_toString(window.device.platform);
		    	//object_toString(window.device.version);
		    	StatusBar.overlaysWebView(false); 
	    	}  
		}  
    }
	
	run();
	
	   
	   
}

function run(){
	storage=$.localStorage;	
	
	if(!Check.isEmpty(storage.get('email'))){
		$('.login-username').val(storage.get('email'));
	}
	
	$.ytLoad();
	//storage.deleteAll();
	if(Check.isEmpty(storage.get('token'))){
		Login.init();
		return false;
	}else{
		Request.send({});
		if(responseObj.status != 100 || Check.isEmpty(responseObj.state)){
			Login.init();
			return false;
		}else{
			IAP.initialize();
			initGame();
			return false;
		}		
	}
	
}

function initGame() {
	/*var hideKeyboard = function(ev) {
	   // Don't actually hide the keyboard if we are on the INPUT
	   if ( ev.target.tagName === 'INPUT' || ev.target.tagName === 'TEXTAREA' ) return;
	     $("input, textarea").blur();
	}
	window.addEventListener('touchstart',hideKeyboard);*/
	
	$('.register-servers-inside').hide();
	makeClickDelay();
	Login.close();
	Overview.init();
	//Buildings.init();
	
	setTimeout(function(){
		$('html, body').scrollTop(0);
	},200);
	
	var item = $('.open-left');
	var score = $('.header-bg-right');
	var itemWidth = item.width();
	var scoreWidth = score.width();

	var offset1 = item.offset().left;
	var offset2 = score.offset().left;
	var fillerWidth = (offset2 - offset1) - (itemWidth + scoreWidth);

	$('.header-bg-middle').css('width', fillerWidth+5);
	
	//Server Auth
	//Request.send({object:'auth',action:'fb-login',access_token:accessToken});
	
	$('.open-left').on('click', function(){
		initLeftMenu();
	});
	
	
	$('.outer-fleet-bar').on('click', function(){
		initRightMenu();
	});
	
	
	$(document).hammer().on('swipeleft', function(){
	   initRightMenu();
    });
	
    // Show left bar
    $(document).hammer().on('swiperight', function(){
		initLeftMenu();
    });
	
}


function initLeftMenu(){
	//Init Menu
		$('nav#menu-left').mmenu(
			{
				position:'left'
			},
			{
				panelNodetype: 'ul',
				clone:true
			}
		);
		
		//Open Menu
		$('#mm-menu-left').trigger( 'open.mm' ).on('closed.mm', function(e){
			$('#mm-menu-left').html('').remove();
		});
		
		//Make it scroll
		makeScroll('mm-menu-left');
}

function initRightMenu(){
	//Generate Fleet table
		Overview.generateFleet();
		
		//Init Menu
		$('nav#menu-right').mmenu(
			{
				position:'right'
			},
			{
				panelNodetype: 'ul',
				clone:true
			}
		);
		
		//Open Menu
		$('#mm-menu-right').trigger( 'open.mm' ).on('closed.mm', function(e){
			$('#mm-menu-right').html('').remove();
			onSubPage = '';
		}).on('close.mm', function(e){
			onSubPage = '';
		}).on('closing.mm', function(e){
			onSubPage = '';
		});
		
		//Make it scroll
		makeScroll('mm-menu-right');
		onSubPage = 'fleet';
}

function makeClickDelay(){

    
    /*last_click_time = new Date().getTime();
    
    document.addEventListener('click', function(e){
        click_time = e['timeStamp'];
        if (click_time && (click_time - last_click_time) < 5000){
            e.stopImmediatePropagation();
            e.preventDefault();
            return false;
        }
        last_click_time = click_time;
    }, true);*/
    
}

