var gameID = 0;

function switch_tab(tab){
	goto_page(1,tab);
}


function show_loggin(){
	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
		response = html.split('|');
		type = response[0];
		sdata = response[1];
		document.getElementById('login_field').innerHTML = sdata;
		if(type == 0) {
			document.getElementById('i_register').style.display = "block";
			$("#l_password").keydown(function(event)
			{
				if(event.keyCode==13) {
					login();
				}
			});
			$("#l_username").keydown(function(event)
			{
				if(event.keyCode==13) {
					login();
				}
			});
		} else {
			document.getElementById('i_register').style.display = "none";
		}
	  },
	  data: 'mode=show_loggin',
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function logout(){
	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        var response = html;
		document.getElementById('login_field').innerHTML = response;
		window.location = "/";
	  },
	  data: 'mode=logout',
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function login(){
	var username = document.getElementById('l_username').value;
	var password = document.getElementById('l_password').value;

	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        if(html.length == 0) {
        	window.location = "/members/";
        } else {
        	alert(html);
        }
	  },
	  data: 'mode=login&username='+username+'&password='+password,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function login2(){
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;

	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        if(html.length == 0) {
        	window.location = "/members/";
        } else {
        	alert(html);
        }
	  },
	  data: 'mode=login&username='+username+'&password='+password,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function goto_page(page,section){
	try{
	document.getElementById(section+'_1').className = "";
	document.getElementById(section+'_2').className = "";
	document.getElementById(section+'_3').className = "";
	document.getElementById(section+'_4').className = "";
	document.getElementById(section+'_5').className = "";
	document.getElementById(section+'_'+page).className = "pg_act";
	}catch(e){}

	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        var response = html;
        document.getElementById(section+'_ajax').innerHTML = response;
        addwarning();
	  },
	  data: 'mode=frontpage_games&page='+page+'&type='+section,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}



function vote_game(type, game){
	gameID = game;

	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
		alert(html);
		show_votes(gameID);
		show_debug_votes(gameID);
	  },
	  data: 'mode=vote&type='+type+'&game='+game,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function show_debug_votes(game) {
	show_debug_votes2(game);
}

function show_debug_votes2(game) {
	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        var response = html;
        try{document.getElementById('votes_debug').innerHTML = response;}catch(e){}
	  },
	  data: 'mode=show_debug_vote&BatchID='+game,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function show_votes(game){
	var ajaxManager = $.manageAjax({manageType: 'queue', maxReq: 0, blockSameRequest: true});
	ajaxManager.add({
	  success: function(html) {
        var response = html;
			response = response.split('|');
			voteUp = response[0];
			voteDown = response[1];
			votedAlready = response[2];

		if(votedAlready == 1) {
			document.getElementById('vote_panel_content_left').style.display = "none";
			document.getElementById('vote_panel_content_left_voted').style.display = "block";
		}

		if(!is_numeric(voteUp)) {
			voteUp = 50;
		}
		if(!is_numeric(voteDown)) {
			voteDown = 50;
		}

        document.getElementById('yes_text').innerHTML = voteUp+'%';
        if(voteUp == 100) {
        	document.getElementById('yes_bar').style.width = '95%';
        } else {
        	document.getElementById('yes_bar').style.width = voteUp+'%';
        }
        document.getElementById('no_text').innerHTML = voteDown+'%';
        if(voteDown == 100) {
        	document.getElementById('no_bar').style.width = '95%';
        } else {
        	document.getElementById('no_bar').style.width = voteDown+'%';
        }

        if(voteUp<10) {
        	document.getElementById('yes_text').style.color = '#000000';
        } else {
        	document.getElementById('yes_text').style.color = '#FFFFFF';
        }

        if(voteDown<10) {
        	document.getElementById('no_text').style.color = '#000000';
		} else {
        	document.getElementById('no_text').style.color = '#FFFFFF';
        }
	  },
	  data: 'mode=show_vote&BatchID='+game,
	  type: "post",
	  url: '/ajax.requests.php'
	});
	ajaxManager = null;
}

function is_numeric( mixed_var ) {
    if (mixed_var === '') {
        return false;
    }

    return !isNaN(mixed_var * 1);
}

$(function()
{
	$("#password").keydown(function(event)
	{
		if(event.keyCode==13) {
			login2();
		}
	});
	$("#username").keydown(function(event)
	{
		if(event.keyCode==13) {
			login2();
		}
	});
});
