$(document).ready(function(){
	
	$('.req-perms').click(function() {
		checkPerms();
	});
			
	$(document).ajaxSend(function(event, xhr, settings) {
	    function safeMethod(method) {
	        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
	    }

	    if (!safeMethod(settings.type) && !settings.crossDomain) {
	        xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
	    }
	});
	
	jQuery('a[rel*=facebox]').facebox();
	
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    // Is this a version of IE?
    if($.browser.msie){
        $('body').addClass('browserIE');
        
        // Add the version number
        $('body').addClass('browserIE' + $.browser.version.substring(0,1));
    }
    
    
    // Is this a version of Chrome?
    if($.browser.chrome){
    
        $('body').addClass('browserChrome');
        
        //Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserChrome' + userAgent);
        
        // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
        $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    if($.browser.safari){
        $('body').addClass('browserSafari');
        
        // Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserSafari' + userAgent);
    }
    
    // Is this a version of Mozilla?
    if($.browser.mozilla){
        
        //Is it Firefox?
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('browserFirefox');
            
            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('browserFirefox' + userAgent);
        }
        // If not then it must be another Mozilla
        else{
            $('body').addClass('browserMozilla');
        }
    }
    
    // Is this a version of Opera?
    if($.browser.opera){
        $('body').addClass('browserOpera');
    }
});

function isSafari(){
	if ($('body').hasClass('browserSafari')){return true;}
}

function login(){
	FB.ui({method: 'oauth', perms: fbAppInitPerms.join()}, function(response) {
		if(response){
			checkPerms();
			if(gotAllPerms){
				if(isSafari()){
					window.parent.location = "/login?app=" + appModule + "&redirect_uri=" + fbAppTabUrl;
				} else {
					window.parent.location = fbAppTabUrl;
				}
			}
		}
	});
}

var checkPerms = function() {
	FB.getLoginStatus(function(response) {
		if(response.authResponse) {
			FB.api('/me/permissions', function(response) {
				var perms = response.data[0];

		        for (var i in fbAppInitPerms) {
					if (perms[fbAppInitPerms[i]] == null) {
						gotAllPerms = false;
						promptForPerms();
					}
				}
			});
		}
	}, true);
};

var promptForPerms = function() {
	FB.ui({method: 'oauth', perms: fbAppInitPerms.join()}, function(response) {
	});
};


