var rootPath = '/game/indexLayoutless';
var pageStack = new Array(rootPath);

/* PAGE NAVIGATION */

function redirect(url) {
	window.location = url;
}

//not logged in, return to login page
function redirectToLogin() {
	redirect('http://www.soundbirds.com');
}

//reset the page stack with only the home page in the stack
function resetPageStack() {
	pageStack = new Array(rootPath);
	$('#back_button').hide();
}

//load the page and set it as the only page in the stack
function loadPage(path) {
	showLoading();
	$.post(path, function(results) {
		hideLoading();
		$('#back_button').hide();
		pageStack = new Array(path);
		
		$('#content').html(results);
	}).error(function() {
		redirectToLogin();
	});
}

function pushPage(path) {
	showLoading();
	$.post(path, function(results) {
		hideLoading();
		$('#back_button').show();
		pageStack.push(path);
		
		$('#content').html(results);
	}).error(function() {
		redirectToLogin();
	});
}

function popPage() {
	var path = pageStack[pageStack.length-2];
	showLoading();
	$.post(path, function(results) {
		hideLoading();
		pageStack.pop();
		
		if(pageStack.length <= 1) {
			//this is the last screen in the stack so no back button
			$('#back_button').hide();
		}
		
		$('#content').html(results);
	}).error(function() {
		redirectToLogin();
	});
}

/* MISC */
function showLoading() {
	
	$('#loading_widget').show();
	
}

function hideLoading() {
	$('#loading_widget').hide();
}

function updateHud() {
	$.post('/game/getHud', function(results) {
		$('#hud').html(results);
		if(pageStack.length <= 1) {
			//this is the last screen in the stack so no back button
			$('#back_button').hide();
		} else {
			$('#back_button').show();
		}
	}).error(function() {
		redirectToLogin();
	});
}

function redirectToFbLogin(recruiterUserId)
{
	var fbLoginUrl = 'http://www.facebook.com/dialog/oauth'+
	    '?client_id=198772176810675'+
	    '&scope=offline_access'+
	    '&display=touch';
	
	if (recruiterUserId) {
		fbLoginUrl += '&redirect_uri=http://www.soundbirds.com/users/authfblogin/'+recruiterUserId;
	} else { 
		fbLoginUrl += '&redirect_uri=http://www.soundbirds.com/users/authfblogin';
	}
	
	window.location = fbLoginUrl;
}

/* GAMEPLAY ACTIONS */

function buyWeed() {
	var supplierId = $('input#BuySupplierId').val();
	var strainId = $('input#BuyStrainId').val();
	var amount = $('input#BuyAmount').val();
	//var amount = $('#buy_amount_slider').slider("option", "value");
	var dataString = 'supplierId=' + supplierId + '&strainId=' + strainId + '&amount=' + amount; 
	
	//$('#loading_widget').show();
	showLoading();
	$.post('/buy/details/' + supplierId, dataString, function(results) {
		//$('#loading_widget').hide();
		hideLoading();
		resetPageStack();
		if(results != 'fail') {
			$('#content').html(results);
			
			updateHud();
		}
		
		
	}).error(function() {
		redirectToLogin();
	});
}

function sellWeed(userCustomerId) {
	showLoading();
	$.post('/sell/sellWeed/' + userCustomerId, function(results) {
		hideLoading();
		if(results != 'fail') {
			$('#content').html(results);
			
			updateHud();
		}
		
		
	}).error(function() {
		redirectToLogin();
	});
	
}

function smokeUpRecruit(recruitId) {
	showLoading();
	$.post('/recruit/smokeUpRecruit/' + recruitId, function(results) {
		hideLoading();
		if(results != 'fail') {
			$('#content').html(results);
			
			updateHud();
		}
		
	}).error(function() {
		redirectToLogin();
	});
}

function addFriendByAlias(alias) {
	showLoading();
	$.post('/friends/addFriendByAlias/'+alias, function(results) {
		hideLoading();
		$('#content').html(results);
		updateHud();
	}).error(function() {
		redirectToLogin();
	});
}
