
$(document).ready(function() {
	
	get_permission();

	//hideAll();
	who_plays();
	var play = setInterval(who_plays,300000);

	reloadComments(0);

/* shhout box code */
	scrollGame = "0";
	scrollAll  = "0";

	msgGame = "_";
	msgAll  = "_";

    var mod = $('#is_mod').val();
    if(mod == "1" ) colorU = '#ff0000';
	$.ajaxSetup({
		async : false		
		});
	getMessages("0");
	$.ajaxSetup({
		async : true		
		});


	$.ajaxSetup({
		async : false		
		});
	getMessagesAll("0");
	$.ajaxSetup({
		async : true	
		});


	$("#send").click(function(){
	    sendMessage(colorU);
	    refresh();
	});

	/* do shouta na cały portal */
	$("#sendAll").click(function(){
	    sendMessageAll(colorU);
	    refreshAll();
	});


	var refre = setInterval(refresh,2200);
	var refre2 = setInterval(refreshAll,2000);

	$('#shout').scroll(function(){
			scrollGame = "1";
	});

	$('#shout2').scroll(function(){
			scrollAll = "1";
	});


	$("#msgShout").keyup(function (e) {
      if (e.which == 13) {
        $('#send').click();
        $('#msgShout').val('');
      }
    });

    $("#msgShoutAll").keyup(function (e) {
      if (e.which == 13) {
        $('#sendAll').click();
        $('#msgShoutAll').val('');
      }
    });


/* end of shoutbox code */

	$('#f_add_comment').submit(function(){
		var comment = $('#f_add_comment #comment').val();
		var userId = $('#f_add_comment #userId').val();
		var gameId = $('#f_add_comment #gameId').val();
		var module = 'game_add_comment';
		$('#add_comment #comment').val('');

		if(comment.length < 5) {
			getAlertFromXml('g_comment_short');
			return (false);
		}
		else if(userId < 1) {
			getAlertFromXml('g_comment_bad');
			return (false);
		}

		var addComment = $.post(url + '/pattern/_modules/index.php', {comment:comment, userId:userId, gameId:gameId, module:module}, function(data) {
			//$('#debug').html(data);
		});

		addComment.onreadystatechange = function() {
			if(addComment.readyState == 4) {
				var response = addComment.responseText;

				if(response == 'ok') {

					reloadComments(0);
					return (false);
				}
				else if(response == -1) {
					getAlertFromXml('g_comment_short');
					return (false);
				}
				else if(response == -2) {
					getAlertFromXml('g_comment_bad');
					return (false);
				}
				else if(response == -3) {
					getAlertFromXml('g_comment_bad');
					return (false);
				}
				else if(response == -4) {
					getAlertFromXml('g_comment_bad');
					return (false);
				}
			}
		}

		return (false);
	});




	$('#menu1').click(function(){
			hideSend();
			hideAll();
			$('#menu1').addClass('menu1-active');
			reloadComments(0);
			$('#komentarze').show();
	});

	$('#menu2').click(function(){
			if(!$(this).hasClass('menu2-active')){
				hideAll();
				showSendGame();
				$('#menu2').addClass('menu2-active');
				$('#wyslij').show();
			}

	});

	$('#menu3').click(function(){
			hideSend();
			hideAll();

			$('#menu3').addClass('menu3-active');
			$('#zamiesc').show();
	});

	$('#menu4').click(function(){
			hideSend();
			hideAll();
			$('#menu4').addClass('menu4-active');
			showReportInvalid();

			$('#blad').show();
	});



	//var iframe = $('#flashShow').val();

	//if(iframe == 1){
     	var f_height = $('#flashHeight').val();
     	var f_width = $('#flashWidth').val();
     	var f_src = $('#flashSrc').val();

	 	$('#flashContent').flash({
			src: f_src,
			width: f_width,
			height: f_height,
			wmode: 'transparent'
		});
//	}

});

function hideAll(){
		$('#blad').hide();
		$('#wyslij').hide();
		$('#zamiesc').hide();
		$('#komentarze').hide();

		$('#menu1').removeClass('menu1-active');
		$('#menu2').removeClass('menu2-active');
		$('#menu3').removeClass('menu3-active');
		$('#menu4').removeClass('menu4-active');
}

function hideSend(){

		if($('#menu2').hasClass('menu2-active')){
				showSendGame();
		}
}


function who_plays(){
	var module ='who_plays';
	var gameid = $('#gameId').val();

	var play = $.post(url + '/pattern/_modules/who_plays/start.php',{gameid : gameid , module:module,plays: 'getlist'},function(data){
		$('#players').empty();
		$('#players').html(data);


	});


}



function reloadComments(page) {
	var module = 'game_show_comments';
	var gameId = $('#gameId').val();

	var comments = $.post(url + '/pattern/_modules/index.php',{page:page, gameId:gameId, module:module}, function(data) {

	});

	comments.onreadystatechange = function() {
		if(comments.readyState == 4) {
			var response = comments.responseText;

			if(response != 'bad') {
				$('#comments_list').html(response);
			}
			else {
				$('#comments_list').html('');
			}
		}
	}
}

function delComment(id) {

	if(confirm("Na pewno chcesz usunać komentarz?")) {
		var module = 'game_comment_del';
		var del = $.post(url + '/pattern/_modules/index.php',{id:id, module:module}, function(data) {

		});

		del.onreadystatechange = function() {
			if(del.readyState == 4) {
				reloadComments(0);
			}
		}
	}
	else {
		return false;
   }
}

function postDuplicte(id) {
	if(id > 0) {
		var module = 'game_error';
		var type = 'duplicate';


		var post = $.post(url + '/pattern/_modules/index.php', {module:module, type:type, gameId:id}, function(data) {
			//$('#debug').html(data);
		});

		post.onreadystatechange = function() {
			if(post.readyState == 4) {

				getInfoFromXml('g_report_ok');
				return(false);

			}
		}
	}
}

function postInvalid(id) {
	var reason = $('#reportInvalidInput').val();
	if (reason.length < 5) {
		getAlertFromXml('g_invalid_reason');
		return false;
	}

	if(id > 0) {
		$('#reportInvalid').hide();
		var module = 'game_error';
		var type = 'invalid';

		var post = $.post(url + '/pattern/_modules/index.php', {module:module, type:type, gameId:id, reason: reason}, function(data) {

		});

		post.onreadystatechange = function() {
			if(post.readyState == 4) {
				getInfoFromXml('g_report_ok');
				return(false);
			}
		}
	}
}

function showReportInvalid() {
	$('#showReportInvalid').hide();
	$('#reportInvalid').show(2);
}

function addFavourities(game_id) {
	var module = 'user_add_favourities';

	var add = $.post(url + '/pattern/_modules/index.php', {game_id: game_id, module:module}, function(data) {});

	add.onreadystatechange = function() {
		if(add.readyState == 4) {
			getAlertFromXml('u_fav_ok');
			reloadFavourities(0);
			$('#addFav').hide();
		} else {
			getAlertFromXml('app_error');
		}
	}
}


function showSendGame() {
	$('#sendGameTo').val('');
	$('#sendGameMsg').val('');
	$('#sendGameButton').toggle();
	$('#sendGame').toggle();
}

function sendGame() {
		var module = 'user_send_game';
		var gameId  = $('#gameId').val();
		var email = $('#sendGameTo').val();
		var msg = $('#sendGameMsg').val();


		if(!email.match(r_email)) {
			getAlertFromXml('c_email');
			return (false);
		}

		var send = $.post(url + '/pattern/_modules/index.php', {gameId: gameId, email: email, msg: msg, module:module}, function(data) {
			//$('#debug').html(data);
		});

		send.onreadystatechange = function() {
			if(send.readyState == 4) {
				var response = send.responseText;

				if(response == 'ok') {
					getAlertFromXml('u_game_send_ok');
					//showSendGame();
					$('#sendGameTo').val('');
					$('#sendGameMsg').val('');
					return (false);
				}
				else {
					getAlertFromXml('c_send_bad');
					return (false);
				}
			}
		}

		return (false);
}


function getbigBoxes(){



if( $('#shout').height() == 345 )
{
    $('#shoutbox').height(155);
    $('#shout').height(105);
    $('#shoutboxAll').height(155);
    $('#shout2').height(105);
	$("#getbigger").text("+ pokaż wiecej");
	document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight;
	document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight;

}else if( $('#shout').height() == 105 )
{
	$('#shoutbox').height(390);
	$('#shout').height(345);
	$('#shoutboxAll').height(390);
	$('#shout2').height(345);

	$("#getbigger").text("- pokaż mniej");
	document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight;
	document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight;
}



}





function sendMessage(colorU){
	var module = 'shoutbox';
		var gameId  = $('#game_id').val();
		var loginU =  $('#loginU').val();
		var userId = $('#user_id').val();
		var msg =    $('#msgShout').val();
		var type = 'send';
		var mod = $('#is_mod').val();

		re = new RegExp("^"+msgGame+"$");
		if(re.test(msg)) return (false);
		msgGame = msg;


		zagr = /(zagrajznami.pl)(.*)/;
		if(!zagr.test(msg)){

		tre = /(http|www|ftp|.com|.pl|.net|.org|. p l|w w w .|. c o m)/;
		erase = /[aoieuy]/;

			if(tre.test(msg)){
			msg = msg.replace(erase,"***");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"***");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");


			}

		}
		
		$.ajaxSetup({
		async : false		
		});

		var send = $.post(url + '/pattern/_modules/shoutbox/start.php', {msg: msg, type: type, module:module,login: loginU,userid : userId ,color : colorU,game: gameId}, function(data) {

		});
		$.ajaxSetup({
		async : true		
		});

		$.ajaxSetup({
		async : false		
		});
		
		send.onreadystatechange = function() {
			if(send.readyState == 4) {
				$('#msgShout').val('');
				var response = send.responseText;


				scrollGame = "0";
				document.getElementById('msgShout').disabled = true;
				document.getElementById('send').disabled = true;
				var refrea = setInterval(unBlock,1000);
				
			}
		}
		$.ajaxSetup({		
		async : true	
		});
		document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight;
		$('#msgShout').focus();


}

function getMessages(lt){
    var userId = $('#user_id').val();
    var gameId  = $('#game_id').val();
    var mod = $('#is_mod').val();
    var last = $('#shout input:last').val();
    if(lt == 0 && last === undefined) last = 0;
	if(last === undefined ) lt = "0";

$.ajaxSetup({
		async : false		
		});

 	$.post(url + '/pattern/_modules/shoutbox/start.php', {type: "get", module: "shoutbox",userid : userId,game: gameId,is_mod:mod,last:last}, function(data) {

			if(lt == 0 ) { $('#shout').empty();$('#shout').html(data);}else $('#shout').append(data);


			if(scrollGame == 0){ document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight; }

		});

$.ajaxSetup({
		async : true		
		});
}
/* do shouta na caly portal */
function sendMessageAll(colorU){
	var module = 'shoutbox';
		var gameId  = '0';
		var loginU =  $('#loginU').val();
		var userId = $('#user_id').val();
		var msg =    $('#msgShoutAll').val();
		var type = 'sendAll';
		var mod = $('#is_mod').val();

		re = new RegExp("^"+msgAll+"$");
		if(re.test(msg)) return (false);
		msgAll = msg;

		zagr = /(zagrajznami.pl)(.*)/;
		if(!zagr.test(msg)){

		tre = /(http|www|ftp|.com|.pl|.net|.org|. p l|w w w .|. c o m)/;
		erase = /[aoieuy]/;

			if(tre.test(msg)){
			msg = msg.replace(erase,"***");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"***");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");
			msg = msg.replace(erase,"*");


			}

		}

		$.ajaxSetup({
		async : false		
		});

		var send = $.post(url + '/pattern/_modules/shoutbox/start.php', {msg: msg, type: type, module:module,login: loginU,userid : userId ,color : colorU,game: gameId}, function(data) {

		});

		$.ajaxSetup({
		async : true	
		})
		
		$.ajaxSetup({
		async : false		
		});
		
		send.onreadystatechange = function() {
			if(send.readyState == 4) {
				
				var response = send.responseText;
			    $('#msgShoutAll').val('');
				scrollAll = "0";
				
				document.getElementById('msgShoutAll').disabled = true;
				document.getElementById('sendAll').disabled = true;
				var refrea = setInterval(unBlockAll,1000);

			}
		}
		
		$.ajaxSetup({
		async : true	
		})

		document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight;
		$('#msgShoutAll').focus();

}

function getMessagesAll(lt){
    var userId = $('#user_id').val();
    var gameId  = '0';
    var mod = $('#is_mod').val();
	var last = $('#shout2 input:last').val();
    if(lt == "0" ) last = 0;
    if(last === undefined ) lt = "0";
$.ajaxSetup({
		async : false		
		});
 	$.post(url + '/pattern/_modules/shoutbox/start.php', {type: "getAll", module: "shoutbox",userid : userId,game: gameId,is_mod:mod,last:last}, function(data) {

			if(lt == "0" ) { $('#shout2').empty();$('#shout2').html(data);}else $('#shout2').append(data);


			if(scrollAll == "0"){ document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight; }
		});
	$.ajaxSetup({
		async : true		
		});
}

function erase(id){
  var gameId  = $('#game_id').val();
  var mod = $('#is_mod').val();
  var last = $('#shout input:last').val();
  if(last === undefined ) last = "0";

  $.post(url + '/pattern/_modules/shoutbox/start.php', {type: "rem", module: "shoutbox",pid: id,game: gameId,is_mod:mod,last:last}, function(data) {
			var userId = $('#user_id').val();
			$('#shout').empty();
			$('#shout').html(data);
			document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight;

		});
	refresh();
}

function eraseAll(id){
  var mod = $('#is_mod').val();
  var last = $('#shout2 input:last').val();
  if(last === undefined ) last = "0";

  $.post(url + '/pattern/_modules/shoutbox/start.php', {type: "remAll", module: "shoutbox",pid: id,game: '0',is_mod:mod,last:'0'}, function(data) {
			var userId = $('#user_id').val();
			$('#shout2').empty();
			$('#shout2').html(data);
			document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight;

		});
	refreshAll();
}

function refresh(){
	getMessages("1");
	if(scrollGame == "0"){ document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight; }
}

function refreshAll(){
	getMessagesAll("1");
	if(scrollAll == "0"){ document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight; }
}

function getDown(){
	document.getElementById('shout').scrollTop = document.getElementById('shout').scrollHeight;
}

function getDownAll(){
	document.getElementById('shout2').scrollTop = document.getElementById('shout2').scrollHeight;
}

function unBlock(){
				document.getElementById('msgShout').disabled = false;
				document.getElementById('send').disabled = false;
				
}

function unBlockAll(){
				document.getElementById('msgShoutAll').disabled = false;
				document.getElementById('sendAll').disabled = false;
				
}


var colors = new Array( "#F0F8FF","#FAEBD7","#00FFFF","#7FFFD4","#F0FFFF","#F5F5DC","#FFE4C4","#FFEBCD",
"#DEB887","#5F9EA0","#7FFF00","#D2691E","#FF7F50","#6495ED","#FFF8DC",
"#00FFFF","#008B8B","#B8860B","#A9A9A9","#A9A9A9","#006400","#BDB76B","#556B2F","#FF8C00",
"#9932CC","#E9967A","#8FBC8F","#00CED1","#FF1493","#00BFFF","#696969","#696969",
"#1E90FF","#FFFAF0","#228B22","#FF00FF","#DCDCDC","#F8F8FF","#FFD700","#DAA520",
"#808080","#008000","#ADFF2F","#F0FFF0","#FF69B4","#CD5C5C","#FFFFF0","#F0E68C","#E6E6FA",
"#FFF0F5","#7CFC00","#FFFACD","#ADD8E6","#F08080","#E0FFFF","#FAFAD2","#D3D3D3","#D3D3D3",
"#90EE90","#FFB6C1","#FFA07A","#20B2AA","#87CEFA","#778899","#778899","#B0C4DE","#FFFFE0",
"#00FF00","#32CD32","#FAF0E6","#66CDAA","#BA55D3","#9370D8","#3CB371","#7B68EE",
"#00FA9A","#48D1CC","#C71585","#F5FFFA","#FFE4E1","#FFE4B5","#FFDEAD","#FDF5E6","#808000",
"#6B8E23","#FFA500","#FF4500","#DA70D6","#EEE8AA","#98FB98","#AFEEEE","#D87093","#FFEFD5",
"#FFDAB9","#CD853F","#FFC0CB","#DDA0DD","#B0E0E6","#BC8F8F","#4169E1","#8B4513","#FA8072",
"#F4A460","#2E8B57","#FFF5EE","#A0522D","#C0C0C0","#87CEEB","#6A5ACD","#708090","#708090",
"#FFFAFA","#00FF7F","#4682B4","#D2B48C","#008080","#D8BFD8","#FF6347","#40E0D0","#EE82EE",
"#F5DEB3","#FFFFFF","#F5F5F5","#FFFF00","#9ACD32");

var b = Math.round(Math.random()*123) ;
var colorU = colors[b];


	function get_permission(){
		var permission = $('#permission').val();
		var server = $('#server').val();
		
		if(permission == 1){
			getAlertFromXml('user_log_must');
			window.location.href='http://' + server + '/6440';
		}
	}

